2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-09-04 06:23:12 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Test\Sync;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2017-05-11 06:41:10 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Loop;
|
2017-03-22 05:19:15 +01:00
|
|
|
use Amp\PHPUnit\TestCase;
|
2015-09-04 06:23:12 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
abstract class AbstractSemaphoreTest extends TestCase {
|
2015-09-04 06:23:12 +02:00
|
|
|
/**
|
2016-08-23 23:47:40 +02:00
|
|
|
* @var \Amp\Parallel\Sync\Semaphore
|
2015-09-04 06:23:12 +02:00
|
|
|
*/
|
|
|
|
protected $semaphore;
|
|
|
|
|
|
|
|
/**
|
2016-08-19 00:36:58 +02:00
|
|
|
* @param int $locks Number of locks in the semaphore.
|
|
|
|
*
|
2016-08-23 23:47:40 +02:00
|
|
|
* @return \Amp\Parallel\Sync\Semaphore
|
2015-09-04 06:23:12 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
abstract public function createSemaphore(int $locks);
|
2015-09-26 06:41:15 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testCount() {
|
2015-09-26 06:41:15 +02:00
|
|
|
$this->semaphore = $this->createSemaphore(4);
|
2015-09-04 06:23:12 +02:00
|
|
|
|
|
|
|
$this->assertCount(4, $this->semaphore);
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testAcquire() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-04 06:23:12 +02:00
|
|
|
$this->semaphore = $this->createSemaphore(1);
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock = yield $this->semaphore->acquire();
|
2015-09-04 06:23:12 +02:00
|
|
|
|
|
|
|
$this->assertFalse($lock->isReleased());
|
|
|
|
|
|
|
|
$lock->release();
|
|
|
|
|
|
|
|
$this->assertTrue($lock->isReleased());
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-04 06:23:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testAcquireMultiple() {
|
2015-09-04 06:23:12 +02:00
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
|
|
|
$this->semaphore = $this->createSemaphore(1);
|
|
|
|
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock1 = yield $this->semaphore->acquire();
|
2016-12-30 02:17:26 +01:00
|
|
|
Loop::delay(500, function () use ($lock1) {
|
2015-09-04 06:23:12 +02:00
|
|
|
$lock1->release();
|
|
|
|
});
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock2 = yield $this->semaphore->acquire();
|
2016-12-30 02:17:26 +01:00
|
|
|
Loop::delay(500, function () use ($lock2) {
|
2015-09-04 06:23:12 +02:00
|
|
|
$lock2->release();
|
|
|
|
});
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock3 = yield $this->semaphore->acquire();
|
2016-12-30 02:17:26 +01:00
|
|
|
Loop::delay(500, function () use ($lock3) {
|
2015-09-04 06:23:12 +02:00
|
|
|
$lock3->release();
|
|
|
|
});
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2017-03-22 05:19:15 +01:00
|
|
|
}, 1500);
|
2015-09-04 06:23:12 +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-04 06:23:12 +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-04 06:23:12 +02:00
|
|
|
|
|
|
|
$this->assertCount(1, $this->semaphore);
|
|
|
|
$this->assertCount(0, $clone);
|
|
|
|
|
|
|
|
$lock->release();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-04 06:23:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testSerializedIsSameSemaphore() {
|
2017-03-16 23:03:59 +01:00
|
|
|
Loop::run(function () {
|
2015-09-04 06:23:12 +02:00
|
|
|
$this->semaphore = $this->createSemaphore(1);
|
|
|
|
$unserialized = unserialize(serialize($this->semaphore));
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock = yield $unserialized->acquire();
|
2015-09-04 06:23:12 +02:00
|
|
|
|
|
|
|
$this->assertCount(0, $this->semaphore);
|
|
|
|
$this->assertCount(0, $unserialized);
|
|
|
|
|
|
|
|
$lock->release();
|
2017-03-16 23:03:59 +01:00
|
|
|
});
|
2015-09-04 06:23:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testSimultaneousAcquire() {
|
2015-09-04 06:23:12 +02:00
|
|
|
$this->semaphore = $this->createSemaphore(1);
|
|
|
|
|
2017-03-22 05:19:15 +01:00
|
|
|
$callback = function () {
|
2016-08-19 00:36:58 +02:00
|
|
|
$awaitable1 = $this->semaphore->acquire();
|
|
|
|
$awaitable2 = $this->semaphore->acquire();
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2017-05-11 06:41:10 +02:00
|
|
|
yield new Delayed(500);
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
(yield $awaitable1)->release();
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2017-05-11 06:41:10 +02:00
|
|
|
yield new Delayed(500);
|
2017-05-18 09:51:31 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
(yield $awaitable2)->release();
|
2017-03-22 05:19:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$this->assertRunTimeGreaterThan('Amp\Loop::run', 1000, [$callback]);
|
2015-09-04 06:23:12 +02:00
|
|
|
}
|
|
|
|
}
|