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

80 lines
1.9 KiB
PHP
Raw Normal View History

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;
use Amp\PHPUnit\TestCase;
use Amp\Promise;
2017-05-18 09:51:31 +02:00
use Amp\Success;
2016-01-15 01:08:06 +01:00
2016-08-19 00:36:58 +02:00
class FunctionsTest extends TestCase {
public function testPool() {
$pool = $this->createMock(Pool::class);
2016-01-15 01:08:06 +01:00
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
*/
2016-08-19 00:36:58 +02:00
public function testEnqueue() {
$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;
$task = new TestTask($value);
2016-08-19 00:36:58 +02:00
$awaitable = Worker\enqueue($task);
2016-01-15 01:08:06 +01:00
$this->assertSame($value, Promise\wait($awaitable));
2016-01-15 01:08:06 +01:00
}
/**
* @depends testPool
2016-01-15 01:08:06 +01:00
*/
2016-08-19 00:36:58 +02:00
public function testGet() {
$pool = $this->createMock(Pool::class);
$pool->expects($this->once())
->method('get')
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\pool($pool);
2016-01-15 01:08:06 +01:00
$worker = Worker\get();
2016-01-15 01:08:06 +01:00
}
2016-08-19 00:36:58 +02:00
public function testFactory() {
$factory = $this->createMock(WorkerFactory::class);
2016-01-15 01:08:06 +01:00
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
*/
2016-08-19 00:36:58 +02:00
public function testCreate() {
$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);
$worker = Worker\create();
}
}