2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-17 15:16:17 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-03-11 14:43:57 +01:00
|
|
|
use Amp\Deferred;
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Producer;
|
2017-03-12 21:02:26 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-12-17 15:16:17 +01:00
|
|
|
|
2017-03-12 21:02:26 +01:00
|
|
|
class ProducerTest extends TestCase {
|
2017-01-04 02:10:27 +01:00
|
|
|
const TIMEOUT = 100;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Error
|
|
|
|
* @expectedExceptionMessage The callable did not return a Generator
|
|
|
|
*/
|
|
|
|
public function testNonGeneratorCallable() {
|
2017-03-10 21:31:57 +01:00
|
|
|
new Producer(function () {});
|
2016-12-17 15:16:17 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
public function testEmit() {
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
2017-01-04 02:10:27 +01:00
|
|
|
$value = 1;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($value) {
|
|
|
|
yield $emit($value);
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertTrue(yield $producer->advance());
|
|
|
|
$this->assertSame($producer->getCurrent(), $value);
|
2017-04-23 13:52:23 +02:00
|
|
|
});
|
2016-12-17 15:16:17 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testEmitSuccessfulPromise() {
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
2017-01-04 02:10:27 +01:00
|
|
|
$deferred = new Deferred();
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($deferred) {
|
2017-04-27 17:32:53 +02:00
|
|
|
yield $emit($deferred->promise());
|
2017-01-04 02:10:27 +01:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$value = 1;
|
|
|
|
$deferred->resolve($value);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertTrue(yield $producer->advance());
|
|
|
|
$this->assertSame($producer->getCurrent(), $value);
|
|
|
|
});
|
2016-12-17 15:16:17 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
2017-01-04 02:10:27 +01:00
|
|
|
* @depends testEmitSuccessfulPromise
|
2016-12-17 15:16:17 +01:00
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testEmitFailedPromise() {
|
|
|
|
$exception = new \Exception;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use ($exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$deferred = new Deferred();
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($deferred) {
|
|
|
|
return yield $emit($deferred->promise());
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$deferred->fail($exception);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
try {
|
|
|
|
yield $producer->advance();
|
|
|
|
} catch (\Exception $reason) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertSame($reason, $exception);
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2016-12-17 15:16:17 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testEmitBackPressure() {
|
|
|
|
$emits = 3;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$time, $emits) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use (&$time, $emits) {
|
|
|
|
$time = microtime(true);
|
|
|
|
for ($i = 0; $i < $emits; ++$i) {
|
|
|
|
yield $emit($i);
|
|
|
|
}
|
|
|
|
$time = microtime(true) - $time;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
while (yield $producer->advance()) {
|
|
|
|
yield new Delayed(self::TIMEOUT);
|
|
|
|
}
|
2017-03-12 21:02:26 +01:00
|
|
|
});
|
|
|
|
|
2017-03-14 22:18:47 +01:00
|
|
|
$this->assertGreaterThan(self::TIMEOUT * $emits - 1 /* 1ms grace period */, $time * 1000);
|
2017-03-12 21:02:26 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testProducerCoroutineThrows() {
|
2016-12-17 15:16:17 +01:00
|
|
|
$exception = new \Exception;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
try {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use ($exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($exception) {
|
|
|
|
yield $emit(1);
|
|
|
|
throw $exception;
|
2016-12-17 15:16:17 +01:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
while (yield $producer->advance());
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2016-12-17 15:16:17 +01:00
|
|
|
} catch (\Exception $caught) {
|
|
|
|
$this->assertSame($exception, $caught);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|