2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Loop;
|
2017-04-13 18:05:41 +02:00
|
|
|
use Amp\Promise;
|
2017-03-15 17:12:49 +01:00
|
|
|
use Amp\Stream;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Success;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
class StreamFromIterableTest extends \PHPUnit\Framework\TestCase {
|
2017-04-13 18:05:41 +02:00
|
|
|
const TIMEOUT = 10;
|
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testSuccessfulPromises() {
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results) {
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\fromIterable([new Success(1), new Success(2), new Success(3)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$stream->onEmit(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame([1, 2, 3], $results);
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testFailedPromises() {
|
|
|
|
$exception = new \Exception;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$reason, $exception) {
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\fromIterable([new Failure($exception), new Failure($exception)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve($callback);
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testMixedPromises() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results, &$reason, $exception) {
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\fromIterable([new Success(1), new Success(2), new Failure($exception), new Success(4)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$stream->onEmit(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve($callback);
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertSame(\range(1, 2), $results);
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testPendingPromises() {
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results) {
|
2017-04-28 14:42:02 +02:00
|
|
|
$stream = Stream\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-03-21 18:24:06 +01:00
|
|
|
$stream->onEmit(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertSame(\range(1, 4), $results);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testTraversable() {
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results) {
|
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-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\fromIterable($generator);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$stream->onEmit(function ($value) use (&$results) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertSame(\range(1, 4), $results);
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-23 19:08:40 +02:00
|
|
|
* @expectedException \TypeError
|
2017-01-06 23:16:06 +01:00
|
|
|
* @dataProvider provideInvalidStreamArguments
|
|
|
|
*/
|
|
|
|
public function testInvalid($arg) {
|
2017-03-15 17:12:49 +01:00
|
|
|
Stream\fromIterable($arg);
|
2017-01-06 23:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInvalidStreamArguments() {
|
|
|
|
return [
|
|
|
|
[null],
|
|
|
|
[new \stdClass],
|
|
|
|
[32],
|
|
|
|
[false],
|
|
|
|
[true],
|
|
|
|
["string"],
|
|
|
|
];
|
|
|
|
}
|
2017-04-13 18:05:41 +02:00
|
|
|
|
|
|
|
public function testInterval() {
|
|
|
|
$count = 3;
|
|
|
|
$stream = Stream\fromIterable(range(1, $count), self::TIMEOUT);
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
$stream = Stream\map($stream, function ($value) use (&$i) {
|
|
|
|
$this->assertSame(++$i, $value);
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise\wait($stream);
|
|
|
|
|
|
|
|
$this->assertSame($count, $i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testInterval
|
|
|
|
*/
|
|
|
|
public function testSlowConsumer() {
|
|
|
|
$invoked = 0;
|
|
|
|
$count = 5;
|
|
|
|
Loop::run(function () use (&$invoked, $count) {
|
|
|
|
$stream = Stream\fromIterable(range(1, $count), self::TIMEOUT);
|
|
|
|
|
|
|
|
$stream->onEmit(function () use (&$invoked) {
|
|
|
|
++$invoked;
|
2017-04-28 14:42:02 +02:00
|
|
|
return new Delayed(self::TIMEOUT * 2);
|
2017-04-13 18:05:41 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($count, $invoked);
|
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|