2015-08-08 05:50:20 +02:00
|
|
|
<?php
|
2015-08-31 01:25:44 +02:00
|
|
|
namespace Icicle\Tests\Concurrent\Threading;
|
2015-08-08 05:50:20 +02:00
|
|
|
|
2015-08-31 01:25:44 +02:00
|
|
|
use Icicle\Concurrent\Threading\Mutex;
|
2015-08-08 05:50:20 +02:00
|
|
|
use Icicle\Coroutine;
|
|
|
|
use Icicle\Loop;
|
|
|
|
use Icicle\Tests\Concurrent\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group threading
|
2015-08-28 22:09:07 +02:00
|
|
|
* @requires extension pthreads
|
2015-08-08 05:50:20 +02:00
|
|
|
*/
|
2015-08-31 01:25:44 +02:00
|
|
|
class MutexTest extends TestCase
|
2015-08-08 05:50:20 +02:00
|
|
|
{
|
|
|
|
public function testAcquire()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
2015-08-31 01:25:44 +02:00
|
|
|
$mutex = new Mutex();
|
2015-08-08 05:50:20 +02:00
|
|
|
$lock = (yield $mutex->acquire());
|
|
|
|
$lock->release();
|
|
|
|
$this->assertTrue($lock->isReleased());
|
2015-08-28 21:41:27 +02:00
|
|
|
})->done();
|
2015-08-08 05:50:20 +02:00
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAcquireMultiple()
|
|
|
|
{
|
2015-08-28 23:58:15 +02:00
|
|
|
Loop\loop();
|
|
|
|
|
2015-08-08 05:50:20 +02:00
|
|
|
$this->assertRunTimeBetween(function () {
|
|
|
|
Coroutine\create(function () {
|
2015-08-31 01:25:44 +02:00
|
|
|
$mutex = new Mutex();
|
2015-08-08 05:50:20 +02:00
|
|
|
|
|
|
|
$lock1 = (yield $mutex->acquire());
|
|
|
|
Loop\timer(0.5, function () use ($lock1) {
|
|
|
|
$lock1->release();
|
|
|
|
});
|
|
|
|
|
|
|
|
$lock2 = (yield $mutex->acquire());
|
|
|
|
Loop\timer(0.5, function () use ($lock2) {
|
|
|
|
$lock2->release();
|
|
|
|
});
|
|
|
|
|
|
|
|
$lock3 = (yield $mutex->acquire());
|
|
|
|
Loop\timer(0.5, function () use ($lock3) {
|
|
|
|
$lock3->release();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop\run();
|
2015-08-28 23:58:15 +02:00
|
|
|
}, 1.5, 1.65);
|
2015-08-08 05:50:20 +02:00
|
|
|
}
|
|
|
|
}
|