1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 14:01:14 +01:00

Fail queued tasks after shutdown is invoked

This commit is contained in:
Aaron Piotrowski 2018-06-07 14:11:45 -05:00
parent d81b6fccd2
commit ca051a0aa6
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
2 changed files with 26 additions and 1 deletions

View File

@ -71,7 +71,7 @@ abstract class AbstractWorker implements Worker {
}
}
if (!$this->context->isRunning()) {
if ($this->shutdown || !$this->context->isRunning()) {
throw new WorkerException("The worker was shutdown");
}

View File

@ -7,6 +7,7 @@ use Amp\Parallel\Sync\SerializationException;
use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\Task;
use Amp\Parallel\Worker\TaskError;
use Amp\Parallel\Worker\WorkerException;
use Amp\PHPUnit\TestCase;
class NonAutoloadableTask implements Task {
@ -97,10 +98,34 @@ abstract class AbstractWorkerTest extends TestCase {
});
}
yield $promises; // Wait until all tasks have finished before invoking $worker->shutdown().
yield $worker->shutdown();
});
}
public function testEnqueueMultipleThenShutdown() {
Loop::run(function () {
$worker = $this->createWorker();
$promises = [
$worker->enqueue(new TestTask(42, 200)),
$worker->enqueue(new TestTask(56, 300)),
$worker->enqueue(new TestTask(72, 100))
];
yield $worker->shutdown();
\array_shift($promises); // First task will succeed.
foreach ($promises as $promise) {
$promise->onResolve(function ($e, $v) {
$this->assertInstanceOf(WorkerException::class, $e);
});
}
});
}
public function testNotIdleOnEnqueue() {
Loop::run(function () {
$worker = $this->createWorker();