1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 17:52:14 +01:00
parallel/test/Worker/AbstractWorkerTest.php

123 lines
3.3 KiB
PHP
Raw Normal View History

2016-12-30 02:16:04 +01:00
<?php
2015-08-29 03:55:30 +02:00
2016-08-23 23:47:40 +02:00
namespace Amp\Parallel\Test\Worker;
2016-08-18 18:04:48 +02:00
use Amp\Loop;
use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\Task;
use Amp\Parallel\Worker\TaskError;
use Amp\PHPUnit\TestCase;
2015-08-29 03:55:30 +02:00
class NonAutoloadableTask implements Task {
public function run(Environment $environment) {
return 1;
}
}
2016-08-19 00:36:58 +02:00
abstract class AbstractWorkerTest extends TestCase {
2015-08-29 03:55:30 +02:00
/**
2016-08-23 23:47:40 +02:00
* @return \Amp\Parallel\Worker\Worker
2015-08-29 03:55:30 +02:00
*/
2015-12-12 06:31:50 +01:00
abstract protected function createWorker();
2015-08-29 03:55:30 +02:00
2017-07-28 06:49:43 +02:00
public function testWorkerConstantDefined() {
Loop::run(function () {
$worker = $this->createWorker();
$worker->start();
$this->assertTrue(yield $worker->enqueue(new ConstantTask));
yield $worker->shutdown();
});
}
2016-08-19 00:36:58 +02:00
public function testIsRunning() {
Loop::run(function () {
2015-12-12 06:31:50 +01:00
$worker = $this->createWorker();
$this->assertFalse($worker->isRunning());
2015-08-29 03:55:30 +02:00
$worker->start();
$this->assertTrue($worker->isRunning());
2015-08-29 03:55:30 +02:00
2016-08-19 00:36:58 +02:00
yield $worker->shutdown();
$this->assertFalse($worker->isRunning());
});
2015-08-29 03:55:30 +02:00
}
2016-08-19 00:36:58 +02:00
public function testIsIdleOnStart() {
Loop::run(function () {
2015-12-12 06:31:50 +01:00
$worker = $this->createWorker();
$worker->start();
2015-08-29 03:55:30 +02:00
$this->assertTrue($worker->isIdle());
2015-08-29 03:55:30 +02:00
2016-08-19 00:36:58 +02:00
yield $worker->shutdown();
});
2015-08-29 03:55:30 +02:00
}
2016-08-19 00:36:58 +02:00
public function testEnqueue() {
Loop::run(function () {
2015-12-12 06:31:50 +01:00
$worker = $this->createWorker();
2015-08-29 03:55:30 +02:00
$worker->start();
2016-08-19 00:36:58 +02:00
$returnValue = yield $worker->enqueue(new TestTask(42));
2015-08-29 03:55:30 +02:00
$this->assertEquals(42, $returnValue);
2016-08-19 00:36:58 +02:00
yield $worker->shutdown();
});
2015-08-29 03:55:30 +02:00
}
2016-08-19 00:36:58 +02:00
public function testEnqueueMultiple() {
Loop::run(function () {
2015-12-12 06:31:50 +01:00
$worker = $this->createWorker();
$worker->start();
2017-05-18 09:51:31 +02:00
$values = yield \Amp\Promise\all([
2016-08-19 00:36:58 +02:00
$worker->enqueue(new TestTask(42)),
$worker->enqueue(new TestTask(56)),
$worker->enqueue(new TestTask(72))
2016-01-23 07:00:56 +01:00
]);
$this->assertEquals([42, 56, 72], $values);
2016-08-19 00:36:58 +02:00
yield $worker->shutdown();
});
}
2016-08-19 00:36:58 +02:00
public function testNotIdleOnEnqueue() {
Loop::run(function () {
2015-12-12 06:31:50 +01:00
$worker = $this->createWorker();
2015-08-29 03:55:30 +02:00
$worker->start();
2016-08-19 00:36:58 +02:00
$coroutine = $worker->enqueue(new TestTask(42));
2015-08-29 03:55:30 +02:00
$this->assertFalse($worker->isIdle());
yield $coroutine;
2016-08-19 00:36:58 +02:00
yield $worker->shutdown();
});
2015-08-29 03:55:30 +02:00
}
2016-08-19 00:36:58 +02:00
public function testKill() {
2015-12-12 06:31:50 +01:00
$worker = $this->createWorker();
$worker->start();
$this->assertRunTimeLessThan([$worker, 'kill'], 250);
$this->assertFalse($worker->isRunning());
}
public function testUnserializableTask() {
Loop::run(function () {
$worker = $this->createWorker();
$worker->start();
try {
yield $worker->enqueue(new NonAutoloadableTask);
$this->fail("Tasks that cannot be autoloaded should throw an exception");
} catch (TaskError $exception) {
$this->assertSame("Error", $exception->getName());
$this->assertGreaterThan(0, \strpos($exception->getMessage(), \sprintf("Classes implementing %s", Task::class)));
}
yield $worker->shutdown();
});
}
2015-08-29 03:55:30 +02:00
}