2015-08-07 22:32:18 +02:00
|
|
|
<?php
|
2015-08-31 01:25:44 +02:00
|
|
|
namespace Icicle\Tests\Concurrent\Threading;
|
2015-08-07 22:32:18 +02:00
|
|
|
|
2015-09-04 01:10:19 +02:00
|
|
|
use Icicle\Concurrent\Sync\Lock;
|
2015-08-31 01:25:44 +02:00
|
|
|
use Icicle\Concurrent\Threading\Semaphore;
|
2015-09-04 01:10:19 +02:00
|
|
|
use Icicle\Concurrent\Threading\Thread;
|
2015-08-07 22:32:18 +02:00
|
|
|
use Icicle\Coroutine;
|
|
|
|
use Icicle\Loop;
|
|
|
|
use Icicle\Tests\Concurrent\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group threading
|
2015-08-28 22:09:07 +02:00
|
|
|
* @requires extension pthreads
|
2015-08-07 22:32:18 +02:00
|
|
|
*/
|
2015-08-31 01:25:44 +02:00
|
|
|
class SemaphoreTest extends TestCase
|
2015-08-07 22:32:18 +02:00
|
|
|
{
|
|
|
|
public function testCount()
|
|
|
|
{
|
2015-08-31 01:25:44 +02:00
|
|
|
$semaphore = new Semaphore(1);
|
2015-08-07 22:32:18 +02:00
|
|
|
$this->assertEquals(1, $semaphore->count());
|
|
|
|
}
|
|
|
|
|
2015-09-04 01:10:19 +02:00
|
|
|
/**
|
|
|
|
* @depends testCount
|
|
|
|
*/
|
|
|
|
public function testInvalidLockCount()
|
|
|
|
{
|
|
|
|
$semaphore = new Semaphore(0);
|
|
|
|
$this->assertEquals(1, $semaphore->count());
|
|
|
|
}
|
|
|
|
|
2015-08-07 22:32:18 +02:00
|
|
|
public function testAcquire()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
2015-08-31 01:25:44 +02:00
|
|
|
$semaphore = new Semaphore(1);
|
2015-08-07 22:32:18 +02:00
|
|
|
$lock = (yield $semaphore->acquire());
|
|
|
|
$lock->release();
|
|
|
|
$this->assertTrue($lock->isReleased());
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAcquireMultiple()
|
|
|
|
{
|
2015-08-31 21:00:07 +02:00
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
2015-08-07 22:32:18 +02:00
|
|
|
Coroutine\create(function () {
|
2015-08-31 01:25:44 +02:00
|
|
|
$semaphore = new Semaphore(1);
|
2015-08-07 22:32:18 +02:00
|
|
|
|
|
|
|
$lock1 = (yield $semaphore->acquire());
|
|
|
|
Loop\timer(0.5, function () use ($lock1) {
|
|
|
|
$lock1->release();
|
|
|
|
});
|
|
|
|
|
|
|
|
$lock2 = (yield $semaphore->acquire());
|
|
|
|
Loop\timer(0.5, function () use ($lock2) {
|
|
|
|
$lock2->release();
|
|
|
|
});
|
|
|
|
|
|
|
|
$lock3 = (yield $semaphore->acquire());
|
|
|
|
Loop\timer(0.5, function () use ($lock3) {
|
|
|
|
$lock3->release();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop\run();
|
2015-08-31 21:00:07 +02:00
|
|
|
}, 1.5);
|
2015-08-07 22:32:18 +02:00
|
|
|
}
|
2015-09-04 01:10:19 +02:00
|
|
|
|
|
|
|
public function testSimultaneousAcquire()
|
|
|
|
{
|
|
|
|
$semaphore = new Semaphore(1);
|
|
|
|
|
|
|
|
$coroutine1 = new Coroutine\Coroutine($semaphore->acquire());
|
|
|
|
$coroutine2 = new Coroutine\Coroutine($semaphore->acquire());
|
|
|
|
|
|
|
|
$coroutine1->delay(0.5)->then(function (Lock $lock) {
|
|
|
|
$lock->release();
|
|
|
|
});
|
|
|
|
|
|
|
|
$coroutine2->delay(0.5)->then(function (Lock $lock) {
|
|
|
|
$lock->release();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertRunTimeGreaterThan('Icicle\Loop\run', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAcquireMultiple
|
|
|
|
*/
|
|
|
|
public function testAcquireInMultipleThreads()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$semaphore = new Semaphore(1);
|
|
|
|
|
|
|
|
$thread1 = new Thread(function (Semaphore $semaphore) {
|
|
|
|
$lock = (yield $semaphore->acquire());
|
|
|
|
|
|
|
|
usleep(1e5);
|
|
|
|
|
|
|
|
$lock->release();
|
|
|
|
|
|
|
|
yield 0;
|
|
|
|
}, $semaphore);
|
|
|
|
|
|
|
|
$thread2 = new Thread(function (Semaphore $semaphore) {
|
|
|
|
$lock = (yield $semaphore->acquire());
|
|
|
|
|
|
|
|
usleep(1e5);
|
|
|
|
|
|
|
|
$lock->release();
|
|
|
|
|
|
|
|
yield 1;
|
|
|
|
}, $semaphore);
|
|
|
|
|
|
|
|
$start = microtime(true);
|
|
|
|
|
|
|
|
$thread1->start();
|
|
|
|
$thread2->start();
|
|
|
|
|
|
|
|
yield $thread1->join();
|
|
|
|
yield $thread2->join();
|
|
|
|
|
|
|
|
$this->assertGreaterThan(1, microtime(true) - $start);
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
2015-08-07 22:32:18 +02:00
|
|
|
}
|