|
| 1 | +import sys |
| 2 | +from time import sleep |
| 3 | + |
| 4 | +from cadence.clock_decision_context import DEFAULT_VERSION |
| 5 | +from cadence.workerfactory import WorkerFactory |
| 6 | +from cadence.workflow import workflow_method, Workflow, WorkflowClient |
| 7 | + |
| 8 | +TASK_LIST = "TestWorkflowGetVersion" |
| 9 | +DOMAIN = "sample" |
| 10 | + |
| 11 | +v1_hits = 0 |
| 12 | +v2_hits = 0 |
| 13 | + |
| 14 | +version_found_in_v2_step_1 = None |
| 15 | +version_found_in_v2_step_2 = None |
| 16 | + |
| 17 | +v2_done = False |
| 18 | + |
| 19 | + |
| 20 | +class TestWorkflowGetVersion: |
| 21 | + @workflow_method(task_list=TASK_LIST) |
| 22 | + async def get_greetings(self) -> list: |
| 23 | + raise NotImplementedError |
| 24 | + |
| 25 | + |
| 26 | +class TestWorkflowGetVersionImplV1(TestWorkflowGetVersion): |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + pass |
| 30 | + |
| 31 | + async def get_greetings(self): |
| 32 | + global v1_hits |
| 33 | + v1_hits += 1 |
| 34 | + await Workflow.sleep(60) |
| 35 | + |
| 36 | + |
| 37 | +class TestWorkflowGetVersionImplV2(TestWorkflowGetVersion): |
| 38 | + |
| 39 | + def __init__(self): |
| 40 | + pass |
| 41 | + |
| 42 | + async def get_greetings(self): |
| 43 | + global v2_hits |
| 44 | + global version_found_in_v2_step_1, version_found_in_v2_step_2 |
| 45 | + global v2_done |
| 46 | + v2_hits += 1 |
| 47 | + |
| 48 | + version_found_in_v2_step_1 = Workflow.get_version("first-item", DEFAULT_VERSION, 2) |
| 49 | + await Workflow.sleep(60) |
| 50 | + version_found_in_v2_step_2 = Workflow.get_version("first-item", DEFAULT_VERSION, 2) |
| 51 | + v2_done = True |
| 52 | + |
| 53 | + |
| 54 | +def test_workflow_workflow_get_version(): |
| 55 | + global v1_hits, v2_hits |
| 56 | + factory = WorkerFactory("localhost", 7933, DOMAIN) |
| 57 | + worker = factory.new_worker(TASK_LIST) |
| 58 | + worker.register_workflow_implementation_type(TestWorkflowGetVersionImplV1) |
| 59 | + factory.start() |
| 60 | + |
| 61 | + client = WorkflowClient.new_client(domain=DOMAIN) |
| 62 | + workflow: TestWorkflowGetVersion = client.new_workflow_stub(TestWorkflowGetVersion) |
| 63 | + |
| 64 | + client.start(workflow.get_greetings) |
| 65 | + while v1_hits == 0: |
| 66 | + print(".", end="") |
| 67 | + sleep(2) |
| 68 | + |
| 69 | + worker.register_workflow_implementation_type(TestWorkflowGetVersionImplV2) |
| 70 | + |
| 71 | + while not v2_done: |
| 72 | + print(".", end="") |
| 73 | + sleep(2) |
| 74 | + |
| 75 | + assert v1_hits == 1 |
| 76 | + assert v2_hits == 1 |
| 77 | + assert version_found_in_v2_step_1 == DEFAULT_VERSION |
| 78 | + assert version_found_in_v2_step_2 == 2 |
| 79 | + |
| 80 | + print("Stopping workers") |
| 81 | + worker.stop(background=True) |
0 commit comments