1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-11 16:49:51 +01:00
parallel/tests/Threading/SemaphoreTest.php

60 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace Icicle\Tests\Concurrent\Threading;
2015-12-05 06:50:32 +01:00
use Icicle\Concurrent\Sync\Semaphore as SyncSemaphore;
2016-01-23 07:00:56 +01:00
use Icicle\Concurrent\Threading\{Semaphore, Thread};
use Icicle\Coroutine;
use Icicle\Loop;
2015-09-04 06:23:12 +02:00
use Icicle\Tests\Concurrent\Sync\AbstractSemaphoreTest;
/**
* @group threading
* @requires extension pthreads
*/
2015-09-04 06:23:12 +02:00
class SemaphoreTest extends AbstractSemaphoreTest
{
2015-09-04 06:23:12 +02:00
public function createSemaphore($locks)
{
2015-09-04 06:23:12 +02:00
return new Semaphore($locks);
}
public function testAcquireInMultipleThreads()
{
Coroutine\create(function () {
2015-09-04 06:23:12 +02:00
$this->semaphore = $this->createSemaphore(1);
2015-12-05 06:50:32 +01:00
$thread1 = new Thread(function (SyncSemaphore $semaphore) {
2016-01-23 07:00:56 +01:00
$lock = yield from $semaphore->acquire();
usleep(1e5);
$lock->release();
2016-01-23 07:00:56 +01:00
return 0;
2015-09-04 06:23:12 +02:00
}, $this->semaphore);
2015-12-05 06:50:32 +01:00
$thread2 = new Thread(function (SyncSemaphore $semaphore) {
2016-01-23 07:00:56 +01:00
$lock = yield from $semaphore->acquire();
usleep(1e5);
$lock->release();
2016-01-23 07:00:56 +01:00
return 1;
2015-09-04 06:23:12 +02:00
}, $this->semaphore);
$start = microtime(true);
$thread1->start();
$thread2->start();
2016-01-25 06:03:45 +01:00
yield from $thread1->join();
yield from $thread2->join();
$this->assertGreaterThan(1, microtime(true) - $start);
});
Loop\run();
}
}