forked from microsoft/durabletask-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_batch_actions.py
More file actions
501 lines (386 loc) · 18.1 KB
/
Copy pathtest_batch_actions.py
File metadata and controls
501 lines (386 loc) · 18.1 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Tests for batch query and purge APIs using the InMemoryOrchestrationBackend.
"""
import logging
import time
from datetime import datetime, timedelta, timezone
import pytest
from durabletask import client, entities, task
from durabletask.client import TaskHubGrpcClient
from durabletask.testing import create_test_backend
from durabletask.worker import TaskHubGrpcWorker
from tests.durabletask._port_utils import find_free_port
BATCH_TEST_PORT = find_free_port()
HOST = f"localhost:{BATCH_TEST_PORT}"
@pytest.fixture
def backend():
"""Create an in-memory backend for batch action testing."""
backend = create_test_backend(port=BATCH_TEST_PORT)
yield backend
backend.stop()
backend.reset()
def empty_orchestrator(ctx: task.OrchestrationContext, _):
return "Complete"
def failing_orchestrator(ctx: task.OrchestrationContext, _):
raise Exception("Orchestration failed")
def test_get_all_orchestration_states(backend):
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_orchestrator(empty_orchestrator)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
id = c.schedule_new_orchestration(empty_orchestrator, input="Hello")
c.wait_for_orchestration_completion(id, timeout=30)
all_orchestrations = c.get_all_orchestration_states()
query = client.OrchestrationQuery()
query.fetch_inputs_and_outputs = True
all_orchestrations_with_state = c.get_all_orchestration_states(query)
this_orch = c.get_orchestration_state(id)
finally:
worker.stop()
assert this_orch is not None
assert this_orch.instance_id == id
assert all_orchestrations is not None
matching_orchestrations = [o for o in all_orchestrations if o.instance_id == id]
assert len(matching_orchestrations) == 1
orchestration_state = matching_orchestrations[0]
assert orchestration_state.runtime_status == client.OrchestrationStatus.COMPLETED
assert orchestration_state.serialized_input is None
assert orchestration_state.serialized_output is None
assert orchestration_state.failure_details is None
assert all_orchestrations_with_state is not None
matching_orchestrations = [o for o in all_orchestrations_with_state if o.instance_id == id]
assert len(matching_orchestrations) == 1
orchestration_state = matching_orchestrations[0]
assert orchestration_state.runtime_status == client.OrchestrationStatus.COMPLETED
assert orchestration_state.serialized_input == '"Hello"'
assert orchestration_state.serialized_output == '"Complete"'
assert orchestration_state.failure_details is None
def test_get_orchestration_state_by_status(backend):
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_orchestrator(empty_orchestrator)
worker.add_orchestrator(failing_orchestrator)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
# Schedule completed orchestration
completed_id = c.schedule_new_orchestration(empty_orchestrator, input="Hello")
c.wait_for_orchestration_completion(completed_id, timeout=30)
# Schedule failed orchestration
failed_id = c.schedule_new_orchestration(failing_orchestrator)
try:
c.wait_for_orchestration_completion(failed_id, timeout=30)
except client.OrchestrationFailedError:
pass # Expected failure
# Query by completed status
query = client.OrchestrationQuery()
query.runtime_status = [client.OrchestrationStatus.COMPLETED]
query.fetch_inputs_and_outputs = True
completed_orchestrations = c.get_all_orchestration_states(query)
# Query by failed status
query = client.OrchestrationQuery()
query.runtime_status = [client.OrchestrationStatus.FAILED]
query.fetch_inputs_and_outputs = True
failed_orchestrations = c.get_all_orchestration_states(query)
finally:
worker.stop()
assert len([o for o in completed_orchestrations if o.instance_id == completed_id]) == 1
completed_orch = [o for o in completed_orchestrations if o.instance_id == completed_id][0]
assert completed_orch.runtime_status == client.OrchestrationStatus.COMPLETED
assert completed_orch.serialized_output == '"Complete"'
assert len([o for o in failed_orchestrations if o.instance_id == failed_id]) == 1
failed_orch = [o for o in failed_orchestrations if o.instance_id == failed_id][0]
assert failed_orch.runtime_status == client.OrchestrationStatus.FAILED
assert failed_orch.failure_details is not None
def test_get_orchestration_state_by_time_range(backend):
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_orchestrator(empty_orchestrator)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
# Get current time
before_creation = datetime.now(timezone.utc) - timedelta(seconds=5)
# Schedule orchestration
id = c.schedule_new_orchestration(empty_orchestrator, input="TimeTest")
c.wait_for_orchestration_completion(id, timeout=30)
after_creation = datetime.now(timezone.utc) + timedelta(seconds=5)
# Query by time range
query = client.OrchestrationQuery(
created_time_from=before_creation,
created_time_to=after_creation,
fetch_inputs_and_outputs=True
)
orchestrations_in_range = c.get_all_orchestration_states(query)
# Query outside time range
query = client.OrchestrationQuery(
created_time_from=after_creation,
created_time_to=after_creation + timedelta(hours=1),
fetch_inputs_and_outputs=True
)
orchestrations_outside_range = c.get_all_orchestration_states(query)
finally:
worker.stop()
assert len([o for o in orchestrations_in_range if o.instance_id == id]) == 1
assert len([o for o in orchestrations_outside_range if o.instance_id == id]) == 0
def test_get_orchestration_state_pagination_succeeds(backend):
# Create a custom handler to capture log messages
log_records = []
class ListHandler(logging.Handler):
def emit(self, record):
log_records.append(record)
handler = ListHandler()
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_orchestrator(empty_orchestrator)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST, log_handler=handler) as c:
# Create at least 3 orchestrations to test the limit
ids = []
for i in range(3):
id = c.schedule_new_orchestration(empty_orchestrator, input=f"Test{i}")
ids.append(id)
# Wait for all to complete
for id in ids:
c.wait_for_orchestration_completion(id, timeout=30)
# Query with max_instance_count=2
query = client.OrchestrationQuery(max_instance_count=2)
orchestrations = c.get_all_orchestration_states(query)
finally:
worker.stop()
# Should return more than 2 instances since we created at least 3
assert len(orchestrations) > 2
# Verify the pagination loop ran by checking for the continuation token log message
assert any("Received continuation token" in record.getMessage() for record in log_records), \
"Expected pagination loop to execute with continuation token"
def test_purge_orchestration(backend):
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_orchestrator(empty_orchestrator)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
# Schedule and complete orchestration
id = c.schedule_new_orchestration(empty_orchestrator, input="ToPurge")
c.wait_for_orchestration_completion(id, timeout=30)
# Verify it exists
state_before = c.get_orchestration_state(id)
assert state_before is not None
# Purge the orchestration
result = c.purge_orchestration(id, recursive=True)
# Verify purge result
assert result.deleted_instance_count >= 1
# Verify it no longer exists
state_after = c.get_orchestration_state(id)
assert state_after is None
finally:
worker.stop()
def test_purge_orchestrations_by_status(backend):
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_orchestrator(failing_orchestrator)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
# Schedule and let it fail
failed_id = c.schedule_new_orchestration(failing_orchestrator)
try:
c.wait_for_orchestration_completion(failed_id, timeout=30)
except client.OrchestrationFailedError:
pass # Expected failure
# Verify it exists and is failed
state_before = c.get_orchestration_state(failed_id)
assert state_before is not None
assert state_before.runtime_status == client.OrchestrationStatus.FAILED
# Purge failed orchestrations
result = c.purge_orchestrations_by(
runtime_status=[client.OrchestrationStatus.FAILED],
recursive=True
)
# Verify purge result
assert result.deleted_instance_count >= 1
# Verify the failed orchestration no longer exists
state_after = c.get_orchestration_state(failed_id)
assert state_after is None
finally:
worker.stop()
def test_purge_orchestrations_by_time_range(backend):
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_orchestrator(empty_orchestrator)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
# Get current time
before_creation = datetime.now(timezone.utc) - timedelta(seconds=5)
# Schedule orchestration
id = c.schedule_new_orchestration(empty_orchestrator, input="ToPurgeByTime")
c.wait_for_orchestration_completion(id, timeout=30)
after_creation = datetime.now(timezone.utc) + timedelta(seconds=5)
# Verify it exists
state_before = c.get_orchestration_state(id)
assert state_before is not None
# Purge by time range
result = c.purge_orchestrations_by(
created_time_from=before_creation,
created_time_to=after_creation,
runtime_status=[client.OrchestrationStatus.COMPLETED],
recursive=True
)
# Verify purge result
assert result.deleted_instance_count >= 1
# Verify it no longer exists
state_after = c.get_orchestration_state(id)
assert state_after is None
finally:
worker.stop()
def test_list_instance_ids_paginates_terminal_instances(backend):
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_orchestrator(empty_orchestrator)
worker.add_orchestrator(failing_orchestrator)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
completed_id = c.schedule_new_orchestration(empty_orchestrator, input='done')
c.wait_for_orchestration_completion(completed_id, timeout=30)
failed_id = c.schedule_new_orchestration(failing_orchestrator)
failed_state = c.wait_for_orchestration_completion(failed_id, timeout=30)
window_start = datetime.now(timezone.utc) - timedelta(minutes=1)
first_page = c.list_instance_ids(
runtime_status=[client.OrchestrationStatus.COMPLETED, client.OrchestrationStatus.FAILED],
completed_time_from=window_start,
page_size=1,
)
second_page = c.list_instance_ids(
runtime_status=[client.OrchestrationStatus.COMPLETED, client.OrchestrationStatus.FAILED],
completed_time_from=window_start,
page_size=1,
continuation_token=first_page.continuation_token,
)
finally:
worker.stop()
assert len(first_page.items) == 1
assert len(second_page.items) == 1
assert set(first_page.items + second_page.items) == {completed_id, failed_id}
assert failed_state is not None
assert failed_state.runtime_status == client.OrchestrationStatus.FAILED
assert first_page.continuation_token is not None
assert any(instance_id in first_page.continuation_token for instance_id in {completed_id, failed_id})
assert second_page.continuation_token is None
def test_get_all_entities(backend):
counter_value = 0
def counter_entity(ctx: entities.EntityContext, input):
nonlocal counter_value
if ctx.operation == "add":
counter_value += input
ctx.set_state(counter_value)
elif ctx.operation == "get":
return ctx.get_state(int, 0)
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_entity(counter_entity)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
# Create entity
entity_id = entities.EntityInstanceId("counter_entity", "testCounter1")
c.signal_entity(entity_id, "add", input=5)
time.sleep(3) # Wait for signal to be processed
# Get all entities without state
query = client.EntityQuery(include_state=False)
all_entities = c.get_all_entities(query)
assert len([e for e in all_entities if e.id == entity_id]) == 1
entity_without_state = [e for e in all_entities if e.id == entity_id][0]
assert entity_without_state.get_state(int) is None
# Get all entities with state
query = client.EntityQuery(include_state=True)
all_entities_with_state = c.get_all_entities(query)
assert len([e for e in all_entities_with_state if e.id == entity_id]) == 1
entity_with_state = [e for e in all_entities_with_state if e.id == entity_id][0]
assert entity_with_state.get_state(int) == 5
finally:
worker.stop()
def test_get_entities_by_instance_id_prefix(backend):
def counter_entity(ctx: entities.EntityContext, input):
if ctx.operation == "set":
ctx.set_state(input)
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_entity(counter_entity)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
# Create entities with different prefixes
entity_id_1 = entities.EntityInstanceId("counter_entity", "prefix1_counter")
entity_id_2 = entities.EntityInstanceId("counter_entity", "prefix2_counter")
c.signal_entity(entity_id_1, "set", input=10)
c.signal_entity(entity_id_2, "set", input=20)
time.sleep(3) # Wait for signals to be processed
# Query by prefix
query = client.EntityQuery(
instance_id_starts_with="@counter_entity@prefix1",
include_state=True
)
entities_prefix1 = c.get_all_entities(query)
query = client.EntityQuery(
instance_id_starts_with="@counter_entity@prefix2",
include_state=True
)
entities_prefix2 = c.get_all_entities(query)
finally:
worker.stop()
assert len([e for e in entities_prefix1 if e.id == entity_id_1]) == 1
assert len([e for e in entities_prefix1 if e.id == entity_id_2]) == 0
assert len([e for e in entities_prefix2 if e.id == entity_id_2]) == 1
assert len([e for e in entities_prefix2 if e.id == entity_id_1]) == 0
def test_get_entities_by_time_range(backend):
def simple_entity(ctx: entities.EntityContext, input):
if ctx.operation == "set":
ctx.set_state(input)
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_entity(simple_entity)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
# Get current time
before_creation = datetime.now(timezone.utc) - timedelta(seconds=5)
# Create entity
entity_id = entities.EntityInstanceId("simple_entity", "timeTestEntity")
c.signal_entity(entity_id, "set", input="test_value")
time.sleep(3) # Wait for signal to be processed
after_creation = datetime.now(timezone.utc) + timedelta(seconds=5)
# Query by time range
query = client.EntityQuery(
last_modified_from=before_creation,
last_modified_to=after_creation,
include_state=True
)
entities_in_range = c.get_all_entities(query)
# Query outside time range
query = client.EntityQuery(
last_modified_from=after_creation,
last_modified_to=after_creation + timedelta(hours=1)
)
entities_outside_range = c.get_all_entities(query)
finally:
worker.stop()
assert len([e for e in entities_in_range if e.id == entity_id]) == 1
assert len([e for e in entities_outside_range if e.id == entity_id]) == 0
def test_clean_entity_storage(backend):
class EmptyEntity(entities.DurableEntity):
pass
worker = TaskHubGrpcWorker(host_address=HOST)
worker.add_entity(EmptyEntity)
worker.start()
try:
with TaskHubGrpcClient(host_address=HOST) as c:
# Create an entity and then delete its state to make it empty
entity_id = entities.EntityInstanceId("EmptyEntity", "toClean")
c.signal_entity(entity_id, "delete")
time.sleep(3) # Wait for signal to be processed
# Clean entity storage
result = c.clean_entity_storage(
remove_empty_entities=True,
release_orphaned_locks=True
)
finally:
worker.stop()
# Verify clean result
assert result.empty_entities_removed >= 0
assert result.orphaned_locks_released >= 0