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\Iterator;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
2017-04-27 17:51:06 +02:00
|
|
|
use Amp\Iterator;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Loop;
|
2017-04-28 06:53:04 +02:00
|
|
|
use Amp\PHPUnit\TestException;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Success;
|
2020-05-13 17:15:21 +02:00
|
|
|
use Amp\Test\BaseTest;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
class FromIterableTest extends BaseTest
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-04-13 18:05:41 +02:00
|
|
|
const TIMEOUT = 10;
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testSuccessfulPromises()
|
|
|
|
{
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$expected = \range(1, 3);
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterator = Iterator\fromIterable([new Success(1), new Success(2), new Success(3)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance()) {
|
|
|
|
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testFailedPromises()
|
|
|
|
{
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$exception = new \Exception;
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterator = Iterator\fromIterable([new Failure($exception), new Failure($exception)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
try {
|
2017-04-27 17:51:06 +02:00
|
|
|
yield $iterator->advance();
|
2017-04-27 17:32:53 +02:00
|
|
|
} catch (\Exception $reason) {
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testMixedPromises()
|
|
|
|
{
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
2017-04-28 06:53:04 +02:00
|
|
|
$exception = new TestException;
|
2017-04-27 17:32:53 +02:00
|
|
|
$expected = \range(1, 2);
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterator = Iterator\fromIterable([new Success(1), new Success(2), new Failure($exception), new Success(4)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
try {
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance()) {
|
|
|
|
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2017-04-28 06:53:04 +02:00
|
|
|
$this->fail("A failed promise in the iterable should fail the iterator and be thrown from advance()");
|
|
|
|
} catch (TestException $reason) {
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertEmpty($expected);
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPendingPromises()
|
|
|
|
{
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$expected = \range(1, 4);
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterator = Iterator\fromIterable([new Delayed(30, 1), new Delayed(10, 2), new Delayed(20, 3), new Success(4)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance()) {
|
|
|
|
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testTraversable()
|
|
|
|
{
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$expected = \range(1, 4);
|
2017-01-04 02:10:27 +01:00
|
|
|
$generator = (function () {
|
|
|
|
foreach (\range(1, 4) as $value) {
|
|
|
|
yield $value;
|
|
|
|
}
|
|
|
|
})();
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterator = Iterator\fromIterable($generator);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance()) {
|
|
|
|
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertEmpty($expected);
|
|
|
|
});
|
2017-01-04 02:10:27 +01:00
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
|
|
|
/**
|
2017-05-01 07:32:56 +02:00
|
|
|
* @dataProvider provideInvalidIteratorArguments
|
2017-01-06 23:16:06 +01:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testInvalid($arg)
|
|
|
|
{
|
2020-05-13 17:15:21 +02:00
|
|
|
$this->expectException(\TypeError::class);
|
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
Iterator\fromIterable($arg);
|
2017-01-06 23:16:06 +01:00
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function provideInvalidIteratorArguments()
|
|
|
|
{
|
2017-01-06 23:16:06 +01:00
|
|
|
return [
|
|
|
|
[null],
|
|
|
|
[new \stdClass],
|
|
|
|
[32],
|
|
|
|
[false],
|
|
|
|
[true],
|
|
|
|
["string"],
|
|
|
|
];
|
|
|
|
}
|
2017-04-13 18:05:41 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testInterval()
|
|
|
|
{
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$count = 3;
|
2017-11-29 13:36:50 +01:00
|
|
|
$iterator = Iterator\fromIterable(\range(1, $count), self::TIMEOUT);
|
2017-04-13 18:05:41 +02:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
$i = 0;
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance()) {
|
|
|
|
$this->assertSame(++$i, $iterator->getCurrent());
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2017-04-13 18:05:41 +02:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertSame($count, $i);
|
|
|
|
});
|
2017-04-13 18:05:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInterval
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testSlowConsumer()
|
|
|
|
{
|
2017-04-13 18:05:41 +02:00
|
|
|
$count = 5;
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () use ($count) {
|
2017-11-29 13:36:50 +01:00
|
|
|
$iterator = Iterator\fromIterable(\range(1, $count), self::TIMEOUT);
|
2017-04-13 18:05:41 +02:00
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
for ($i = 0; yield $iterator->advance(); ++$i) {
|
2017-04-27 17:32:53 +02:00
|
|
|
yield new Delayed(self::TIMEOUT * 2);
|
|
|
|
}
|
2017-04-13 18:05:41 +02:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertSame($count, $i);
|
|
|
|
});
|
2017-04-13 18:05:41 +02:00
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|