1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Server\Status\Servers\Decorator; |
5
|
|
|
|
6
|
|
|
use Innmind\Server\Status\{ |
7
|
|
|
Servers\Decorator\CacheLoadAverage, |
8
|
|
|
Server, |
9
|
|
|
Server\Cpu, |
10
|
|
|
Server\Cpu\Percentage, |
11
|
|
|
Server\Cpu\Cores, |
12
|
|
|
Server\Memory, |
13
|
|
|
Server\Memory\Bytes, |
14
|
|
|
Server\LoadAverage, |
15
|
|
|
Server\Processes, |
16
|
|
|
Server\Disk, |
17
|
|
|
}; |
18
|
|
|
use Innmind\TimeContinuum\{ |
19
|
|
|
Clock, |
20
|
|
|
Earth\ElapsedPeriod, |
21
|
|
|
PointInTime, |
22
|
|
|
}; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
|
25
|
|
|
class CacheLoadAverageTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public function testInterface() |
28
|
|
|
{ |
29
|
|
|
$this->assertInstanceOf( |
30
|
|
|
Server::class, |
31
|
|
|
new CacheLoadAverage( |
32
|
|
|
$this->createMock(Server::class), |
33
|
|
|
$this->createMock(Clock::class), |
34
|
|
|
new ElapsedPeriod(0) |
35
|
|
|
) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testCpu() |
40
|
|
|
{ |
41
|
|
|
$decorator = new CacheLoadAverage( |
42
|
|
|
$server = $this->createMock(Server::class), |
43
|
|
|
$clock = $this->createMock(Clock::class), |
44
|
|
|
new ElapsedPeriod(42) |
45
|
|
|
); |
46
|
|
|
$server |
47
|
|
|
->expects($this->once()) |
48
|
|
|
->method('cpu') |
49
|
|
|
->willReturn($expected = new Cpu(new Percentage(33), new Percentage(33), new Percentage(34), new Cores(1))); |
50
|
|
|
$clock |
51
|
|
|
->expects($this->never()) |
52
|
|
|
->method('now'); |
53
|
|
|
|
54
|
|
|
$this->assertSame($expected, $decorator->cpu()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testMemory() |
58
|
|
|
{ |
59
|
|
|
$decorator = new CacheLoadAverage( |
60
|
|
|
$server = $this->createMock(Server::class), |
61
|
|
|
$clock = $this->createMock(Clock::class), |
62
|
|
|
new ElapsedPeriod(42) |
63
|
|
|
); |
64
|
|
|
$server |
65
|
|
|
->expects($this->once()) |
66
|
|
|
->method('memory') |
67
|
|
|
->willReturn($expected = new Memory( |
68
|
|
|
new Bytes(42), |
69
|
|
|
new Bytes(42), |
70
|
|
|
new Bytes(42), |
71
|
|
|
new Bytes(42), |
72
|
|
|
new Bytes(42), |
73
|
|
|
new Bytes(42) |
74
|
|
|
)); |
75
|
|
|
$clock |
76
|
|
|
->expects($this->never()) |
77
|
|
|
->method('now'); |
78
|
|
|
|
79
|
|
|
$this->assertSame($expected, $decorator->memory()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testProcesses() |
83
|
|
|
{ |
84
|
|
|
$decorator = new CacheLoadAverage( |
85
|
|
|
$server = $this->createMock(Server::class), |
86
|
|
|
$clock = $this->createMock(Clock::class), |
87
|
|
|
new ElapsedPeriod(42) |
88
|
|
|
); |
89
|
|
|
$server |
90
|
|
|
->expects($this->once()) |
91
|
|
|
->method('processes') |
92
|
|
|
->willReturn($expected = $this->createMock(Processes::class)); |
93
|
|
|
$clock |
94
|
|
|
->expects($this->never()) |
95
|
|
|
->method('now'); |
96
|
|
|
|
97
|
|
|
$this->assertSame($expected, $decorator->processes()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testLoadAverage() |
101
|
|
|
{ |
102
|
|
|
$decorator = new CacheLoadAverage( |
103
|
|
|
$server = $this->createMock(Server::class), |
104
|
|
|
$clock = $this->createMock(Clock::class), |
105
|
|
|
new ElapsedPeriod(42) |
106
|
|
|
); |
107
|
|
|
$server |
108
|
|
|
->expects($this->exactly(2)) |
109
|
|
|
->method('loadAverage') |
110
|
|
|
->will( |
111
|
|
|
$this->onConsecutiveCalls( |
112
|
|
|
$load1 = new LoadAverage(1, 5, 15), |
113
|
|
|
$load2 = new LoadAverage(1, 5, 15) |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
$clock |
117
|
|
|
->expects($this->at(0)) |
118
|
|
|
->method('now') |
119
|
|
|
->willReturn( |
120
|
|
|
$first = $this->createMock(PointInTime::class) |
121
|
|
|
); |
122
|
|
|
$clock |
123
|
|
|
->expects($this->at(1)) |
124
|
|
|
->method('now') |
125
|
|
|
->willReturn( |
126
|
|
|
$second = $this->createMock(PointInTime::class) |
127
|
|
|
); |
128
|
|
|
$second |
129
|
|
|
->expects($this->once()) |
130
|
|
|
->method('elapsedSince') |
131
|
|
|
->with($first) |
132
|
|
|
->willReturn(new ElapsedPeriod(24)); |
133
|
|
|
$clock |
134
|
|
|
->expects($this->at(2)) |
135
|
|
|
->method('now') |
136
|
|
|
->willReturn( |
137
|
|
|
$third = $this->createMock(PointInTime::class) |
138
|
|
|
); |
139
|
|
|
$third |
140
|
|
|
->expects($this->once()) |
141
|
|
|
->method('elapsedSince') |
142
|
|
|
->with($first) |
143
|
|
|
->willReturn(new ElapsedPeriod(50)); |
144
|
|
|
$clock |
145
|
|
|
->expects($this->exactly(3)) |
146
|
|
|
->method('now'); |
147
|
|
|
|
148
|
|
|
$this->assertSame($load1, $decorator->loadAverage()); //put in cache |
149
|
|
|
$this->assertSame($load1, $decorator->loadAverage()); //load cache |
150
|
|
|
$this->assertSame($load2, $decorator->loadAverage()); //stale |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testDisk() |
154
|
|
|
{ |
155
|
|
|
$decorator = new CacheLoadAverage( |
156
|
|
|
$server = $this->createMock(Server::class), |
157
|
|
|
$clock = $this->createMock(Clock::class), |
158
|
|
|
new ElapsedPeriod(42) |
159
|
|
|
); |
160
|
|
|
$server |
161
|
|
|
->expects($this->once()) |
162
|
|
|
->method('disk') |
163
|
|
|
->willReturn($expected = $this->createMock(Disk::class)); |
164
|
|
|
$clock |
165
|
|
|
->expects($this->never()) |
166
|
|
|
->method('now'); |
167
|
|
|
|
168
|
|
|
$this->assertSame($expected, $decorator->disk()); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|