1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 04:44:56 +01:00
parallel/test/Sync/AbstractSemaphoreTest.php

112 lines
2.9 KiB
PHP
Raw Normal View History

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;
use Amp\Loop;
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);
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() {
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());
});
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);
Loop::run(function () {
2016-08-19 00:36:58 +02:00
$lock1 = yield $this->semaphore->acquire();
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();
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();
Loop::delay(500, function () use ($lock3) {
2015-09-04 06:23:12 +02:00
$lock3->release();
});
});
}, 1500);
2015-09-04 06:23:12 +02:00
}
2016-08-19 00:36:58 +02:00
public function testCloneIsNewSemaphore() {
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();
});
2015-09-04 06:23:12 +02:00
}
2016-08-19 00:36:58 +02:00
public function testSerializedIsSameSemaphore() {
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();
});
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);
$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();
};
$this->assertRunTimeGreaterThan('Amp\Loop::run', 1000, [$callback]);
2015-09-04 06:23:12 +02:00
}
}