From af4ee8f7ec173c9e5832dfc5a5f4a3e3ded71019 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 19 Jul 2019 20:59:06 +0200 Subject: [PATCH] Created TaskRunnerTest --- module/EventDispatcher/src/Async/Task.php | 33 ------ .../src/Async/TaskInterface.php | 13 +++ .../EventDispatcher/src/Async/TaskRunner.php | 7 +- .../src/Listener/AsyncEventListener.php | 3 +- .../src/Listener/EventListenerTask.php | 34 ++++++ .../test/Async/TaskRunnerTest.php | 107 ++++++++++++++++++ 6 files changed, 158 insertions(+), 39 deletions(-) delete mode 100644 module/EventDispatcher/src/Async/Task.php create mode 100644 module/EventDispatcher/src/Async/TaskInterface.php create mode 100644 module/EventDispatcher/src/Listener/EventListenerTask.php create mode 100644 module/EventDispatcher/test/Async/TaskRunnerTest.php diff --git a/module/EventDispatcher/src/Async/Task.php b/module/EventDispatcher/src/Async/Task.php deleted file mode 100644 index 7a44faa2..00000000 --- a/module/EventDispatcher/src/Async/Task.php +++ /dev/null @@ -1,33 +0,0 @@ -regularListenerName = $regularListenerName; - $this->event = $event; - } - - public function __invoke(ContainerInterface $container) - { - ($container->get($this->regularListenerName))($this->event); - } - - public function toString(): string - { - return sprintf('Listener -> "%s", Event -> "%s"', $this->regularListenerName, get_class($this->event)); - } -} diff --git a/module/EventDispatcher/src/Async/TaskInterface.php b/module/EventDispatcher/src/Async/TaskInterface.php new file mode 100644 index 00000000..b9f20486 --- /dev/null +++ b/module/EventDispatcher/src/Async/TaskInterface.php @@ -0,0 +1,13 @@ +logger->error('Invalid task provided to task worker: {type}', [ + if (! $task instanceof TaskInterface) { + $this->logger->warning('Invalid task provided to task worker: {type}. Task ignored', [ 'type' => is_object($task) ? get_class($task) : gettype($task), ]); $server->finish(''); @@ -41,14 +41,13 @@ class TaskRunner ]); try { - $task($this->container); + $task->run($this->container); } catch (Throwable $e) { $this->logger->error('Error processing task {taskId}: {e}', [ 'taskId' => $taskId, 'e' => $e, ]); } finally { - // Notify the server that processing of the task has finished: $server->finish(''); } } diff --git a/module/EventDispatcher/src/Listener/AsyncEventListener.php b/module/EventDispatcher/src/Listener/AsyncEventListener.php index 28132ae8..7f59221a 100644 --- a/module/EventDispatcher/src/Listener/AsyncEventListener.php +++ b/module/EventDispatcher/src/Listener/AsyncEventListener.php @@ -3,7 +3,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\EventDispatcher\Listener; -use Shlinkio\Shlink\EventDispatcher\Async\Task; use Swoole\Http\Server as HttpServer; class AsyncEventListener @@ -21,6 +20,6 @@ class AsyncEventListener public function __invoke(object $event): void { - $this->server->task(new Task($this->regularListenerName, $event)); + $this->server->task(new EventListenerTask($this->regularListenerName, $event)); } } diff --git a/module/EventDispatcher/src/Listener/EventListenerTask.php b/module/EventDispatcher/src/Listener/EventListenerTask.php new file mode 100644 index 00000000..bc1e3689 --- /dev/null +++ b/module/EventDispatcher/src/Listener/EventListenerTask.php @@ -0,0 +1,34 @@ +listenerName = $listenerName; + $this->event = $event; + } + + public function run(ContainerInterface $container): void + { + ($container->get($this->listenerName))($this->event); + } + + public function toString(): string + { + return sprintf('Listener -> "%s", Event -> "%s"', $this->listenerName, get_class($this->event)); + } +} diff --git a/module/EventDispatcher/test/Async/TaskRunnerTest.php b/module/EventDispatcher/test/Async/TaskRunnerTest.php new file mode 100644 index 00000000..a78a741b --- /dev/null +++ b/module/EventDispatcher/test/Async/TaskRunnerTest.php @@ -0,0 +1,107 @@ +logger = $this->prophesize(LoggerInterface::class); + $this->container = $this->prophesize(ContainerInterface::class); + $this->task = $this->prophesize(TaskInterface::class); + + $this->server = $this->createMock(HttpServer::class); + $this->server + ->expects($this->once()) + ->method('finish') + ->with(''); + + $this->taskRunner = new TaskRunner($this->logger->reveal(), $this->container->reveal()); + } + + /** @test */ + public function warningIsLoggedWhenProvidedTaskIsInvalid(): void + { + $logWarning = $this->logger->warning('Invalid task provided to task worker: {type}. Task ignored', [ + 'type' => 'string', + ]); + $logInfo = $this->logger->info(Argument::cetera()); + $logError = $this->logger->error(Argument::cetera()); + + ($this->taskRunner)($this->server, 1, 1, 'invalid_task'); + + $logWarning->shouldHaveBeenCalledOnce(); + $logInfo->shouldNotHaveBeenCalled(); + $logError->shouldNotHaveBeenCalled(); + } + + /** @test */ + public function properTasksAreRun(): void + { + $logWarning = $this->logger->warning(Argument::cetera()); + $logInfo = $this->logger->notice('Starting work on task {taskId}: {task}', [ + 'taskId' => 1, + 'task' => 'The task', + ]); + $logError = $this->logger->error(Argument::cetera()); + $taskToString = $this->task->toString()->willReturn('The task'); + $taskRun = $this->task->run($this->container->reveal())->will(function () { + }); + + ($this->taskRunner)($this->server, 1, 1, $this->task->reveal()); + + $logWarning->shouldNotHaveBeenCalled(); + $logInfo->shouldHaveBeenCalledOnce(); + $logError->shouldNotHaveBeenCalled(); + $taskToString->shouldHaveBeenCalledOnce(); + $taskRun->shouldHaveBeenCalledOnce(); + } + + /** @test */ + public function errorIsLoggedWhenTasksFail(): void + { + $e = new Exception('Error'); + + $logWarning = $this->logger->warning(Argument::cetera()); + $logInfo = $this->logger->notice('Starting work on task {taskId}: {task}', [ + 'taskId' => 1, + 'task' => 'The task', + ]); + $logError = $this->logger->error('Error processing task {taskId}: {e}', [ + 'taskId' => 1, + 'e' => $e, + ]); + $taskToString = $this->task->toString()->willReturn('The task'); + $taskRun = $this->task->run($this->container->reveal())->willThrow($e); + + ($this->taskRunner)($this->server, 1, 1, $this->task->reveal()); + + $logWarning->shouldNotHaveBeenCalled(); + $logInfo->shouldHaveBeenCalledOnce(); + $logError->shouldHaveBeenCalledOnce(); + $taskToString->shouldHaveBeenCalledOnce(); + $taskRun->shouldHaveBeenCalledOnce(); + } +}