2016-01-15 01:08:06 +01:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Tests\Concurrent\Worker;
|
|
|
|
|
|
|
|
use Icicle\Concurrent\Worker;
|
2016-01-23 07:00:56 +01:00
|
|
|
use Icicle\Concurrent\Worker\{Environment, Pool, Task, WorkerFactory};
|
2016-01-15 01:08:06 +01:00
|
|
|
use Icicle\Coroutine\Coroutine;
|
|
|
|
use Icicle\Tests\Concurrent\TestCase;
|
|
|
|
|
|
|
|
class FunctionsTest extends TestCase
|
|
|
|
{
|
2016-01-15 16:06:57 +01:00
|
|
|
public function testPool()
|
2016-01-15 01:08:06 +01:00
|
|
|
{
|
|
|
|
$pool = $this->getMock(Pool::class);
|
|
|
|
|
|
|
|
Worker\pool($pool);
|
|
|
|
|
|
|
|
$this->assertSame($pool, Worker\pool());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-15 16:06:57 +01:00
|
|
|
* @depends testPool
|
2016-01-15 01:08:06 +01:00
|
|
|
*/
|
|
|
|
public function testEnqueue()
|
|
|
|
{
|
|
|
|
$pool = $this->getMock(Pool::class);
|
|
|
|
$pool->method('enqueue')
|
|
|
|
->will($this->returnCallback(function (Task $task) {
|
|
|
|
yield $task->run($this->getMock(Environment::class));
|
|
|
|
}));
|
|
|
|
|
|
|
|
Worker\pool($pool);
|
|
|
|
|
|
|
|
$value = 42;
|
|
|
|
|
|
|
|
$task = new TestTask($value);
|
|
|
|
|
|
|
|
$coroutine = new Coroutine(Worker\enqueue($task));
|
|
|
|
|
|
|
|
$this->assertSame($value, $coroutine->wait());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-15 16:06:57 +01:00
|
|
|
* @depends testPool
|
2016-01-15 01:08:06 +01:00
|
|
|
*/
|
2016-01-15 01:54:53 +01:00
|
|
|
public function testGet()
|
2016-01-15 01:08:06 +01:00
|
|
|
{
|
2016-01-15 01:54:53 +01:00
|
|
|
$pool = $this->getMock(Pool::class);
|
|
|
|
$pool->expects($this->once())
|
|
|
|
->method('get')
|
2016-01-15 01:08:06 +01:00
|
|
|
->will($this->returnValue($this->getMock(Worker\Worker::class)));
|
|
|
|
|
2016-01-15 01:54:53 +01:00
|
|
|
Worker\pool($pool);
|
2016-01-15 01:08:06 +01:00
|
|
|
|
2016-01-15 01:54:53 +01:00
|
|
|
$worker = Worker\get();
|
2016-01-15 01:08:06 +01:00
|
|
|
}
|
|
|
|
|
2016-01-15 16:06:57 +01:00
|
|
|
public function testFactory()
|
2016-01-15 01:08:06 +01:00
|
|
|
{
|
|
|
|
$factory = $this->getMock(WorkerFactory::class);
|
|
|
|
|
|
|
|
Worker\factory($factory);
|
|
|
|
|
|
|
|
$this->assertSame($factory, Worker\factory());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-15 16:06:57 +01:00
|
|
|
* @depends testFactory
|
2016-01-15 01:08:06 +01:00
|
|
|
*/
|
|
|
|
public function testCreate()
|
|
|
|
{
|
|
|
|
$factory = $this->getMock(WorkerFactory::class);
|
|
|
|
$factory->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->will($this->returnValue($this->getMock(Worker\Worker::class)));
|
|
|
|
|
|
|
|
Worker\factory($factory);
|
|
|
|
|
|
|
|
$worker = Worker\create();
|
|
|
|
}
|
|
|
|
}
|