1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00
amp/test/StreamFromIterableTest.php

147 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
use Amp\Delayed;
use Amp\Failure;
2017-04-23 14:39:19 +02:00
use Amp\Loop;
use Amp\Promise;
use Amp\Stream;
use Amp\Success;
class StreamFromIterableTest extends \PHPUnit\Framework\TestCase {
const TIMEOUT = 10;
public function testSuccessfulPromises() {
$results = [];
Loop::run(function () use (&$results) {
$stream = Stream\fromIterable([new Success(1), new Success(2), new Success(3)]);
2017-01-06 23:16:06 +01:00
$stream->onEmit(function ($value) use (&$results) {
$results[] = $value;
});
});
2017-01-06 23:16:06 +01:00
$this->assertSame([1, 2, 3], $results);
}
2017-01-06 23:16:06 +01:00
public function testFailedPromises() {
$exception = new \Exception;
Loop::run(function () use (&$reason, $exception) {
$stream = Stream\fromIterable([new Failure($exception), new Failure($exception)]);
2017-01-06 23:16:06 +01:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2017-01-06 23:16:06 +01:00
$stream->onResolve($callback);
});
2017-01-06 23:16:06 +01:00
$this->assertSame($exception, $reason);
}
2017-01-06 23:16:06 +01:00
public function testMixedPromises() {
$exception = new \Exception;
$results = [];
Loop::run(function () use (&$results, &$reason, $exception) {
$stream = Stream\fromIterable([new Success(1), new Success(2), new Failure($exception), new Success(4)]);
2017-01-06 23:16:06 +01:00
$stream->onEmit(function ($value) use (&$results) {
$results[] = $value;
});
2017-01-06 23:16:06 +01:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2017-01-06 23:16:06 +01:00
$stream->onResolve($callback);
});
2017-01-06 23:16:06 +01:00
2017-01-04 02:10:27 +01:00
$this->assertSame(\range(1, 2), $results);
$this->assertSame($exception, $reason);
}
2017-01-06 23:16:06 +01:00
public function testPendingPromises() {
$results = [];
Loop::run(function () use (&$results) {
$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
$stream->onEmit(function ($value) use (&$results) {
$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-01-04 02:10:27 +01:00
public function testTraversable() {
$results = [];
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
$stream = Stream\fromIterable($generator);
2017-01-06 23:16: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) {
Stream\fromIterable($arg);
2017-01-06 23:16:06 +01:00
}
public function provideInvalidStreamArguments() {
return [
[null],
[new \stdClass],
[32],
[false],
[true],
["string"],
];
}
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;
return new Delayed(self::TIMEOUT * 2);
});
});
$this->assertSame($count, $invoked);
}
}