1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-08 05:38:21 +01:00
parallel/test/Sync/AbstractSemaphoreTest.php

109 lines
2.9 KiB
PHP
Raw Normal View History

2016-08-22 06:40:48 +02:00
<?php declare(strict_types = 1);
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
2016-08-23 23:47:40 +02:00
use Amp\Parallel\Test\TestCase;
2016-08-19 00:36:58 +02:00
use Amp\Pause;
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);
2016-08-19 00:36:58 +02:00
public function testCount() {
$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() {
\Amp\execute(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());
2016-08-19 00:36:58 +02: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);
2016-08-19 00:36:58 +02:00
\Amp\execute(function () {
$lock1 = yield $this->semaphore->acquire();
\Amp\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();
\Amp\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();
\Amp\delay(500, function () use ($lock3) {
2015-09-04 06:23:12 +02:00
$lock3->release();
});
2016-08-19 00:36:58 +02:00
});
2015-09-04 06:23:12 +02:00
}, 1.5);
}
2016-08-19 00:36:58 +02:00
public function testCloneIsNewSemaphore() {
\Amp\execute(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();
2016-08-19 00:36:58 +02:00
});
2015-09-04 06:23:12 +02:00
}
2016-08-19 00:36:58 +02:00
public function testSerializedIsSameSemaphore() {
\Amp\execute(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();
2016-08-19 00:36:58 +02: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);
2016-08-19 00:36:58 +02:00
\Amp\execute(function () {
$awaitable1 = $this->semaphore->acquire();
$awaitable2 = $this->semaphore->acquire();
yield new Pause(500);
(yield $awaitable1)->release();
yield new Pause(500);
(yield $awaitable2)->release();
2015-09-04 06:23:12 +02:00
});
}
}