<?php
/**
* TEST: Semaphore test.
*/
declare(strict_types=1);
use Tester\Assert;
require(__DIR__ . '/../bootstrap.php');
$semaphore = new Awaited\Semaphore(3, 1);
$runs = [];
$semaphore->await()->then(function() use(&$runs): void {
$runs[] = 1;
});
$semaphore->await()->then(function() use(&$runs): void {
$runs[] = 2;
});
$semaphore->await()->then(function(callable $release) use(&$runs): void {
$runs[] = 3;
$release();
});
$semaphore->await()->then(function(callable $release) use(&$runs): void {
$runs[] = 4;
$release();
});
Assert::true($semaphore->isBlocked());
Assert::same([1, 2], $runs);
$semaphore->release();
Assert::same([1, 2, 3, 4], $runs);
Assert::false($semaphore->isBlocked());
$semaphore = new Awaited\Semaphore;
$semaphore->release();
Assert::exception(function(): void {
new Awaited\Semaphore(0);
}, InvalidArgumentException::class);
Assert::exception(function(): void {
new Awaited\Semaphore(1, 2);
}, InvalidArgumentException::class);
Assert::exception(function(): void {
new Awaited\Semaphore(1, -1);
}, InvalidArgumentException::class);