1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-12-02 09:17:50 +01:00
byte-stream/test/IteratorStreamTest.php

162 lines
4.2 KiB
PHP
Raw Normal View History

2017-04-13 16:05:37 +02:00
<?php
namespace Amp\ByteStream\Test;
use Amp\ByteStream\IteratorStream;
use Amp\Emitter;
use Amp\Loop;
2017-04-13 16:05:37 +02:00
use Amp\PHPUnit\TestCase;
2017-05-05 16:55:43 +02:00
class IteratorStreamTest extends TestCase {
2017-04-13 16:05:37 +02:00
public function testBufferingAll() {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
2017-05-05 16:55:43 +02:00
$stream = new IteratorStream($emitter->iterate());
2017-04-13 16:05:37 +02:00
foreach ($values as $value) {
$emitter->emit($value);
}
2017-05-05 16:55:43 +02:00
$emitter->complete();
2017-04-13 16:05:37 +02:00
2017-05-11 01:04:10 +02:00
$this->assertSame(\implode($values), yield $stream);
2017-04-13 16:05:37 +02:00
});
}
public function testFullStreamConsumption() {
Loop::run(function () use (&$invoked) {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
2017-05-05 16:55:43 +02:00
$stream = new IteratorStream($emitter->iterate());
2017-04-13 16:05:37 +02:00
foreach ($values as $value) {
$emitter->emit($value);
}
Loop::delay(5, function () use ($emitter) {
2017-05-05 16:55:43 +02:00
$emitter->complete();
2017-04-13 16:05:37 +02:00
});
$buffer = "";
2017-05-05 16:55:43 +02:00
while (($chunk = yield $stream->read()) !== null) {
$buffer .= $chunk;
2017-04-13 16:05:37 +02:00
}
$this->assertSame(\implode($values), $buffer);
2017-05-05 16:55:43 +02:00
$this->assertSame("", yield $stream);
2017-04-13 16:05:37 +02:00
});
}
public function testFastResolvingStream() {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
2017-05-05 16:55:43 +02:00
$stream = new IteratorStream($emitter->iterate());
2017-04-13 16:05:37 +02:00
foreach ($values as $value) {
$emitter->emit($value);
}
2017-05-05 16:55:43 +02:00
$emitter->complete();
2017-04-13 16:05:37 +02:00
$emitted = [];
2017-05-05 16:55:43 +02:00
while (($chunk = yield $stream->read()) !== null) {
$emitted[] = $chunk;
2017-04-13 16:05:37 +02:00
}
2017-05-11 01:04:10 +02:00
$this->assertSame($values, $emitted);
$this->assertSame("", yield $stream);
});
}
public function testFastResolvingStreamBufferingOnly() {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
$stream = new IteratorStream($emitter->iterate());
foreach ($values as $value) {
$emitter->emit($value);
}
$emitter->complete();
2017-05-05 16:55:43 +02:00
$this->assertSame(\implode($values), yield $stream);
2017-04-13 16:05:37 +02:00
});
}
2017-05-11 01:04:10 +02:00
2017-04-13 16:05:37 +02:00
public function testPartialStreamConsumption() {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
2017-05-05 16:55:43 +02:00
$stream = new IteratorStream($emitter->iterate());
$emitter->emit($values[0]);
$chunk = yield $stream->read();
$this->assertSame(\array_shift($values), $chunk);
2017-04-13 16:05:37 +02:00
foreach ($values as $value) {
$emitter->emit($value);
}
2017-05-05 16:55:43 +02:00
$emitter->complete();
2017-04-13 16:05:37 +02:00
2017-05-05 16:55:43 +02:00
$this->assertSame(\implode($values), yield $stream);
2017-04-13 16:05:37 +02:00
});
}
public function testFailingStream() {
Loop::run(function () {
$exception = new \Exception;
$value = "abc";
$emitter = new Emitter;
2017-05-05 16:55:43 +02:00
$stream = new IteratorStream($emitter->iterate());
2017-04-13 16:05:37 +02:00
$emitter->emit($value);
$emitter->fail($exception);
try {
2017-05-05 16:55:43 +02:00
while (($chunk = yield $stream->read()) !== null) {
$this->assertSame($value, $chunk);
2017-04-13 16:05:37 +02:00
}
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
}
});
}
public function testEmptyStream() {
Loop::run(function () {
$emitter = new Emitter;
2017-05-05 16:55:43 +02:00
$emitter->complete();
$stream = new IteratorStream($emitter->iterate());
2017-04-13 16:05:37 +02:00
2017-05-05 16:55:43 +02:00
$this->assertNull(yield $stream->read());
2017-04-13 16:05:37 +02:00
});
}
2017-05-05 16:55:43 +02:00
public function testReadAfterCompletion() {
2017-04-13 16:05:37 +02:00
Loop::run(function () {
$value = "abc";
$emitter = new Emitter;
2017-05-05 16:55:43 +02:00
$stream = new IteratorStream($emitter->iterate());
2017-04-13 16:05:37 +02:00
$emitter->emit($value);
2017-05-05 16:55:43 +02:00
$emitter->complete();
2017-04-13 16:05:37 +02:00
2017-05-05 16:55:43 +02:00
$this->assertSame($value, yield $stream->read());
$this->assertNull(yield $stream->read());
2017-04-13 16:05:37 +02:00
});
}
}