|
11 | 11 | use OC\Files\Cache\Storage as StorageCache; |
12 | 12 | use OC\Files\Storage\Temporary; |
13 | 13 | use OC\Files\Storage\Wrapper\Availability; |
| 14 | +use OCP\Files\StorageAuthException; |
14 | 15 | use OCP\Files\StorageNotAvailableException; |
| 16 | +use OCP\IConfig; |
| 17 | +use PHPUnit\Framework\Attributes\DataProvider; |
15 | 18 |
|
16 | 19 | class AvailabilityTest extends \Test\TestCase { |
17 | 20 | /** @var \PHPUnit\Framework\MockObject\MockObject|StorageCache */ |
@@ -157,6 +160,71 @@ public function testAvailableThrow(): void { |
157 | 160 | $this->wrapper->mkdir('foobar'); |
158 | 161 | } |
159 | 162 |
|
| 163 | + public static function dataUnavailableDuration(): array { |
| 164 | + return [ |
| 165 | + 'not available' => [new StorageNotAvailableException(), 1800, Availability::RECHECK_TTL_SEC], |
| 166 | + 'auth, default delay' => [new StorageAuthException(), 1800, 1800], |
| 167 | + 'auth, custom delay' => [new StorageAuthException(), 3600, 3600], |
| 168 | + 'auth, delay below TTL' => [new StorageAuthException(), 60, Availability::RECHECK_TTL_SEC], |
| 169 | + ]; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * A failing call keeps the storage unavailable for exactly the expected duration |
| 174 | + */ |
| 175 | + #[DataProvider('dataUnavailableDuration')] |
| 176 | + public function testUnavailableDuration(StorageNotAvailableException $exception, int $authDelay, int $expectedDuration): void { |
| 177 | + $config = $this->createMock(IConfig::class); |
| 178 | + $config->method('getSystemValueInt') |
| 179 | + ->with('external_storage.auth_availability_delay', 1800) |
| 180 | + ->willReturn($authDelay); |
| 181 | + $wrapper = new Availability(['storage' => $this->storage, 'config' => $config]); |
| 182 | + |
| 183 | + $this->storage->method('getAvailability') |
| 184 | + ->willReturn(['available' => true, 'last_checked' => 0]); |
| 185 | + $this->storage->method('mkdir') |
| 186 | + ->willThrowException($exception); |
| 187 | + |
| 188 | + $additionalDelay = null; |
| 189 | + $this->storageCache->expects($this->once()) |
| 190 | + ->method('setAvailability') |
| 191 | + ->willReturnCallback(function (bool $isAvailable, int $passedAdditionalDelay) use (&$additionalDelay): void { |
| 192 | + $this->assertFalse($isAvailable); |
| 193 | + $additionalDelay = $passedAdditionalDelay; |
| 194 | + }); |
| 195 | + |
| 196 | + $failedAt = time(); |
| 197 | + try { |
| 198 | + $wrapper->mkdir('foobar'); |
| 199 | + $this->fail('Expected exception was not thrown'); |
| 200 | + } catch (StorageNotAvailableException $e) { |
| 201 | + $this->assertSame($exception, $e); |
| 202 | + } |
| 203 | + |
| 204 | + // Storage\Cache::setAvailability() stores time() + $additionalDelay as last_checked |
| 205 | + $lastChecked = $failedAt + $additionalDelay; |
| 206 | + $margin = 5; |
| 207 | + $this->assertFalse( |
| 208 | + $this->shouldRecheckAfter($lastChecked, $failedAt, $expectedDuration - $margin), |
| 209 | + 'storage must stay unavailable until the expected duration has passed' |
| 210 | + ); |
| 211 | + $this->assertTrue( |
| 212 | + $this->shouldRecheckAfter($lastChecked, $failedAt, $expectedDuration + $margin), |
| 213 | + 'storage must be rechecked once the expected duration has passed' |
| 214 | + ); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Evaluates shouldRecheck() as if $elapsed seconds had passed since $failedAt |
| 219 | + */ |
| 220 | + private function shouldRecheckAfter(int $lastChecked, int $failedAt, int $elapsed): bool { |
| 221 | + $alreadyElapsed = time() - $failedAt; |
| 222 | + return Availability::shouldRecheck([ |
| 223 | + 'available' => false, |
| 224 | + 'last_checked' => $lastChecked - $elapsed + $alreadyElapsed, |
| 225 | + ]); |
| 226 | + } |
| 227 | + |
160 | 228 | public function testUnavailableMultiple(): void { |
161 | 229 | $this->storage->expects($this->once()) |
162 | 230 | ->method('getAvailability') |
|
0 commit comments