1
0
mirror of https://github.com/danog/amp.git synced 2024-12-13 01:47:33 +01:00
amp/test/ProducerTest.php

118 lines
2.9 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;
2020-09-28 05:19:52 +02:00
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
2017-04-23 14:39:19 +02:00
use Amp\Producer;
2020-09-28 05:19:52 +02:00
use function Amp\await;
use function Amp\delay;
2016-12-17 15:16:17 +01:00
2020-09-28 05:19:52 +02:00
class ProducerTest extends AsyncTestCase
2018-06-18 20:00:01 +02:00
{
2017-01-04 02:10:27 +01:00
const TIMEOUT = 100;
2020-09-28 05:19:52 +02:00
public function testNonGeneratorCallable(): void
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$this->expectException(\Error::class);
$this->expectExceptionMessage("The callable did not return a Generator");
2018-06-18 20:00:01 +02:00
new Producer(function () {
});
2016-12-17 15:16:17 +01:00
}
2020-09-28 05:19:52 +02:00
public function testEmit(): void
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$value = 1;
2020-09-28 05:19:52 +02:00
$producer = new Producer(function (callable $emit) use ($value) {
yield $emit($value);
2017-04-23 13:52:23 +02:00
});
2020-09-28 05:19:52 +02:00
$this->assertTrue(await($producer->advance()));
$this->assertSame($producer->getCurrent(), $value);
2016-12-17 15:16:17 +01:00
}
2016-12-17 15:16:17 +01:00
/**
* @depends testEmit
*/
2020-09-28 05:19:52 +02:00
public function testEmitSuccessfulPromise(): void
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$deferred = new Deferred();
2020-09-28 05:19:52 +02:00
$producer = new Producer(function (callable $emit) use ($deferred) {
yield $emit($deferred->promise());
});
2020-09-28 05:19:52 +02:00
$value = 1;
$deferred->resolve($value);
2020-09-28 05:19:52 +02:00
$this->assertTrue(await($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
*/
2020-09-28 05:19:52 +02:00
public function testEmitFailedPromise(): void
2018-06-18 20:00:01 +02:00
{
$exception = new TestException;
2020-09-28 05:19:52 +02:00
$deferred = new Deferred();
2020-09-28 05:19:52 +02:00
$producer = new Producer(function (callable $emit) use ($deferred) {
return yield $emit($deferred->promise());
});
2020-09-28 05:19:52 +02:00
$deferred->fail($exception);
2020-09-28 05:19:52 +02:00
try {
await($producer->advance());
$this->fail("Emitting a failed promise should fail the iterator");
} catch (TestException $reason) {
$this->assertSame($reason, $exception);
}
}
2016-12-17 15:16:17 +01:00
/**
* @depends testEmit
*/
2020-09-28 05:19:52 +02:00
public function testEmitBackPressure(): void
2018-06-18 20:00:01 +02:00
{
2017-01-04 02:10:27 +01:00
$emits = 3;
2020-09-28 05:19:52 +02:00
$producer = new Producer(function (callable $emit) use (&$time, $emits) {
$time = \microtime(true);
for ($i = 0; $i < $emits; ++$i) {
yield $emit($i);
2017-04-27 17:32:53 +02:00
}
2020-09-28 05:19:52 +02:00
$time = \microtime(true) - $time;
});
2020-09-28 05:19:52 +02:00
while (await($producer->advance())) {
delay(self::TIMEOUT);
2020-09-28 05:19:52 +02:00
}
$this->assertGreaterThan(self::TIMEOUT * ($emits - 1), $time * 1000);
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
*/
2020-09-28 05:19:52 +02:00
public function testProducerCoroutineThrows(): void
2018-06-18 20:00:01 +02:00
{
$exception = new TestException;
2016-12-17 15:16:17 +01:00
try {
2020-09-28 05:19:52 +02:00
$producer = new Producer(function (callable $emit) use ($exception) {
yield $emit(1);
throw $exception;
});
2020-09-28 05:19:52 +02:00
while (await($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);
}
}
}