2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
namespace Amp\Test\Stream;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
use Amp\AsyncGenerator;
|
2017-05-02 07:22:53 +02:00
|
|
|
use Amp\Delayed;
|
2020-05-17 21:41:42 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-04-28 06:53:04 +02:00
|
|
|
use Amp\PHPUnit\TestException;
|
2020-05-13 17:15:21 +02:00
|
|
|
use Amp\Stream;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
class MergeTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
public function getArrays(): array
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 00:28:22 +01:00
|
|
|
return [
|
2017-03-14 00:52:57 +01:00
|
|
|
[[\range(1, 3), \range(4, 6)], [1, 4, 2, 5, 3, 6]],
|
|
|
|
[[\range(1, 5), \range(6, 8)], [1, 6, 2, 7, 3, 8, 4, 5]],
|
|
|
|
[[\range(1, 4), \range(5, 10)], [1, 5, 2, 6, 3, 7, 4, 8, 9, 10]],
|
2016-12-16 00:28:22 +01:00
|
|
|
];
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
2017-03-14 00:52:57 +01:00
|
|
|
* @dataProvider getArrays
|
2016-12-16 00:28:22 +01:00
|
|
|
*
|
2020-05-17 21:41:42 +02:00
|
|
|
* @param array $streams
|
2016-12-16 00:28:22 +01:00
|
|
|
* @param array $expected
|
|
|
|
*/
|
2020-05-17 21:41:42 +02:00
|
|
|
public function testMerge(array $streams, array $expected)
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$streams = \array_map(static function (array $iterator): Stream {
|
|
|
|
return Stream\fromIterable($iterator);
|
|
|
|
}, $streams);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
$stream = Stream\merge($streams);
|
2017-03-14 00:52:57 +01:00
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
while (null !== $value = yield $stream->continue()) {
|
|
|
|
$this->assertSame(\array_shift($expected), $value);
|
2020-05-17 21:41:42 +02:00
|
|
|
}
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testMerge
|
|
|
|
*/
|
2020-05-13 17:15:21 +02:00
|
|
|
public function testMergeWithDelayedYields()
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$streams = [];
|
|
|
|
$values1 = [new Delayed(10, 1), new Delayed(50, 2), new Delayed(70, 3)];
|
|
|
|
$values2 = [new Delayed(20, 4), new Delayed(40, 5), new Delayed(60, 6)];
|
|
|
|
$expected = [1, 4, 5, 2, 6, 3];
|
|
|
|
|
|
|
|
$streams[] = new AsyncGenerator(function (callable $yield) use ($values1) {
|
|
|
|
foreach ($values1 as $value) {
|
|
|
|
yield $yield(yield $value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$streams[] = new AsyncGenerator(function (callable $yield) use ($values2) {
|
|
|
|
foreach ($values2 as $value) {
|
|
|
|
yield $yield(yield $value);
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
|
|
|
});
|
2020-05-17 21:41:42 +02:00
|
|
|
|
|
|
|
$stream = Stream\merge($streams);
|
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
while (null !== $value = yield $stream->continue()) {
|
|
|
|
$this->assertSame(\array_shift($expected), $value);
|
2020-05-17 21:41:42 +02:00
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testMerge
|
|
|
|
*/
|
2020-05-13 17:15:21 +02:00
|
|
|
public function testMergeWithFailedStream()
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$exception = new TestException;
|
|
|
|
$generator = new AsyncGenerator(static function (callable $yield) use ($exception) {
|
|
|
|
yield $yield(1); // Emit once before failing.
|
|
|
|
throw $exception;
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2020-05-17 21:41:42 +02:00
|
|
|
|
|
|
|
$stream = Stream\merge([$generator, Stream\fromIterable(\range(1, 5))]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
/** @noinspection PhpStatementHasEmptyBodyInspection */
|
|
|
|
while (yield $stream->continue()) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
$this->fail("The exception used to fail the stream should be thrown from continue()");
|
|
|
|
} catch (TestException $reason) {
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
public function testNonStream()
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-13 17:15:21 +02:00
|
|
|
$this->expectException(\TypeError::class);
|
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
/** @noinspection PhpParamsInspection */
|
2020-05-13 17:15:21 +02:00
|
|
|
Stream\merge([1]);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
}
|