Skip to content

Commit 6eb993a

Browse files
committed
test(files): cover how long a storage stays unavailable
Signed-off-by: silver <s.szmajduch@posteo.de> Assisted-by: ClaudeCode:claude-opus-5-5
1 parent 037fd19 commit 6eb993a

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

‎tests/lib/Files/Storage/Wrapper/AvailabilityTest.php‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
use OC\Files\Cache\Storage as StorageCache;
1212
use OC\Files\Storage\Temporary;
1313
use OC\Files\Storage\Wrapper\Availability;
14+
use OCP\Files\StorageAuthException;
1415
use OCP\Files\StorageNotAvailableException;
16+
use OCP\IConfig;
17+
use PHPUnit\Framework\Attributes\DataProvider;
1518

1619
class AvailabilityTest extends \Test\TestCase {
1720
/** @var \PHPUnit\Framework\MockObject\MockObject|StorageCache */
@@ -157,6 +160,71 @@ public function testAvailableThrow(): void {
157160
$this->wrapper->mkdir('foobar');
158161
}
159162

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+
160228
public function testUnavailableMultiple(): void {
161229
$this->storage->expects($this->once())
162230
->method('getAvailability')

0 commit comments

Comments
 (0)