1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-03 10:07:49 +01:00
parallel/test/Sync/ChannelledStreamTest.php

169 lines
4.6 KiB
PHP
Raw Normal View History

2015-08-03 07:20:06 +02:00
<?php
2016-08-18 18:04:48 +02:00
namespace Amp\Tests\Concurrent\Sync;
use Amp\Concurrent\Sync\ChannelledStream;
use Amp\Coroutine;
use Amp\Loop;
use Amp\Stream\{DuplexStream, ReadableStream};
use Amp\Stream\Exception\{UnreadableException, UnwritableException};
use Amp\Tests\Concurrent\TestCase;
2015-08-03 07:20:06 +02:00
class ChannelledStreamTest extends TestCase
2015-08-03 07:20:06 +02:00
{
2015-09-27 18:15:47 +02:00
/**
2016-08-18 18:04:48 +02:00
* @return \Amp\Stream\DuplexStream|\PHPUnit_Framework_MockObject_MockObject
2015-09-27 18:15:47 +02:00
*/
protected function createMockStream()
{
2015-12-05 04:54:55 +01:00
$mock = $this->getMock(DuplexStream::class);
2015-09-27 18:15:47 +02:00
$buffer = '';
$mock->method('write')
->will($this->returnCallback(function ($data) use (&$buffer) {
$buffer .= $data;
2016-01-23 07:00:56 +01:00
return yield strlen($data);
2015-09-27 18:15:47 +02:00
}));
$mock->method('read')
->will($this->returnCallback(function ($length, $byte = null, $timeout = 0) use (&$buffer) {
$result = substr($buffer, 0, $length);
$buffer = substr($buffer, $length);
2016-01-23 07:00:56 +01:00
return yield $result;
2015-09-27 18:15:47 +02:00
}));
return $mock;
}
2015-09-03 01:29:48 +02:00
/**
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Exception\InvalidArgumentError
2015-09-03 01:29:48 +02:00
*/
public function testReadableWithoutWritable()
{
2015-12-05 04:54:55 +01:00
$mock = $this->getMock(ReadableStream::class);
2015-08-03 07:20:06 +02:00
$channel = new ChannelledStream($mock);
2015-08-03 07:20:06 +02:00
}
public function testSendReceive()
{
Coroutine\create(function () {
2015-09-27 18:15:47 +02:00
$mock = $this->createMockStream();
$a = new ChannelledStream($mock);
$b = new ChannelledStream($mock);
2015-08-03 07:20:06 +02:00
2015-09-27 18:15:47 +02:00
$message = 'hello';
2016-01-23 07:00:56 +01:00
yield from $a->send($message);
$data = yield from $b->receive();
2015-09-27 18:15:47 +02:00
$this->assertSame($message, $data);
})->done();
Loop\run();
}
/**
* @depends testSendReceive
*/
public function testSendReceiveLongData()
{
Coroutine\create(function () {
$mock = $this->createMockStream();
$a = new ChannelledStream($mock);
$b = new ChannelledStream($mock);
2015-09-27 18:15:47 +02:00
$length = 0xffff;
$message = '';
for ($i = 0; $i < $length; ++$i) {
$message .= chr(mt_rand(0, 255));
}
2016-01-23 07:00:56 +01:00
yield from $a->send($message);
$data = yield from $b->receive();
2015-09-27 18:15:47 +02:00
$this->assertSame($message, $data);
})->done();
2015-08-03 07:20:06 +02:00
Loop\run();
}
2015-09-03 01:29:48 +02:00
/**
* @depends testSendReceive
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\ChannelException
2015-09-03 01:29:48 +02:00
*/
public function testInvalidDataReceived()
{
Coroutine\create(function () {
2015-09-27 18:15:47 +02:00
$mock = $this->createMockStream();
$a = new ChannelledStream($mock);
$b = new ChannelledStream($mock);
2015-09-03 01:29:48 +02:00
// Close $a. $b should close on next read...
2016-01-23 07:00:56 +01:00
yield from $mock->write(pack('L', 10) . '1234567890');
$data = yield from $b->receive();
2015-09-03 01:29:48 +02:00
})->done();
Loop\run();
}
/**
* @depends testSendReceive
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\ChannelException
2015-09-03 01:29:48 +02:00
*/
public function testSendUnserializableData()
{
Coroutine\create(function () {
2015-09-27 18:15:47 +02:00
$mock = $this->createMockStream();
$a = new ChannelledStream($mock);
$b = new ChannelledStream($mock);
2015-09-03 01:29:48 +02:00
// Close $a. $b should close on next read...
2016-01-23 07:00:56 +01:00
yield from $a->send(function () {});
$data = yield from $b->receive();
2015-09-03 01:29:48 +02:00
})->done();
Loop\run();
}
/**
* @depends testSendReceive
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\ChannelException
2015-09-03 01:29:48 +02:00
*/
public function testSendAfterClose()
{
Coroutine\create(function () {
2015-12-05 04:54:55 +01:00
$mock = $this->getMock(DuplexStream::class);
2015-09-27 18:15:47 +02:00
$mock->expects($this->once())
->method('write')
->will($this->throwException(new UnwritableException()));
2015-09-03 01:29:48 +02:00
$a = new ChannelledStream($mock);
$b = new ChannelledStream($this->getMock(DuplexStream::class));
2015-09-03 01:29:48 +02:00
2016-01-23 07:00:56 +01:00
yield from $a->send('hello');
2015-09-03 01:29:48 +02:00
})->done();
Loop\run();
}
/**
* @depends testSendReceive
2016-08-18 18:04:48 +02:00
* @expectedException \Amp\Concurrent\Exception\ChannelException
2015-09-03 01:29:48 +02:00
*/
public function testReceiveAfterClose()
{
Coroutine\create(function () {
2015-12-05 04:54:55 +01:00
$mock = $this->getMock(DuplexStream::class);
2015-09-27 18:15:47 +02:00
$mock->expects($this->once())
->method('read')
->will($this->throwException(new UnreadableException()));
2015-09-03 01:29:48 +02:00
$a = new ChannelledStream($mock);
2015-09-03 01:29:48 +02:00
2016-01-23 07:00:56 +01:00
$data = yield from $a->receive();
2015-09-03 01:29:48 +02:00
})->done();
Loop\run();
}
2015-08-03 07:20:06 +02:00
}