ProcessManagerTest::basicRulesAndTasksWork()   B
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 57

Duplication

Lines 18
Ratio 31.58 %

Importance

Changes 0
Metric Value
dl 18
loc 57
c 0
b 0
f 0
rs 7.0715
cc 10
nc 15
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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