mirror of
https://github.com/danog/parallel.git
synced 2024-11-27 04:44:56 +01:00
100 lines
2.2 KiB
PHP
100 lines
2.2 KiB
PHP
<?php
|
|
namespace Icicle\Tests\Concurrent\Sync;
|
|
|
|
use Icicle\Concurrent\Forking\Fork;
|
|
use Icicle\Concurrent\Sync\PosixSemaphore;
|
|
use Icicle\Concurrent\Sync\Semaphore;
|
|
use Icicle\Coroutine;
|
|
use Icicle\Loop;
|
|
|
|
/**
|
|
* @group posix
|
|
* @requires extension sysvmsg
|
|
*/
|
|
class PosixSemaphoreTest extends AbstractSemaphoreTest
|
|
{
|
|
public function createSemaphore($locks)
|
|
{
|
|
return new PosixSemaphore($locks);
|
|
}
|
|
|
|
public function tearDown()
|
|
{
|
|
if ($this->semaphore && !$this->semaphore->isFreed()) {
|
|
$this->semaphore->free();
|
|
}
|
|
}
|
|
|
|
public function testCloneIsNewSemaphore()
|
|
{
|
|
Coroutine\create(function () {
|
|
$this->semaphore = $this->createSemaphore(1);
|
|
$clone = clone $this->semaphore;
|
|
|
|
$lock = (yield $clone->acquire());
|
|
|
|
$this->assertCount(1, $this->semaphore);
|
|
$this->assertCount(0, $clone);
|
|
|
|
$lock->release();
|
|
|
|
$clone->free();
|
|
})->done();
|
|
|
|
Loop\run();
|
|
}
|
|
|
|
public function testFree()
|
|
{
|
|
$this->semaphore = $this->createSemaphore(1);
|
|
|
|
$this->assertFalse($this->semaphore->isFreed());
|
|
|
|
$this->semaphore->free();
|
|
|
|
$this->assertTrue($this->semaphore->isFreed());
|
|
}
|
|
|
|
/**
|
|
* @requires extension pcntl
|
|
*/
|
|
public function testAcquireInMultipleForks()
|
|
{
|
|
Coroutine\create(function () {
|
|
$this->semaphore = $this->createSemaphore(1);
|
|
|
|
$fork1 = new Fork(function (Semaphore $semaphore) {
|
|
$lock = (yield $semaphore->acquire());
|
|
|
|
usleep(1e5);
|
|
|
|
$lock->release();
|
|
|
|
yield 0;
|
|
}, $this->semaphore);
|
|
|
|
$fork2 = new Fork(function (Semaphore $semaphore) {
|
|
$lock = (yield $semaphore->acquire());
|
|
|
|
usleep(1e5);
|
|
|
|
$lock->release();
|
|
|
|
yield 1;
|
|
}, $this->semaphore);
|
|
|
|
$start = microtime(true);
|
|
|
|
$fork1->start();
|
|
$fork2->start();
|
|
|
|
yield $fork1->join();
|
|
yield $fork2->join();
|
|
|
|
$this->assertGreaterThan(1, microtime(true) - $start);
|
|
});
|
|
|
|
Loop\run();
|
|
}
|
|
}
|