1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/test/ProducerTest.php

128 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2016-12-17 15:16:17 +01:00
namespace Amp\Test;
2017-03-11 14:43:57 +01:00
use Amp\Deferred;
use Amp\Delayed;
use Amp\Loop;
use Amp\PHPUnit\TestException;
2017-04-23 14:39:19 +02:00
use Amp\Producer;
2016-12-17 15:16:17 +01:00
class ProducerTest extends BaseTest
2018-06-18 20:00:01 +02:00
{
2017-01-04 02:10:27 +01:00
const TIMEOUT = 100;
2017-01-04 02:10:27 +01:00
/**
* @expectedException \Error
* @expectedExceptionMessage The callable did not return a Generator
*/
2018-06-18 20:00:01 +02:00
public function testNonGeneratorCallable()
{
new Producer(function () {
});
2016-12-17 15:16:17 +01:00
}
2018-06-18 20:00:01 +02: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-04 02:10:27 +01:00
$producer = new Producer(function (callable $emit) use ($value) {
yield $emit($value);
});
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
}
2016-12-17 15:16:17 +01:00
/**
* @depends testEmit
*/
2018-06-18 20:00:01 +02:00
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-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-04 02:10:27 +01:00
$value = 1;
$deferred->resolve($value);
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
}
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
*/
2018-06-18 20:00:01 +02:00
public function testEmitFailedPromise()
{
$exception = new TestException;
Loop::run(function () use ($exception) {
2017-01-04 02:10:27 +01:00
$deferred = new Deferred();
2017-01-04 02:10:27 +01:00
$producer = new Producer(function (callable $emit) use ($deferred) {
return yield $emit($deferred->promise());
});
2017-01-04 02:10:27 +01:00
$deferred->fail($exception);
2017-04-27 17:32:53 +02:00
try {
yield $producer->advance();
$this->fail("Emitting a failed promise should fail the iterator");
} catch (TestException $reason) {
2017-01-04 02:10:27 +01:00
$this->assertSame($reason, $exception);
2017-04-27 17:32:53 +02:00
}
});
2016-12-17 15:16:17 +01:00
}
2016-12-17 15:16:17 +01:00
/**
* @depends testEmit
*/
2018-06-18 20:00:01 +02:00
public function testEmitBackPressure()
{
2017-01-04 02:10:27 +01:00
$emits = 3;
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);
2017-01-04 02:10:27 +01:00
for ($i = 0; $i < $emits; ++$i) {
yield $emit($i);
}
$time = \microtime(true) - $time;
2017-01-04 02:10:27 +01:00
});
2017-04-27 17:32:53 +02:00
while (yield $producer->advance()) {
yield new Delayed(self::TIMEOUT);
}
});
$this->assertGreaterThan(self::TIMEOUT * ($emits - 1), $time * 1000);
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
*/
2018-06-18 20:00:01 +02:00
public function testProducerCoroutineThrows()
{
$exception = new TestException;
2016-12-17 15:16:17 +01:00
try {
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
});
2018-06-18 20:00:01 +02:00
while (yield $producer->advance()) ;
$this->fail("The exception thrown from the coroutine should fail the iterator");
});
} catch (TestException $caught) {
2016-12-17 15:16:17 +01:00
$this->assertSame($exception, $caught);
}
}
}