1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-03 10:07:49 +01:00
parallel/tests/Worker/AbstractWorkerTest.php

106 lines
2.6 KiB
PHP
Raw Normal View History

2015-08-29 03:55:30 +02:00
<?php
namespace Icicle\Tests\Concurrent\Worker;
use Icicle\Awaitable;
2015-08-29 03:55:30 +02:00
use Icicle\Coroutine;
use Icicle\Loop;
use Icicle\Tests\Concurrent\TestCase;
abstract class AbstractWorkerTest extends TestCase
{
/**
2015-12-12 06:31:50 +01:00
* @return \Icicle\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()
{
Coroutine\create(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
yield $worker->shutdown();
$this->assertFalse($worker->isRunning());
})->done();
Loop\run();
2015-08-29 03:55:30 +02:00
}
public function testIsIdleOnStart()
{
Coroutine\create(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
yield $worker->shutdown();
})->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();
$returnValue = (yield $worker->enqueue(new TestTask(42)));
$this->assertEquals(42, $returnValue);
yield $worker->shutdown();
})->done();
Loop\run();
}
public function testEnqueueMultiple()
{
Coroutine\create(function () {
2015-12-12 06:31:50 +01:00
$worker = $this->createWorker();
$worker->start();
$values = (yield Awaitable\all([
new Coroutine\Coroutine($worker->enqueue(new TestTask(42))),
new Coroutine\Coroutine($worker->enqueue(new TestTask(56))),
new Coroutine\Coroutine($worker->enqueue(new TestTask(72)))
]));
$this->assertEquals([42, 56, 72], $values);
yield $worker->shutdown();
})->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;
yield $worker->shutdown();
})->done();
Loop\run();
}
public function testKill()
{
2015-12-12 06:31:50 +01:00
$worker = $this->createWorker();
$worker->start();
$this->assertRunTimeLessThan([$worker, 'kill'], 0.2);
$this->assertFalse($worker->isRunning());
}
2015-08-29 03:55:30 +02:00
}