|
| 1 | +import uuid |
| 2 | +from datetime import timedelta |
| 3 | + |
| 4 | +from temporalio import activity |
| 5 | +from temporalio.testing import WorkflowEnvironment |
| 6 | +from temporalio.worker import Worker |
| 7 | + |
| 8 | +from sleep_for_days.starter import TASK_QUEUE |
| 9 | +from sleep_for_days.workflows import SendEmailInput, SleepForDaysWorkflow |
| 10 | + |
| 11 | + |
| 12 | +async def test_sleep_for_days_workflow(): |
| 13 | + num_activity_executions = 0 |
| 14 | + |
| 15 | + # Mock out the activity to count executions |
| 16 | + @activity.defn(name="send_email") |
| 17 | + async def send_email_mock(input: SendEmailInput) -> str: |
| 18 | + nonlocal num_activity_executions |
| 19 | + num_activity_executions += 1 |
| 20 | + return input.email_msg |
| 21 | + |
| 22 | + async with await WorkflowEnvironment.start_time_skipping() as env: |
| 23 | + # if env.supports_time_skipping: |
| 24 | + # pytest.skip( |
| 25 | + # "Java test server: https://github.com/temporalio/sdk-java/issues/1903" |
| 26 | + # ) |
| 27 | + async with Worker( |
| 28 | + env.client, |
| 29 | + task_queue=TASK_QUEUE, |
| 30 | + workflows=[SleepForDaysWorkflow], |
| 31 | + activities=[send_email_mock], |
| 32 | + ): |
| 33 | + handle = await env.client.start_workflow( |
| 34 | + SleepForDaysWorkflow.run, |
| 35 | + id=str(uuid.uuid4()), |
| 36 | + task_queue=TASK_QUEUE, |
| 37 | + ) |
| 38 | + |
| 39 | + start_time = await env.get_current_time() |
| 40 | + # Time-skip 5 minutes. |
| 41 | + await env.sleep(timedelta(minutes=5)) |
| 42 | + # Check that the activity has been called, we're now waiting for the sleep to finish. |
| 43 | + assert num_activity_executions == 1 |
| 44 | + # Time-skip 3 days. |
| 45 | + await env.sleep(timedelta(days=90)) |
| 46 | + # Expect 3 more activity calls. |
| 47 | + assert num_activity_executions == 4 |
| 48 | + # Send the signal to complete the workflow. |
| 49 | + await handle.signal(SleepForDaysWorkflow.complete) |
| 50 | + # Expect no more activity calls to have been made - workflow is complete. |
| 51 | + assert num_activity_executions == 4 |
| 52 | + # Expect more than 90 days to have passed. |
| 53 | + assert (await env.get_current_time() - start_time) > timedelta(days=90) |
0 commit comments