2015-08-29 03:55:30 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Tests\Concurrent\Worker;
|
|
|
|
|
|
|
|
use Icicle\Coroutine;
|
|
|
|
use Icicle\Loop;
|
|
|
|
use Icicle\Tests\Concurrent\TestCase;
|
|
|
|
|
|
|
|
abstract class AbstractWorkerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return \Icicle\Concurrent\Worker\WorkerInterface
|
|
|
|
*/
|
|
|
|
abstract protected function createWorker();
|
|
|
|
|
|
|
|
public function testIsRunning()
|
|
|
|
{
|
2015-08-31 19:57:40 +02:00
|
|
|
Coroutine\create(function () {
|
|
|
|
$worker = $this->createWorker();
|
|
|
|
$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
|
|
|
|
2015-08-31 19:57:40 +02:00
|
|
|
yield $worker->shutdown();
|
|
|
|
$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 () {
|
|
|
|
$worker = $this->createWorker();
|
|
|
|
$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
|
|
|
|
2015-08-31 19:57:40 +02:00
|
|
|
yield $worker->shutdown();
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
2015-08-29 03:55:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnqueue()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$worker = $this->createWorker();
|
|
|
|
$worker->start();
|
|
|
|
|
|
|
|
$returnValue = (yield $worker->enqueue(new TestTask(42)));
|
|
|
|
$this->assertEquals(42, $returnValue);
|
|
|
|
|
|
|
|
yield $worker->shutdown();
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotIdleOnEnqueue()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$worker = $this->createWorker();
|
|
|
|
$worker->start();
|
|
|
|
|
|
|
|
$coroutine = new Coroutine\Coroutine($worker->enqueue(new TestTask(42)));
|
|
|
|
$this->assertFalse($worker->isIdle());
|
|
|
|
yield $coroutine;
|
|
|
|
|
|
|
|
yield $worker->shutdown();
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
}
|