2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-08-11 05:27:10 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Test\Sync;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\Forking\Fork;
|
2016-08-31 01:27:14 +02:00
|
|
|
use Amp\Parallel\Sync\{ PosixSemaphore, Semaphore };
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Loop;
|
2015-08-11 05:27:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group posix
|
2015-08-31 11:18:14 +02:00
|
|
|
* @requires extension sysvmsg
|
2015-08-11 05:27:10 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
class PosixSemaphoreTest extends AbstractSemaphoreTest {
|
|
|
|
/**
|
|
|
|
* @param $locks
|
|
|
|
*
|
2016-08-23 23:47:40 +02:00
|
|
|
* @return \Amp\Parallel\Sync\PosixSemaphore
|
2016-08-19 00:36:58 +02:00
|
|
|
*/
|
|
|
|
public function createSemaphore(int $locks) {
|
2015-09-04 06:23:12 +02:00
|
|
|
return new PosixSemaphore($locks);
|
|
|
|
}
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function tearDown() {
|
2015-09-26 06:41:15 +02:00
|
|
|
if ($this->semaphore && !$this->semaphore->isFreed()) {
|
2015-09-04 06:23:12 +02:00
|
|
|
$this->semaphore->free();
|
|
|
|
}
|
2015-08-31 11:18:14 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testCloneIsNewSemaphore() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-26 06:41:15 +02:00
|
|
|
$this->semaphore = $this->createSemaphore(1);
|
|
|
|
$clone = clone $this->semaphore;
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock = yield $clone->acquire();
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
$this->assertCount(1, $this->semaphore);
|
|
|
|
$this->assertCount(0, $clone);
|
|
|
|
|
|
|
|
$lock->release();
|
|
|
|
|
|
|
|
$clone->free();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testFree() {
|
2015-09-04 06:23:12 +02:00
|
|
|
$this->semaphore = $this->createSemaphore(1);
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2015-09-04 06:23:12 +02:00
|
|
|
$this->assertFalse($this->semaphore->isFreed());
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2015-09-04 06:23:12 +02:00
|
|
|
$this->semaphore->free();
|
|
|
|
|
|
|
|
$this->assertTrue($this->semaphore->isFreed());
|
2015-08-31 11:18:14 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 06:23:12 +02:00
|
|
|
/**
|
|
|
|
* @requires extension pcntl
|
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testAcquireInMultipleForks() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-04 06:23:12 +02:00
|
|
|
$this->semaphore = $this->createSemaphore(1);
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
$fork1 = new Fork(function (Semaphore $semaphore) {
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock = yield $semaphore->acquire();
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2016-08-22 06:40:48 +02:00
|
|
|
usleep(100000);
|
2015-08-11 05:35:59 +02:00
|
|
|
|
2015-09-04 06:23:12 +02:00
|
|
|
$lock->release();
|
2015-08-11 05:27:10 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
return 0;
|
2015-09-04 06:23:12 +02:00
|
|
|
}, $this->semaphore);
|
2015-08-11 05:27:10 +02:00
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
$fork2 = new Fork(function (Semaphore $semaphore) {
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock = yield $semaphore->acquire();
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2016-08-22 06:40:48 +02:00
|
|
|
usleep(100000);
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2015-09-04 06:23:12 +02:00
|
|
|
$lock->release();
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
return 1;
|
2015-09-04 06:23:12 +02:00
|
|
|
}, $this->semaphore);
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2015-09-04 06:23:12 +02:00
|
|
|
$start = microtime(true);
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2015-09-04 06:23:12 +02:00
|
|
|
$fork1->start();
|
|
|
|
$fork2->start();
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
yield $fork1->join();
|
|
|
|
yield $fork2->join();
|
2015-08-31 11:18:14 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$this->assertGreaterThan(0.1, microtime(true) - $start);
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-08-31 11:18:14 +02:00
|
|
|
}
|
2015-08-11 05:27:10 +02:00
|
|
|
}
|