2015-08-03 07:20:06 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Tests\Concurrent\Sync;
|
|
|
|
|
2015-12-06 07:32:06 +01:00
|
|
|
use Icicle\Concurrent\Sync\ChannelledStream;
|
2015-08-03 07:58:08 +02:00
|
|
|
use Icicle\Coroutine;
|
2015-08-05 09:48:43 +02:00
|
|
|
use Icicle\Loop;
|
2015-12-05 04:54:55 +01:00
|
|
|
use Icicle\Stream\DuplexStream;
|
2015-09-27 18:15:47 +02:00
|
|
|
use Icicle\Stream\Exception\UnreadableException;
|
|
|
|
use Icicle\Stream\Exception\UnwritableException;
|
2015-12-05 04:54:55 +01:00
|
|
|
use Icicle\Stream\ReadableStream;
|
2015-08-05 09:48:43 +02:00
|
|
|
use Icicle\Tests\Concurrent\TestCase;
|
2015-08-03 07:20:06 +02:00
|
|
|
|
2015-12-06 07:32:06 +01:00
|
|
|
class ChannelledStreamTest extends TestCase
|
2015-08-03 07:20:06 +02:00
|
|
|
{
|
2015-09-27 18:15:47 +02:00
|
|
|
/**
|
2015-12-05 04:54:55 +01:00
|
|
|
* @return \Icicle\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;
|
|
|
|
}));
|
|
|
|
|
|
|
|
$mock->method('read')
|
|
|
|
->will($this->returnCallback(function ($length, $byte = null, $timeout = 0) use (&$buffer) {
|
|
|
|
$result = substr($buffer, 0, $length);
|
|
|
|
$buffer = substr($buffer, $length);
|
|
|
|
return $result;
|
|
|
|
}));
|
|
|
|
|
|
|
|
return $mock;
|
|
|
|
}
|
|
|
|
|
2015-09-03 01:29:48 +02:00
|
|
|
/**
|
2015-12-06 07:40:48 +01:00
|
|
|
* @expectedException \Icicle\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
|
|
|
|
2015-12-06 07:32:06 +01:00
|
|
|
$channel = new ChannelledStream($mock);
|
2015-08-03 07:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendReceive()
|
|
|
|
{
|
2015-08-03 08:22:17 +02:00
|
|
|
Coroutine\create(function () {
|
2015-09-27 18:15:47 +02:00
|
|
|
$mock = $this->createMockStream();
|
2015-12-06 07:32:06 +01:00
|
|
|
$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';
|
|
|
|
|
|
|
|
yield $a->send($message);
|
|
|
|
$data = (yield $b->receive());
|
|
|
|
$this->assertSame($message, $data);
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
*/
|
|
|
|
public function testSendReceiveLongData()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$mock = $this->createMockStream();
|
2015-12-06 07:32:06 +01:00
|
|
|
$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));
|
|
|
|
}
|
|
|
|
|
|
|
|
yield $a->send($message);
|
2015-08-03 08:22:17 +02:00
|
|
|
$data = (yield $b->receive());
|
2015-09-27 18:15:47 +02:00
|
|
|
$this->assertSame($message, $data);
|
2015-08-27 20:07:56 +02:00
|
|
|
})->done();
|
2015-08-03 07:20:06 +02:00
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
2015-09-03 01:29:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
* @expectedException \Icicle\Concurrent\Exception\ChannelException
|
|
|
|
*/
|
|
|
|
public function testInvalidDataReceived()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
2015-09-27 18:15:47 +02:00
|
|
|
$mock = $this->createMockStream();
|
2015-12-06 07:32:06 +01:00
|
|
|
$a = new ChannelledStream($mock);
|
|
|
|
$b = new ChannelledStream($mock);
|
2015-09-03 01:29:48 +02:00
|
|
|
|
|
|
|
// Close $a. $b should close on next read...
|
2015-09-27 18:15:47 +02:00
|
|
|
yield $mock->write(pack('L', 10) . '1234567890');
|
2015-09-03 01:29:48 +02:00
|
|
|
$data = (yield $b->receive());
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
* @expectedException \Icicle\Concurrent\Exception\ChannelException
|
|
|
|
*/
|
|
|
|
public function testSendUnserializableData()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
2015-09-27 18:15:47 +02:00
|
|
|
$mock = $this->createMockStream();
|
2015-12-06 07:32:06 +01:00
|
|
|
$a = new ChannelledStream($mock);
|
|
|
|
$b = new ChannelledStream($mock);
|
2015-09-03 01:29:48 +02:00
|
|
|
|
|
|
|
// Close $a. $b should close on next read...
|
|
|
|
yield $a->send(function () {});
|
|
|
|
$data = (yield $b->receive());
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
* @expectedException \Icicle\Concurrent\Exception\ChannelException
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2015-12-06 07:32:06 +01:00
|
|
|
$a = new ChannelledStream($mock);
|
|
|
|
$b = new ChannelledStream($this->getMock(DuplexStream::class));
|
2015-09-03 01:29:48 +02:00
|
|
|
|
|
|
|
yield $a->send('hello');
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
* @expectedException \Icicle\Concurrent\Exception\ChannelException
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2015-12-06 07:32:06 +01:00
|
|
|
$a = new ChannelledStream($mock);
|
2015-09-03 01:29:48 +02:00
|
|
|
|
|
|
|
$data = (yield $a->receive());
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
2015-08-03 07:20:06 +02:00
|
|
|
}
|