1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 02:17:54 +01:00
amp/test/Pipeline/FromIterableTest.php

145 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2020-08-23 16:18:28 +02:00
namespace Amp\Test\Pipeline;
2020-05-17 21:41:42 +02:00
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
2020-08-23 16:18:28 +02:00
use Amp\Pipeline;
2021-04-04 20:10:23 +02:00
use Revolt\Future\Future;
2021-03-26 22:34:32 +01:00
use function Revolt\EventLoop\delay;
2021-04-04 20:10:23 +02:00
use function Revolt\Future\spawn;
2020-05-17 21:41:42 +02:00
class FromIterableTest extends AsyncTestCase
2018-06-18 20:00:01 +02:00
{
const TIMEOUT = 10;
2020-09-28 05:19:52 +02:00
public function testSuccessfulPromises(): void
2018-06-18 20:00:01 +02:00
{
2020-05-17 21:41:42 +02:00
$expected = \range(1, 3);
2021-04-04 20:10:23 +02:00
$pipeline = Pipeline\fromIterable([Future::complete(1), Future::complete(2), Future::complete(3)]);
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
while (null !== $value = $pipeline->continue()) {
2021-03-26 22:34:32 +01:00
self::assertSame(\array_shift($expected), $value);
2020-05-17 21:41:42 +02:00
}
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
public function testFailedPromises(): void
2018-06-18 20:00:01 +02:00
{
2020-05-17 21:41:42 +02:00
$exception = new \Exception;
2021-04-04 20:10:23 +02:00
$iterator = Pipeline\fromIterable([Future::error($exception), Future::error($exception)]);
2020-05-17 21:41:42 +02:00
$this->expectExceptionObject($exception);
2020-09-28 05:19:52 +02:00
$iterator->continue();
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
public function testMixedPromises(): void
2018-06-18 20:00:01 +02:00
{
2020-05-17 21:41:42 +02:00
$exception = new TestException;
$expected = \range(1, 2);
2021-04-04 20:10:23 +02:00
$pipeline = Pipeline\fromIterable([Future::complete(1), Future::complete(2), Future::error($exception), Future::complete(4)]);
2020-05-17 21:41:42 +02:00
try {
2020-09-28 05:19:52 +02:00
while (null !== $value = $pipeline->continue()) {
2021-03-26 22:34:32 +01:00
self::assertSame(\array_shift($expected), $value);
2017-04-27 17:32:53 +02:00
}
2021-03-26 22:34:32 +01:00
self::fail("A failed promise in the iterable should fail the pipeline and be thrown from continue()");
2020-05-17 21:41:42 +02:00
} catch (TestException $reason) {
2021-03-26 22:34:32 +01:00
self::assertSame($exception, $reason);
2020-05-17 21:41:42 +02:00
}
2017-01-06 23:16:06 +01:00
2021-03-26 22:34:32 +01:00
self::assertEmpty($expected);
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
public function testPendingPromises(): void
2018-06-18 20:00:01 +02:00
{
2020-05-17 21:41:42 +02:00
$expected = \range(1, 4);
2020-08-23 16:18:28 +02:00
$pipeline = Pipeline\fromIterable([
2021-04-04 20:10:23 +02:00
$this->asyncValue(30, 1),
$this->asyncValue(10, 2),
$this->asyncValue(20, 3),
Future::complete(4),
2020-05-17 21:41:42 +02:00
]);
2020-09-28 05:19:52 +02:00
while (null !== $value = $pipeline->continue()) {
2021-03-26 22:34:32 +01:00
self::assertSame(\array_shift($expected), $value);
2020-05-17 21:41:42 +02:00
}
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
public function testTraversable(): void
2018-06-18 20:00:01 +02:00
{
2020-05-17 21:41:42 +02:00
$expected = \range(1, 4);
$generator = (static function () {
foreach (\range(1, 4) as $value) {
yield $value;
}
})();
2017-01-06 23:16:06 +01:00
2020-08-23 16:18:28 +02:00
$pipeline = Pipeline\fromIterable($generator);
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
while (null !== $value = $pipeline->continue()) {
2021-03-26 22:34:32 +01:00
self::assertSame(\array_shift($expected), $value);
2020-05-17 21:41:42 +02:00
}
2017-01-06 23:16:06 +01:00
2021-03-26 22:34:32 +01:00
self::assertEmpty($expected);
2017-01-04 02:10:27 +01:00
}
2017-01-06 23:16:06 +01:00
/**
* @dataProvider provideInvalidIteratorArguments
2017-01-06 23:16:06 +01:00
*/
2020-09-28 05:19:52 +02:00
public function testInvalid($arg): void
2018-06-18 20:00:01 +02:00
{
2020-05-13 17:15:21 +02:00
$this->expectException(\TypeError::class);
2020-08-23 16:18:28 +02:00
Pipeline\fromIterable($arg);
2017-01-06 23:16:06 +01:00
}
2020-05-17 21:41:42 +02:00
public function provideInvalidIteratorArguments(): array
2018-06-18 20:00:01 +02:00
{
2017-01-06 23:16:06 +01:00
return [
[null],
[new \stdClass],
[32],
[false],
[true],
["string"],
];
}
2020-09-28 05:19:52 +02:00
public function testInterval(): void
2018-06-18 20:00:01 +02:00
{
2020-05-17 21:41:42 +02:00
$count = 3;
2020-08-23 16:18:28 +02:00
$pipeline = Pipeline\fromIterable(\range(1, $count), self::TIMEOUT);
2020-05-17 21:41:42 +02:00
$i = 0;
2020-09-28 05:19:52 +02:00
while (null !== $value = $pipeline->continue()) {
2021-03-26 22:34:32 +01:00
self::assertSame(++$i, $value);
2020-05-17 21:41:42 +02:00
}
2021-03-26 22:34:32 +01:00
self::assertSame($count, $i);
}
/**
* @depends testInterval
*/
2020-09-28 05:19:52 +02:00
public function testSlowConsumer(): void
2018-06-18 20:00:01 +02:00
{
$count = 5;
2020-08-23 16:18:28 +02:00
$pipeline = Pipeline\fromIterable(\range(1, $count), self::TIMEOUT);
2020-09-28 05:19:52 +02:00
for ($i = 0; $value = $pipeline->continue(); ++$i) {
delay(self::TIMEOUT * 2);
2020-05-17 21:41:42 +02:00
}
2021-03-26 22:34:32 +01:00
self::assertSame($count, $i);
}
2021-04-04 20:10:23 +02:00
private function asyncValue(int $delay, mixed $value): Future
{
return spawn(static function () use ($delay, $value): mixed {
delay($delay);
return $value;
});
}
}