|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import uuid |
| 5 | +from datetime import timedelta |
| 6 | + |
| 7 | +import nexusrpc.handler |
| 8 | +import pytest |
| 9 | + |
| 10 | +from temporalio import workflow |
| 11 | +from temporalio.testing import WorkflowEnvironment |
| 12 | +from tests.helpers import new_worker |
| 13 | +from tests.helpers.nexus import create_nexus_endpoint, make_nexus_endpoint_name |
| 14 | + |
| 15 | + |
| 16 | +@workflow.defn |
| 17 | +class NexusCallerWorkflow: |
| 18 | + """Workflow that calls a Nexus operation.""" |
| 19 | + |
| 20 | + @workflow.run |
| 21 | + async def run(self) -> None: |
| 22 | + nexus_client = workflow.create_nexus_client( |
| 23 | + endpoint=make_nexus_endpoint_name(workflow.info().task_queue), |
| 24 | + service="MaxConcurrentTestService", |
| 25 | + ) |
| 26 | + |
| 27 | + await nexus_client.execute_operation( |
| 28 | + "op", None, schedule_to_close_timeout=timedelta(seconds=60) |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +async def test_max_concurrent_nexus_tasks(env: WorkflowEnvironment): |
| 33 | + if env.supports_time_skipping: |
| 34 | + pytest.skip("Nexus tests don't work with Javas test server") |
| 35 | + |
| 36 | + @nexusrpc.handler.service_handler |
| 37 | + class MaxConcurrentTestService: |
| 38 | + @nexusrpc.handler.sync_operation |
| 39 | + async def op( |
| 40 | + self, _ctx: nexusrpc.handler.StartOperationContext, id: None |
| 41 | + ) -> None: |
| 42 | + await asyncio.Event().wait() |
| 43 | + |
| 44 | + async with new_worker( |
| 45 | + env.client, |
| 46 | + NexusCallerWorkflow, |
| 47 | + nexus_service_handlers=[MaxConcurrentTestService()], |
| 48 | + ) as worker: |
| 49 | + await create_nexus_endpoint(worker.task_queue, env.client) |
| 50 | + |
| 51 | + execute_workflow = env.client.execute_workflow( |
| 52 | + NexusCallerWorkflow.run, |
| 53 | + id=str(uuid.uuid4()), |
| 54 | + task_queue=worker.task_queue, |
| 55 | + ) |
| 56 | + try: |
| 57 | + await asyncio.wait_for(execute_workflow, timeout=3) |
| 58 | + except TimeoutError: |
| 59 | + print("Saw expected timeout") |
| 60 | + pass |
| 61 | + else: |
| 62 | + pytest.fail("Expected timeout") |
0 commit comments