1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of graze/parallel-process. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @license https://github.com/graze/parallel-process/blob/master/LICENSE.md |
12
|
|
|
* @link https://github.com/graze/parallel-process |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Graze\ParallelProcess\Test\Unit; |
16
|
|
|
|
17
|
|
|
use Graze\ParallelProcess\Pool; |
18
|
|
|
use Graze\ParallelProcess\Run; |
19
|
|
|
use Graze\ParallelProcess\Test\TestCase; |
20
|
|
|
use Mockery; |
21
|
|
|
use Symfony\Component\Process\Process; |
22
|
|
|
|
23
|
|
|
class PoolMaxSimultaneousTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** @var mixed */ |
26
|
|
|
private $process; |
27
|
|
|
|
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
$this->process = Mockery::mock(Process::class); |
33
|
|
|
$this->process->shouldReceive('stop'); |
34
|
|
|
$this->process->shouldReceive('isStarted')->andReturn(false); |
35
|
|
|
$this->process->shouldReceive('isRunning')->andReturn(false); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testProperties() |
39
|
|
|
{ |
40
|
|
|
$pool = new Pool(); |
41
|
|
|
|
42
|
|
|
$this->assertEquals(-1, $pool->getMaxSimultaneous()); |
43
|
|
|
$this->assertSame($pool, $pool->setMaxSimultaneous(1)); |
44
|
|
|
$this->assertEquals(1, $pool->getMaxSimultaneous()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testSingleMaxAdding2Processes() |
48
|
|
|
{ |
49
|
|
|
$pool = new Pool(); |
50
|
|
|
$this->assertSame($pool, $pool->setMaxSimultaneous(1)); |
51
|
|
|
|
52
|
|
|
$this->assertEquals(1, $pool->getMaxSimultaneous()); |
53
|
|
|
|
54
|
|
|
$pool->add($this->process); |
55
|
|
|
|
56
|
|
|
$process = Mockery::mock(Process::class); |
57
|
|
|
$process->shouldReceive('stop'); |
58
|
|
|
$process->shouldReceive('isStarted')->andReturn(false); |
59
|
|
|
$process->shouldReceive('isRunning')->andReturn(false); |
60
|
|
|
|
61
|
|
|
$pool->add($process); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$this->process->shouldReceive('start'); |
64
|
|
|
|
65
|
|
|
$pool->start(); |
66
|
|
|
|
67
|
|
|
$this->assertCount(1, $pool->getRunning()); |
68
|
|
|
$this->assertCount(1, $pool->getWaiting()); |
69
|
|
|
|
70
|
|
|
$running = $pool->getRunning(); |
71
|
|
|
$run = reset($running); |
72
|
|
|
$this->assertInstanceOf(Run::class, $run); |
73
|
|
|
$this->assertSame($this->process, $run->getProcess(), 'first process added should be run first'); |
74
|
|
|
|
75
|
|
|
$waiting = $pool->getWaiting(); |
76
|
|
|
$run = reset($waiting); |
77
|
|
|
$this->assertInstanceOf(Run::class, $run); |
78
|
|
|
$this->assertSame($process, $run->getProcess(), 'second process added should be waiting'); |
79
|
|
|
|
80
|
|
|
$process->shouldReceive('start'); |
81
|
|
|
|
82
|
|
|
$this->assertTrue($pool->poll()); // check running state |
83
|
|
|
|
84
|
|
|
$this->assertCount(1, $pool->getRunning()); |
85
|
|
|
$this->assertCount(0, $pool->getWaiting()); |
86
|
|
|
|
87
|
|
|
$running = $pool->getRunning(); |
88
|
|
|
$run = reset($running); |
89
|
|
|
$this->assertInstanceOf(Run::class, $run); |
90
|
|
|
$this->assertSame($process, $run->getProcess(), 'second process added should now be running'); |
91
|
|
|
|
92
|
|
|
$this->assertFalse($pool->poll()); |
93
|
|
|
|
94
|
|
|
$this->assertCount(0, $pool->getRunning()); |
95
|
|
|
$this->assertCount(0, $pool->getWaiting()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testAddingTooManyProcessesPutsThemOnTheWaitingList() |
99
|
|
|
{ |
100
|
|
|
$pool = new Pool([], null, null, null, null, 1); |
101
|
|
|
$this->assertEquals(1, $pool->getMaxSimultaneous()); |
102
|
|
|
|
103
|
|
|
$pool->add($this->process); |
104
|
|
|
|
105
|
|
|
$process = Mockery::mock(Process::class); |
106
|
|
|
$process->shouldReceive('stop'); |
107
|
|
|
$process->shouldReceive('isStarted')->andReturn(false); |
108
|
|
|
$process->shouldReceive('isRunning')->andReturn(false); |
109
|
|
|
|
110
|
|
|
$this->process->shouldReceive('start'); |
111
|
|
|
|
112
|
|
|
$pool->start(); |
113
|
|
|
|
114
|
|
|
$this->assertCount(1, $pool->getRunning()); |
115
|
|
|
$this->assertCount(0, $pool->getWaiting()); |
116
|
|
|
|
117
|
|
|
$pool->add($process); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$this->assertCount(1, $pool->getRunning()); |
120
|
|
|
$this->assertCount(1, $pool->getWaiting()); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|