| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\PoolCounter\PoolCounter; |
| 25 |
use MediaWiki\PoolCounter\PoolCounterWork; |
| 26 |
use MediaWiki\Status\Status; |
| 27 |
use PHPUnit\Framework\MockObject\MockObject; |
| 28 |
use PHPUnit\Framework\MockObject\Stub\Stub; |
| 29 |
use Psr\Log\LoggerInterface; |
| 30 |
use Wikimedia\TestingAccessWrapper; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class PoolCounterWorkTest extends MediaWikiIntegrationTestCase { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private function configureMock( $obj, $configMethods ) { |
| 44 |
$obj->expects( $this->never() ) |
| 45 |
->method( $this->anythingBut( ...array_keys( $configMethods ) ) ); |
| 46 |
foreach ( $configMethods as $method => $value ) { |
| 47 |
$obj->expects( $this->once() ) |
| 48 |
->method( $method ) |
| 49 |
->will( $value ); |
| 50 |
} |
| 51 |
return $obj; |
| 52 |
} |
| 53 |
|
| 54 |
private function configureFixture( |
| 55 |
array $workerMethods, |
| 56 |
array $poolCounterMethods, |
| 57 |
array $loggerMethods |
| 58 |
) { |
| 59 |
$logger = $this->createMock( LoggerInterface::class ); |
| 60 |
$this->configureMock( $logger, $loggerMethods ); |
| 61 |
|
| 62 |
$poolCounter = $this->getMockBuilder( PoolCounter::class ) |
| 63 |
->disableOriginalConstructor() |
| 64 |
->getMockForAbstractClass(); |
| 65 |
$poolCounter->setLogger( $logger ); |
| 66 |
|
| 67 |
$this->configureMock( $poolCounter, $poolCounterMethods ); |
| 68 |
|
| 69 |
$worker = $this->getMockBuilder( PoolCounterWork::class ) |
| 70 |
->setConstructorArgs( [ 'PoolType', 'PoolKey', $poolCounter ] ) |
| 71 |
->onlyMethods( array_keys( $workerMethods ) ) |
| 72 |
->getMockForAbstractClass(); |
| 73 |
|
| 74 |
$this->configureMock( $worker, $workerMethods ); |
| 75 |
|
| 76 |
return TestingAccessWrapper::newFromObject( $worker ); |
| 77 |
} |
| 78 |
|
| 79 |
public function provideExecuteFlow() { |
| 80 |
yield "Not Cacheable and not skipCache should call acquireForMe" => [ |
| 81 |
false, false, |
| 82 |
[ |
| 83 |
'acquireForMe' => $this->returnValue( Status::newGood( 'StupidAction' ) ) |
| 84 |
], [], [ |
| 85 |
'info' => $this->returnValue( null ) |
| 86 |
], |
| 87 |
false |
| 88 |
]; |
| 89 |
|
| 90 |
yield "Not Cacheable and skipCache should call acquireForMe" => [ |
| 91 |
false, true, |
| 92 |
[ |
| 93 |
'acquireForMe' => $this->returnValue( Status::newGood( 'StupidAction' ) ) |
| 94 |
], [], [ |
| 95 |
'info' => $this->returnValue( null ) |
| 96 |
], |
| 97 |
false |
| 98 |
]; |
| 99 |
|
| 100 |
yield "Cacheable and not skipCache should call acquireForAnyone" => [ |
| 101 |
true, false, |
| 102 |
[ |
| 103 |
'acquireForAnyone' => $this->returnValue( Status::newGood( 'StupidAction' ) ) |
| 104 |
], [], [ |
| 105 |
'info' => $this->returnValue( null ) |
| 106 |
], |
| 107 |
false |
| 108 |
]; |
| 109 |
|
| 110 |
yield "Cacheable and skipCache should call acquireForMe" => [ |
| 111 |
true, true, |
| 112 |
[ |
| 113 |
'acquireForMe' => $this->returnValue( Status::newGood( 'StupidAction' ) ) |
| 114 |
], [], [ |
| 115 |
'info' => $this->returnValue( null ) |
| 116 |
], |
| 117 |
false |
| 118 |
]; |
| 119 |
|
| 120 |
yield 'If Fatal error call doWork and log error' => [ |
| 121 |
false, false, |
| 122 |
[ |
| 123 |
'acquireForMe' => $this->returnValue( Status::newFatal( 'apierror' ) ) |
| 124 |
], [ |
| 125 |
'doWork' => $this->returnValue( "SomeResults" ) |
| 126 |
], [ |
| 127 |
'info' => $this->returnValue( null ) |
| 128 |
], |
| 129 |
"SomeResults" |
| 130 |
]; |
| 131 |
|
| 132 |
foreach ( [ |
| 133 |
'LOCK_HELD' => PoolCounter::LOCK_HELD, |
| 134 |
'LOCKED' => PoolCounter::LOCKED |
| 135 |
] as $name => $acquireResult ) { |
| 136 |
yield 'If ' . $name . ' call doWork and return' => [ |
| 137 |
false, false, |
| 138 |
[ |
| 139 |
'acquireForMe' => $this->returnValue( Status::newGood( $acquireResult ) ), |
| 140 |
'release' => $this->returnValue( null ) |
| 141 |
], [ |
| 142 |
'doWork' => $this->returnValue( "SomeResults" ) |
| 143 |
], [], |
| 144 |
"SomeResults" |
| 145 |
]; |
| 146 |
} |
| 147 |
|
| 148 |
yield 'If DONE and not chacheable call getCachedWork and return if OK' => [ |
| 149 |
false, false, |
| 150 |
[ |
| 151 |
'acquireForMe' => $this->returnValue( Status::newGood( PoolCounter::DONE ) ), |
| 152 |
], [ |
| 153 |
'getCachedWork' => $this->returnValue( "SomeResults" ) |
| 154 |
], [], |
| 155 |
"SomeResults" |
| 156 |
]; |
| 157 |
|
| 158 |
yield 'If DONE and chacheable call getCachedWork and return if OK' => [ |
| 159 |
true, false, |
| 160 |
[ |
| 161 |
'acquireForAnyone' => $this->returnValue( Status::newGood( PoolCounter::DONE ) ), |
| 162 |
], [ |
| 163 |
'getCachedWork' => $this->returnValue( "SomeResults" ) |
| 164 |
], [], |
| 165 |
"SomeResults" |
| 166 |
]; |
| 167 |
|
| 168 |
yield 'If DONE and chacheable call getCachedWork and repeat if not OK' => [ |
| 169 |
true, false, |
| 170 |
[ |
| 171 |
'acquireForAnyone' => $this->returnValue( Status::newGood( PoolCounter::DONE ) ), |
| 172 |
'acquireForMe' => $this->returnValue( Status::newGood( PoolCounter::LOCKED ) ), |
| 173 |
'release' => $this->returnValue( '' ) |
| 174 |
], [ |
| 175 |
'getCachedWork' => $this->returnValue( false ), |
| 176 |
'doWork' => $this->returnValue( 'SomeResults' ) |
| 177 |
], [], |
| 178 |
"SomeResults" |
| 179 |
]; |
| 180 |
|
| 181 |
foreach ( [ |
| 182 |
'QUEUE_FULL' => PoolCounter::QUEUE_FULL, |
| 183 |
'TIMEOUT' => PoolCounter::TIMEOUT |
| 184 |
] as $name => $acquireResult ) { |
| 185 |
yield 'If ' . $name . ' and not chacheable call fallback and return if OK' => [ |
| 186 |
false, false, |
| 187 |
[ |
| 188 |
'acquireForMe' => $this->returnValue( Status::newGood( $acquireResult ) ), |
| 189 |
], [ |
| 190 |
'fallback' => $this->returnValue( "SomeResults" ) |
| 191 |
], [], |
| 192 |
"SomeResults" |
| 193 |
]; |
| 194 |
|
| 195 |
yield 'If ' . $name . ' and not chacheable call fallback and log if not OK' => [ |
| 196 |
false, false, |
| 197 |
[ |
| 198 |
'acquireForMe' => $this->returnValue( Status::newGood( $acquireResult ) ), |
| 199 |
], [ |
| 200 |
], [ |
| 201 |
'info' => $this->returnValue( null ) |
| 202 |
], |
| 203 |
false |
| 204 |
]; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function testExecuteFlow( |
| 213 |
bool $cacheable, |
| 214 |
bool $skipCache, |
| 215 |
array $poolCounterMethods, |
| 216 |
array $workerMethods, |
| 217 |
array $loggerMethods, |
| 218 |
$expectedResult |
| 219 |
) { |
| 220 |
$worker = $this->configureFixture( |
| 221 |
$workerMethods, |
| 222 |
$poolCounterMethods, |
| 223 |
$loggerMethods |
| 224 |
); |
| 225 |
|
| 226 |
$worker->cacheable = $cacheable; |
| 227 |
$this->assertSame( $expectedResult, $worker->execute( $skipCache ) ); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function testDoWorkRaiseException() { |
| 234 |
$expectedException = new RuntimeException( __METHOD__ ); |
| 235 |
$worker = $this->configureFixture( |
| 236 |
[ 'doWork' => $this->throwException( $expectedException ) ], |
| 237 |
[ |
| 238 |
'acquireForMe' => $this->returnValue( Status::newGood( PoolCounter::LOCK_HELD ) ), |
| 239 |
'release' => $this->returnValue( '' ) |
| 240 |
], |
| 241 |
[] |
| 242 |
); |
| 243 |
|
| 244 |
$this->expectExceptionObject( $expectedException ); |
| 245 |
$worker->execute(); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function testDefaults() { |
| 255 |
$worker = $this->configureFixture( [], [], [] ); |
| 256 |
$this->assertFalse( $worker->cacheable ); |
| 257 |
$this->assertFalse( $worker->getCachedWork() ); |
| 258 |
$this->assertFalse( $worker->error( Status::newFatal( 'apierror' ) ) ); |
| 259 |
$this->assertFalse( $worker->fallback( false ) ); |
| 260 |
} |
| 261 |
} |