Repository navigation
Expand file tree
/
Copy pathtest_retry_timeout.py
More file actions
206 lines (175 loc) · 8.4 KB
/
Copy pathtest_retry_timeout.py
File metadata and controls
206 lines (175 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# pyright: reportPrivateUsage=false
import logging
from collections.abc import Generator
from datetime import UTC, datetime, timedelta, timezone, tzinfo
from typing import Any
import pytest
from durabletask import task, worker
from durabletask.internal import helpers
from durabletask.internal import orchestrator_service_pb2 as pb
from durabletask.serialization import JsonDataConverter
def _set_wall_clock(monkeypatch: pytest.MonkeyPatch, instant: datetime) -> None:
class FrozenDateTime(datetime):
@classmethod
def now(cls, tz: tzinfo | None = None) -> datetime:
return instant.astimezone(tz) if tz is not None else instant.replace(tzinfo=None)
monkeypatch.setattr(task, "datetime", FrozenDateTime)
@pytest.mark.parametrize("start_zone", [None, UTC, timezone(timedelta(hours=2))])
@pytest.mark.parametrize("current_zone", [None, UTC, timezone(timedelta(hours=-3))])
@pytest.mark.parametrize("elapsed", [9, 10, 11])
def test_retry_timeout_uses_supplied_time(
start_zone: tzinfo | None, current_zone: tzinfo | None, elapsed: int,
) -> None:
start = datetime(2020, 1, 1, tzinfo=UTC)
current = start + timedelta(seconds=elapsed)
start = start.astimezone(start_zone) if start_zone is not None else start.replace(tzinfo=None)
current = current.astimezone(current_zone) if current_zone is not None else current.replace(tzinfo=None)
retry = task.RetryableTask[Any](
task.RetryPolicy(
first_retry_interval=timedelta(seconds=1),
max_number_of_attempts=3,
retry_timeout=timedelta(seconds=10),
),
pb.OrchestratorAction(),
start,
is_sub_orch=False,
)
assert retry.compute_next_delay(current) == (timedelta(seconds=1) if elapsed < 10 else None)
@pytest.mark.parametrize("undefined_start", [False, True])
@pytest.mark.parametrize("undefined_current", [False, True])
@pytest.mark.parametrize("elapsed", [1, 10, 11])
def test_retry_timeout_treats_undefined_offsets_as_naive(
undefined_start: bool, undefined_current: bool, elapsed: int,
) -> None:
class UndefinedOffset(tzinfo):
def utcoffset(self, dt: datetime | None) -> None:
return None
def dst(self, dt: datetime | None) -> None:
return None
class NaiveDateTime(datetime):
def astimezone(self, tz: tzinfo | None = None) -> datetime:
raise AssertionError("Naive timestamps must not be converted using the host timezone")
start = NaiveDateTime(2020, 1, 1, tzinfo=UndefinedOffset() if undefined_start else None)
current = NaiveDateTime(2020, 1, 1, 0, 0, elapsed, tzinfo=UndefinedOffset() if undefined_current else None)
retry = task.RetryableTask[Any](
task.RetryPolicy(
first_retry_interval=timedelta(seconds=1),
max_number_of_attempts=3,
retry_timeout=timedelta(seconds=10),
),
pb.OrchestratorAction(),
start,
is_sub_orch=False,
)
assert retry.compute_next_delay(current) == (timedelta(seconds=1) if elapsed < 10 else None)
@pytest.mark.parametrize("timeout", [None, timedelta(seconds=10)])
@pytest.mark.parametrize("aware_start", [False, True])
@pytest.mark.parametrize("elapsed", [1, 10])
def test_retry_timeout_default_clock(
monkeypatch: pytest.MonkeyPatch, timeout: timedelta | None, aware_start: bool, elapsed: int,
) -> None:
start = datetime(2020, 1, 1, tzinfo=UTC)
_set_wall_clock(monkeypatch, start + timedelta(seconds=elapsed))
retry = task.RetryableTask[Any](
task.RetryPolicy(
first_retry_interval=timedelta(seconds=1),
max_number_of_attempts=3,
retry_timeout=timeout,
),
pb.OrchestratorAction(),
start if aware_start else start.replace(tzinfo=None),
is_sub_orch=False,
)
expected = None if timeout is not None and elapsed >= 10 else timedelta(seconds=1)
assert retry.compute_next_delay() == expected
assert retry.compute_next_delay(start + timedelta(seconds=elapsed)) == expected
@pytest.mark.parametrize("timeout", [timedelta(0), timedelta.max])
def test_retry_timeout_extreme_durations(timeout: timedelta) -> None:
start = datetime(2020, 1, 1)
retry = task.RetryableTask[Any](
task.RetryPolicy(
first_retry_interval=timedelta(seconds=1),
max_number_of_attempts=3,
retry_timeout=timeout,
),
pb.OrchestratorAction(),
start,
is_sub_orch=False,
)
assert retry.compute_next_delay(start) == (None if timeout == timedelta(0) else timedelta(seconds=1))
@pytest.mark.parametrize("is_sub_orch", [False, True], ids=["activity", "sub-orchestration"])
@pytest.mark.parametrize("elapsed", [1, 9, 10, 11])
@pytest.mark.parametrize("wall_clock_year", [2019, 2030])
def test_retry_timeout_replays_history(
monkeypatch: pytest.MonkeyPatch, is_sub_orch: bool, elapsed: int, wall_clock_year: int,
) -> None:
_set_wall_clock(monkeypatch, datetime(wall_clock_year, 1, 1, tzinfo=UTC))
start = datetime(2020, 1, 1)
failed_at = start + timedelta(seconds=elapsed)
def orchestrator(ctx: task.OrchestrationContext, _: Any) -> Generator[task.Task[Any], Any, Any]:
retry_policy = task.RetryPolicy(
first_retry_interval=timedelta(seconds=1),
max_number_of_attempts=3,
retry_timeout=timedelta(seconds=10),
)
if is_sub_orch:
return (yield ctx.call_sub_orchestrator("child", retry_policy=retry_policy))
return (yield ctx.call_activity("child", retry_policy=retry_policy))
registry = worker._Registry()
name = registry.add_orchestrator(orchestrator)
history = [
helpers.new_orchestrator_started_event(start),
helpers.new_execution_started_event(name, "instance", encoded_input=None),
helpers.new_sub_orchestration_created_event(1, "child", "child-instance")
if is_sub_orch else helpers.new_task_scheduled_event(1, "child"),
]
failure_events = [
helpers.new_orchestrator_started_event(failed_at),
helpers.new_sub_orchestration_failed_event(1, ValueError("transient"))
if is_sub_orch else helpers.new_task_failed_event(1, ValueError("transient")),
]
def execute(old: list[pb.HistoryEvent], new: list[pb.HistoryEvent]) -> worker.ExecutionResults:
executor = worker._OrchestrationExecutor(registry, logging.getLogger(__name__), JsonDataConverter())
return executor.execute("instance", old, new)
result = execute(history, failure_events)
replayed = execute(history + failure_events, [helpers.new_orchestrator_started_event(failed_at)])
assert len(result.actions) == 1
assert len(replayed.actions) == 1
if elapsed >= 10:
for action in (result.actions[0], replayed.actions[0]):
assert action.HasField("completeOrchestration")
completion = action.completeOrchestration
assert completion.orchestrationStatus == pb.ORCHESTRATION_STATUS_FAILED
assert completion.failureDetails.innerFailure.errorMessage == "transient"
return
assert replayed.actions == result.actions
timer = result.actions[0]
assert timer.HasField("createTimer")
fire_at = failed_at + timedelta(seconds=1)
assert timer.createTimer.fireAt.ToDatetime() == fire_at
history += failure_events + [helpers.new_timer_created_event(timer.id, fire_at)]
timer_events = [
helpers.new_orchestrator_started_event(fire_at),
helpers.new_timer_fired_event(timer.id, fire_at),
]
result = execute(history, timer_events)
retries = [
action for action in result.actions
if action.HasField("createSubOrchestration" if is_sub_orch else "scheduleTask")
]
assert len(retries) == 1
assert retries[0].id == 1
history += timer_events + [
helpers.new_sub_orchestration_created_event(1, "child", "child-instance")
if is_sub_orch else helpers.new_task_scheduled_event(1, "child"),
]
result = execute(history, [
helpers.new_orchestrator_started_event(fire_at),
helpers.new_sub_orchestration_completed_event(1, '"success"')
if is_sub_orch else helpers.new_task_completed_event(1, '"success"'),
])
assert len(result.actions) == 1
assert result.actions[0].completeOrchestration.orchestrationStatus == pb.ORCHESTRATION_STATUS_COMPLETED
assert result.actions[0].completeOrchestration.result.value == '"success"'