-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
415 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Amp\ByteStream\Test; | ||
|
||
use Amp\ByteStream\IteratorStream; | ||
use Amp\ByteStream\StreamException; | ||
use Amp\Emitter; | ||
use Amp\Loop; | ||
use Amp\PHPUnit\TestCase; | ||
use Amp\PHPUnit\TestException; | ||
|
||
class IteratorStreamTest extends TestCase { | ||
public function testReadIterator() { | ||
Loop::run(function () { | ||
$values = ["abc", "def", "ghi"]; | ||
|
||
$emitter = new Emitter; | ||
$stream = new IteratorStream($emitter->iterate()); | ||
|
||
foreach ($values as $value) { | ||
$emitter->emit($value); | ||
} | ||
|
||
$emitter->complete(); | ||
|
||
$buffer = ""; | ||
while (($chunk = yield $stream->read()) !== null) { | ||
$buffer .= $chunk; | ||
} | ||
|
||
$this->assertSame(\implode($values), $buffer); | ||
$this->assertNull(yield $stream->read()); | ||
}); | ||
} | ||
|
||
public function testFailingIterator() { | ||
Loop::run(function () { | ||
$exception = new TestException; | ||
$value = "abc"; | ||
|
||
$emitter = new Emitter; | ||
$stream = new IteratorStream($emitter->iterate()); | ||
|
||
$emitter->emit($value); | ||
$emitter->fail($exception); | ||
|
||
$callable = $this->createCallback(1); | ||
|
||
try { | ||
while (($chunk = yield $stream->read()) !== null) { | ||
$this->assertSame($value, $chunk); | ||
} | ||
|
||
$this->fail("No exception has been thrown"); | ||
} catch (TestException $reason) { | ||
$this->assertSame($exception, $reason); | ||
$callable(); // <-- ensure this point is reached | ||
} | ||
}); | ||
} | ||
|
||
public function testThrowsOnNonStringIteration() { | ||
$this->expectException(StreamException::class); | ||
Loop::run(function () { | ||
$value = 42; | ||
|
||
$emitter = new Emitter; | ||
$stream = new IteratorStream($emitter->iterate()); | ||
|
||
$emitter->emit($value); | ||
|
||
yield $stream->read(); | ||
}); | ||
} | ||
|
||
public function testFailsAfterException() { | ||
$this->expectException(StreamException::class); | ||
Loop::run(function () { | ||
$value = 42; | ||
|
||
$emitter = new Emitter; | ||
$stream = new IteratorStream($emitter->iterate()); | ||
|
||
$emitter->emit($value); | ||
|
||
try { | ||
yield $stream->read(); | ||
} catch (StreamException $e) { | ||
yield $stream->read(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.