2016-01-15 01:08:06 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Tests\Concurrent\Worker;
|
|
|
|
|
|
|
|
use Amp\Concurrent\Worker;
|
|
|
|
use Amp\Concurrent\Worker\{Environment, Pool, Task, WorkerFactory};
|
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Tests\Concurrent\TestCase;
|
2016-01-15 01:08:06 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-01-23 18:37:01 +01:00
|
|
|
$this->assertTrue($pool === Worker\pool());
|
2016-01-15 01:08:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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) {
|
2016-01-23 18:37:01 +01:00
|
|
|
return yield $task->run($this->getMock(Environment::class));
|
2016-01-15 01:08:06 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-01-23 18:37:01 +01:00
|
|
|
$this->assertTrue($factory === Worker\factory());
|
2016-01-15 01:08:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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();
|
|
|
|
}
|
|
|
|
}
|