mirror of
https://github.com/danog/byte-stream.git
synced 2024-11-26 11:54:54 +01:00
Add Message tests
This commit is contained in:
parent
7cc313a633
commit
4ddca11779
28
phpunit.xml.dist
Normal file
28
phpunit.xml.dist
Normal file
@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Main">
|
||||
<directory>test</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory suffix=".php">lib</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<listeners>
|
||||
<listener class="Amp\PHPUnit\LoopReset"/>
|
||||
</listeners>
|
||||
</phpunit>
|
169
test/MessageTest.php
Normal file
169
test/MessageTest.php
Normal file
@ -0,0 +1,169 @@
|
||||
<?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 = "";
|
||||
while (yield $message->wait()) {
|
||||
$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 = [];
|
||||
while (yield $message->wait()) {
|
||||
$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 = "";
|
||||
for ($i = 0; $i < 1 && yield $message->wait(); ++$i) {
|
||||
$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 {
|
||||
while (yield $message->wait()) {
|
||||
$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));
|
||||
|
||||
$this->assertFalse(yield $message->wait());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @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) {
|
||||
yield $message->wait();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @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();
|
||||
|
||||
while (yield $message->wait());
|
||||
|
||||
$message->getChunk();
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user