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;
|
|
|
|
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-05 09:48:43 +02:00
|
|
|
$a = new Channel($a);
|
|
|
|
$b = new Channel($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-05 09:48:43 +02:00
|
|
|
$a = new Channel($a);
|
|
|
|
$b = new Channel($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);
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
2015-08-05 09:48:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group threading
|
|
|
|
*/
|
|
|
|
public function testThreadTransfer()
|
|
|
|
{
|
2015-08-07 08:38:53 +02:00
|
|
|
list($a, $b) = Channel::createSocketPair();
|
2015-08-05 09:48:43 +02:00
|
|
|
$a = new Channel($a);
|
|
|
|
$b = new Channel($b);
|
|
|
|
|
|
|
|
$thread = \Thread::from(function () {
|
|
|
|
$a = $this->a;
|
|
|
|
});
|
|
|
|
|
|
|
|
$thread->a; // <-- Transfer channel $a to the thread
|
|
|
|
$thread->start(PTHREADS_INHERIT_INI);
|
|
|
|
$thread->join();
|
|
|
|
}
|
2015-08-03 07:20:06 +02:00
|
|
|
}
|