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

143 lines
4.0 KiB
PHP
Raw Normal View History

2016-08-22 06:40:48 +02:00
<?php declare(strict_types = 1);
2015-08-03 07:20:06 +02:00
2016-08-19 00:36:58 +02:00
namespace Amp\Concurrent\Test\Sync;
2016-08-18 18:04:48 +02:00
use Amp\Concurrent\Sync\ChannelledStream;
2016-08-19 00:36:58 +02:00
use Amp\Stream\Stream;
use Amp\Stream\ClosedException;
use Amp\Concurrent\Test\TestCase;
use Amp\Success;
class ChannelledStreamTest extends TestCase {
2015-09-27 18:15:47 +02:00
/**
2016-08-19 00:36:58 +02:00
* @return \Amp\Stream\Stream|\PHPUnit_Framework_MockObject_MockObject
2015-09-27 18:15:47 +02:00
*/
2016-08-19 00:36:58 +02:00
protected function createMockStream() {
$mock = $this->createMock(Stream::class);
2015-09-27 18:15:47 +02:00
$buffer = '';
$mock->method('write')
->will($this->returnCallback(function ($data) use (&$buffer) {
$buffer .= $data;
2016-08-19 00:36:58 +02:00
return new Success(\strlen($data));
2015-09-27 18:15:47 +02:00
}));
$mock->method('read')
->will($this->returnCallback(function ($length, $byte = null, $timeout = 0) use (&$buffer) {
2016-08-19 00:36:58 +02:00
$result = \substr($buffer, 0, $length);
$buffer = \substr($buffer, $length);
return new Success($result);
2015-09-27 18:15:47 +02:00
}));
return $mock;
}
2016-08-19 00:36:58 +02:00
public function testSendReceive() {
\Amp\execute(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-08-19 00:36:58 +02:00
yield $a->send($message);
$data = yield $b->receive();
2015-09-27 18:15:47 +02:00
$this->assertSame($message, $data);
2016-08-19 00:36:58 +02:00
});
2015-09-27 18:15:47 +02:00
}
/**
* @depends testSendReceive
*/
2016-08-19 00:36:58 +02:00
public function testSendReceiveLongData() {
\Amp\execute(function () {
2015-09-27 18:15:47 +02:00
$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-08-19 00:36:58 +02:00
yield $a->send($message);
$data = yield $b->receive();
2015-09-27 18:15:47 +02:00
$this->assertSame($message, $data);
2016-08-19 00:36:58 +02:00
});
2015-08-03 07:20:06 +02:00
}
2015-09-03 01:29:48 +02:00
/**
* @depends testSendReceive
2016-08-19 00:36:58 +02:00
* @expectedException \Amp\Concurrent\ChannelException
2015-09-03 01:29:48 +02:00
*/
2016-08-19 00:36:58 +02:00
public function testInvalidDataReceived() {
\Amp\execute(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-08-19 00:36:58 +02:00
yield $mock->write(pack('L', 10) . '1234567890');
$data = yield $b->receive();
});
2015-09-03 01:29:48 +02:00
}
/**
* @depends testSendReceive
2016-08-19 00:36:58 +02:00
* @expectedException \Amp\Concurrent\ChannelException
2015-09-03 01:29:48 +02:00
*/
2016-08-19 00:36:58 +02:00
public function testSendUnserializableData() {
\Amp\execute(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-08-19 00:36:58 +02:00
yield $a->send(function () {});
$data = yield $b->receive();
});
2015-09-03 01:29:48 +02:00
}
/**
* @depends testSendReceive
2016-08-19 00:36:58 +02:00
* @expectedException \Amp\Concurrent\ChannelException
2015-09-03 01:29:48 +02:00
*/
2016-08-19 00:36:58 +02:00
public function testSendAfterClose() {
\Amp\execute(function () {
$mock = $this->createMock(Stream::class);
2015-09-27 18:15:47 +02:00
$mock->expects($this->once())
->method('write')
2016-08-19 00:36:58 +02:00
->will($this->throwException(new ClosedException));
2015-09-03 01:29:48 +02:00
$a = new ChannelledStream($mock);
2016-08-19 00:36:58 +02:00
$b = new ChannelledStream($this->createMock(Stream::class));
2015-09-03 01:29:48 +02:00
2016-08-19 00:36:58 +02:00
yield $a->send('hello');
});
2015-09-03 01:29:48 +02:00
}
/**
* @depends testSendReceive
2016-08-19 00:36:58 +02:00
* @expectedException \Amp\Concurrent\ChannelException
2015-09-03 01:29:48 +02:00
*/
2016-08-19 00:36:58 +02:00
public function testReceiveAfterClose() {
\Amp\execute(function () {
$mock = $this->createMock(Stream::class);
2015-09-27 18:15:47 +02:00
$mock->expects($this->once())
->method('read')
2016-08-19 00:36:58 +02:00
->will($this->throwException(new ClosedException));
2015-09-03 01:29:48 +02:00
$a = new ChannelledStream($mock);
2015-09-03 01:29:48 +02:00
2016-08-19 00:36:58 +02:00
$data = yield $a->receive();
});
2015-09-03 01:29:48 +02:00
}
2015-08-03 07:20:06 +02:00
}