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

SemaphoreTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 56
rs 10
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