Completed
Push — master ( 8cc4ca...2aa5db )
by Taosikai
11:59
created

SemaphoreTest::testRelease()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace Slince\Process\Tests\SystemV;
3
4
use PHPUnit\Framework\TestCase;
5
use Slince\Process\Process;
6
use Slince\Process\SystemV\Semaphore;
7
8
class SemaphoreTest extends TestCase
9
{
10
    public function testAcquire()
11
    {
12
        $semaphore = new Semaphore();
13
        $this->assertTrue($semaphore->acquire());
14
        if (version_compare(PHP_VERSION, '5.6.1') < 0) {
15
            $this->markTestSkipped();
16
        }
17
        $this->assertFalse($semaphore->acquire(false));
18
    }
19
20
    public function testRelease()
21
    {
22
        $semaphore = new Semaphore();
23
        $semaphore->acquire();
24
        $this->assertTrue($semaphore->release());
25
        $this->assertFalse($semaphore->release());
26
    }
27
28
    public function testMutex()
29
    {
30
        if (version_compare(PHP_VERSION, '5.6.1') < 0) {
31
            $this->markTestSkipped();
32
        }
33
        $process = new Process(function () {
34
            $semaphore = new Semaphore();
35
            $semaphore->acquire();
36
            sleep(2);
37
            $semaphore->release();
38
        });
39
        $process->start();
40
        sleep(1);
41
        $semaphore = new Semaphore();
42
        $this->assertFalse($semaphore->acquire(false));
43
        $process->wait();
44
        $this->assertTrue($semaphore->acquire(false));
45
        $semaphore->release();
46
    }
47
48
    public function testBlockingMutex()
49
    {
50
        $process = new Process(function () {
51
            $semaphore = new Semaphore();
52
            $semaphore->acquire();
53
            sleep(2);
54
            $semaphore->release();
55
        });
56
        $process->start();
57
        sleep(1);
58
        $semaphore = new Semaphore();
59
        $this->assertTrue($semaphore->acquire(true));
60
        $semaphore->release();
61
        $process->wait();
62
    }
63
}
64