2015-08-03 07:20:06 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Tests\Concurrent\Sync;
|
|
|
|
|
|
|
|
use Icicle\Concurrent\Sync\Channel;
|
|
|
|
use Icicle\Loop;
|
2015-08-03 07:58:08 +02:00
|
|
|
use Icicle\Coroutine;
|
2015-08-03 07:20:06 +02:00
|
|
|
|
|
|
|
class ChannelTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testCreate()
|
|
|
|
{
|
|
|
|
list($a, $b) = Channel::create();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Channel::class, $a);
|
|
|
|
$this->assertInstanceOf(Channel::class, $b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClose()
|
|
|
|
{
|
|
|
|
list($a, $b) = Channel::create();
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
{
|
|
|
|
list($a, $b) = Channel::create();
|
|
|
|
|
|
|
|
$a->send('hello')->then(function () use ($b) {
|
2015-08-03 07:58:08 +02:00
|
|
|
return new Coroutine\Coroutine($b->receive());
|
2015-08-03 07:20:06 +02:00
|
|
|
})->done(function ($data) {
|
|
|
|
$this->assertEquals('hello', $data);
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
}
|