2016-12-29 19:16:04 -06:00
|
|
|
<?php
|
2015-09-25 23:41:15 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
namespace Amp\Parallel\Test\Sync;
|
2016-08-18 11:04:48 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
use Amp\Parallel\Sync\FileMutex;
|
|
|
|
use Amp\Parallel\Test\TestCase;
|
2016-12-29 19:17:26 -06:00
|
|
|
use Interop\Async\Loop;
|
2016-08-18 17:36:58 -05:00
|
|
|
|
|
|
|
class FileMutexTest extends TestCase {
|
|
|
|
public function testAcquire() {
|
2016-12-29 19:17:26 -06:00
|
|
|
Loop::execute(\Amp\wrap(function () {
|
2016-08-22 18:25:19 -05:00
|
|
|
$mutex = new FileMutex;
|
2016-08-18 17:36:58 -05:00
|
|
|
$lock = yield $mutex->acquire();
|
2015-09-25 23:41:15 -05:00
|
|
|
$lock->release();
|
|
|
|
$this->assertTrue($lock->isReleased());
|
2016-12-29 19:17:26 -06:00
|
|
|
}));
|
2015-09-25 23:41:15 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testAcquireMultiple() {
|
2015-09-25 23:41:15 -05:00
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
2016-12-29 19:17:26 -06:00
|
|
|
Loop::execute(\Amp\wrap(function () {
|
2016-08-22 18:25:19 -05:00
|
|
|
$mutex = new FileMutex;
|
2015-09-25 23:41:15 -05:00
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
$lock1 = yield $mutex->acquire();
|
2016-12-29 19:17:26 -06:00
|
|
|
Loop::delay(500, function () use ($lock1) {
|
2015-09-25 23:41:15 -05:00
|
|
|
$lock1->release();
|
|
|
|
});
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
$lock2 = yield $mutex->acquire();
|
2016-12-29 19:17:26 -06:00
|
|
|
Loop::delay(500, function () use ($lock2) {
|
2015-09-25 23:41:15 -05:00
|
|
|
$lock2->release();
|
|
|
|
});
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
$lock3 = yield $mutex->acquire();
|
2016-12-29 19:17:26 -06:00
|
|
|
Loop::delay(500, function () use ($lock3) {
|
2015-09-25 23:41:15 -05:00
|
|
|
$lock3->release();
|
|
|
|
});
|
2016-12-29 19:17:26 -06:00
|
|
|
}));
|
2015-09-25 23:41:15 -05:00
|
|
|
}, 1.5);
|
|
|
|
}
|
|
|
|
}
|