2016-08-22 06:40:48 +02:00
|
|
|
<?php declare(strict_types = 1);
|
2015-08-08 05:50:20 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
namespace Amp\Concurrent\Test\Threading;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
|
|
|
use Amp\Concurrent\Threading\Mutex;
|
2016-08-19 00:36:58 +02:00
|
|
|
use Amp\Concurrent\Test\TestCase;
|
2015-08-08 05:50:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group threading
|
2015-08-28 22:09:07 +02:00
|
|
|
* @requires extension pthreads
|
2015-08-08 05:50:20 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
class MutexTest extends TestCase {
|
|
|
|
public function testAcquire() {
|
|
|
|
\Amp\execute(function () {
|
2015-08-31 01:25:44 +02:00
|
|
|
$mutex = new Mutex();
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock = yield $mutex->acquire();
|
2015-08-08 05:50:20 +02:00
|
|
|
$lock->release();
|
|
|
|
$this->assertTrue($lock->isReleased());
|
2016-08-19 00:36:58 +02:00
|
|
|
});
|
2015-08-08 05:50:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
public function testAcquireMultiple() {
|
2015-08-31 21:00:07 +02:00
|
|
|
$this->assertRunTimeGreaterThan(function () {
|
2016-08-19 00:36:58 +02:00
|
|
|
\Amp\execute(function () {
|
2015-08-31 01:25:44 +02:00
|
|
|
$mutex = new Mutex();
|
2015-08-08 05:50:20 +02:00
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock1 = yield $mutex->acquire();
|
|
|
|
\Amp\delay(500, function () use ($lock1) {
|
2015-08-08 05:50:20 +02:00
|
|
|
$lock1->release();
|
|
|
|
});
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock2 = yield $mutex->acquire();
|
|
|
|
\Amp\delay(500, function () use ($lock2) {
|
2015-08-08 05:50:20 +02:00
|
|
|
$lock2->release();
|
|
|
|
});
|
|
|
|
|
2016-08-19 00:36:58 +02:00
|
|
|
$lock3 = yield $mutex->acquire();
|
|
|
|
\Amp\delay(500, function () use ($lock3) {
|
2015-08-08 05:50:20 +02:00
|
|
|
$lock3->release();
|
|
|
|
});
|
|
|
|
});
|
2015-08-31 21:00:07 +02:00
|
|
|
}, 1.5);
|
2015-08-08 05:50:20 +02:00
|
|
|
}
|
|
|
|
}
|