| 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\CacheCpu, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 CacheCpuTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function testInterface() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->assertInstanceOf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             Server::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             new CacheCpu( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 CacheCpu( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $server = $this->createMock(Server::class), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $clock = $this->createMock(Clock::class), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             new ElapsedPeriod(42) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ->expects($this->exactly(2)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->method('cpu') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->will( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 $this->onConsecutiveCalls( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     $cpu1 = new Cpu(new Percentage(33), new Percentage(33), new Percentage(34), new Cores(1)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     $cpu2 = new Cpu(new Percentage(33), new Percentage(33), new Percentage(34), new Cores(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $clock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ->expects($this->at(0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ->method('now') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ->willReturn( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 $first = $this->createMock(PointInTime::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $clock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->expects($this->at(1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ->method('now') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ->willReturn( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $second = $this->createMock(PointInTime::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $second | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ->method('elapsedSince') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ->with($first) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ->willReturn(new ElapsedPeriod(24)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $clock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ->expects($this->at(2)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ->method('now') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ->willReturn( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $third = $this->createMock(PointInTime::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $third | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ->method('elapsedSince') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ->with($first) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             ->willReturn(new ElapsedPeriod(50)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $clock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ->expects($this->exactly(3)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             ->method('now'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->assertSame($cpu1, $decorator->cpu()); //put in cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->assertSame($cpu1, $decorator->cpu()); //load cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->assertSame($cpu2, $decorator->cpu()); //stale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function testMemory() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $decorator = new CacheCpu( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $server = $this->createMock(Server::class), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $clock = $this->createMock(Clock::class), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             new ElapsedPeriod(42) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ->method('memory') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ->willReturn($expected = new Memory( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 new Bytes(42), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 new Bytes(42), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 new Bytes(42), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 new Bytes(42), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 new Bytes(42), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 new Bytes(42) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $clock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             ->expects($this->never()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             ->method('now'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->assertSame($expected, $decorator->memory()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function testProcesses() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $decorator = new CacheCpu( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $server = $this->createMock(Server::class), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $clock = $this->createMock(Clock::class), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             new ElapsedPeriod(42) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             ->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             ->method('processes') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             ->willReturn($expected = $this->createMock(Processes::class)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $clock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             ->expects($this->never()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             ->method('now'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $this->assertSame($expected, $decorator->processes()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |     public function testLoadAverage() | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |         $decorator = new CacheCpu( | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |             $server = $this->createMock(Server::class), | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |             $clock = $this->createMock(Clock::class), | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |             new ElapsedPeriod(42) | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         $server | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |             ->expects($this->once()) | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |             ->method('loadAverage') | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |             ->willReturn($expected = new LoadAverage(1, 5, 15)); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |         $clock | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |             ->expects($this->never()) | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |             ->method('now'); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |         $this->assertSame($expected, $decorator->loadAverage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     public function testDisk() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $decorator = new CacheCpu( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  |