1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 17:52:14 +01:00
parallel/test/Worker/FunctionsTest.php

82 lines
1.8 KiB
PHP
Raw Normal View History

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
{
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
}
/**
* @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());
}
/**
* @depends testPool
2016-01-15 01:08:06 +01:00
*/
public function testGet()
2016-01-15 01:08:06 +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)));
Worker\pool($pool);
2016-01-15 01:08:06 +01:00
$worker = Worker\get();
2016-01-15 01:08:06 +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
}
/**
* @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();
}
}