|
| 1 | +"""Prove TUS retry attempt state resets after recovered progress.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +from io import BytesIO |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from tusclient import client as tus |
| 8 | + |
| 9 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 10 | +from api2devdock import ( |
| 11 | + TusConformancePlanServer, |
| 12 | + conformance_input_options, |
| 13 | + conformance_input_source_bytes, |
| 14 | + conformance_retry_decisions, |
| 15 | + fail, |
| 16 | + int_array_value, |
| 17 | + load_scenario, |
| 18 | + object_value, |
| 19 | + scenario_id, |
| 20 | + string_value, |
| 21 | + write_result, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +class RetryStateObserver: |
| 26 | + def __init__(self, retry_decisions, retry_delays): |
| 27 | + self.retry_decisions = retry_decisions |
| 28 | + self.retry_delays = retry_delays |
| 29 | + self.events = [] |
| 30 | + self.index = 0 |
| 31 | + |
| 32 | + def on_should_retry(self, error, retry_attempt): |
| 33 | + if self.index >= len(self.retry_decisions): |
| 34 | + fail( |
| 35 | + "retry state scenario observed unexpected retry attempt {}".format( |
| 36 | + retry_attempt, |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + expected = self.retry_decisions[self.index] |
| 41 | + if retry_attempt != expected["retryAttempt"]: |
| 42 | + fail( |
| 43 | + "retry state scenario expected retry attempt {}, got {}".format( |
| 44 | + expected["retryAttempt"], |
| 45 | + retry_attempt, |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + decision = expected["decision"] |
| 50 | + self.events.append( |
| 51 | + { |
| 52 | + "decision": decision, |
| 53 | + "kind": "should-retry", |
| 54 | + "retryAttempt": retry_attempt, |
| 55 | + } |
| 56 | + ) |
| 57 | + |
| 58 | + if decision: |
| 59 | + if retry_attempt >= len(self.retry_delays): |
| 60 | + fail( |
| 61 | + "retry state scenario has no delay for retry attempt {}".format( |
| 62 | + retry_attempt, |
| 63 | + ) |
| 64 | + ) |
| 65 | + self.events.append( |
| 66 | + { |
| 67 | + "delay": self.retry_delays[retry_attempt], |
| 68 | + "kind": "retry-schedule", |
| 69 | + } |
| 70 | + ) |
| 71 | + |
| 72 | + self.index += 1 |
| 73 | + return decision |
| 74 | + |
| 75 | + def assert_complete(self): |
| 76 | + if self.index == len(self.retry_decisions): |
| 77 | + return |
| 78 | + |
| 79 | + fail( |
| 80 | + "retry state scenario observed {} retry decision(s), expected {}".format( |
| 81 | + self.index, |
| 82 | + len(self.retry_decisions), |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def upload_with_retry_state_transitions(conformance_scenario): |
| 88 | + input_options = conformance_input_options(conformance_scenario) |
| 89 | + content = conformance_input_source_bytes(conformance_scenario) |
| 90 | + endpoint_url = string_value(input_options["endpointUrl"], "endpointUrl") |
| 91 | + metadata = object_value(input_options["metadata"], "metadata") |
| 92 | + retry_delays = int_array_value(input_options["retryDelays"], "retryDelays") |
| 93 | + retry_decisions = conformance_retry_decisions(conformance_scenario) |
| 94 | + completion = object_value( |
| 95 | + conformance_scenario["completion"], |
| 96 | + "conformanceScenario.completion", |
| 97 | + ) |
| 98 | + completion_kind = string_value( |
| 99 | + completion["kind"], |
| 100 | + "conformanceScenario.completion.kind", |
| 101 | + ) |
| 102 | + observer = RetryStateObserver(retry_decisions, retry_delays) |
| 103 | + |
| 104 | + with TusConformancePlanServer(conformance_scenario, endpoint_url) as conformance_server: |
| 105 | + client = tus.TusClient(conformance_server.endpoint_url()) |
| 106 | + uploader = client.uploader( |
| 107 | + file_stream=BytesIO(content), |
| 108 | + metadata=metadata, |
| 109 | + on_should_retry=observer.on_should_retry, |
| 110 | + retry_delays=retry_delays, |
| 111 | + ) |
| 112 | + uploader.upload() |
| 113 | + observer.assert_complete() |
| 114 | + |
| 115 | + if not uploader.url: |
| 116 | + fail("retry state scenario did not expose an upload URL") |
| 117 | + if uploader.offset != len(content): |
| 118 | + fail( |
| 119 | + "retry state scenario upload offset {}, expected {}".format( |
| 120 | + uploader.offset, |
| 121 | + len(content), |
| 122 | + ) |
| 123 | + ) |
| 124 | + |
| 125 | + conformance_server.assert_exhausted() |
| 126 | + result = conformance_server.result() |
| 127 | + result["completionKind"] = completion_kind |
| 128 | + result["errorCalled"] = False |
| 129 | + result["eventCount"] = len(observer.events) |
| 130 | + result["events"] = observer.events |
| 131 | + result["successCalled"] = True |
| 132 | + result["uploadUrl"] = conformance_server.canonical_url(uploader.url) |
| 133 | + return result |
| 134 | + |
| 135 | + |
| 136 | +def main(): |
| 137 | + scenario = load_scenario(Path(__file__).with_name("api2-scenario.json")) |
| 138 | + conformance_scenario = object_value( |
| 139 | + scenario["conformanceScenario"], |
| 140 | + "conformanceScenario", |
| 141 | + ) |
| 142 | + result = upload_with_retry_state_transitions(conformance_scenario) |
| 143 | + write_result(result) |
| 144 | + print( |
| 145 | + "Python TUS SDK devdock scenario {} proved retry state transitions".format( |
| 146 | + scenario_id(scenario), |
| 147 | + ) |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + main() |
0 commit comments