|
1 | 1 | import asyncio |
| 2 | +import uuid |
2 | 3 |
|
3 | 4 | import pytest |
| 5 | +from temporalio import workflow |
4 | 6 | from temporalio.client import Client |
5 | 7 | from temporalio.testing import WorkflowEnvironment |
| 8 | +from temporalio.worker import Worker |
6 | 9 |
|
7 | | -import nexus_sync_operations.caller.app |
8 | | -import nexus_sync_operations.caller.workflows |
| 10 | +import nexus_sync_operations.handler.service_handler |
9 | 11 | import nexus_sync_operations.handler.worker |
| 12 | +from message_passing.introduction import Language |
| 13 | +from message_passing.introduction.workflows import GetLanguagesInput, SetLanguageInput |
| 14 | +from nexus_sync_operations.caller.workflows import CallerWorkflow |
10 | 15 | from tests.helpers.nexus import create_nexus_endpoint, delete_nexus_endpoint |
11 | 16 |
|
| 17 | +with workflow.unsafe.imports_passed_through(): |
| 18 | + from nexus_sync_operations.service import GreetingService |
| 19 | + |
| 20 | + |
| 21 | +NEXUS_ENDPOINT = "nexus-sync-operations-nexus-endpoint" |
| 22 | + |
| 23 | + |
| 24 | +@workflow.defn |
| 25 | +class TestCallerWorkflow: |
| 26 | + """Test workflow that calls Nexus operations and makes assertions.""" |
| 27 | + |
| 28 | + @workflow.run |
| 29 | + async def run(self) -> None: |
| 30 | + nexus_client = workflow.create_nexus_client( |
| 31 | + service=GreetingService, |
| 32 | + endpoint=NEXUS_ENDPOINT, |
| 33 | + ) |
| 34 | + |
| 35 | + supported_languages = await nexus_client.execute_operation( |
| 36 | + GreetingService.get_languages, GetLanguagesInput(include_unsupported=False) |
| 37 | + ) |
| 38 | + assert supported_languages == [Language.CHINESE, Language.ENGLISH] |
| 39 | + |
| 40 | + initial_language = await nexus_client.execute_operation( |
| 41 | + GreetingService.get_language, None |
| 42 | + ) |
| 43 | + assert initial_language == Language.ENGLISH |
| 44 | + |
| 45 | + previous_language = await nexus_client.execute_operation( |
| 46 | + GreetingService.set_language, |
| 47 | + SetLanguageInput(language=Language.CHINESE), |
| 48 | + ) |
| 49 | + assert previous_language == Language.ENGLISH |
| 50 | + |
| 51 | + current_language = await nexus_client.execute_operation( |
| 52 | + GreetingService.get_language, None |
| 53 | + ) |
| 54 | + assert current_language == Language.CHINESE |
| 55 | + |
| 56 | + previous_language = await nexus_client.execute_operation( |
| 57 | + GreetingService.set_language, |
| 58 | + SetLanguageInput(language=Language.ARABIC), |
| 59 | + ) |
| 60 | + assert previous_language == Language.CHINESE |
| 61 | + |
| 62 | + current_language = await nexus_client.execute_operation( |
| 63 | + GreetingService.get_language, None |
| 64 | + ) |
| 65 | + assert current_language == Language.ARABIC |
| 66 | + |
12 | 67 |
|
13 | 68 | async def test_nexus_sync_operations(client: Client, env: WorkflowEnvironment): |
14 | 69 | if env.supports_time_skipping: |
15 | 70 | pytest.skip("Nexus tests don't work under the Java test server") |
16 | 71 |
|
17 | 72 | create_response = await create_nexus_endpoint( |
18 | | - name=nexus_sync_operations.caller.workflows.NEXUS_ENDPOINT, |
| 73 | + name=NEXUS_ENDPOINT, |
19 | 74 | task_queue=nexus_sync_operations.handler.worker.TASK_QUEUE, |
20 | 75 | client=client, |
21 | 76 | ) |
22 | 77 | try: |
| 78 | + await ( |
| 79 | + nexus_sync_operations.handler.service_handler.GreetingServiceHandler.start( |
| 80 | + client, nexus_sync_operations.handler.worker.TASK_QUEUE |
| 81 | + ) |
| 82 | + ) |
23 | 83 | handler_worker_task = asyncio.create_task( |
24 | | - nexus_sync_operations.handler.worker.main( |
| 84 | + nexus_sync_operations.handler.worker.main(client) |
| 85 | + ) |
| 86 | + try: |
| 87 | + async with Worker( |
25 | 88 | client, |
| 89 | + task_queue="test-caller-task-queue", |
| 90 | + workflows=[TestCallerWorkflow], |
| 91 | + ): |
| 92 | + await client.execute_workflow( |
| 93 | + TestCallerWorkflow.run, |
| 94 | + id=str(uuid.uuid4()), |
| 95 | + task_queue="test-caller-task-queue", |
| 96 | + ) |
| 97 | + finally: |
| 98 | + nexus_sync_operations.handler.worker.interrupt_event.set() |
| 99 | + await handler_worker_task |
| 100 | + nexus_sync_operations.handler.worker.interrupt_event.clear() |
| 101 | + try: |
| 102 | + await client.get_workflow_handle( |
| 103 | + nexus_sync_operations.handler.service_handler.GreetingServiceHandler.LONG_RUNNING_WORKFLOW_ID |
| 104 | + ).terminate() |
| 105 | + except Exception: |
| 106 | + pass |
| 107 | + finally: |
| 108 | + await delete_nexus_endpoint( |
| 109 | + id=create_response.endpoint.id, |
| 110 | + version=create_response.endpoint.version, |
| 111 | + client=client, |
| 112 | + ) |
| 113 | + |
| 114 | + |
| 115 | +async def test_nexus_sync_operations_caller_workflow( |
| 116 | + client: Client, env: WorkflowEnvironment |
| 117 | +): |
| 118 | + """ |
| 119 | + Runs the CallerWorkflow from the sample to ensure it executes without errors. |
| 120 | + """ |
| 121 | + if env.supports_time_skipping: |
| 122 | + pytest.skip("Nexus tests don't work under the Java test server") |
| 123 | + |
| 124 | + create_response = await create_nexus_endpoint( |
| 125 | + name=NEXUS_ENDPOINT, |
| 126 | + task_queue=nexus_sync_operations.handler.worker.TASK_QUEUE, |
| 127 | + client=client, |
| 128 | + ) |
| 129 | + try: |
| 130 | + await ( |
| 131 | + nexus_sync_operations.handler.service_handler.GreetingServiceHandler.start( |
| 132 | + client, nexus_sync_operations.handler.worker.TASK_QUEUE |
26 | 133 | ) |
27 | 134 | ) |
28 | | - await nexus_sync_operations.caller.app.execute_caller_workflow( |
29 | | - client, |
| 135 | + handler_worker_task = asyncio.create_task( |
| 136 | + nexus_sync_operations.handler.worker.main(client) |
30 | 137 | ) |
31 | | - nexus_sync_operations.handler.worker.interrupt_event.set() |
32 | | - await handler_worker_task |
33 | | - nexus_sync_operations.handler.worker.interrupt_event.clear() |
| 138 | + try: |
| 139 | + async with Worker( |
| 140 | + client, |
| 141 | + task_queue="test-caller-task-queue", |
| 142 | + workflows=[CallerWorkflow], |
| 143 | + ): |
| 144 | + await client.execute_workflow( |
| 145 | + CallerWorkflow.run, |
| 146 | + id=str(uuid.uuid4()), |
| 147 | + task_queue="test-caller-task-queue", |
| 148 | + ) |
| 149 | + finally: |
| 150 | + nexus_sync_operations.handler.worker.interrupt_event.set() |
| 151 | + await handler_worker_task |
| 152 | + nexus_sync_operations.handler.worker.interrupt_event.clear() |
| 153 | + try: |
| 154 | + await client.get_workflow_handle( |
| 155 | + nexus_sync_operations.handler.service_handler.GreetingServiceHandler.LONG_RUNNING_WORKFLOW_ID |
| 156 | + ).terminate() |
| 157 | + except Exception: |
| 158 | + pass |
34 | 159 | finally: |
35 | 160 | await delete_nexus_endpoint( |
36 | 161 | id=create_response.endpoint.id, |
|
0 commit comments