2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2016-01-15 01:08:06 +01:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Test\Worker;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\Worker;
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Parallel\Worker\Environment;
|
|
|
|
use Amp\Parallel\Worker\Pool;
|
|
|
|
use Amp\Parallel\Worker\Task;
|
|
|
|
use Amp\Parallel\Worker\WorkerFactory;
|
2019-08-27 19:17:41 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Promise;
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\Success;
|
2016-01-15 01:08:06 +01:00
|
|
|
|
2019-08-28 01:27:33 +02:00
|
|
|
function nonAutoloadableFunction(): void
|
|
|
|
{
|
|
|
|
// Empty test function
|
|
|
|
}
|
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
class FunctionsTest extends AsyncTestCase
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
|
|
|
public function testPool()
|
|
|
|
{
|
2016-08-19 00:36:58 +02:00
|
|
|
$pool = $this->createMock(Pool::class);
|
2016-01-15 01:08:06 +01:00
|
|
|
|
|
|
|
Worker\pool($pool);
|
|
|
|
|
2018-03-21 21:03:24 +01:00
|
|
|
$this->assertSame(Worker\pool(), $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
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testEnqueue()
|
|
|
|
{
|
2016-08-19 00:36:58 +02:00
|
|
|
$pool = $this->createMock(Pool::class);
|
2016-01-15 01:08:06 +01:00
|
|
|
$pool->method('enqueue')
|
2016-11-15 00:43:44 +01:00
|
|
|
->will($this->returnCallback(function (Task $task): Promise {
|
2016-08-19 00:36:58 +02:00
|
|
|
return new Success($task->run($this->createMock(Environment::class)));
|
2016-01-15 01:08:06 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
Worker\pool($pool);
|
|
|
|
|
|
|
|
$value = 42;
|
|
|
|
|
2019-01-25 21:29:32 +01:00
|
|
|
$task = new Fixtures\TestTask($value);
|
2016-01-15 01:08:06 +01:00
|
|
|
|
2019-08-28 01:27:33 +02:00
|
|
|
$this->assertSame($value, yield Worker\enqueue($task));
|
2016-01-15 01:08:06 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 09:36:21 +01:00
|
|
|
/**
|
|
|
|
* @depends testPool
|
|
|
|
*/
|
|
|
|
public function testEnqueueCallable()
|
|
|
|
{
|
|
|
|
$pool = $this->createMock(Pool::class);
|
|
|
|
$pool->method('enqueue')
|
|
|
|
->will($this->returnCallback(function (Task $task): Promise {
|
|
|
|
return new Success($task->run($this->createMock(Environment::class)));
|
|
|
|
}));
|
|
|
|
|
|
|
|
Worker\pool($pool);
|
|
|
|
|
|
|
|
$value = 42;
|
|
|
|
|
2019-08-28 01:27:33 +02:00
|
|
|
$this->assertSame('42', yield Worker\enqueueCallable('strval', $value));
|
2018-11-01 09:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testEnqueueCallable
|
|
|
|
*/
|
|
|
|
public function testEnqueueCallableIntegration()
|
|
|
|
{
|
2019-08-28 01:27:33 +02:00
|
|
|
Worker\pool(new Worker\DefaultPool);
|
2018-11-01 09:36:21 +01:00
|
|
|
|
|
|
|
$value = 42;
|
|
|
|
|
2019-08-28 01:27:33 +02:00
|
|
|
$this->assertSame('42', yield Worker\enqueueCallable('strval', $value));
|
2018-11-01 09:36:21 +01:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:27:33 +02:00
|
|
|
/**
|
|
|
|
* @depends testEnqueueCallable
|
|
|
|
*/
|
|
|
|
public function testEnqueueNonAutoloadableCallable()
|
|
|
|
{
|
|
|
|
$this->expectException(Worker\TaskError::class);
|
|
|
|
$this->expectExceptionMessage('User-defined functions must be autoloadable');
|
|
|
|
|
|
|
|
Worker\pool(new Worker\DefaultPool);
|
|
|
|
|
|
|
|
yield Worker\enqueueCallable(__NAMESPACE__ . '\\nonAutoloadableFunction');
|
|
|
|
}
|
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
|
|
|
*/
|
2018-10-27 16:56:09 +02:00
|
|
|
public function testWorker()
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2016-08-19 00:36:58 +02:00
|
|
|
$pool = $this->createMock(Pool::class);
|
2016-01-15 01:54:53 +01:00
|
|
|
$pool->expects($this->once())
|
2018-10-27 16:56:09 +02:00
|
|
|
->method('getWorker')
|
2016-08-19 00:36:58 +02:00
|
|
|
->will($this->returnValue($this->createMock(Worker\Worker::class)));
|
2016-01-15 01:08:06 +01:00
|
|
|
|
2016-01-15 01:54:53 +01:00
|
|
|
Worker\pool($pool);
|
2016-01-15 01:08:06 +01:00
|
|
|
|
2018-10-27 16:56:09 +02:00
|
|
|
$worker = Worker\worker();
|
2016-01-15 01:08:06 +01:00
|
|
|
}
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testFactory()
|
|
|
|
{
|
2016-08-19 00:36:58 +02:00
|
|
|
$factory = $this->createMock(WorkerFactory::class);
|
2016-01-15 01:08:06 +01:00
|
|
|
|
|
|
|
Worker\factory($factory);
|
|
|
|
|
2018-03-21 21:03:24 +01:00
|
|
|
$this->assertSame(Worker\factory(), $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
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testCreate()
|
|
|
|
{
|
2016-08-19 00:36:58 +02:00
|
|
|
$factory = $this->createMock(WorkerFactory::class);
|
2016-01-15 01:08:06 +01:00
|
|
|
$factory->expects($this->once())
|
|
|
|
->method('create')
|
2016-08-19 00:36:58 +02:00
|
|
|
->will($this->returnValue($this->createMock(Worker\Worker::class)));
|
2016-01-15 01:08:06 +01:00
|
|
|
|
|
|
|
Worker\factory($factory);
|
2018-11-01 09:36:21 +01:00
|
|
|
Worker\create(); // shouldn't throw
|
2016-01-15 01:08:06 +01:00
|
|
|
}
|
|
|
|
}
|