PoolMaxSimultaneousTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 64
c 1
b 0
f 1
dl 0
loc 105
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testProperties() 0 7 1
A testAddingTooManyProcessesPutsThemOnTheWaitingList() 0 23 1
A setUp() 0 8 1
A testSingleMaxAdding2Processes() 0 56 1
1
<?php
2
3
/**
4
 * This file is part of graze/parallel-process.
5
 *
6
 * Copyright © 2018 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\PriorityPool;
18
use Graze\ParallelProcess\ProcessRun;
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 PriorityPool();
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 PriorityPool();
50
        $this->assertSame($pool, $pool->setMaxSimultaneous(1));
51
52
        $this->assertEquals(1, $pool->getMaxSimultaneous());
53
54
        $process1 = Mockery::mock(Process::class);
55
        $process1->shouldReceive('stop');
56
        $process1->shouldReceive('isStarted')->andReturn(false, false, false, true); //add, add2, start, check
57
        $process1->shouldReceive('isRunning')->andReturn(false); //add, add2, check
58
59
        $pool->add($process1);
0 ignored issues
show
Bug introduced by
$process1 of type Mockery\MockInterface is incompatible with the type Graze\ParallelProcess\Po...mponent\Process\Process expected by parameter $item of Graze\ParallelProcess\PriorityPool::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        $pool->add(/** @scrutinizer ignore-type */ $process1);
Loading history...
60
61
        $process2 = Mockery::mock(Process::class);
62
        $process2->shouldReceive('stop');
63
        $process2->shouldReceive('isStarted')->andReturn(false, false, false, true); //add, add2, start, check
64
        $process2->shouldReceive('isRunning')->andReturn(false); //add, add2, check
65
66
        $pool->add($process2);
67
68
        $process1->shouldReceive('start')->once();
69
        $pool->start();
70
71
        $this->assertCount(1, $pool->getRunning());
72
        $this->assertCount(1, $pool->getWaiting());
73
74
        $running = $pool->getRunning();
75
        $run = reset($running);
76
        $this->assertInstanceOf(ProcessRun::class, $run);
77
        $this->assertSame($process1, $run->getProcess(), 'first process added should be run first');
78
79
        $waiting = $pool->getWaiting();
80
        $run = reset($waiting);
81
        $this->assertInstanceOf(ProcessRun::class, $run);
82
        $this->assertSame($process2, $run->getProcess(), 'second process added should be waiting');
83
84
        $process1->shouldReceive('isSuccessful')->andReturn(true);
85
        $process2->shouldReceive('start');
86
87
        $this->assertTrue($pool->poll()); // check running state
88
89
        $this->assertCount(1, $pool->getRunning());
90
        $this->assertCount(0, $pool->getWaiting());
91
92
        $running = $pool->getRunning();
93
        $run = reset($running);
94
        $this->assertInstanceOf(ProcessRun::class, $run);
95
        $this->assertSame($process2, $run->getProcess(), 'second process added should now be running');
96
97
        $process2->shouldReceive('isSuccessful')->andReturn(true);
98
        $process2->shouldReceive('isSuccessful')->andReturn(true);
99
        $this->assertFalse($pool->poll());
100
101
        $this->assertCount(0, $pool->getRunning());
102
        $this->assertCount(0, $pool->getWaiting());
103
    }
104
105
    public function testAddingTooManyProcessesPutsThemOnTheWaitingList()
106
    {
107
        $pool = new PriorityPool([], 1);
108
        $this->assertEquals(1, $pool->getMaxSimultaneous());
109
110
        $pool->add($this->process);
111
112
        $process = Mockery::mock(Process::class);
113
        $process->shouldReceive('stop');
114
        $process->shouldReceive('isStarted')->andReturn(false);
115
        $process->shouldReceive('isRunning')->andReturn(false);
116
117
        $this->process->shouldReceive('start');
118
119
        $pool->start();
120
121
        $this->assertCount(1, $pool->getRunning());
122
        $this->assertCount(0, $pool->getWaiting());
123
124
        $pool->add($process);
0 ignored issues
show
Bug introduced by
$process of type Mockery\MockInterface is incompatible with the type Graze\ParallelProcess\Po...mponent\Process\Process expected by parameter $item of Graze\ParallelProcess\PriorityPool::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
        $pool->add(/** @scrutinizer ignore-type */ $process);
Loading history...
125
126
        $this->assertCount(1, $pool->getRunning());
127
        $this->assertCount(1, $pool->getWaiting());
128
    }
129
}
130