Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests #43

Merged
merged 5 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
"psr/event-dispatcher": "^1.0",
"psr/log": "^1.1",
"yiisoft/friendly-exception": "^1.0",
"yiisoft/serializer": "^3.0@dev",
"yiisoft/yii-console": "^3.0@dev"
},
"require-dev": {
"yiisoft/composer-config-plugin": "^1.0@dev",
"infection/infection": "^0.15.3",
"pda/pheanstalk": "*",
"phpunit/phpunit": "^9.0",
"yiisoft/composer-config-plugin": "^1.0@dev",
"yiisoft/di": "^3.0@dev",
"yiisoft/log": "^3.0@dev",
"yiisoft/event-dispatcher": "^3.0@dev"
"yiisoft/log": "^3.0@dev"
},
"suggest": {
"ext-pcntl": "Need for process signals."
Expand Down
6 changes: 6 additions & 0 deletions config/common.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
<?php

use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
use Yiisoft\Di\Container;
use Yiisoft\EventDispatcher\Dispatcher\Dispatcher;
use Yiisoft\EventDispatcher\Provider\Provider;
use Yiisoft\Yii\Queue\Cli\LoopInterface;
use Yiisoft\Yii\Queue\Cli\SignalLoop;
use Yiisoft\Yii\Queue\Worker\Worker as QueueWorker;
use Yiisoft\Yii\Queue\Worker\WorkerInterface;

return [
EventDispatcherInterface::class => Dispatcher::class,
WorkerInterface::class => QueueWorker::class,
ListenerProviderInterface::class => Provider::class,
ContainerInterface::class => Container::class,
LoopInterface::class => SignalLoop::class
];
10 changes: 10 additions & 0 deletions config/events-console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Yiisoft\Yii\Queue\Event\JobFailure;
use Yiisoft\Yii\Queue\Queue;

return [
JobFailure::class => [
[Queue::class, 'jobRetry'],
],
];
14 changes: 14 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"source": {
"directories": [
"src"
]
},
"timeout": 10,
"logs": {
"text": "tests/runtime/infection.log",
"summary": "tests/runtime/summary.log",
"perMutator": "tests/runtime/per-mutator.md"
},
"testFrameworkOptions": "-vvv"
}
11 changes: 1 addition & 10 deletions src/Cli/SignalLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ class SignalLoop implements LoopInterface
*/
protected array $resumeSignals = [];

/**
* @var Queue
*/
protected Queue $queue;

/**
* @var bool status when exit signal was got.
*/
Expand All @@ -52,12 +47,8 @@ class SignalLoop implements LoopInterface
*/
protected bool $pause = false;

/**
* @param Queue $queue
*/
public function __construct($queue)
public function __construct()
{
$this->queue = $queue;
if (extension_loaded('pcntl')) {
foreach ($this->exitSignals as $signal) {
pcntl_signal($signal, fn () => $this->exit = true);
Expand Down
20 changes: 7 additions & 13 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use Yiisoft\Yii\Queue\Event\AfterPush;
use Yiisoft\Yii\Queue\Event\BeforePush;
use Yiisoft\Yii\Queue\Event\JobFailure;
use Yiisoft\Yii\Queue\Exception\InvalidJobException;
use Yiisoft\Yii\Queue\Exception\JobNotSupportedException;
use Yiisoft\Yii\Queue\Job\JobInterface;
use Yiisoft\Yii\Queue\Job\RetryableJobInterface;
use Yiisoft\Yii\Queue\Worker\WorkerInterface;

/**
Expand Down Expand Up @@ -48,26 +48,20 @@ public function __construct(
if ($driver instanceof QueueDependentInterface) {
$driver->setQueue($this);
}

$provider->attach([$this, 'jobRetry']);
}

public function __destruct()
{
$this->provider->detach(JobFailure::class);
}

public function jobRetry(JobFailure $event): void
{
$job = $event->getMessage()->getJob();
if (
!$event->getException() instanceof InvalidJobException
$job instanceof RetryableJobInterface
&& !$event->getException() instanceof JobNotSupportedException
&& $event->getQueue() === $this
&& $event->getMessage()->getJob()->canRetry($event->getException())
&& $job->canRetry($event->getException())
) {
$this->logger->debug('Retrying job "{job}".', ['job' => get_class($event->getMessage()->getJob())]);
$event->getMessage()->getJob()->retry();
$this->push($event->getMessage()->getJob());
$this->logger->debug('Retrying job "{job}".', ['job' => get_class($job)]);
$job->retry();
$this->push($job);
}
}

Expand Down
4 changes: 1 addition & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ abstract class TestCase extends BaseTestCase
{
public $container;

public function __construct($name = null, array $data = [], $dataName = '')
protected function setUp(): void
{
parent::__construct($name, $data, $dataName);
Builder::rebuild();
$this->container = new Container(require Builder::path('tests-app'));
}
}
13 changes: 7 additions & 6 deletions tests/unit/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\EventDispatcher\ListenerProviderInterface;
use RuntimeException;
use Yiisoft\EventDispatcher\Provider\Provider;
use Yiisoft\Yii\Console\Config\EventConfigurator;
use Yiisoft\Yii\Queue\Event\BeforeExecution;
use Yiisoft\Yii\Queue\Event\JobFailure;
use Yiisoft\Yii\Queue\Message;
Expand Down Expand Up @@ -48,9 +49,9 @@ public function testJobExecuted(): void
public function testJobNotExecuted(): void
{
$handler = fn (BeforeExecution $event) => $event->stopExecution();
/** @var Provider $provider */
$provider = $this->container->get(ListenerProviderInterface::class);
$provider->attach($handler);
/** @var EventConfigurator $configurator */
$configurator = $this->container->get(EventConfigurator::class);
$configurator->registerListeners([BeforeExecution::class => [$handler]]);

$job = new SimpleJob();
$message = new Message('', $job);
Expand Down Expand Up @@ -79,9 +80,9 @@ public function testThrowException(): void
public function testThrowExceptionPrevented(): void
{
$handler = fn (JobFailure $event) => $event->preventThrowing();
/** @var Provider $provider */
$provider = $this->container->get(ListenerProviderInterface::class);
$provider->attach($handler);
/** @var EventConfigurator $configurator */
$configurator = $this->container->get(EventConfigurator::class);
$configurator->registerListeners([JobFailure::class => [$handler]]);

$job = new ExceptionalSimpleJob();
$message = new Message('', $job);
Expand Down