test: a short R2 failure while closing an upload: the server's steady retry and the browser's (contract contrato-r2-retry)
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 8m2s
Build and deploy / Secret scan and release gate (push) Successful in 8s
Build and deploy / Publish images (push) Successful in 1m12s
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 8m2s
Build and deploy / Secret scan and release gate (push) Successful in 8s
Build and deploy / Publish images (push) Successful in 1m12s
tests/test_storage_steady.py covers steady() and the storage's parts, size and complete (a 5xx retried after 1, 2, 3 s; a 4xx and other errors at once; NoSuchUpload on an object that exists is done). tests/upload_retry_test.mjs covers closing the upload in web/upload.js (again after 3 s and 6 s; only the last attempt reported; a 409 at once; an abort gives the upload back). Both fail with the retry turned off. steady now looks time.sleep up when it waits, so a patch of it reaches the storage methods. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
188
tests/test_storage_steady.py
Normal file
188
tests/test_storage_steady.py
Normal file
@@ -0,0 +1,188 @@
|
||||
"""Steady retries of R2/S3 5xx answers (contract contrato-r2-retry).
|
||||
|
||||
Run from the repository root: python3 -m unittest tests.test_storage_steady -v.
|
||||
"""
|
||||
import unittest
|
||||
from contextlib import contextmanager
|
||||
from unittest import mock
|
||||
|
||||
from botocore.exceptions import ClientError
|
||||
|
||||
from app import adapters
|
||||
from app.adapters import LocalS3Storage
|
||||
|
||||
|
||||
def stored_error(code='InternalError', status=500, operation='ListParts'):
|
||||
return ClientError({'Error': {'Code': code, 'Message': 'x'},
|
||||
'ResponseMetadata': {'HTTPStatusCode': status}}, operation)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def sleep_spy():
|
||||
"""The sleep the storage methods wait with (steady looks time.sleep up when it waits)."""
|
||||
with mock.patch('app.adapters.time.sleep') as sleep:
|
||||
yield sleep
|
||||
|
||||
|
||||
def storage_with(client):
|
||||
storage = LocalS3Storage.__new__(LocalS3Storage)
|
||||
storage.client = client
|
||||
storage.bucket = 'b'
|
||||
return storage
|
||||
|
||||
|
||||
class SteadyTest(unittest.TestCase):
|
||||
def test_success_returns_the_value_without_sleeping(self):
|
||||
sleep = mock.MagicMock()
|
||||
call = mock.MagicMock(return_value='done')
|
||||
self.assertEqual(adapters.steady(call, sleep=sleep), 'done')
|
||||
self.assertEqual(call.call_count, 1)
|
||||
sleep.assert_not_called()
|
||||
|
||||
def test_a_5xx_then_success_returns_the_value_after_sleeping_one_second(self):
|
||||
sleep = mock.MagicMock()
|
||||
call = mock.MagicMock(side_effect=[stored_error(), 'done'])
|
||||
self.assertEqual(adapters.steady(call, sleep=sleep), 'done')
|
||||
self.assertEqual(call.call_count, 2)
|
||||
self.assertEqual(sleep.call_args_list, [mock.call(1)])
|
||||
|
||||
def test_four_5xx_raise_the_fourth_error_after_waiting_one_two_three(self):
|
||||
sleep = mock.MagicMock()
|
||||
error = stored_error()
|
||||
call = mock.MagicMock(side_effect=error)
|
||||
with self.assertRaises(ClientError) as caught:
|
||||
adapters.steady(call, sleep=sleep)
|
||||
self.assertIs(caught.exception, error)
|
||||
self.assertEqual(call.call_count, 4)
|
||||
self.assertEqual(sleep.call_args_list, [mock.call(1), mock.call(2), mock.call(3)])
|
||||
|
||||
def test_a_4xx_is_raised_at_once_without_sleeping(self):
|
||||
for status in (404, 403, 409):
|
||||
with self.subTest(status=status):
|
||||
sleep = mock.MagicMock()
|
||||
error = stored_error(status=status)
|
||||
call = mock.MagicMock(side_effect=error)
|
||||
with self.assertRaises(ClientError) as caught:
|
||||
adapters.steady(call, sleep=sleep)
|
||||
self.assertIs(caught.exception, error)
|
||||
self.assertEqual(call.call_count, 1)
|
||||
sleep.assert_not_called()
|
||||
|
||||
def test_a_non_client_error_is_raised_at_once_without_sleeping(self):
|
||||
for error in (ValueError('bad'), TimeoutError('slow')):
|
||||
with self.subTest(error=type(error).__name__):
|
||||
sleep = mock.MagicMock()
|
||||
call = mock.MagicMock(side_effect=error)
|
||||
with self.assertRaises(type(error)) as caught:
|
||||
adapters.steady(call, sleep=sleep)
|
||||
self.assertIs(caught.exception, error)
|
||||
self.assertEqual(call.call_count, 1)
|
||||
sleep.assert_not_called()
|
||||
|
||||
def test_a_client_error_without_a_status_is_not_a_5xx(self):
|
||||
sleep = mock.MagicMock()
|
||||
error = ClientError({'Error': {'Code': 'InternalError', 'Message': 'x'}}, 'ListParts')
|
||||
call = mock.MagicMock(side_effect=error)
|
||||
with self.assertRaises(ClientError) as caught:
|
||||
adapters.steady(call, sleep=sleep)
|
||||
self.assertIs(caught.exception, error)
|
||||
self.assertEqual(call.call_count, 1)
|
||||
sleep.assert_not_called()
|
||||
|
||||
|
||||
class PartsTest(unittest.TestCase):
|
||||
def test_parts_retries_a_5xx_and_returns_every_part_of_every_page(self):
|
||||
pages = [{'Parts': [{'PartNumber': 1, 'ETag': '"a"'}]},
|
||||
{'Parts': [{'PartNumber': 2, 'ETag': '"b"'}, {'PartNumber': 3, 'ETag': '"c"'}]}]
|
||||
paginator = mock.MagicMock()
|
||||
paginator.paginate.side_effect = [stored_error(operation='ListParts'), pages]
|
||||
client = mock.MagicMock()
|
||||
client.get_paginator.return_value = paginator
|
||||
storage = storage_with(client)
|
||||
with sleep_spy() as sleep:
|
||||
parts = storage.parts('key', 'upload')
|
||||
self.assertEqual(parts, pages[0]['Parts'] + pages[1]['Parts'])
|
||||
self.assertEqual(paginator.paginate.call_count, 2)
|
||||
self.assertEqual(sleep.call_args_list, [mock.call(1)])
|
||||
|
||||
|
||||
class SizeTest(unittest.TestCase):
|
||||
def test_a_404_is_raised_at_once_without_sleeping(self):
|
||||
error = stored_error('NoSuchKey', 404, 'HeadObject')
|
||||
client = mock.MagicMock()
|
||||
client.head_object.side_effect = error
|
||||
storage = storage_with(client)
|
||||
with sleep_spy() as sleep:
|
||||
with self.assertRaises(ClientError) as caught:
|
||||
storage.size('key')
|
||||
self.assertIs(caught.exception, error)
|
||||
self.assertEqual(client.head_object.call_count, 1)
|
||||
sleep.assert_not_called()
|
||||
|
||||
def test_a_5xx_then_200_returns_the_content_length(self):
|
||||
client = mock.MagicMock()
|
||||
client.head_object.side_effect = [stored_error(operation='HeadObject'),
|
||||
{'ContentLength': 1234}]
|
||||
storage = storage_with(client)
|
||||
with sleep_spy() as sleep:
|
||||
self.assertEqual(storage.size('key'), 1234)
|
||||
self.assertEqual(client.head_object.call_count, 2)
|
||||
self.assertEqual(sleep.call_args_list, [mock.call(1)])
|
||||
|
||||
|
||||
class CompleteTest(unittest.TestCase):
|
||||
PARTS = [{'PartNumber': 1, 'ETag': '"a"'}]
|
||||
|
||||
def test_a_5xx_then_success_completes(self):
|
||||
client = mock.MagicMock()
|
||||
client.complete_multipart_upload.side_effect = [
|
||||
stored_error(operation='CompleteMultipartUpload'), {}]
|
||||
storage = storage_with(client)
|
||||
with sleep_spy() as sleep:
|
||||
self.assertIsNone(storage.complete('key', 'upload', self.PARTS))
|
||||
self.assertEqual(client.complete_multipart_upload.call_count, 2)
|
||||
self.assertEqual(sleep.call_args_list, [mock.call(1)])
|
||||
|
||||
def test_no_such_upload_with_the_object_present_returns_none(self):
|
||||
client = mock.MagicMock()
|
||||
client.complete_multipart_upload.side_effect = stored_error(
|
||||
'NoSuchUpload', 404, 'CompleteMultipartUpload')
|
||||
client.head_object.return_value = {'ContentLength': 10}
|
||||
storage = storage_with(client)
|
||||
with sleep_spy() as sleep:
|
||||
self.assertIsNone(storage.complete('key', 'upload', self.PARTS))
|
||||
self.assertEqual(client.complete_multipart_upload.call_count, 1)
|
||||
self.assertEqual(client.head_object.call_count, 1)
|
||||
sleep.assert_not_called()
|
||||
|
||||
def test_no_such_upload_with_a_missing_object_reraises_no_such_upload(self):
|
||||
missing = stored_error('NoSuchUpload', 404, 'CompleteMultipartUpload')
|
||||
client = mock.MagicMock()
|
||||
client.complete_multipart_upload.side_effect = missing
|
||||
client.head_object.side_effect = stored_error('NoSuchKey', 404, 'HeadObject')
|
||||
storage = storage_with(client)
|
||||
with sleep_spy() as sleep:
|
||||
with self.assertRaises(ClientError) as caught:
|
||||
storage.complete('key', 'upload', self.PARTS)
|
||||
self.assertIs(caught.exception, missing)
|
||||
self.assertEqual(caught.exception.response['Error']['Code'], 'NoSuchUpload')
|
||||
self.assertEqual(client.complete_multipart_upload.call_count, 1)
|
||||
self.assertEqual(client.head_object.call_count, 1)
|
||||
sleep.assert_not_called()
|
||||
|
||||
def test_any_other_4xx_is_raised(self):
|
||||
error = stored_error('AccessDenied', 403, 'CompleteMultipartUpload')
|
||||
client = mock.MagicMock()
|
||||
client.complete_multipart_upload.side_effect = error
|
||||
storage = storage_with(client)
|
||||
with sleep_spy() as sleep:
|
||||
with self.assertRaises(ClientError) as caught:
|
||||
storage.complete('key', 'upload', self.PARTS)
|
||||
self.assertIs(caught.exception, error)
|
||||
self.assertEqual(client.complete_multipart_upload.call_count, 1)
|
||||
client.head_object.assert_not_called()
|
||||
sleep.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user