. */ namespace SP\Tests\Services\Task; use PHPUnit\Framework\TestCase; use SP\Core\Context\ContextException; use SP\Services\ServiceException; use SP\Services\Task\Task; use SP\Services\Task\TaskFactory; use SP\Services\Task\TaskService; use SP\Storage\File\FileException; use function SP\Tests\setupContext; /** * Class TaskServiceTest * * @package SP\Tests\Services\Task */ class TaskServiceTest extends TestCase { private static $pids = []; /** * @throws FileException * @throws ContextException * @throws ServiceException */ public function testTrackStatus() { $this->markTestSkipped(); $task = TaskFactory::create(__FUNCTION__, Task::genTaskId(__FUNCTION__)); $this->assertFileExists($task->getFileTask()->getFile()); TaskFactory::update($task, TaskFactory::createMessage($task->getTaskId(), "Test Task (INIT)") ); $dic = setupContext(); $this->fork(function () use ($task, $dic) { $taskService = new TaskService($dic); $taskService->trackStatus($task->getTaskId(), function ($id, $message) { logger("id: $id; data: $message"); }); }); $this->fork(function () use ($task) { $count = 0; while ($count < 2) { sleep(10); TaskFactory::update($task, TaskFactory::createMessage($task->getTaskId(), "Test Task #$count") ); $count++; } TaskFactory::end($task); }); while (count(self::$pids) > 0) { foreach (self::$pids as $key => $pid) { $res = pcntl_waitpid($pid, $status); // If the process has already exited if ($res == -1 || $res > 0) { unset(self::$pids[$key]); } } sleep(1); } $this->assertFileDoesNotExist($task->getFileTask()->getFile()); $this->assertFileDoesNotExist($task->getFileOut()->getFile()); } /** * Fork for running a piece of code in child process * * @param callable $code */ private function fork(callable $code) { $pid = pcntl_fork(); if ($pid == -1) { die('Could not fork'); } elseif ($pid === 0) { echo "Child execution\n"; $code(); exit(); } else { echo "Child $pid\n"; self::$pids[] = $pid; } } }