-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_workflow_execution.py
More file actions
830 lines (663 loc) · 32.2 KB
/
Copy pathtest_workflow_execution.py
File metadata and controls
830 lines (663 loc) · 32.2 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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
import logging
import time
from time import sleep
from conductor.client.automator.task_handler import TaskHandler
from conductor.client.configuration.configuration import Configuration
from conductor.client.http.models import StartWorkflowRequest
from conductor.client.http.models import TaskDef
from conductor.client.worker.worker import ExecuteTaskFunction
from conductor.client.worker.worker import Worker
from conductor.client.workflow.conductor_workflow import ConductorWorkflow
from conductor.client.workflow.executor.workflow_executor import WorkflowExecutor
from conductor.client.workflow.task.simple_task import SimpleTask
from tests.integration.resources.worker.python.python_worker import *
WORKFLOW_NAME = "sdk_python_integration_test_workflow"
WORKFLOW_DESCRIPTION = "Python SDK Integration Test"
TASK_NAME = "python_integration_test_task"
WORKFLOW_VERSION = 1234
WORKFLOW_OWNER_EMAIL = "test@test"
COMPLEX_WF_NAME = 'complex_wf_signal_test'
SUB_WF_1_NAME = 'complex_wf_signal_test_subworkflow_1'
SUB_WF_2_NAME = 'complex_wf_signal_test_subworkflow_2'
logger = logging.getLogger(
Configuration.get_logging_formatted_name(
__name__
)
)
def run_workflow_execution_tests(configuration: Configuration, workflow_executor: WorkflowExecutor):
workers = [
ClassWorker(TASK_NAME),
ClassWorkerWithDomain(TASK_NAME),
generate_worker(worker_with_generic_input_and_generic_output),
generate_worker(worker_with_generic_input_and_task_result_output),
generate_worker(worker_with_task_input_and_generic_output),
generate_worker(worker_with_task_input_and_task_result_output),
]
task_handler = TaskHandler(
workers=workers,
configuration=configuration,
scan_for_annotated_workers=True,
import_modules=['tests.integration.resources.worker.python.python_worker']
)
task_handler.start_processes()
try:
test_get_workflow_by_correlation_ids(workflow_executor)
logger.debug('finished workflow correlation ids test')
test_workflow_registration(workflow_executor)
logger.debug('finished workflow registration tests')
test_workflow_execution(
workflow_quantity=6,
workflow_name=WORKFLOW_NAME,
workflow_executor=workflow_executor,
workflow_completion_timeout=5.0
)
test_decorated_workers(workflow_executor)
logger.debug('finished decorated workers tests')
test_execute_workflow_async_features(workflow_executor)
logger.debug('finished execute_workflow reactive features tests')
test_execute_workflow_error_handling(workflow_executor)
logger.debug('finished execute_workflow error handling tests')
run_signal_tests(configuration, workflow_executor)
logger.debug('finished signal API tests')
except Exception as e:
task_handler.stop_processes()
raise Exception(f'failed integration tests, reason: {e}')
task_handler.stop_processes()
def generate_tasks_defs():
python_simple_task_from_code = TaskDef(
description="desc python_simple_task_from_code",
owner_app="python_integration_test_app",
timeout_seconds=3,
response_timeout_seconds=2,
created_by=WORKFLOW_OWNER_EMAIL,
name=TASK_NAME,
input_keys=["input1"],
output_keys=[],
owner_email=WORKFLOW_OWNER_EMAIL,
)
return [python_simple_task_from_code]
def test_get_workflow_by_correlation_ids(workflow_executor: WorkflowExecutor):
_run_with_retry_attempt(
workflow_executor.get_by_correlation_ids,
{
'workflow_name': WORKFLOW_NAME,
'correlation_ids': [
'2', '5', '33', '4', '32', '7', '34', '1', '3', '6', '1440',
]
}
)
def test_workflow_registration(workflow_executor: WorkflowExecutor):
workflow = generate_workflow(workflow_executor)
try:
workflow_executor.metadata_client.unregister_workflow_def_with_http_info(
workflow.name, workflow.version
)
except Exception as e:
if '404' not in str(e):
raise e
workflow.register(overwrite=True) == None
workflow_executor.register_workflow(
workflow.to_workflow_def(), overwrite=True
)
def test_decorated_workers(
workflow_executor: WorkflowExecutor,
workflow_name: str = 'TestPythonDecoratedWorkerWf',
) -> None:
wf = generate_workflow(
workflow_executor=workflow_executor,
workflow_name=workflow_name,
task_name='test_python_decorated_worker',
)
wf.register(True)
workflow_id = workflow_executor.start_workflow(StartWorkflowRequest(name=workflow_name))
logger.debug(f'started TestPythonDecoratedWorkerWf with id: {workflow_id}')
td_map = {
'test_python_decorated_worker': 'cool'
}
start_wf_req = StartWorkflowRequest(name=workflow_name, task_to_domain=td_map)
workflow_id_2 = workflow_executor.start_workflow(start_wf_req)
logger.debug(f'started TestPythonDecoratedWorkerWf with domain:cool and id: {workflow_id_2}')
sleep(15)
_run_with_retry_attempt(
validate_workflow_status,
{
'workflow_id': workflow_id,
'workflow_executor': workflow_executor
}
)
_run_with_retry_attempt(
validate_workflow_status,
{
'workflow_id': workflow_id_2,
'workflow_executor': workflow_executor
}
)
workflow_executor.metadata_client.unregister_workflow_def(wf.name, wf.version)
def test_workflow_execution(
workflow_quantity: int,
workflow_name: str,
workflow_executor: WorkflowExecutor,
workflow_completion_timeout: float,
) -> None:
start_workflow_requests = [''] * workflow_quantity
for i in range(workflow_quantity):
start_workflow_requests[i] = StartWorkflowRequest(name=workflow_name)
workflow_ids = workflow_executor.start_workflows(*start_workflow_requests)
sleep(workflow_completion_timeout)
for workflow_id in workflow_ids:
_run_with_retry_attempt(
validate_workflow_status,
{
'workflow_id': workflow_id,
'workflow_executor': workflow_executor
}
)
def generate_workflow(workflow_executor: WorkflowExecutor, workflow_name: str = WORKFLOW_NAME,
task_name: str = TASK_NAME) -> ConductorWorkflow:
return ConductorWorkflow(
executor=workflow_executor,
name=workflow_name,
description=WORKFLOW_DESCRIPTION,
version=WORKFLOW_VERSION,
).owner_email(
WORKFLOW_OWNER_EMAIL
).add(
SimpleTask(
task_def_name=task_name,
task_reference_name=task_name,
)
)
def validate_workflow_status(workflow_id: str, workflow_executor: WorkflowExecutor) -> None:
workflow = workflow_executor.get_workflow(
workflow_id=workflow_id,
include_tasks=False,
)
if workflow.status != 'COMPLETED':
raise Exception(
f'workflow expected to be COMPLETED, but received {workflow.status}, workflow_id: {workflow_id}'
)
workflow_status = workflow_executor.get_workflow_status(
workflow_id=workflow_id,
include_output=False,
include_variables=False,
)
if workflow_status.status != 'COMPLETED':
raise Exception(
f'workflow expected to be COMPLETED, but received {workflow_status.status}, workflow_id: {workflow_id}'
)
def generate_worker(execute_function: ExecuteTaskFunction) -> Worker:
return Worker(
task_definition_name=TASK_NAME,
execute_function=execute_function,
poll_interval=750.0
)
def _run_with_retry_attempt(f, params, retries=4) -> None:
for attempt in range(retries):
try:
return f(**params)
except Exception as e:
if attempt == retries - 1:
raise e
sleep(1 << attempt)
def test_execute_workflow_async_features(workflow_executor: WorkflowExecutor):
"""Test the execute_workflow method with reactive features (consistency and return_strategy)"""
logger.debug('Starting execute_workflow reactive features tests')
# Register workflow first
workflow = generate_workflow(workflow_executor)
workflow.register(overwrite=True)
# Test 1: execute_workflow with default values (None -> defaults)
logger.debug('Test 1: execute_workflow with default consistency/return_strategy')
start_request = StartWorkflowRequest(
name=WORKFLOW_NAME,
version=WORKFLOW_VERSION,
input={'test_input': 'default_test'}
)
workflow_run_1 = workflow_executor.execute_workflow_with_return_strategy(request=start_request,
wait_until_task_ref=TASK_NAME,
wait_for_seconds=30)
assert workflow_run_1 is not None, "Workflow run should not be None"
logger.debug(f'Test 1 - Workflow ID: {workflow_run_1.workflow_id}, Status: {workflow_run_1.status}')
# Wait for completion if needed
if workflow_run_1.status == 'RUNNING':
_wait_for_workflow_completion(workflow_executor, workflow_run_1.workflow_id)
# Test 2: execute_workflow with explicit DURABLE consistency
logger.debug('Test 2: execute_workflow with explicit DURABLE consistency')
start_request_2 = StartWorkflowRequest(
name=WORKFLOW_NAME,
version=WORKFLOW_VERSION,
input={'test_input': 'durable_test'}
)
workflow_run_2 = workflow_executor.execute_workflow_with_return_strategy(request=start_request_2,
wait_until_task_ref=TASK_NAME,
wait_for_seconds=30, consistency='DURABLE',
return_strategy='BLOCKING_WORKFLOW')
assert workflow_run_2 is not None, "Workflow run should not be None"
logger.debug(f'Test 2 - Workflow ID: {workflow_run_2.workflow_id}, Status: {workflow_run_2.status}')
if workflow_run_2.status == 'RUNNING':
_wait_for_workflow_completion(workflow_executor, workflow_run_2.workflow_id)
# Test 3: execute_workflow with DURABLE consistency and different return strategy
logger.debug('Test 3: execute_workflow with DURABLE consistency and BLOCKING_WORKFLOW')
start_request_3 = StartWorkflowRequest(
name=WORKFLOW_NAME,
version=WORKFLOW_VERSION,
input={'test_input': 'durable_blocking_test'}
)
try:
workflow_run_3 = workflow_executor.execute_workflow_with_return_strategy(request=start_request_3,
wait_until_task_ref=TASK_NAME,
wait_for_seconds=30,
consistency='DURABLE',
return_strategy='BLOCKING_WORKFLOW')
assert workflow_run_3 is not None, "Workflow run should not be None"
logger.debug(f'Test 3 - Workflow ID: {workflow_run_3.workflow_id}, Status: {workflow_run_3.status}')
if workflow_run_3.status == 'RUNNING':
_wait_for_workflow_completion(workflow_executor, workflow_run_3.workflow_id)
except Exception as e:
logger.error(f'Test 3 failed with error: {e}')
logger.debug('Skipping Test 3 - DURABLE/BLOCKING_WORKFLOW combination may not be supported')
# Create a placeholder workflow_run_3 to continue tests
workflow_run_3 = None
# Test 4: execute_workflow with SYNCHRONOUS consistency and BLOCKING_TASK_INPUT
logger.debug('Test 4: execute_workflow with SYNCHRONOUS consistency and BLOCKING_TASK_INPUT')
start_request_4 = StartWorkflowRequest(
name=WORKFLOW_NAME,
version=WORKFLOW_VERSION,
input={'test_input': 'synchronous_test'}
)
workflow_run_4 = workflow_executor.execute_workflow_with_return_strategy(request=start_request_4,
wait_until_task_ref=TASK_NAME,
wait_for_seconds=30,
consistency='SYNCHRONOUS',
return_strategy='BLOCKING_TASK_INPUT')
print(f"Raw response type: {type(workflow_run_4)}")
if workflow_run_4 is None:
print("Response is None - checking API call...")
assert workflow_run_4 is not None, "Workflow run should not be None"
logger.debug(f'Test 4 - Workflow ID: {workflow_run_4.workflow_id}, Status: {workflow_run_4.status}')
if workflow_run_4.status == 'RUNNING':
_wait_for_workflow_completion(workflow_executor, workflow_run_4.workflow_id)
# Test 5: Compare with original execute method
logger.debug('Test 5: Compare with original execute method')
workflow_run_5 = workflow_executor.execute(
name=WORKFLOW_NAME,
version=WORKFLOW_VERSION,
workflow_input={'test_input': 'original_test'},
wait_until_task_ref=TASK_NAME,
wait_for_seconds=30
)
assert workflow_run_5 is not None, "Workflow run should not be None"
logger.debug(f'Test 5 - Workflow ID: {workflow_run_5.workflow_id}, Status: {workflow_run_5.status}')
if workflow_run_5.status == 'RUNNING':
_wait_for_workflow_completion(workflow_executor, workflow_run_5.workflow_id)
# Validate all workflows completed successfully
workflow_ids = [
workflow_run_1.workflow_id,
workflow_run_2.workflow_id,
workflow_run_4.workflow_id,
workflow_run_5.workflow_id
]
# Add workflow_run_3 only if it was successful
if workflow_run_3 is not None:
workflow_ids.insert(2, workflow_run_3.workflow_id)
for i, workflow_id in enumerate(workflow_ids, 1):
_run_with_retry_attempt(
validate_workflow_status,
{
'workflow_id': workflow_id,
'workflow_executor': workflow_executor
}
)
logger.debug(f'Test {i} - Workflow {workflow_id} completed successfully')
logger.debug('All execute_workflow reactive features tests passed!')
def test_execute_workflow_error_handling(workflow_executor: WorkflowExecutor):
"""Test error handling in execute_workflow with invalid parameters"""
logger.debug('Starting execute_workflow error handling tests')
# Test with invalid consistency value (should still work due to defaults)
start_request = StartWorkflowRequest(
name=WORKFLOW_NAME,
version=WORKFLOW_VERSION,
input={'test_input': 'error_test'}
)
try:
# This should work because None values get converted to defaults
workflow_run = workflow_executor.execute_workflow_with_return_strategy(request=start_request,
wait_until_task_ref=TASK_NAME,
wait_for_seconds=5, consistency=None,
return_strategy=None)
logger.debug(f'Error handling test - Workflow created: {workflow_run.workflow_id}')
if workflow_run.status == 'RUNNING':
_wait_for_workflow_completion(workflow_executor, workflow_run.workflow_id)
_run_with_retry_attempt(
validate_workflow_status,
{
'workflow_id': workflow_run.workflow_id,
'workflow_executor': workflow_executor
}
)
except Exception as e:
logger.error(f'Unexpected error in error handling test: {e}')
raise e
logger.debug('Execute_workflow error handling tests passed!')
def _wait_for_workflow_completion(workflow_executor: WorkflowExecutor, workflow_id: str, max_wait_seconds: int = 60):
"""Helper function to wait for workflow completion"""
import time
start_time = time.time()
while time.time() - start_time < max_wait_seconds:
workflow = workflow_executor.get_workflow(workflow_id, True)
if workflow.status in ['COMPLETED', 'FAILED', 'TERMINATED', 'TIMED_OUT']:
logger.debug(f'Workflow {workflow_id} finished with status: {workflow.status}')
return workflow
logger.debug(f'Waiting for workflow {workflow_id}... Status: {workflow.status}')
time.sleep(2)
# Return final state even if not completed
return workflow_executor.get_workflow(workflow_id, True)
# ===== SIGNAL TESTS =====
def run_signal_tests(configuration: Configuration, workflow_executor: WorkflowExecutor):
"""Run all signal API tests using WorkflowExecutor methods"""
logger.info('START: Signal API tests using WorkflowExecutor')
try:
# Register signal test workflows (same as original test)
_register_signal_test_workflows(workflow_executor)
# Test sync signal with different return strategies
test_signal_target_workflow(workflow_executor)
test_signal_blocking_workflow(workflow_executor)
test_signal_blocking_task(workflow_executor)
test_signal_blocking_task_input(workflow_executor)
# Test default return strategy
test_signal_default_strategy(workflow_executor)
# Test async signal
test_signal_async(workflow_executor)
# Test to_dict fix
test_signal_to_dict_fix(workflow_executor)
logger.info('All signal tests completed successfully')
except Exception as e:
logger.error(f'Signal tests failed: {e}')
raise
finally:
# Cleanup
try:
workflow_executor.metadata_client.unregister_workflow_def(
COMPLEX_WF_NAME, 1
)
workflow_executor.metadata_client.unregister_workflow_def(
SUB_WF_1_NAME, 1
)
workflow_executor.metadata_client.unregister_workflow_def(
SUB_WF_2_NAME, 1
)
except Exception as cleanup_error:
logger.warning(f'Cleanup failed: {cleanup_error}')
logger.info('END: Signal API tests using WorkflowExecutor')
def _register_signal_test_workflows(workflow_executor: WorkflowExecutor):
"""Register the complex signal test workflows from JSON files"""
import json
import os
def _get_workflow_definition(path):
"""Get workflow definition from JSON file, following existing pattern"""
# Get directory of current script
current_dir = os.path.dirname(os.path.abspath(__file__))
# Path to project root (adjust based on your structure)
project_root = os.path.abspath(os.path.join(current_dir, '..', '..', '..'))
# Construct path from project root
actual_path = os.path.join(project_root, path)
# For debugging
logger.info(f"Attempting to load workflow from: {actual_path}")
try:
with open(actual_path, "r") as f:
workflow_json = json.loads(f.read())
# Convert to WorkflowDef object
api_client = workflow_executor.workflow_client.api_client
workflow_def = api_client.deserialize_class(workflow_json, "WorkflowDef")
return workflow_def
except FileNotFoundError:
logger.error(f"Workflow definition file not found: {actual_path}")
raise
except Exception as e:
logger.error(f"Error loading workflow definition: {e}")
raise
try:
# Register main workflow
complex_wf_def = _get_workflow_definition(f'tests/integration/resources/test_data/{COMPLEX_WF_NAME}.json')
workflow_executor.metadata_client.update1(body=[complex_wf_def], overwrite=True)
logger.info(f'Registered workflow: {COMPLEX_WF_NAME}')
# Register subworkflows
sub_wf1_def = _get_workflow_definition(f'tests/integration/resources/test_data/{SUB_WF_1_NAME}.json')
workflow_executor.metadata_client.update1(body=[sub_wf1_def], overwrite=True)
logger.info(f'Registered workflow: {SUB_WF_1_NAME}')
sub_wf2_def = _get_workflow_definition(f'tests/integration/resources/test_data/{SUB_WF_2_NAME}.json')
workflow_executor.metadata_client.update1(body=[sub_wf2_def], overwrite=True)
logger.info(f'Registered workflow: {SUB_WF_2_NAME}')
except Exception as e:
logger.warning(f'Some workflows may already be registered: {e}')
# Give time for workflow registration
time.sleep(1.0)
def _start_complex_workflow(workflow_executor: WorkflowExecutor) -> str:
"""Start complex workflow and return workflow ID"""
try:
start_request = StartWorkflowRequest(
name=COMPLEX_WF_NAME,
version=1,
input={}
)
# Use the workflow_executor.start_workflow method
workflow_id = workflow_executor.start_workflow(start_request)
assert workflow_id is not None, "Failed to start workflow"
# Brief wait for workflow initialization
time.sleep(0.5)
logger.info(f'Started workflow {workflow_id}')
return workflow_id
except Exception as e:
logger.error(f'Failed to start workflow: {e}')
raise
def _complete_workflow(workflow_executor: WorkflowExecutor, workflow_id: str):
"""Complete workflow by sending required signals"""
try:
# Send completion signals using signal_async method
time.sleep(5)
workflow_executor.signal_async(
workflow_id=workflow_id,
status="COMPLETED",
body={"result": "signal1"}
)
workflow_executor.signal_async(
workflow_id=workflow_id,
status="COMPLETED",
body={"result": "signal2"}
)
except Exception as e:
logger.error(f'Failed to complete workflow {workflow_id}: {e}')
raise
def test_signal_target_workflow(workflow_executor: WorkflowExecutor):
"""Test signal with TARGET_WORKFLOW return strategy"""
logger.info('Testing signal with TARGET_WORKFLOW strategy...')
# Start workflow
workflow_id = _start_complex_workflow(workflow_executor)
# Wait and check workflow status
time.sleep(1.0)
# Debug: Check workflow status before signaling
try:
workflow = workflow_executor.get_workflow(workflow_id, include_tasks=True)
logger.info(f"Workflow status before signal: {workflow.status}")
logger.info(f"Workflow tasks: {len(workflow.tasks) if workflow.tasks else 0}")
if workflow.tasks:
for task in workflow.tasks:
logger.info(f"Task: {task.task_type}, Status: {task.status}, Ref: {task.reference_task_name}")
except Exception as e:
logger.warning(f"Could not get workflow status: {e}")
# Send signal with TARGET_WORKFLOW strategy
response = workflow_executor.signal(
workflow_id=workflow_id,
status="COMPLETED",
body={"result": "test_target_workflow"},
return_strategy="TARGET_WORKFLOW"
)
# Debug: Print response details
logger.info(f"Response object: {response}")
logger.info(f"Response type: {type(response)}")
if hasattr(response, '__dict__'):
logger.info(f"Response attributes: {response.__dict__}")
if hasattr(response, 'to_dict'):
try:
logger.info(f"Response to_dict: {response.to_dict()}")
except Exception as e:
logger.warning(f"to_dict failed: {e}")
# Validate response
assert response is not None, "Signal response is None"
assert hasattr(response, 'response_type'), "Response missing response_type attribute"
assert response.response_type == "TARGET_WORKFLOW", f"Expected TARGET_WORKFLOW, got {response.response_type}"
assert response.target_workflow_id == workflow_id, "Target workflow ID mismatch"
# Test helper methods
assert response.is_target_workflow(), "is_target_workflow() should return True"
workflow_data = response.get_workflow()
assert workflow_data is not None, "get_workflow() should return data"
assert workflow_data.get('workflowId') == workflow_id, "Workflow data should contain correct workflow_id"
# Wait for workflow completion
_complete_workflow(workflow_executor, workflow_id)
logger.info('TARGET_WORKFLOW strategy test completed')
# Test helper methods
assert response.is_target_workflow(), "is_target_workflow() should return True"
workflow_data = response.get_workflow()
assert workflow_data is not None, "get_workflow() should return data"
assert workflow_data.get('workflowId') == workflow_id, "Workflow data should contain correct workflow_id"
# Wait for workflow completion
_wait_for_workflow_completion(workflow_executor, workflow_id)
logger.info('TARGET_WORKFLOW strategy test completed')
def test_signal_blocking_workflow(workflow_executor: WorkflowExecutor):
"""Test signal with BLOCKING_WORKFLOW return strategy"""
logger.info('Testing signal with BLOCKING_WORKFLOW strategy...')
workflow_id = _start_complex_workflow(workflow_executor)
time.sleep(0.5)
response = workflow_executor.signal(
workflow_id=workflow_id,
status="COMPLETED",
body={"result": "test_blocking_workflow"},
return_strategy="BLOCKING_WORKFLOW"
)
# Validate response
assert response is not None, "Signal response is None"
assert response.response_type == "BLOCKING_WORKFLOW", f"Expected BLOCKING_WORKFLOW, got {response.response_type}"
assert response.target_workflow_id == workflow_id, "Target workflow ID mismatch"
# Test helper methods
assert response.is_blocking_workflow(), "is_blocking_workflow() should return True"
workflow_data = response.get_workflow()
assert workflow_data is not None, "get_workflow() should return data"
_complete_workflow(workflow_executor, workflow_id)
logger.info('BLOCKING_WORKFLOW strategy test completed')
def test_signal_blocking_task(workflow_executor: WorkflowExecutor):
"""Test signal with BLOCKING_TASK return strategy"""
logger.info('Testing signal with BLOCKING_TASK strategy...')
workflow_id = _start_complex_workflow(workflow_executor)
time.sleep(0.5)
response = workflow_executor.signal(
workflow_id=workflow_id,
status="COMPLETED",
body={"result": "test_blocking_task"},
return_strategy="BLOCKING_TASK"
)
# Validate response
assert response is not None, "Signal response is None"
assert response.response_type == "BLOCKING_TASK", f"Expected BLOCKING_TASK, got {response.response_type}"
assert response.target_workflow_id == workflow_id, "Target workflow ID mismatch"
# Test helper methods
assert response.is_blocking_task(), "is_blocking_task() should return True"
task_data = response.get_blocking_task()
assert task_data is not None, "get_blocking_task() should return data"
assert task_data.get('taskId') is not None, "Task data should contain task_id"
_complete_workflow(workflow_executor, workflow_id)
logger.info('BLOCKING_TASK strategy test completed')
def test_signal_blocking_task_input(workflow_executor: WorkflowExecutor):
"""Test signal with BLOCKING_TASK_INPUT return strategy"""
logger.info('Testing signal with BLOCKING_TASK_INPUT strategy...')
workflow_id = _start_complex_workflow(workflow_executor)
time.sleep(0.5)
response = workflow_executor.signal(
workflow_id=workflow_id,
status="COMPLETED",
body={"result": "test_blocking_task_input"},
return_strategy="BLOCKING_TASK_INPUT"
)
# Validate response
assert response is not None, "Signal response is None"
assert response.response_type == "BLOCKING_TASK_INPUT", f"Expected BLOCKING_TASK_INPUT, got {response.response_type}"
assert response.target_workflow_id == workflow_id, "Target workflow ID mismatch"
# Test helper methods
assert response.is_blocking_task_input(), "is_blocking_task_input() should return True"
task_data = response.get_blocking_task()
task_input = response.get_task_input()
assert task_data is not None, "get_blocking_task() should return data"
assert task_input is not None, "get_task_input() should return data"
_complete_workflow(workflow_executor, workflow_id)
logger.info('BLOCKING_TASK_INPUT strategy test completed')
def test_signal_default_strategy(workflow_executor: WorkflowExecutor):
"""Test signal with default return strategy"""
logger.info('Testing signal with default strategy...')
workflow_id = _start_complex_workflow(workflow_executor)
time.sleep(0.5)
# Don't specify return_strategy - should default to TARGET_WORKFLOW
response = workflow_executor.signal(
workflow_id=workflow_id,
status="COMPLETED",
body={"result": "test_default"}
)
# Validate response
assert response is not None, "Signal response is None"
assert response.response_type == "TARGET_WORKFLOW", f"Expected TARGET_WORKFLOW (default), got {response.response_type}"
assert response.target_workflow_id == workflow_id, "Target workflow ID mismatch"
assert response.is_target_workflow(), "Default should be TARGET_WORKFLOW"
_complete_workflow(workflow_executor, workflow_id)
logger.info('Default strategy test completed')
def test_signal_async(workflow_executor: WorkflowExecutor):
"""Test async signal"""
logger.info('Testing async signal...')
workflow_id = _start_complex_workflow(workflow_executor)
time.sleep(0.5)
# Send async signal (should not return response)
result = workflow_executor.signal_async(
workflow_id=workflow_id,
status="COMPLETED",
body={"result": "test_async"}
)
# Async signal should return None
assert result is None, "Async signal should return None"
_complete_workflow(workflow_executor, workflow_id)
logger.info('Async signal test completed')
def test_signal_to_dict_fix(workflow_executor: WorkflowExecutor):
"""Test that to_dict() returns actual values, not property objects"""
logger.info('Testing to_dict() method fix...')
workflow_id = _start_complex_workflow(workflow_executor)
time.sleep(0.5)
response = workflow_executor.signal(
workflow_id=workflow_id,
status="COMPLETED",
body={"result": "test_to_dict"},
return_strategy="BLOCKING_TASK"
)
response_dict = response.to_dict()
# Ensure no property objects in the output
response_str = str(response_dict)
assert 'property object' not in response_str, f"Found property objects in response: {response_str}"
# Verify actual string values
assert isinstance(response_dict.get('responseType'), str), "responseType should be string"
assert response_dict['responseType'] == 'BLOCKING_TASK', "responseType value incorrect"
assert isinstance(response_dict.get('taskId'), str), "taskId should be string"
assert isinstance(response_dict.get('targetWorkflowId'), str), "targetWorkflowId should be string"
_complete_workflow(workflow_executor, workflow_id)
logger.info('to_dict() method test completed')
# dict['responseType'] == 'BLOCKING_TASK', "responseType value incorrect"
assert isinstance(response_dict.get('taskId'), str), "taskId should be string"
assert isinstance(response_dict.get('targetWorkflowId'), str), "targetWorkflowId should be string"
_wait_for_workflow_completion(workflow_executor, workflow_id)
logger.info('to_dict() method test completed')
def _wait_for_workflow_completion(workflow_executor: WorkflowExecutor, workflow_id: str, timeout: int = 10):
"""Wait for workflow to complete with timeout"""
max_iterations = timeout * 10 # Check every 0.1 seconds
for i in range(max_iterations):
try:
workflow = workflow_executor.get_workflow(workflow_id, include_tasks=False)
if workflow.status in ["COMPLETED", "FAILED", "TERMINATED"]:
logger.debug(f'Workflow {workflow_id} completed with status: {workflow.status}')
return
except Exception as e:
logger.warning(f'Error checking workflow status: {e}')
time.sleep(0.1)
logger.warning(f'Workflow {workflow_id} did not complete within {timeout} seconds')