2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2016-08-31 01:27:14 +02:00
|
|
|
|
|
|
|
namespace Amp\Parallel\Test\Sync;
|
|
|
|
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Loop;
|
2016-08-31 01:27:14 +02:00
|
|
|
use Amp\Parallel\Sync\ChannelledSocket;
|
2017-03-22 05:19:15 +01:00
|
|
|
use Amp\PHPUnit\TestCase;
|
2016-08-31 01:27:14 +02:00
|
|
|
|
|
|
|
class ChannelledSocketTest extends TestCase {
|
|
|
|
/**
|
|
|
|
* @return resource[]
|
|
|
|
*/
|
|
|
|
protected function createSockets() {
|
|
|
|
if (($sockets = @\stream_socket_pair(\stripos(PHP_OS, "win") === 0 ? STREAM_PF_INET : STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)) === false) {
|
|
|
|
$message = "Failed to create socket pair";
|
|
|
|
if ($error = \error_get_last()) {
|
|
|
|
$message .= \sprintf(" Errno: %d; %s", $error["type"], $error["message"]);
|
|
|
|
}
|
|
|
|
$this->fail($message);
|
|
|
|
}
|
|
|
|
return $sockets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendReceive() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2016-08-31 01:27:14 +02:00
|
|
|
list($left, $right) = $this->createSockets();
|
|
|
|
$a = new ChannelledSocket($left, $left);
|
|
|
|
$b = new ChannelledSocket($right, $right);
|
|
|
|
|
|
|
|
$message = 'hello';
|
|
|
|
|
|
|
|
yield $a->send($message);
|
|
|
|
$data = yield $b->receive();
|
|
|
|
$this->assertSame($message, $data);
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2016-08-31 01:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
*/
|
|
|
|
public function testSendReceiveLongData() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2016-08-31 01:27:14 +02:00
|
|
|
list($left, $right) = $this->createSockets();
|
|
|
|
$a = new ChannelledSocket($left, $left);
|
|
|
|
$b = new ChannelledSocket($right, $right);
|
|
|
|
|
|
|
|
$length = 0xffff;
|
|
|
|
$message = '';
|
|
|
|
for ($i = 0; $i < $length; ++$i) {
|
|
|
|
$message .= chr(mt_rand(0, 255));
|
|
|
|
}
|
|
|
|
|
|
|
|
$a->send($message);
|
|
|
|
$data = yield $b->receive();
|
|
|
|
$this->assertSame($message, $data);
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2016-08-31 01:27:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
* @expectedException \Amp\Parallel\ChannelException
|
|
|
|
*/
|
|
|
|
public function testInvalidDataReceived() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2016-08-31 01:27:14 +02:00
|
|
|
list($left, $right) = $this->createSockets();
|
|
|
|
$a = new ChannelledSocket($left, $left);
|
|
|
|
$b = new ChannelledSocket($right, $right);
|
|
|
|
|
|
|
|
fwrite($left, pack('L', 10) . '1234567890');
|
|
|
|
$data = yield $b->receive();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2016-08-31 01:27:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
* @expectedException \Amp\Parallel\ChannelException
|
|
|
|
*/
|
|
|
|
public function testSendUnserializableData() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2016-08-31 01:27:14 +02:00
|
|
|
list($left, $right) = $this->createSockets();
|
|
|
|
$a = new ChannelledSocket($left, $left);
|
|
|
|
$b = new ChannelledSocket($right, $right);
|
|
|
|
|
|
|
|
// Close $a. $b should close on next read...
|
|
|
|
yield $a->send(function () {});
|
|
|
|
$data = yield $b->receive();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2016-08-31 01:27:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
* @expectedException \Amp\Parallel\ChannelException
|
|
|
|
*/
|
|
|
|
public function testSendAfterClose() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2016-08-31 01:27:14 +02:00
|
|
|
list($left, $right) = $this->createSockets();
|
|
|
|
$a = new ChannelledSocket($left, $left);
|
|
|
|
$a->close();
|
|
|
|
|
|
|
|
yield $a->send('hello');
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2016-08-31 01:27:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSendReceive
|
|
|
|
* @expectedException \Amp\Parallel\ChannelException
|
|
|
|
*/
|
|
|
|
public function testReceiveAfterClose() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2016-08-31 01:27:14 +02:00
|
|
|
list($left, $right) = $this->createSockets();
|
|
|
|
$a = new ChannelledSocket($left, $left);
|
|
|
|
$a->close();
|
|
|
|
|
|
|
|
$data = yield $a->receive();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2016-08-31 01:27:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|