Skip to content

Commit

Permalink
More tests and bugfixes (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorprogger authored Jun 15, 2020
1 parent cff3fea commit 8876150
Show file tree
Hide file tree
Showing 22 changed files with 372 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- windows-latest

php-version:
- "7.4"
- "7.4.5"
- "8.0"

steps:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
"tests-app": [
"$common",
"$console",
"tests/App/config/main.php",
"tests/App/config/console.php"
]
],
"events-console": "config/events-console.php"
}
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion config/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
EventDispatcherInterface::class => Dispatcher::class,
WorkerInterface::class => QueueWorker::class,
ListenerProviderInterface::class => Provider::class,
ContainerInterface::class => Container::class,
ContainerInterface::class => fn (ContainerInterface $container) => $container,
LoopInterface::class => SignalLoop::class
];
6 changes: 6 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
<directory>./tests/unit</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
3 changes: 2 additions & 1 deletion src/DriverInterface.php → src/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

declare(strict_types=1);

namespace Yiisoft\Yii\Queue;
namespace Yiisoft\Yii\Queue\Driver;

use InvalidArgumentException;
use Yiisoft\Yii\Queue\Enum\JobStatus;
use Yiisoft\Yii\Queue\Job\DelayableJobInterface;
use Yiisoft\Yii\Queue\Job\JobInterface;
use Yiisoft\Yii\Queue\Job\PrioritisedJobInterface;
use Yiisoft\Yii\Queue\Job\RetryableJobInterface;
use Yiisoft\Yii\Queue\MessageInterface;

interface DriverInterface
{
Expand Down
100 changes: 100 additions & 0 deletions src/Driver/SynchronousDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Driver;

use InvalidArgumentException;
use Yiisoft\Yii\Queue\Cli\LoopInterface;
use Yiisoft\Yii\Queue\Enum\JobStatus;
use Yiisoft\Yii\Queue\Job\DelayableJobInterface;
use Yiisoft\Yii\Queue\Job\JobInterface;
use Yiisoft\Yii\Queue\Job\PrioritisedJobInterface;
use Yiisoft\Yii\Queue\Message;
use Yiisoft\Yii\Queue\MessageInterface;
use Yiisoft\Yii\Queue\Queue;
use Yiisoft\Yii\Queue\QueueDependentInterface;
use Yiisoft\Yii\Queue\Worker\WorkerInterface;

final class SynchronousDriver implements DriverInterface, QueueDependentInterface
{
private array $messages = [];
private Queue $queue;
private LoopInterface $loop;
private WorkerInterface $worker;
private int $current = 0;

public function __construct(LoopInterface $loop, WorkerInterface $worker)
{
$this->loop = $loop;
$this->worker = $worker;
}

public function __destruct()
{
$this->run([$this->worker, 'process']);
}

public function nextMessage(): ?MessageInterface
{
$message = null;

if (isset($this->messages[$this->current])) {
$message = $this->messages[$this->current];
unset($this->messages[$this->current]);
$this->current++;
}

return $message;
}

public function status(string $id): JobStatus
{
$id = (int) $id;

if ($id < 0) {
throw new InvalidArgumentException('This driver ids starts with 0');
}

if ($id < $this->current) {
return JobStatus::done();
}

if (isset($this->messages[$id])) {
return JobStatus::waiting();
}

throw new InvalidArgumentException('There is no job with the given id.');
}

public function push(JobInterface $job): MessageInterface
{
$key = count($this->messages) + $this->current;
$message = new Message((string) $key, $job);
$this->messages[] = $message;

return $message;
}

public function subscribe(callable $handler): void
{
$this->run($handler);
}

public function canPush(JobInterface $job): bool
{
return !($job instanceof DelayableJobInterface || $job instanceof PrioritisedJobInterface);
}

public function setQueue(Queue $queue): void
{
$this->queue = $queue;
}

private function run(callable $handler): void
{
while ($this->loop->canContinue() && $message = $this->nextMessage()) {
$handler($message, $this->queue);
}
}
}
2 changes: 1 addition & 1 deletion src/Exception/JobNotSupportedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Throwable;
use UnexpectedValueException;
use Yiisoft\FriendlyException\FriendlyExceptionInterface;
use Yiisoft\Yii\Queue\DriverInterface;
use Yiisoft\Yii\Queue\Driver\DriverInterface;
use Yiisoft\Yii\Queue\Job\DelayableJobInterface;
use Yiisoft\Yii\Queue\Job\JobInterface;
use Yiisoft\Yii\Queue\Job\PrioritisedJobInterface;
Expand Down
8 changes: 2 additions & 6 deletions src/Job/RetryableJobInterface.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?php
/**
* @link http://www.yiiframework.com/
*
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Job;

Expand Down
6 changes: 4 additions & 2 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Log\LoggerInterface;
use Yiisoft\EventDispatcher\Provider\Provider;
use Yiisoft\Yii\Queue\Cli\LoopInterface;
use Yiisoft\Yii\Queue\Driver\DriverInterface;
use Yiisoft\Yii\Queue\Enum\JobStatus;
use Yiisoft\Yii\Queue\Event\AfterPush;
use Yiisoft\Yii\Queue\Event\BeforePush;
Expand Down Expand Up @@ -59,6 +60,7 @@ public function jobRetry(JobFailure $event): void
&& $event->getQueue() === $this
&& $job->canRetry($event->getException())
) {
$event->preventThrowing();
$this->logger->debug('Retrying job "{job}".', ['job' => get_class($job)]);
$job->retry();
$this->push($job);
Expand All @@ -70,9 +72,9 @@ public function jobRetry(JobFailure $event): void
*
* @param JobInterface|mixed $job
*
* @return string|null id of a job message
* @return string id of a job message
*/
public function push(JobInterface $job): ?string
public function push(JobInterface $job): string
{
$this->logger->debug('Preparing to push job "{job}".', ['job' => get_class($job)]);
$event = new BeforePush($this, $job);
Expand Down
15 changes: 15 additions & 0 deletions tests/App/DelayableJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Tests\App;

use Yiisoft\Yii\Queue\Job\DelayableJobInterface;

class DelayableJob extends SimpleJob implements DelayableJobInterface
{
public function getDelay(): int
{
return 1;
}
}
20 changes: 20 additions & 0 deletions tests/App/EventManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Tests\App;

use Yiisoft\Yii\Queue\Event\AfterExecution;
use Yiisoft\Yii\Queue\Event\AfterPush;
use Yiisoft\Yii\Queue\Event\BeforeExecution;
use Yiisoft\Yii\Queue\Event\BeforePush;
use Yiisoft\Yii\Queue\Event\JobFailure;

interface EventManager
{
public function beforePushHandler(BeforePush $event);
public function afterPushHandler(AfterPush $event);
public function beforeExecutionHandler(BeforeExecution $event);
public function afterExecutionHandler(AfterExecution $event);
public function jobFailureHandler(JobFailure $event);
}
15 changes: 15 additions & 0 deletions tests/App/PrioritizedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Tests\App;

use Yiisoft\Yii\Queue\Job\PrioritisedJobInterface;

class PrioritizedJob extends SimpleJob implements PrioritisedJobInterface
{
public function getPriority(): int
{
return 1;
}
}
38 changes: 0 additions & 38 deletions tests/App/PriorityJob.php

This file was deleted.

50 changes: 0 additions & 50 deletions tests/App/RetryJob.php

This file was deleted.

32 changes: 32 additions & 0 deletions tests/App/RetryableJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Tests\App;

use RuntimeException;
use Yiisoft\Yii\Queue\Job\AttemptsRestrictedJob;

class RetryableJob extends AttemptsRestrictedJob
{
public bool $executed = false;

public function __construct(int $attemptsMax = 2)
{
$this->attemptsMax = $attemptsMax;
}

public function getTtr(): int
{
return 1;
}

public function execute(): void
{
if ($this->canRetry()) {
throw new RuntimeException('Test exception');
}

$this->executed = true;
}
}
8 changes: 2 additions & 6 deletions tests/App/SimpleJob.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?php
/**
* @link http://www.yiiframework.com/
*
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Tests\App;

Expand Down
Loading

0 comments on commit 8876150

Please sign in to comment.