-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_resumable.py
More file actions
553 lines (433 loc) · 21.4 KB
/
test_resumable.py
File metadata and controls
553 lines (433 loc) · 21.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
"""Tests for UiPathResumableRuntime with multiple triggers."""
from __future__ import annotations
from typing import Any, AsyncGenerator, cast
from unittest.mock import AsyncMock, Mock
import pytest
from uipath.core.errors import ErrorCategory, UiPathPendingTriggerError
from uipath.runtime import (
UiPathExecuteOptions,
UiPathResumeTrigger,
UiPathResumeTriggerType,
UiPathRuntimeResult,
UiPathRuntimeStatus,
UiPathStreamOptions,
)
from uipath.runtime.events import UiPathRuntimeEvent
from uipath.runtime.resumable.protocols import (
UiPathResumeTriggerProtocol,
)
from uipath.runtime.resumable.runtime import UiPathResumableRuntime
from uipath.runtime.schema import UiPathRuntimeSchema
class MultiTriggerMockRuntime:
"""Mock runtime that simulates parallel branching with multiple interrupts."""
def __init__(self) -> None:
self.execution_count = 0
async def dispose(self) -> None:
pass
async def execute(
self,
input: dict[str, Any] | None = None,
options: UiPathExecuteOptions | None = None,
) -> UiPathRuntimeResult:
"""Simulate parallel branches with progressive suspensions."""
self.execution_count += 1
is_resume = options and options.resume
if self.execution_count == 1:
# First execution: suspend with 2 parallel interrupts
return UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUSPENDED,
output={
"int-1": {"action": "approve_branch_1"},
"int-2": {"action": "approve_branch_2"},
},
)
elif self.execution_count == 2:
# Second execution: int-1 completed, int-2 still pending + new int-3
# input should contain: {"int-1": {"approved": True}}
assert is_resume
assert input is not None
assert "int-1" in input
return UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUSPENDED,
output={
"int-2": {"action": "approve_branch_2"}, # still pending
"int-3": {"action": "approve_branch_3"}, # new interrupt
},
)
else:
# Third execution: all completed
assert is_resume
assert input is not None
assert "int-2" in input
assert "int-3" in input
return UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUCCESSFUL,
output={"completed": True, "resume_data": input},
)
async def stream(
self,
input: dict[str, Any] | None = None,
options: UiPathStreamOptions | None = None,
) -> AsyncGenerator[UiPathRuntimeEvent, None]:
"""Stream version of execute."""
result = await self.execute(input, options)
yield result
async def get_schema(self) -> UiPathRuntimeSchema:
raise NotImplementedError()
class StatefulStorageMock:
"""Stateful storage mock that tracks triggers."""
def __init__(self) -> None:
self.triggers: list[UiPathResumeTrigger] = []
async def get_triggers(self, runtime_id: str) -> list[UiPathResumeTrigger]:
return list(self.triggers)
async def save_triggers(
self, runtime_id: str, triggers: list[UiPathResumeTrigger]
) -> None:
self.triggers = list(triggers)
async def delete_trigger(
self, runtime_id: str, trigger: UiPathResumeTrigger
) -> None:
self.triggers = [
t for t in self.triggers if t.interrupt_id != trigger.interrupt_id
]
async def set_value(
self, runtime_id: str, namespace: str, key: str, value: Any
) -> None:
pass
async def get_value(self, runtime_id: str, namespace: str, key: str) -> Any:
return None
def make_trigger_manager_mock() -> UiPathResumeTriggerProtocol:
"""Create trigger manager mock."""
manager = Mock(spec=UiPathResumeTriggerProtocol)
def create_trigger_impl(data: dict[str, Any]) -> UiPathResumeTrigger:
return UiPathResumeTrigger(
interrupt_id="", # Will be set by resumable runtime
trigger_type=UiPathResumeTriggerType.TASK,
payload=data,
)
async def read_trigger_default(trigger: UiPathResumeTrigger) -> dict[str, Any]:
raise UiPathPendingTriggerError(ErrorCategory.USER, "Trigger not fired yet")
manager.create_trigger = AsyncMock(side_effect=create_trigger_impl)
manager.read_trigger = AsyncMock(side_effect=read_trigger_default)
return cast(UiPathResumeTriggerProtocol, manager)
class TestResumableRuntime:
@pytest.mark.asyncio
async def test_resumable_creates_multiple_triggers_on_first_suspension(
self,
) -> None:
"""First suspension with parallel branches should create multiple triggers."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
result = await resumable.execute({})
# Should be suspended with 2 triggers
assert result.status == UiPathRuntimeStatus.SUSPENDED
assert result.triggers is not None
assert len(result.triggers) == 2
assert {t.interrupt_id for t in result.triggers} == {"int-1", "int-2"}
# Check payloads by interrupt_id (order should be preserved)
assert result.triggers[0].interrupt_id == "int-1"
assert result.triggers[0].payload == {"action": "approve_branch_1"}
assert result.triggers[1].interrupt_id == "int-2"
assert result.triggers[1].payload == {"action": "approve_branch_2"}
# Both triggers should be created and saved
assert cast(AsyncMock, trigger_manager.create_trigger).await_count == 2
assert len(storage.triggers) == 2
@pytest.mark.asyncio
async def test_resumable_adds_only_new_triggers_on_partial_resume(self) -> None:
"""Partial resume should keep pending trigger and add only new ones."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
# First execution
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
result1 = await resumable.execute({})
assert result1.triggers is not None
assert len(result1.triggers) == 2 # int-1, int-2
# Create async side effect function for read_trigger
async def read_trigger_impl(trigger: UiPathResumeTrigger) -> dict[str, Any]:
if trigger.interrupt_id == "int-1":
return {"approved": True}
raise UiPathPendingTriggerError(ErrorCategory.USER, "still pending")
# Replace the mock with new side_effect
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl) # type: ignore
# Second execution (resume)
result2 = await resumable.execute(
None, options=UiPathExecuteOptions(resume=True)
)
# Should have 2 triggers: int-2 (existing) + int-3 (new)
assert result2.status == UiPathRuntimeStatus.SUSPENDED
assert result2.triggers is not None
assert len(result2.triggers) == 2
assert {t.interrupt_id for t in result2.triggers} == {"int-2", "int-3"}
# Only one new trigger created (int-3) - total 3 calls (2 from first + 1 new)
assert cast(AsyncMock, trigger_manager.create_trigger).await_count == 3
@pytest.mark.asyncio
async def test_resumable_completes_after_all_triggers_resolved(self) -> None:
"""After all triggers resolved, execution should complete successfully."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
# First execution - creates int-1, int-2
await resumable.execute({})
# Create async side effect for second resume
async def read_trigger_impl_2(trigger: UiPathResumeTrigger) -> dict[str, Any]:
if trigger.interrupt_id == "int-1":
return {"approved": True}
raise UiPathPendingTriggerError(ErrorCategory.USER, "pending")
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl_2) # type: ignore
# Second execution - int-1 resolved, creates int-3
await resumable.execute(None, options=UiPathExecuteOptions(resume=True))
# Create async side effect for final resume
async def read_trigger_impl_3(trigger: UiPathResumeTrigger) -> dict[str, Any]:
return {"approved": True}
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl_3) # type: ignore
# Third execution - int-2 and int-3 both resolved
result = await resumable.execute(
None, options=UiPathExecuteOptions(resume=True)
)
# Should be successful now
assert result.status == UiPathRuntimeStatus.SUCCESSFUL
assert isinstance(result.output, dict)
assert result.output["completed"] is True
assert "int-2" in result.output["resume_data"]
assert "int-3" in result.output["resume_data"]
@pytest.mark.asyncio
async def test_resumable_auto_resumes_when_triggers_already_fired(self) -> None:
"""When triggers are already fired during suspension, runtime should auto-resume."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
# Configure trigger manager: only int-1 is immediately fired, int-2 stays pending
async def read_trigger_impl(trigger: UiPathResumeTrigger) -> dict[str, Any]:
# Only int-1 is immediately available
if trigger.interrupt_id == "int-1":
return {"approved": True}
# All other triggers are pending
raise UiPathPendingTriggerError(ErrorCategory.USER, "pending")
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl) # type: ignore
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
# First execution - should suspend with int-1 and int-2, but since int-1 is
# already fired, it should auto-resume and suspend again with int-2 and int-3
result = await resumable.execute({})
# The runtime should have auto-resumed once and suspended again
assert result.status == UiPathRuntimeStatus.SUSPENDED
assert result.triggers is not None
assert len(result.triggers) == 2
# After auto-resume with int-1, we should be at second suspension with int-2, int-3
assert {t.interrupt_id for t in result.triggers} == {"int-2", "int-3"}
# Delegate should have been executed twice (initial + auto-resume)
assert runtime_impl.execution_count == 2
@pytest.mark.asyncio
async def test_resumable_auto_resumes_partial_fired_triggers(self) -> None:
"""When only some triggers are fired during suspension, auto-resume with those."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
# Configure trigger manager so int-1 is fired but int-2 is pending
async def read_trigger_impl(trigger: UiPathResumeTrigger) -> dict[str, Any]:
if trigger.interrupt_id == "int-1":
return {"approved": True}
raise UiPathPendingTriggerError(ErrorCategory.USER, "still pending")
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl) # type: ignore
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
# First execution - int-1 fires immediately, int-2 stays pending
# Should auto-resume with int-1 and suspend with int-2, int-3
result = await resumable.execute({})
assert result.status == UiPathRuntimeStatus.SUSPENDED
assert result.triggers is not None
# After auto-resume with int-1, should have int-2 (still pending) + int-3 (new)
assert {t.interrupt_id for t in result.triggers} == {"int-2", "int-3"}
# Verify int-1 was consumed (deleted from storage)
remaining_triggers = await storage.get_triggers("runtime-1")
assert all(t.interrupt_id != "int-1" for t in remaining_triggers)
@pytest.mark.asyncio
async def test_resumable_auto_resumes_multiple_times(self) -> None:
"""When triggers keep being fired immediately, keep auto-resuming until complete."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
# Track which phase we're in to fire the right triggers
checked_triggers = set()
async def read_trigger_impl(trigger: UiPathResumeTrigger) -> dict[str, Any]:
# First time we see int-1: fire it immediately
if trigger.interrupt_id == "int-1" and "int-1" not in checked_triggers:
checked_triggers.add("int-1")
return {"approved": True}
# After int-1 fires, we'll see int-2 and int-3
# Fire them both immediately
if trigger.interrupt_id in ["int-2", "int-3"]:
return {"approved": True}
raise UiPathPendingTriggerError(ErrorCategory.USER, "pending")
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl) # type: ignore
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
# Execute once - should auto-resume through all suspensions
result = await resumable.execute({})
# Should complete successfully after auto-resuming twice
# 1st exec: suspend with int-1, int-2 -> int-1 fires -> auto-resume
# 2nd exec: suspend with int-2, int-3 -> both fire -> auto-resume
# 3rd exec: complete
assert result.status == UiPathRuntimeStatus.SUCCESSFUL
assert isinstance(result.output, dict)
assert result.output["completed"] is True
# Delegate should have been executed 3 times
assert runtime_impl.execution_count == 3
@pytest.mark.asyncio
async def test_resumable_stream_auto_resumes_when_triggers_fired(self) -> None:
"""Stream should auto-resume when triggers are already fired."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
# Configure int-1 to be immediately fired, int-2 pending
async def read_trigger_impl(trigger: UiPathResumeTrigger) -> dict[str, Any]:
if trigger.interrupt_id == "int-1":
return {"approved": True}
raise UiPathPendingTriggerError(ErrorCategory.USER, "pending")
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl) # type: ignore
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
# Stream should auto-resume and yield final result after auto-resume
events = []
async for event in resumable.stream({}):
events.append(event)
# Should have received exactly one final result (after auto-resume)
assert len(events) == 1
assert isinstance(events[0], UiPathRuntimeResult)
assert events[0].status == UiPathRuntimeStatus.SUSPENDED
# Should be at second suspension (after auto-resume with int-1)
assert events[0].triggers is not None
assert {t.interrupt_id for t in events[0].triggers} == {"int-2", "int-3"}
@pytest.mark.asyncio
async def test_resumable_no_auto_resume_when_all_triggers_pending(self) -> None:
"""When all triggers are pending, should NOT auto-resume."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
# All triggers are pending
async def read_trigger_impl(trigger: UiPathResumeTrigger) -> dict[str, Any]:
raise UiPathPendingTriggerError(ErrorCategory.USER, "pending")
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl) # type: ignore
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
# Execute - should suspend
result = await resumable.execute({})
assert result.status == UiPathRuntimeStatus.SUSPENDED
assert result.triggers is not None
assert {t.interrupt_id for t in result.triggers} == {"int-1", "int-2"}
# Delegate should have been executed only once)
assert runtime_impl.execution_count == 1
@pytest.mark.asyncio
async def test_resumable_skips_api_triggers_on_auto_resume_check(self) -> None:
"""API triggers should be skipped when checking for auto-resume after suspension."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
# Create trigger manager that returns API trigger type
def create_api_trigger(data: dict[str, Any]) -> UiPathResumeTrigger:
return UiPathResumeTrigger(
interrupt_id="", # Will be set by resumable runtime
trigger_type=UiPathResumeTriggerType.API,
payload=data,
)
trigger_manager.create_trigger = AsyncMock(side_effect=create_api_trigger) # type: ignore
async def read_trigger_impl(trigger: UiPathResumeTrigger) -> dict[str, Any]:
return {"approved": True}
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl) # type: ignore
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
# Execute - should suspend and NOT auto-resume because they are API triggers
result = await resumable.execute({})
assert result.status == UiPathRuntimeStatus.SUSPENDED
assert result.triggers is not None
assert len(result.triggers) == 2
assert {t.interrupt_id for t in result.triggers} == {"int-1", "int-2"}
# Verify all triggers are API type
assert all(
t.trigger_type == UiPathResumeTriggerType.API for t in result.triggers
)
# Delegate should have been executed only once (no auto-resume)
assert runtime_impl.execution_count == 1
@pytest.mark.asyncio
async def test_resumable_auto_resumes_task_triggers_but_not_api_triggers(
self,
) -> None:
"""Mixed triggers: TASK triggers should trigger auto-resume, API triggers should not."""
runtime_impl = MultiTriggerMockRuntime()
storage = StatefulStorageMock()
trigger_manager = make_trigger_manager_mock()
# Create different trigger types: int-1 is TASK, int-2 is API
def create_typed_trigger(data: dict[str, Any]) -> UiPathResumeTrigger:
# Determine trigger type based on payload action
if "approve_branch_1" in str(data):
trigger_type = UiPathResumeTriggerType.TASK
else:
trigger_type = UiPathResumeTriggerType.API
return UiPathResumeTrigger(
interrupt_id="", # Will be set by resumable runtime
trigger_type=trigger_type,
payload=data,
)
trigger_manager.create_trigger = AsyncMock(side_effect=create_typed_trigger) # type: ignore
# only TASK should trigger auto-resume
async def read_trigger_impl(trigger: UiPathResumeTrigger) -> dict[str, Any]:
return {"approved": True}
trigger_manager.read_trigger = AsyncMock(side_effect=read_trigger_impl) # type: ignore
resumable = UiPathResumableRuntime(
delegate=runtime_impl,
storage=storage,
trigger_manager=trigger_manager,
runtime_id="runtime-1",
)
# Execute - should auto-resume based on int-1 (TASK) but skip int-2 (API)
result = await resumable.execute({})
# Should have auto-resumed once (because of TASK trigger)
assert result.status == UiPathRuntimeStatus.SUSPENDED
assert result.triggers is not None
# After auto-resume with int-1, should be at second suspension with int-2 and int-3
assert {t.interrupt_id for t in result.triggers} == {"int-2", "int-3"}
# Delegate should have been executed twice (initial + auto-resume for TASK trigger)
assert runtime_impl.execution_count == 2