1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-13 01:27:24 +01:00
parallel/tests/Sync/ChannelTest.php

68 lines
1.5 KiB
PHP
Raw Normal View History

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;
use Icicle\Loop;
use Icicle\Tests\Concurrent\TestCase;
2015-08-03 07:20:06 +02:00
class ChannelTest extends TestCase
2015-08-03 07:20:06 +02:00
{
public function testCreate()
{
list($a, $b) = Channel::create();
$this->assertInternalType('resource', $a);
$this->assertInternalType('resource', $b);
2015-08-03 07:20:06 +02:00
}
public function testClose()
{
list($a, $b) = Channel::create();
$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()
{
Coroutine\create(function () {
list($a, $b) = Channel::create();
$a = new Channel($a);
$b = new Channel($b);
2015-08-03 07:20:06 +02:00
yield $a->send('hello');
$data = (yield $b->receive());
2015-08-03 07:20:06 +02:00
$this->assertEquals('hello', $data);
});
Loop\run();
}
/**
* @group threading
*/
public function testThreadTransfer()
{
list($a, $b) = Channel::create();
$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
}