mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-27 01:11:39 -06:00
Created TaskRunnerTest
This commit is contained in:
parent
af40e8de5c
commit
af4ee8f7ec
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\EventDispatcher\Async;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
use function get_class;
|
||||
use function sprintf;
|
||||
|
||||
class Task
|
||||
{
|
||||
/** @var string */
|
||||
private $regularListenerName;
|
||||
/** @var object */
|
||||
private $event;
|
||||
|
||||
public function __construct(string $regularListenerName, object $event)
|
||||
{
|
||||
$this->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));
|
||||
}
|
||||
}
|
13
module/EventDispatcher/src/Async/TaskInterface.php
Normal file
13
module/EventDispatcher/src/Async/TaskInterface.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\EventDispatcher\Async;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
interface TaskInterface
|
||||
{
|
||||
public function run(ContainerInterface $container): void;
|
||||
|
||||
public function toString(): string;
|
||||
}
|
@ -27,8 +27,8 @@ class TaskRunner
|
||||
|
||||
public function __invoke(HttpServer $server, int $taskId, int $fromId, $task): void
|
||||
{
|
||||
if (! $task instanceof Task) {
|
||||
$this->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('');
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
34
module/EventDispatcher/src/Listener/EventListenerTask.php
Normal file
34
module/EventDispatcher/src/Listener/EventListenerTask.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\EventDispatcher\Listener;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Shlinkio\Shlink\EventDispatcher\Async\TaskInterface;
|
||||
|
||||
use function get_class;
|
||||
use function sprintf;
|
||||
|
||||
class EventListenerTask implements TaskInterface
|
||||
{
|
||||
/** @var string */
|
||||
private $listenerName;
|
||||
/** @var object */
|
||||
private $event;
|
||||
|
||||
public function __construct(string $listenerName, object $event)
|
||||
{
|
||||
$this->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));
|
||||
}
|
||||
}
|
107
module/EventDispatcher/test/Async/TaskRunnerTest.php
Normal file
107
module/EventDispatcher/test/Async/TaskRunnerTest.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\EventDispatcher\Async;
|
||||
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Shlinkio\Shlink\EventDispatcher\Async\TaskInterface;
|
||||
use Shlinkio\Shlink\EventDispatcher\Async\TaskRunner;
|
||||
use Swoole\Http\Server as HttpServer;
|
||||
|
||||
class TaskRunnerTest extends TestCase
|
||||
{
|
||||
/** @var TaskRunner */
|
||||
private $taskRunner;
|
||||
/** @var ObjectProphecy */
|
||||
private $logger;
|
||||
/** @var ObjectProphecy */
|
||||
private $container;
|
||||
/** @var ObjectProphecy */
|
||||
private $server;
|
||||
/** @var ObjectProphecy */
|
||||
private $task;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user