2016-08-21 23:40:48 -05:00
|
|
|
<?php declare(strict_types = 1);
|
2016-01-14 18:08:06 -06:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
namespace Amp\Parallel\Test\Worker;
|
2016-08-18 11:04:48 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
use Amp\Parallel\Worker;
|
|
|
|
use Amp\Parallel\Worker\{ Environment, Pool, Task, WorkerFactory };
|
|
|
|
use Amp\Parallel\Test\TestCase;
|
2016-08-18 17:36:58 -05:00
|
|
|
use Amp\Success;
|
2016-11-14 17:43:44 -06:00
|
|
|
use Interop\Async\Promise;
|
2016-01-14 18:08:06 -06:00
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
class FunctionsTest extends TestCase {
|
|
|
|
public function testPool() {
|
|
|
|
$pool = $this->createMock(Pool::class);
|
2016-01-14 18:08:06 -06:00
|
|
|
|
|
|
|
Worker\pool($pool);
|
|
|
|
|
2016-01-23 11:37:01 -06:00
|
|
|
$this->assertTrue($pool === Worker\pool());
|
2016-01-14 18:08:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-15 09:06:57 -06:00
|
|
|
* @depends testPool
|
2016-01-14 18:08:06 -06:00
|
|
|
*/
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testEnqueue() {
|
|
|
|
$pool = $this->createMock(Pool::class);
|
2016-01-14 18:08:06 -06:00
|
|
|
$pool->method('enqueue')
|
2016-11-14 17:43:44 -06:00
|
|
|
->will($this->returnCallback(function (Task $task): Promise {
|
2016-08-18 17:36:58 -05:00
|
|
|
return new Success($task->run($this->createMock(Environment::class)));
|
2016-01-14 18:08:06 -06:00
|
|
|
}));
|
|
|
|
|
|
|
|
Worker\pool($pool);
|
|
|
|
|
|
|
|
$value = 42;
|
|
|
|
|
|
|
|
$task = new TestTask($value);
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
$awaitable = Worker\enqueue($task);
|
2016-01-14 18:08:06 -06:00
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
$this->assertSame($value, \Amp\wait($awaitable));
|
2016-01-14 18:08:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-15 09:06:57 -06:00
|
|
|
* @depends testPool
|
2016-01-14 18:08:06 -06:00
|
|
|
*/
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testGet() {
|
|
|
|
$pool = $this->createMock(Pool::class);
|
2016-01-14 18:54:53 -06:00
|
|
|
$pool->expects($this->once())
|
|
|
|
->method('get')
|
2016-08-18 17:36:58 -05:00
|
|
|
->will($this->returnValue($this->createMock(Worker\Worker::class)));
|
2016-01-14 18:08:06 -06:00
|
|
|
|
2016-01-14 18:54:53 -06:00
|
|
|
Worker\pool($pool);
|
2016-01-14 18:08:06 -06:00
|
|
|
|
2016-01-14 18:54:53 -06:00
|
|
|
$worker = Worker\get();
|
2016-01-14 18:08:06 -06:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testFactory() {
|
|
|
|
$factory = $this->createMock(WorkerFactory::class);
|
2016-01-14 18:08:06 -06:00
|
|
|
|
|
|
|
Worker\factory($factory);
|
|
|
|
|
2016-01-23 11:37:01 -06:00
|
|
|
$this->assertTrue($factory === Worker\factory());
|
2016-01-14 18:08:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-15 09:06:57 -06:00
|
|
|
* @depends testFactory
|
2016-01-14 18:08:06 -06:00
|
|
|
*/
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testCreate() {
|
|
|
|
$factory = $this->createMock(WorkerFactory::class);
|
2016-01-14 18:08:06 -06:00
|
|
|
$factory->expects($this->once())
|
|
|
|
->method('create')
|
2016-08-18 17:36:58 -05:00
|
|
|
->will($this->returnValue($this->createMock(Worker\Worker::class)));
|
2016-01-14 18:08:06 -06:00
|
|
|
|
|
|
|
Worker\factory($factory);
|
|
|
|
|
|
|
|
$worker = Worker\create();
|
|
|
|
}
|
|
|
|
}
|