2015-09-26 06:41:15 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Tests\Concurrent\Sync;
|
|
|
|
|
|
|
|
use Amp\Concurrent\Sync\FileMutex;
|
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Tests\Concurrent\TestCase;
|
2015-09-26 06:41:15 +02:00
|
|
|
|
|
|
|
class FileMutexTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testAcquire()
|
|
|
|
{
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$mutex = new FileMutex();
|
2016-01-23 07:00:56 +01:00
|
|
|
$lock = yield from $mutex->acquire();
|
2015-09-26 06:41:15 +02:00
|
|
|
$lock->release();
|
|
|
|
$this->assertTrue($lock->isReleased());
|
|
|
|
})->done();
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAcquireMultiple()
|
|
|
|
{
|
|
|
|
Loop\loop();
|
|
|
|
|
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
|
|
|
Coroutine\create(function () {
|
|
|
|
$mutex = new FileMutex();
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
$lock1 = yield from $mutex->acquire();
|
2015-09-26 06:41:15 +02:00
|
|
|
Loop\timer(0.5, function () use ($lock1) {
|
|
|
|
$lock1->release();
|
|
|
|
});
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
$lock2 = yield from $mutex->acquire();
|
2015-09-26 06:41:15 +02:00
|
|
|
Loop\timer(0.5, function () use ($lock2) {
|
|
|
|
$lock2->release();
|
|
|
|
});
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
$lock3 = yield from $mutex->acquire();
|
2015-09-26 06:41:15 +02:00
|
|
|
Loop\timer(0.5, function () use ($lock3) {
|
|
|
|
$lock3->release();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop\run();
|
|
|
|
}, 1.5);
|
|
|
|
}
|
|
|
|
}
|