ProcessManagerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace AsyncPHP\Doorman\Tests\Manager;
4
5
use AsyncPHP\Doorman\Manager\ProcessManager;
6
use AsyncPHP\Doorman\Rule\InMemoryRule;
7
use AsyncPHP\Doorman\Rules\InMemoryRules;
8
use AsyncPHP\Doorman\Shell\BashShell;
9
use AsyncPHP\Doorman\Task\ProcessCallbackTask;
10
use AsyncPHP\Doorman\Tests\Test;
11
12
/**
13
 * @covers AsyncPHP\Doorman\Manager\ProcessManager
14
 */
15
class ProcessManagerTest extends Test
16
{
17
    /**
18
     * @var ProcessManager
19
     */
20
    protected $manager;
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function setUp()
26
    {
27
        parent::setUp();
28
29
        $this->manager = new ProcessManager();
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function gettersAndSettersWork()
36
    {
37
        $this->manager->setLogPath(__DIR__);
38
39
        $this->assertEquals(__DIR__, $this->manager->getLogPath());
40
41
        $this->assertInstanceOf("AsyncPHP\\Doorman\\Shell", $this->manager->getShell());
42
43
        $shell = new BashShell();
44
45
        $this->manager->setShell($shell);
46
47
        $this->assertEquals($shell, $this->manager->getShell());
48
49
        $this->assertInstanceOf("AsyncPHP\\Doorman\\Rules", $this->manager->getRules());
50
51
        $rules = new InMemoryRules();
52
53
        $this->manager->setRules($rules);
54
55
        $this->assertEquals($rules, $this->manager->getRules());
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function basicRulesAndTasksWork()
62
    {
63 View Code Duplication
        $task1 = new ProcessCallbackTask(function () {
64
            touch(__DIR__ . "/task1.temp");
65
66
            for ($i = 0; $i < 10; $i++) {
67
                usleep(50000);
68
            }
69
70
            unlink(__DIR__ . "/task1.temp");
71
        });
72
73 View Code Duplication
        $task2 = new ProcessCallbackTask(function () {
74
            touch(__DIR__ . "/task2.temp");
75
76
            for ($i = 0; $i < 10; $i++) {
77
                usleep(50000);
78
            }
79
80
            unlink(__DIR__ . "/task2.temp");
81
        });
82
83
        $rule = new InMemoryRule();
84
        $rule->setProcesses(1);
85
        $rule->setMinimumProcessorUsage(0);
86
        $rule->setMaximumProcessorUsage(100);
87
88
        $added = false;
89
90
        $this->manager->addRule($rule);
91
        $this->manager->addTask($task1);
92
93
        while ($this->manager->tick()) {
94
            usleep(50000);
95
96
            if (!$added) {
97
                $this->manager->addTask($task2);
98
                $added = true;
99
            }
100
101
            if (file_exists(__DIR__ . "/task1.temp") && file_exists(__DIR__ . "/task2.temp")) {
102
                $this->fail("Tasks should not be run concurrently");
103
            }
104
        }
105
106
        $this->manager->removeRule($rule);
107
        $this->manager->addTask($task1);
108
        $this->manager->addTask($task2);
109
110
        if ($this->manager->tick()) {
111
            usleep(50000);
112
113
            if (!file_exists(__DIR__ . "/task1.temp") || !file_exists(__DIR__ . "/task2.temp")) {
114
                $this->fail("Tasks should be run concurrently");
115
            }
116
        }
117
    }
118
}
119