GroupProcessManagerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace AsyncPHP\Doorman\Tests\Manager;
4
5
use AsyncPHP\Doorman\Manager\GroupProcessManager;
6
use AsyncPHP\Doorman\Manager\ProcessManager;
7
use AsyncPHP\Doorman\Task\ProcessCallbackTask;
8
use AsyncPHP\Doorman\Tests\Test;
9
10
/**
11
 * @covers AsyncPHP\Doorman\Manager\GroupProcessManager
12
 */
13
class GroupProcessManagerTest extends Test
14
{
15
    /**
16
     * @var GroupProcessManager
17
     */
18
    protected $manager;
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function setUp()
24
    {
25
        parent::setUp();
26
27
        $this->manager = new GroupProcessManager(
28
            new ProcessManager()
29
        );
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function groupsExecuteInPredictableOrder()
36
    {
37
        $this->unlink("task1");
38
        $this->unlink("task2");
39
        $this->unlink("task3");
40
        $this->unlink("task4");
41
42
        $task1 = new ProcessCallbackTask(function () {
43
            GroupProcessManagerTest::dawdle("task1");
44
        });
45
46
        $task2 = new ProcessCallbackTask(function () {
47
            GroupProcessManagerTest::dawdle("task2");
48
        });
49
50
        $task3 = new ProcessCallbackTask(function () {
51
            GroupProcessManagerTest::dawdle("task3");
52
        });
53
54
        $task4 = new ProcessCallbackTask(function () {
55
            GroupProcessManagerTest::dawdle("task4");
56
        });
57
58
        $this->manager->addTask($task1);
59
        $this->manager->addTaskGroup(array($task2, $task3));
60
        $this->manager->addTask($task4);
61
62
        while ($this->manager->tick()) {
63
            $exists1 = $this->exists("task1");
64
            $exists2 = $this->exists("task2");
65
            $exists3 = $this->exists("task3");
66
            $exists4 = $this->exists("task4");
67
68
            if ($exists1 && ($exists2 || $exists3)) {
69
                $this->fail("task1 should not run at the same time as task2 and/or task3");
70
            }
71
72
            if ($exists4 && ($exists2 || $exists3)) {
73
                $this->fail("task4 should not run at the same time as task2 and/or task3");
74
            }
75
76
            usleep(25000);
77
        }
78
    }
79
80
    /**
81
     * @param string $name
82
     */
83
    protected function unlink($name)
84
    {
85
        if ($this->exists($name)) {
86
            unlink(__DIR__ . "/{$name}.temp");
87
        }
88
    }
89
90
    /**
91
     * @param string $name
92
     *
93
     * @return bool
94
     */
95
    protected function exists($name)
96
    {
97
        return file_exists(__DIR__ . "/{$name}.temp");
98
    }
99
100
    /**
101
     * @param string $name
102
     */
103 View Code Duplication
    public static function dawdle($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
104
    {
105
        touch(__DIR__ . "/{$name}.temp");
106
107
        for ($i = 0; $i < 5; $i++) {
108
            usleep(25000);
109
        }
110
111
        unlink(__DIR__ . "/{$name}.temp");
112
    }
113
}
114