1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-04 18:47:50 +01:00
parallel/test/Threading/MutexTest.php

56 lines
1.3 KiB
PHP
Raw Normal View History

2015-08-08 05:50:20 +02:00
<?php
2016-08-18 18:04:48 +02:00
namespace Amp\Tests\Concurrent\Threading;
use Amp\Concurrent\Threading\Mutex;
use Amp\Coroutine;
use Amp\Loop;
use Amp\Tests\Concurrent\TestCase;
2015-08-08 05:50:20 +02:00
/**
* @group threading
* @requires extension pthreads
2015-08-08 05:50:20 +02:00
*/
class MutexTest extends TestCase
2015-08-08 05:50:20 +02:00
{
public function testAcquire()
{
Coroutine\create(function () {
$mutex = new Mutex();
2016-01-23 07:00:56 +01:00
$lock = yield from $mutex->acquire();
2015-08-08 05:50:20 +02:00
$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-31 21:00:07 +02:00
$this->assertRunTimeGreaterThan(function () {
2015-08-08 05:50:20 +02:00
Coroutine\create(function () {
$mutex = new Mutex();
2015-08-08 05:50:20 +02:00
2016-01-23 07:00:56 +01:00
$lock1 = yield from $mutex->acquire();
2015-08-08 05:50:20 +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-08-08 05:50:20 +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-08-08 05:50:20 +02:00
Loop\timer(0.5, function () use ($lock3) {
$lock3->release();
});
});
Loop\run();
2015-08-31 21:00:07 +02:00
}, 1.5);
2015-08-08 05:50:20 +02:00
}
}