taskStorage = $tasks->getTaskStorage(); $this->taskStorage->reset(); // And load the ShellExecutor using the execution settings $this->executor = new ShellExecutor([ 'bootstrapFile' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'bootstrap.php', 'workerFile' => dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'worker' ]); } public function testClass() { $this->assertInstanceOf('FuzeWorks\Async\Executors\ShellExecutor', $this->executor); } /** * @depends testClass */ public function testNoWorkerFile() { $this->expectException(TasksException::class); new ShellExecutor(['bootstrapFile' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'bootstrap.php']); } /** * @depends testClass */ public function testNoBoostrapFile() { $this->expectException(TasksException::class); new ShellExecutor(['workerFile' => dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'worker']); } /** * @depends testClass */ public function testInvalidWorkerFile() { $this->expectException(TasksException::class); new ShellExecutor([ 'bootstrapFile' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'bootstrap.php', 'workerFile' => 'not_found' ]); } /** * @depends testClass */ public function testInvalidBootstrapFile() { $this->expectException(TasksException::class); new ShellExecutor([ 'bootstrapFile' => 'not_found', 'workerFile' => dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'worker' ]); } /* ---------------------------------- Writing and reading tasks ----------------------- */ /** * @depends testClass * @covers ::startTask * @covers ::getTaskRunning */ public function testStartAndReadTasks() { // First we create a dummy task $dummyTask = new Task('testStartAndReadTasks', 'Mock\Handlers\TestStartAndReadTasksHandler'); // Then we write this task to the TaskStorage $this->taskStorage->addTask($dummyTask); // Assert that no PID exists yet $this->assertNull($dummyTask->attribute('pid')); // Then we fire the task $task = $this->executor->startTask($dummyTask); // Pause 1/10th of a second usleep(100000); // Assert that the output is the same $this->assertSame($dummyTask, $task); // Also assert that a PID has been added $this->assertIsInt($task->attribute('pid')); // Also assert that the task is currently running $this->assertTrue($this->executor->getTaskRunning($task)); } /** * @depends testStartAndReadTasks */ public function testGetStats() { // First we create a dummy task, using the previous handler since nothing changes $dummyTask = new Task('testGetStats', 'Mock\Handlers\TestStartAndReadTasksHandler'); // Then we write this task to the TaskStorage $this->taskStorage->addTask($dummyTask); // Then we start the task $dummyTask = $this->executor->startTask($dummyTask); // Pause 1/10th of a second usleep(100000); // And we fetch some task statistics $stats = $this->executor->getTaskStats($dummyTask); // Assert some assumptions $this->assertNotNull($stats); $this->assertIsInt($stats['pid']); $this->assertIsFloat($stats['cpu']); $this->assertIsFloat($stats['mem']); $this->assertIsString($stats['state']); $this->assertIsString($stats['start']); } /** * @depends testGetStats */ public function testGetStatsNotExist() { // First we create a dummy task, using the previous handler since nothing changes $dummyTask = new Task('testGetStatsNotExist', 'none'); // And add a fake PID, since otherwise it will immediately fail $dummyTask->addAttribute('pid', 1005); // Then we fetch the process details $this->assertNull($this->executor->getTaskStats($dummyTask)); } /** * @depends testStartAndReadTasks */ public function testStopTask() { // First we create a dummy task $dummyTask = new Task('testStopTask', 'Mock\Handlers\TestStopTaskHandler'); // Then we write this task to the TaskStorage $this->taskStorage->addTask($dummyTask); // First we start the task and confirm its running $dummyTask = $this->executor->startTask($dummyTask); // Pause 1/10th of a second usleep(100000); // Check if the task is running $this->assertTrue($this->executor->getTaskRunning($dummyTask)); // But then we try and stop it $output = $this->executor->stopTask($dummyTask); // Pause 1/10th of a second usleep(100000); // We check that the output actually is the task $this->assertSame($dummyTask, $output); // And check if the Task has actually stopped now $this->assertFalse($this->executor->getTaskRunning($dummyTask)); } }