1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-12-03 09:47:49 +01:00
byte-stream/test/MessageTest.php

170 lines
4.4 KiB
PHP
Raw Normal View History

2017-04-13 16:05:37 +02:00
<?php
namespace Amp\ByteStream\Test;
use Amp\{ Emitter, Loop, Success };
use Amp\ByteStream\Message;
use Amp\PHPUnit\TestCase;
class MessageTest extends TestCase {
public function testBufferingAll() {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
$message = new Message($emitter->stream());
foreach ($values as $value) {
$emitter->emit($value);
}
$emitter->resolve();
$result = yield $message;
$this->assertSame(\implode($values), $result);
});
}
public function testFullStreamConsumption() {
Loop::run(function () use (&$invoked) {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
$message = new Message($emitter->stream());
foreach ($values as $value) {
$emitter->emit($value);
}
Loop::delay(5, function () use ($emitter) {
$emitter->resolve();
});
$buffer = "";
2017-04-14 23:44:26 +02:00
while (yield $message->advance()) {
2017-04-13 16:05:37 +02:00
$buffer .= $message->getChunk();
}
$this->assertSame(\implode($values), $buffer);
$this->assertSame("", yield $message);
});
}
public function testFastResolvingStream() {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
$message = new Message($emitter->stream());
foreach ($values as $value) {
$emitter->emit($value);
}
$emitter->resolve();
$emitted = [];
2017-04-14 23:44:26 +02:00
while (yield $message->advance()) {
2017-04-13 16:05:37 +02:00
$emitted[] = $message->getChunk();
}
$this->assertSame([\implode($values)], $emitted);
$this->assertSame(\implode($values), yield $message);
});
}
public function testPartialStreamConsumption() {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
$message = new Message($emitter->stream());
foreach ($values as $value) {
$emitter->emit($value);
}
$buffer = "";
2017-04-14 23:44:26 +02:00
for ($i = 0; $i < 1 && yield $message->advance(); ++$i) {
2017-04-13 16:05:37 +02:00
$buffer .= $message->getChunk();
}
$this->assertSame(\array_shift($values), $buffer);
$emitter->resolve();
$this->assertSame(\implode($values), yield $message);
});
}
public function testFailingStream() {
Loop::run(function () {
$exception = new \Exception;
$value = "abc";
$emitter = new Emitter;
$message = new Message($emitter->stream());
$emitter->emit($value);
$emitter->fail($exception);
try {
2017-04-14 23:44:26 +02:00
while (yield $message->advance()) {
2017-04-13 16:05:37 +02:00
$this->assertSame($value, $message->getChunk());
}
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
}
});
}
public function testEmptyStream() {
Loop::run(function () {
$value = 1;
$message = new Message(new Success($value));
2017-04-14 23:44:26 +02:00
$this->assertFalse(yield $message->advance());
2017-04-13 16:05:37 +02:00
});
}
/**
* @expectedException \Error
* @expectedExceptionMessage The stream has resolved
*/
public function testAdvanceAfterCompletion() {
Loop::run(function () {
$value = "abc";
$emitter = new Emitter;
$message = new Message($emitter->stream());
$emitter->emit($value);
$emitter->resolve();
for ($i = 0; $i < 3; ++$i) {
2017-04-14 23:44:26 +02:00
yield $message->advance();
2017-04-13 16:05:37 +02:00
}
});
}
/**
* @expectedException \Error
* @expectedExceptionMessage The stream has resolved
*/
public function testGetCurrentAfterCompletion() {
Loop::run(function () {
$value = "abc";
$emitter = new Emitter;
$message = new Message($emitter->stream());
$emitter->emit($value);
$emitter->resolve();
2017-04-14 23:44:26 +02:00
while (yield $message->advance());
2017-04-13 16:05:37 +02:00
$message->getChunk();
});
}
}