forked from DAGWorks-Inc/burr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_end_to_end.py
371 lines (316 loc) · 12.7 KB
/
test_end_to_end.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""End-to-end tests -- these are more like integration tests,
but they're specifically meant to be a smoke-screen. If you ever
see failures in these tests, you should make a unit test, demonstrate the failure there,
then fix both in that test and the end-to-end test."""
import asyncio
import uuid
from concurrent.futures import ThreadPoolExecutor
from io import StringIO
from typing import Any, AsyncGenerator, Dict, Generator, Tuple
from unittest.mock import patch
from burr.core import (
Action,
ApplicationBuilder,
ApplicationContext,
GraphBuilder,
State,
action,
persistence,
)
from burr.core.action import Input, Result, expr
from burr.core.parallelism import MapStates, RunnableGraph, SubgraphType
from burr.lifecycle import base
def test_end_to_end_collatz_with_function_api():
"""End-to-end test for collatz conjecture. This is a fun (unproven) finite state machine."""
class CountHook(base.PostRunStepHook):
def __init__(self):
self.count = 0
def post_run_step(self, action: "Action", **future_kwargs: Any):
if action.name != "result":
self.count += 1
hook = CountHook()
@action(reads=["n"], writes=["n", "n_history"])
def even(state: State) -> Tuple[dict, State]:
result = {"n": state["n"] // 2}
return result, state.update(**result).append(n_history=result["n"])
@action(reads=["n"], writes=["n", "n_history"])
def odd(state: State) -> Tuple[dict, State]:
result = {"n": 3 * state["n"] + 1}
return result, state.update(**result).append(n_history=result["n"])
done = expr("n == 1")
is_even = expr("n % 2 == 0")
is_odd = expr("n % 2 != 0")
application = (
ApplicationBuilder()
.with_state(n_history=[])
.with_actions(
original=Input("n"),
even=even,
odd=odd,
result=Result("n_history"),
)
.with_transitions(
(["original", "even"], "result", done),
(["original", "even", "odd"], "even", is_even),
(["original", "even", "odd"], "odd", is_odd),
)
.with_entrypoint("original")
.with_hooks(hook)
.build()
)
run_action, result, state = application.run(halt_after=["result"], inputs={"n": 1000})
assert result["n_history"][-1] == 1
assert hook.count == 112
def test_end_to_end_parallel_collatz_many_unreliable_tasks(tmpdir):
"""Tests collatz conjecture search on multiple tasks.
Each of these persists its own capabilities and state.
This uses the in memory persister to store the state of each task.
Each task is unreliable -- it will break every few times. We will restart
to ensure it is eventually successful using a while loop.
This simulates running a complex workflow in parallel.
"""
MIN_NUMBER = 80
MAX_NUMBER = 100
FAILURE_ODDS = 0.05 # 1 in twenty chance of faiulre, will be hit but not all the time
seen = set()
class UnreliableFailureError(Exception):
pass
def _fail_at_random():
import random
if random.random() < FAILURE_ODDS:
raise UnreliableFailureError("Random failure")
# dummy as we want an initial action to decide between odd/even next
@action(reads=["n"], writes=["n", "original_n"])
def initial(state: State, __context: ApplicationContext) -> State:
# This assert ensures we only visit once per app, globally
# Thus if we're restarting this will break
assert __context.app_id not in seen, f"App id {__context.app_id} already seen"
seen.add(__context.app_id)
return state.update(original_n=state["n"])
@action(reads=["n"], writes=["n", "n_history"])
def even(state: State) -> State:
_fail_at_random()
result = {"n": state["n"] // 2}
return state.update(**result).append(n_history=result["n"])
@action(reads=["n"], writes=["n", "n_history"])
def odd(state: State) -> Tuple[dict, State]:
_fail_at_random()
result = {"n": 3 * state["n"] + 1}
return result, state.update(**result).append(n_history=result["n"])
collatz_graph = (
GraphBuilder()
.with_actions(
initial,
even,
odd,
result=Result("n_history"),
)
.with_transitions(
(["initial", "even"], "result", expr("n == 1")),
(["initial", "even", "odd"], "even", expr("n % 2 == 0")),
(["initial", "even", "odd"], "odd", expr("n % 2 != 0")),
)
.build()
)
@action(reads=[], writes=["ns"])
def map_step(state: State, min_number: int = MIN_NUMBER, max_number: int = MAX_NUMBER) -> State:
return state.update(ns=list(range(min_number, max_number)))
class ParallelCollatz(MapStates):
def states(
self, state: State, context: ApplicationContext, inputs: Dict[str, Any]
) -> Generator[State, None, None]:
for item in state["ns"]:
yield state.update(n=item)
def action(self, state: State, inputs: Dict[str, Any]) -> SubgraphType:
return RunnableGraph(
collatz_graph,
entrypoint="initial",
halt_after=["result"],
)
def reduce(self, state: State, results: Generator[State, None, None]) -> State:
new_state = state
count_mapping = {}
for result in results:
count_mapping[result["original_n"]] = len(result["n_history"])
return new_state.update(counts=count_mapping)
@property
def writes(self) -> list[str]:
return ["counts"]
@property
def reads(self) -> list[str]:
return ["ns"]
persister = persistence.InMemoryPersister()
app_id = f"collatz_test_{str(uuid.uuid4())}"
final_state = None
while final_state is None:
try:
containing_application = (
ApplicationBuilder()
.with_actions(
map_step,
parallel_collatz=ParallelCollatz(),
final=Result("counts"),
)
.with_transitions(
("map_step", "parallel_collatz"),
("parallel_collatz", "final"),
)
.with_state_persister(persister)
.with_identifiers(app_id=app_id)
.initialize_from(
persister,
resume_at_next_action=True,
default_state={},
default_entrypoint="map_step",
)
# Uncomment for debugging/visualizing
# .with_tracker("local", project="test_persister")
.with_parallel_executor(lambda: ThreadPoolExecutor(max_workers=10))
.build()
)
*_, final_state = containing_application.run(halt_after=["final"])
except UnreliableFailureError:
continue
# We want to ensure that initial is called once per app
assert len(seen) == len(range(MIN_NUMBER, MAX_NUMBER)), "Should have seen all numbers"
async def test_end_to_end_parallel_collatz_many_unreliable_tasks_async(tmpdir):
"""Tests collatz conjecture search on multiple tasks.
Each of these persists its own capabilities and state.
This uses the in memory persister to store the state of each task.
Each task is unreliable -- it will break every few times. We will restart
to ensure it is eventually successful using a while loop.
This simulates running a complex workflow in parallel.
"""
MIN_NUMBER = 80
MAX_NUMBER = 100
FAILURE_ODDS = 0.05 # 1 in twenty chance of faiulre, will be hit but not all the time
seen = set()
class UnreliableFailureError(Exception):
pass
def _fail_at_random():
import random
if random.random() < FAILURE_ODDS:
raise UnreliableFailureError("Random failure")
# dummy as we want an initial action to decide between odd/even next
@action(reads=["n"], writes=["n", "original_n"])
async def initial(state: State, __context: ApplicationContext) -> State:
# This assert ensures we only visit once per app, globally
# Thus if we're restarting this will break
await asyncio.sleep(0.001)
assert __context.app_id not in seen, f"App id {__context.app_id} already seen"
seen.add(__context.app_id)
return state.update(original_n=state["n"])
@action(reads=["n"], writes=["n", "n_history"])
async def even(state: State) -> State:
await asyncio.sleep(0.001)
_fail_at_random()
result = {"n": state["n"] // 2}
return state.update(**result).append(n_history=result["n"])
@action(reads=["n"], writes=["n", "n_history"])
async def odd(state: State) -> Tuple[dict, State]:
await asyncio.sleep(0.001)
_fail_at_random()
result = {"n": 3 * state["n"] + 1}
return result, state.update(**result).append(n_history=result["n"])
collatz_graph = (
GraphBuilder()
.with_actions(
initial,
even,
odd,
result=Result("n_history"),
)
.with_transitions(
(["initial", "even"], "result", expr("n == 1")),
(["initial", "even", "odd"], "even", expr("n % 2 == 0")),
(["initial", "even", "odd"], "odd", expr("n % 2 != 0")),
)
.build()
)
@action(reads=[], writes=["ns"])
def map_step(state: State, min_number: int = MIN_NUMBER, max_number: int = MAX_NUMBER) -> State:
return state.update(ns=list(range(min_number, max_number)))
class ParallelCollatz(MapStates):
async def states(
self, state: State, context: ApplicationContext, inputs: Dict[str, Any]
) -> AsyncGenerator[State, None]:
for item in state["ns"]:
yield state.update(n=item)
def action(self, state: State, inputs: Dict[str, Any]) -> SubgraphType:
return RunnableGraph(
collatz_graph,
entrypoint="initial",
halt_after=["result"],
)
async def reduce(self, state: State, results: AsyncGenerator[State, None]) -> State:
new_state = state
count_mapping = {}
async for result in results:
count_mapping[result["original_n"]] = len(result["n_history"])
return new_state.update(counts=count_mapping)
@property
def writes(self) -> list[str]:
return ["counts"]
@property
def reads(self) -> list[str]:
return ["ns"]
def is_async(self) -> bool:
return True
persister = persistence.InMemoryPersister()
app_id = f"collatz_test_{str(uuid.uuid4())}"
final_state = None
while final_state is None:
try:
containing_application = (
ApplicationBuilder()
.with_actions(
map_step,
parallel_collatz=ParallelCollatz(),
final=Result("counts"),
)
.with_transitions(
("map_step", "parallel_collatz"),
("parallel_collatz", "final"),
)
.with_state_persister(persister)
.with_identifiers(app_id=app_id)
.initialize_from(
persister,
resume_at_next_action=True,
default_state={},
default_entrypoint="map_step",
)
.build()
)
*_, final_state = await containing_application.arun(halt_after=["final"])
except UnreliableFailureError:
continue
# We want to ensure that initial is called once per app
assert len(seen) == len(range(MIN_NUMBER, MAX_NUMBER)), "Should have seen all numbers"
def test_echo_bot():
@action(reads=["prompt"], writes=["response"])
def echo(state: State) -> Tuple[dict, State]:
return {"response": state["prompt"]}, state.update(response=state["prompt"])
application = (
ApplicationBuilder()
.with_actions(
prompt=Input("prompt"),
response=echo,
)
.with_transitions(("prompt", "response"))
.with_entrypoint("prompt")
.build()
)
prompt = "hello"
with patch("sys.stdin", new=StringIO(prompt)):
run_action, result, state = application.run(
halt_after=["response"], inputs={"prompt": input()}
)
application.visualize(
output_file_path="digraph",
include_conditions=True,
view=False,
include_state=True,
format="png",
)
assert result["response"] == prompt