Passed
Pull Request — master (#18)
by Harry
03:55
created

PoolLoggerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 116
dl 0
loc 208
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testRunCompleted() 0 25 1
A testPoolRunUpdatedOnRunStateChange() 0 45 1
A testRunStarted() 0 25 1
A testRunSuccessful() 0 25 1
A testRunFailed() 0 27 1
A testPoolRunAdded() 0 33 1
1
<?php
2
/**
3
 * This file is part of graze/parallel-process.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/parallel-process/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/parallel-process
12
 */
13
14
namespace Graze\ParallelProcess\Test\Unit\Display;
15
16
use Graze\ParallelProcess\CallbackRun;
17
use Graze\ParallelProcess\Monitor\PoolLogger;
18
use Graze\ParallelProcess\Pool;
19
use Graze\ParallelProcess\Test\TestCase;
20
use Psr\Log\LogLevel;
21
use Symfony\Component\Debug\BufferingLogger;
22
23
class PoolLoggerTest extends TestCase
24
{
25
    /** @var BufferingLogger */
26
    private $logger;
27
    /** @var PoolLogger */
28
    private $monitor;
29
30
    public function setUp()
31
    {
32
        $this->logger = new BufferingLogger();
33
        $this->monitor = new PoolLogger($this->logger);
34
    }
35
36
    public function testRunStarted()
37
    {
38
        $run = new CallbackRun(
39
            function () {
40
                return true;
41
            },
42
            ['key' => 'value']
43
        );
44
        $this->monitor->monitor($run);
45
46
        $run->start();
47
48
        $logs = $this->logger->cleanLogs();
49
50
        $this->assertCount(3, $logs); // add, successful, complete
51
        list($level, $message, $context) = reset($logs);
52
        $this->assertEquals(LogLevel::DEBUG, $level);
53
        $this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:\d+\]: has started$/i', $message);
54
        $this->assertArraySubset(
55
            [
56
                'run' => [
57
                    'tags' => ['key' => 'value'], 'hasStarted' => true, 'isRunning' => false, 'isSuccessful' => false,
58
                ],
59
            ],
60
            $context
61
        );
62
    }
63
64
    public function testRunSuccessful()
65
    {
66
        $run = new CallbackRun(
67
            function () {
68
                return true;
69
            },
70
            ['key' => 'value']
71
        );
72
        $this->monitor->monitor($run);
73
74
        $run->start();
75
76
        $logs = $this->logger->cleanLogs();
77
78
        $this->assertCount(3, $logs); // add, successful, complete
79
        list($level, $message, $context) = $logs[1];
80
        $this->assertEquals(LogLevel::DEBUG, $level);
81
        $this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:\d+\]: successfully finished$/i', $message);
82
        $this->assertArraySubset(
83
            [
84
                'run' => [
85
                    'tags' => ['key' => 'value'], 'hasStarted' => true, 'isRunning' => false, 'isSuccessful' => true,
86
                ],
87
            ],
88
            $context
89
        );
90
    }
91
92
    public function testRunFailed()
93
    {
94
        $exception = new \RuntimeException('failed');
95
        $run = new CallbackRun(
96
            function () use ($exception) {
97
                throw $exception;
98
            },
99
            ['key' => 'value']
100
        );
101
        $this->monitor->monitor($run);
102
103
        $run->start();
104
105
        $logs = $this->logger->cleanLogs();
106
107
        $this->assertCount(3, $logs); // add, failed, complete
108
        list($level, $message, $context) = $logs[1];
109
        $this->assertEquals(LogLevel::DEBUG, $level);
110
        $this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:\d+\]: failed - failed$/i', $message);
111
        $this->assertArraySubset(
112
            [
113
                'run'    => [
114
                    'tags' => ['key' => 'value'], 'hasStarted' => true, 'isRunning' => false, 'isSuccessful' => false,
115
                ],
116
                'errors' => ['failed'],
117
            ],
118
            $context
119
        );
120
    }
121
122
    public function testRunCompleted()
123
    {
124
        $run = new CallbackRun(
125
            function () {
126
                return true;
127
            },
128
            ['key' => 'value']
129
        );
130
        $this->monitor->monitor($run);
131
132
        $run->start();
133
134
        $logs = $this->logger->cleanLogs();
135
136
        $this->assertCount(3, $logs); // add, successful, complete
137
        list($level, $message, $context) = $logs[2];
138
        $this->assertEquals(LogLevel::DEBUG, $level);
139
        $this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:\d+\]: has finished running$/i', $message);
140
        $this->assertArraySubset(
141
            [
142
                'run' => [
143
                    'tags' => ['key' => 'value'], 'hasStarted' => true, 'isRunning' => false, 'isSuccessful' => true,
144
                ],
145
            ],
146
            $context
147
        );
148
    }
149
150
    public function testPoolRunAdded()
151
    {
152
        $pool = new Pool();
153
        $run = new CallbackRun(
154
            function () {
155
                return true;
156
            },
157
            ['key' => 'value']
158
        );
159
160
        $this->monitor->monitor($pool);
161
162
        $pool->add($run);
163
164
        $logs = $this->logger->cleanLogs();
165
166
        $this->assertCount(1, $logs);
167
        list($level, $message, $context) = $logs[0];
168
        $this->assertEquals(LogLevel::DEBUG, $level);
169
        $this->assertRegExp('/^pool \[[\\a-zA-Z0-9]+\:\d+\]: run \[[\\a-zA-Z0-9]+\:\d+\] has been added$/i', $message);
170
        $this->assertArraySubset(
171
            [
172
                'pool' => [
173
                    'tags'        => [], 'hasStarted' => false, 'isRunning' => false, 'isSuccessful' => false,
174
                    'duration'    => 0, 'progress' => 0.0,
175
                    'num_waiting' => 1, 'num_running' => 0, 'num_finished' => 0,
176
                ],
177
                'run'  => [
178
                    'tags'         => ['key' => 'value'], 'hasStarted' => false, 'isRunning' => false,
179
                    'isSuccessful' => false, 'duration' => 0, 'progress' => null,
180
                ],
181
            ],
182
            $context
183
        );
184
    }
185
186
    public function testPoolRunUpdatedOnRunStateChange()
187
    {
188
        $pool = new Pool();
189
        $run = new CallbackRun(
190
            function () {
191
                return true;
192
            },
193
            ['key' => 'value']
194
        );
195
196
        $pool->add($run);
197
198
        $this->monitor->monitor($pool);
199
200
        $run->start();
201
202
        $logs = $this->logger->cleanLogs();
203
204
        $this->assertCount(8, $logs);
205
        list($level, $message, $context) = $logs[1];
206
        $this->assertEquals(LogLevel::DEBUG, $level);
207
        $this->assertRegExp('/^pool \[[\\a-zA-Z0-9]+\:\d+\]: updated$/i', $message);
208
        $this->assertArraySubset(
209
            [
210
                'pool' => [
211
                    'tags'        => [], 'hasStarted' => true, 'isRunning' => true, 'isSuccessful' => false,
212
                    'progress'    => 0.0,
213
                    'num_waiting' => 0, 'num_running' => 1, 'num_finished' => 0,
214
                ],
215
            ],
216
            $context
217
        );
218
219
        list($level, $message, $context) = $logs[6];
220
        $this->assertEquals(LogLevel::DEBUG, $level);
221
        $this->assertRegExp('/^run \[[\\a-zA-Z0-9]+\:\d+\]: has finished running$/i', $message);
222
        $this->assertArraySubset(
223
            [
224
                'pool' => [
225
                    'type'         => Pool::class, 'tags' => [], 'hasStarted' => true, 'isRunning' => false,
226
                    'isSuccessful' => true, 'progress' => 1.0,
227
                    'num_waiting'  => 0, 'num_running' => 0, 'num_finished' => 1,
228
                ],
229
            ],
230
            $context
231
        );
232
    }
233
}
234