2015-08-03 07:20:06 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Tests\Concurrent\Sync;
|
|
|
|
|
|
|
|
use Icicle\Concurrent\Sync\Channel;
|
2015-08-03 07:58:08 +02:00
|
|
|
use Icicle\Coroutine;
|
2015-08-05 09:48:43 +02:00
|
|
|
use Icicle\Loop;
|
2015-08-27 20:07:56 +02:00
|
|
|
use Icicle\Socket\Stream\DuplexStream;
|
2015-08-05 09:48:43 +02:00
|
|
|
use Icicle\Tests\Concurrent\TestCase;
|
2015-08-03 07:20:06 +02:00
|
|
|
|
2015-08-05 09:48:43 +02:00
|
|
|
class ChannelTest extends TestCase
|
2015-08-03 07:20:06 +02:00
|
|
|
{
|
|
|
|
public function testCreate()
|
|
|
|
{
|
2015-08-07 08:38:53 +02:00
|
|
|
list($a, $b) = Channel::createSocketPair();
|
2015-08-03 07:20:06 +02:00
|
|
|
|
2015-08-05 09:48:43 +02:00
|
|
|
$this->assertInternalType('resource', $a);
|
|
|
|
$this->assertInternalType('resource', $b);
|
2015-08-03 07:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClose()
|
|
|
|
{
|
2015-08-07 08:38:53 +02:00
|
|
|
list($a, $b) = Channel::createSocketPair();
|
2015-08-27 20:07:56 +02:00
|
|
|
$a = new Channel(new DuplexStream($a));
|
|
|
|
$b = new Channel(new DuplexStream($b));
|
2015-08-03 07:20:06 +02:00
|
|
|
|
|
|
|
// Close $a. $b should close on next read...
|
|
|
|
$a->close();
|
2015-08-03 07:58:08 +02:00
|
|
|
new Coroutine\Coroutine($b->receive());
|
2015-08-03 07:20:06 +02:00
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
|
|
|
|
$this->assertFalse($a->isOpen());
|
|
|
|
$this->assertFalse($b->isOpen());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendReceive()
|
|
|
|
{
|
2015-08-03 08:22:17 +02:00
|
|
|
Coroutine\create(function () {
|
2015-08-07 08:38:53 +02:00
|
|
|
list($a, $b) = Channel::createSocketPair();
|
2015-08-27 20:07:56 +02:00
|
|
|
$a = new Channel(new DuplexStream($a));
|
|
|
|
$b = new Channel(new DuplexStream($b));
|
2015-08-03 07:20:06 +02:00
|
|
|
|
2015-08-03 08:22:17 +02:00
|
|
|
yield $a->send('hello');
|
|
|
|
$data = (yield $b->receive());
|
2015-08-03 07:20:06 +02:00
|
|
|
$this->assertEquals('hello', $data);
|
2015-08-28 20:38:16 +02:00
|
|
|
|
|
|
|
$a->close();
|
|
|
|
$b->close();
|
2015-08-27 20:07:56 +02:00
|
|
|
})->done();
|
2015-08-03 07:20:06 +02:00
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
}
|