2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-08-03 07:20:06 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Test\Sync;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2017-05-10 09:05:35 +02:00
|
|
|
use Amp\ByteStream\InputStream;
|
|
|
|
use Amp\ByteStream\OutputStream;
|
2017-05-18 09:51:31 +02:00
|
|
|
use Amp\ByteStream\StreamException;
|
2019-08-27 19:17:41 +02:00
|
|
|
use Amp\Parallel\Sync\ChannelException;
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\Sync\ChannelledStream;
|
2019-08-27 19:17:41 +02:00
|
|
|
use Amp\Parallel\Sync\SerializationException;
|
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-05-10 09:05:35 +02:00
|
|
|
use Amp\Promise;
|
2016-08-19 00:36:58 +02:00
|
|
|
use Amp\Success;
|
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
class ChannelledStreamTest extends AsyncTestCase
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2015-09-27 18:15:47 +02:00
|
|
|
/**
|
2017-05-10 09:05:35 +02:00
|
|
|
* @return \Amp\ByteStream\InputStream|\Amp\ByteStream\OutputStream
|
2015-09-27 18:15:47 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
protected function createMockStream()
|
|
|
|
{
|
2017-05-10 09:05:35 +02:00
|
|
|
return new class implements InputStream, OutputStream {
|
|
|
|
private $buffer = "";
|
2015-09-27 18:15:47 +02:00
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function read(): Promise
|
|
|
|
{
|
2017-05-10 09:05:35 +02:00
|
|
|
$data = $this->buffer;
|
|
|
|
$this->buffer = "";
|
|
|
|
return new Success($data);
|
|
|
|
}
|
2015-09-27 18:15:47 +02:00
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function write(string $data): Promise
|
|
|
|
{
|
2017-05-10 09:05:35 +02:00
|
|
|
$this->buffer .= $data;
|
2016-08-19 00:36:58 +02:00
|
|
|
return new Success(\strlen($data));
|
2017-05-10 09:05:35 +02:00
|
|
|
}
|
2017-04-16 17:12:42 +02:00
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function end(string $finalData = ""): Promise
|
|
|
|
{
|
2017-05-10 09:05:35 +02:00
|
|
|
throw new \BadMethodCallException;
|
|
|
|
}
|
2015-09-27 18:15:47 +02:00
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function close()
|
|
|
|
{
|
2017-05-10 09:05:35 +02:00
|
|
|
throw new \BadMethodCallException;
|
|
|
|
}
|
|
|
|
};
|
2015-09-27 18:15:47 +02:00
|
|
|
}
|
2017-05-10 09:05:35 +02:00
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testSendReceive()
|
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$mock = $this->createMockStream();
|
|
|
|
$a = new ChannelledStream($mock, $mock);
|
|
|
|
$b = new ChannelledStream($mock, $mock);
|
2015-08-03 07:20:06 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$message = 'hello';
|
2015-09-27 18:15:47 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
yield $a->send($message);
|
|
|
|
$data = yield $b->receive();
|
|
|
|
$this->assertSame($message, $data);
|
2015-09-27 18:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testSendReceiveLongData()
|
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$mock = $this->createMockStream();
|
|
|
|
$a = new ChannelledStream($mock, $mock);
|
|
|
|
$b = new ChannelledStream($mock, $mock);
|
|
|
|
|
|
|
|
$length = 0xffff;
|
|
|
|
$message = '';
|
|
|
|
for ($i = 0; $i < $length; ++$i) {
|
|
|
|
$message .= \chr(\mt_rand(0, 255));
|
|
|
|
}
|
|
|
|
|
|
|
|
yield $a->send($message);
|
|
|
|
$data = yield $b->receive();
|
|
|
|
$this->assertSame($message, $data);
|
2015-08-03 07:20:06 +02:00
|
|
|
}
|
2015-09-03 01:29:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testInvalidDataReceived()
|
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectException(ChannelException::class);
|
|
|
|
|
|
|
|
$mock = $this->createMockStream();
|
|
|
|
$a = new ChannelledStream($mock, $mock);
|
|
|
|
$b = new ChannelledStream($mock, $mock);
|
|
|
|
|
|
|
|
// Close $a. $b should close on next read...
|
|
|
|
yield $mock->write(\pack('L', 10) . '1234567890');
|
|
|
|
$data = yield $b->receive();
|
2015-09-03 01:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testSendUnserializableData()
|
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectException(SerializationException::class);
|
|
|
|
|
|
|
|
$mock = $this->createMockStream();
|
|
|
|
$a = new ChannelledStream($mock, $mock);
|
|
|
|
$b = new ChannelledStream($mock, $mock);
|
|
|
|
|
|
|
|
// Close $a. $b should close on next read...
|
|
|
|
yield $a->send(function () {});
|
|
|
|
$data = yield $b->receive();
|
2015-09-03 01:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testSendAfterClose()
|
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectException(ChannelException::class);
|
|
|
|
|
|
|
|
$mock = $this->createMock(OutputStream::class);
|
|
|
|
$mock->expects($this->once())
|
2015-09-27 18:15:47 +02:00
|
|
|
->method('write')
|
2017-04-16 17:12:42 +02:00
|
|
|
->will($this->throwException(new StreamException));
|
2015-09-03 01:29:48 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$a = new ChannelledStream($this->createMock(InputStream::class), $mock);
|
|
|
|
$b = new ChannelledStream(
|
2019-08-28 01:27:33 +02:00
|
|
|
$this->createMock(InputStream::class),
|
|
|
|
$this->createMock(OutputStream::class)
|
|
|
|
);
|
2015-09-03 01:29:48 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
yield $a->send('hello');
|
2015-09-03 01:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testReceiveAfterClose()
|
|
|
|
{
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->expectException(ChannelException::class);
|
|
|
|
|
|
|
|
$mock = $this->createMock(InputStream::class);
|
|
|
|
$mock->expects($this->once())
|
2017-05-10 09:05:35 +02:00
|
|
|
->method('read')
|
|
|
|
->willReturn(new Success(null));
|
2015-09-03 01:29:48 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$a = new ChannelledStream($mock, $this->createMock(OutputStream::class));
|
2015-09-03 01:29:48 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$data = yield $a->receive();
|
2015-09-03 01:29:48 +02:00
|
|
|
}
|
2015-08-03 07:20:06 +02:00
|
|
|
}
|