1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 05:51:14 +01:00
parallel/tests/Sync/PosixSemaphoreTest.php

81 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace Icicle\Tests\Concurrent\Sync;
2015-09-03 23:23:12 -05:00
use Icicle\Concurrent\Forking\Fork;
use Icicle\Concurrent\Sync\PosixSemaphore;
2015-09-03 23:23:12 -05:00
use Icicle\Concurrent\Sync\SemaphoreInterface;
use Icicle\Coroutine;
use Icicle\Loop;
/**
* @group posix
* @requires extension sysvmsg
*/
2015-09-03 23:23:12 -05:00
class PosixSemaphoreTest extends AbstractSemaphoreTest
{
2015-09-03 23:23:12 -05:00
public function createSemaphore($locks)
{
2015-09-03 23:23:12 -05:00
return new PosixSemaphore($locks);
}
2015-09-03 23:23:12 -05:00
public function tearDown()
{
if (!$this->semaphore->isFreed()) {
$this->semaphore->free();
}
}
2015-09-03 23:23:12 -05:00
public function testFree()
{
2015-09-03 23:23:12 -05:00
$this->semaphore = $this->createSemaphore(1);
2015-09-03 23:23:12 -05:00
$this->assertFalse($this->semaphore->isFreed());
2015-09-03 23:23:12 -05:00
$this->semaphore->free();
$this->assertTrue($this->semaphore->isFreed());
}
2015-09-03 23:23:12 -05:00
/**
* @requires extension pcntl
*/
public function testAcquireInMultipleForks()
{
Coroutine\create(function () {
2015-09-03 23:23:12 -05:00
$this->semaphore = $this->createSemaphore(1);
2015-09-03 23:23:12 -05:00
$fork1 = new Fork(function (SemaphoreInterface $semaphore) {
$lock = (yield $semaphore->acquire());
2015-09-03 23:23:12 -05:00
usleep(1e5);
2015-08-10 22:35:59 -05:00
2015-09-03 23:23:12 -05:00
$lock->release();
2015-09-03 23:23:12 -05:00
yield 0;
}, $this->semaphore);
2015-09-03 23:23:12 -05:00
$fork2 = new Fork(function (SemaphoreInterface $semaphore) {
$lock = (yield $semaphore->acquire());
2015-09-03 23:23:12 -05:00
usleep(1e5);
2015-09-03 23:23:12 -05:00
$lock->release();
2015-09-03 23:23:12 -05:00
yield 1;
}, $this->semaphore);
2015-09-03 23:23:12 -05:00
$start = microtime(true);
2015-09-03 23:23:12 -05:00
$fork1->start();
$fork2->start();
2015-09-03 23:23:12 -05:00
yield $fork1->join();
yield $fork2->join();
2015-09-03 23:23:12 -05:00
$this->assertGreaterThan(1, microtime(true) - $start);
});
Loop\run();
}
}