2015-08-29 03:55:30 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Tests\Concurrent\Worker;
|
|
|
|
|
|
|
|
use Amp\Awaitable;
|
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Tests\Concurrent\TestCase;
|
2015-08-29 03:55:30 +02:00
|
|
|
|
|
|
|
abstract class AbstractWorkerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @return \Amp\Concurrent\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
|
|
|
|
|
|
|
public function testIsRunning()
|
|
|
|
{
|
2015-08-31 19:57:40 +02:00
|
|
|
Coroutine\create(function () {
|
2015-12-12 06:31:50 +01:00
|
|
|
$worker = $this->createWorker();
|
2015-08-31 19:57:40 +02:00
|
|
|
$this->assertFalse($worker->isRunning());
|
2015-08-29 03:55:30 +02:00
|
|
|
|
2015-08-31 19:57:40 +02:00
|
|
|
$worker->start();
|
|
|
|
$this->assertTrue($worker->isRunning());
|
2015-08-29 03:55:30 +02:00
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $worker->shutdown();
|
2015-08-31 19:57:40 +02:00
|
|
|
$this->assertFalse($worker->isRunning());
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
2015-08-29 03:55:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsIdleOnStart()
|
|
|
|
{
|
2015-08-31 19:57:40 +02:00
|
|
|
Coroutine\create(function () {
|
2015-12-12 06:31:50 +01:00
|
|
|
$worker = $this->createWorker();
|
2015-08-31 19:57:40 +02:00
|
|
|
$worker->start();
|
2015-08-29 03:55:30 +02:00
|
|
|
|
2015-08-31 19:57:40 +02:00
|
|
|
$this->assertTrue($worker->isIdle());
|
2015-08-29 03:55:30 +02:00
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $worker->shutdown();
|
2015-08-31 19:57:40 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
2015-08-29 03:55:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnqueue()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
2015-12-12 06:31:50 +01:00
|
|
|
$worker = $this->createWorker();
|
2015-08-29 03:55:30 +02:00
|
|
|
$worker->start();
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
$returnValue = yield from $worker->enqueue(new TestTask(42));
|
2015-08-29 03:55:30 +02:00
|
|
|
$this->assertEquals(42, $returnValue);
|
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $worker->shutdown();
|
2015-08-29 03:55:30 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
2015-12-12 01:15:15 +01:00
|
|
|
public function testEnqueueMultiple()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
2015-12-12 06:31:50 +01:00
|
|
|
$worker = $this->createWorker();
|
2015-12-12 01:15:15 +01:00
|
|
|
$worker->start();
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
$values = yield Awaitable\all([
|
2015-12-12 01:15:15 +01:00
|
|
|
new Coroutine\Coroutine($worker->enqueue(new TestTask(42))),
|
|
|
|
new Coroutine\Coroutine($worker->enqueue(new TestTask(56))),
|
|
|
|
new Coroutine\Coroutine($worker->enqueue(new TestTask(72)))
|
2016-01-23 07:00:56 +01:00
|
|
|
]);
|
2015-12-12 01:15:15 +01:00
|
|
|
|
|
|
|
$this->assertEquals([42, 56, 72], $values);
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
yield from $worker->shutdown();
|
2015-12-12 01:15:15 +01:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
2015-08-29 03:55:30 +02:00
|
|
|
public function testNotIdleOnEnqueue()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
2015-12-12 06:31:50 +01:00
|
|
|
$worker = $this->createWorker();
|
2015-08-29 03:55:30 +02:00
|
|
|
$worker->start();
|
|
|
|
|
|
|
|
$coroutine = new Coroutine\Coroutine($worker->enqueue(new TestTask(42)));
|
|
|
|
$this->assertFalse($worker->isIdle());
|
|
|
|
yield $coroutine;
|
|
|
|
|
2016-01-25 06:03:45 +01:00
|
|
|
yield from $worker->shutdown();
|
2015-08-29 03:55:30 +02:00
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
2015-11-11 09:59:22 +01:00
|
|
|
|
|
|
|
public function testKill()
|
|
|
|
{
|
2015-12-12 06:31:50 +01:00
|
|
|
$worker = $this->createWorker();
|
2015-11-11 09:59:22 +01:00
|
|
|
$worker->start();
|
|
|
|
|
|
|
|
$this->assertRunTimeLessThan([$worker, 'kill'], 0.2);
|
|
|
|
$this->assertFalse($worker->isRunning());
|
|
|
|
}
|
2015-08-29 03:55:30 +02:00
|
|
|
}
|