|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Exercise both workers over gRPC and optionally against the DTS emulator.""" |
| 5 | + |
| 6 | +import json |
| 7 | +import asyncio |
| 8 | +import os |
| 9 | +import socket |
| 10 | +from collections.abc import Generator |
| 11 | +from datetime import timedelta |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from durabletask import client, task, worker |
| 17 | +from durabletask.azuremanaged.client import DurableTaskSchedulerClient |
| 18 | +from durabletask.azuremanaged.worker import DurableTaskSchedulerWorker |
| 19 | +from durabletask.testing import create_test_backend |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("transport", [ |
| 23 | + "core", |
| 24 | + "azuremanaged", |
| 25 | + pytest.param("dts", marks=pytest.mark.dts), |
| 26 | +]) |
| 27 | +@pytest.mark.parametrize("coroutine_parent", [False, True], ids=["generator-parent", "coroutine-parent"]) |
| 28 | +@pytest.mark.parametrize("borrowed", [False, True], ids=["coroutines", "borrowed-handles"]) |
| 29 | +def test_mixed_orchestrators_over_grpc(transport: str, coroutine_parent: bool, borrowed: bool) -> None: |
| 30 | + def increment(ctx: task.ActivityContext, value: int) -> int: |
| 31 | + return value + 1 |
| 32 | + |
| 33 | + async def coroutine_child(ctx: task.AsyncOrchestrationContext, value: int) -> int: |
| 34 | + if not borrowed: |
| 35 | + await ctx.create_timer(timedelta(milliseconds=10)) |
| 36 | + return await ctx.call_activity(increment, input=value, return_type=int) |
| 37 | + await asyncio.sleep(0) |
| 38 | + await asyncio.sleep(0.01) |
| 39 | + return await ctx.as_task_context().call_activity(increment, input=value, return_type=int) |
| 40 | + |
| 41 | + def generator_child(ctx: task.OrchestrationContext, value: int) -> Generator[task.Task[Any], Any, int]: |
| 42 | + return (yield ctx.call_activity(increment, input=value)) |
| 43 | + |
| 44 | + async def coroutine(ctx: task.AsyncOrchestrationContext, value: int) -> list[Any]: |
| 45 | + if not borrowed: |
| 46 | + async with asyncio.TaskGroup() as group: |
| 47 | + first = group.create_task(ctx.call_sub_orchestrator(coroutine_child, input=value)) |
| 48 | + second = group.create_task(ctx.call_sub_orchestrator(generator_child, input=value + 1)) |
| 49 | + return [first.result(), second.result(), await ctx.wait_for_external_event("go")] |
| 50 | + handles = ctx.as_task_context() |
| 51 | + values = list(await asyncio.gather( |
| 52 | + handles.call_sub_orchestrator(coroutine_child, input=value), |
| 53 | + handles.call_sub_orchestrator(generator_child, input=value + 1), |
| 54 | + )) |
| 55 | + event = await ctx.wait_for_external_event("go") |
| 56 | + return values + [event] |
| 57 | + |
| 58 | + def generator(ctx: task.OrchestrationContext, value: int) -> Generator[task.Task[Any], Any, list[Any]]: |
| 59 | + values = yield task.when_all([ |
| 60 | + ctx.call_sub_orchestrator(coroutine_child, input=value), |
| 61 | + ctx.call_sub_orchestrator(generator_child, input=value + 1), |
| 62 | + ]) |
| 63 | + event = yield ctx.wait_for_external_event("go") |
| 64 | + return values + [event] |
| 65 | + |
| 66 | + if transport == "dts": |
| 67 | + backend = None |
| 68 | + host = os.environ["ENDPOINT"] |
| 69 | + taskhub = os.environ["TASKHUB"] |
| 70 | + else: |
| 71 | + with socket.socket() as listener: |
| 72 | + listener.bind(("127.0.0.1", 0)) |
| 73 | + port = listener.getsockname()[1] |
| 74 | + backend = create_test_backend(port=port) |
| 75 | + host = f"localhost:{port}" |
| 76 | + taskhub = "test" |
| 77 | + if transport != "core": |
| 78 | + w = DurableTaskSchedulerWorker( |
| 79 | + host_address=host, secure_channel=False, taskhub=taskhub, token_credential=None) |
| 80 | + c = DurableTaskSchedulerClient( |
| 81 | + host_address=host, secure_channel=False, taskhub=taskhub, token_credential=None) |
| 82 | + else: |
| 83 | + w = worker.TaskHubGrpcWorker(host_address=host) |
| 84 | + c = client.TaskHubGrpcClient(host_address=host) |
| 85 | + try: |
| 86 | + with w, c: |
| 87 | + parent = coroutine if coroutine_parent else generator |
| 88 | + for fn in (parent, coroutine_child, generator_child): |
| 89 | + w.add_orchestrator(fn) |
| 90 | + w.add_activity(increment) |
| 91 | + w.start() |
| 92 | + instance_id = c.schedule_new_orchestration(parent, input=3) |
| 93 | + c.raise_orchestration_event(instance_id, "go", data="approved") |
| 94 | + state = c.wait_for_orchestration_completion(instance_id, timeout=15) |
| 95 | + assert state is not None |
| 96 | + assert state.runtime_status == client.OrchestrationStatus.COMPLETED, state.failure_details |
| 97 | + assert state.serialized_output is not None |
| 98 | + assert json.loads(state.serialized_output) == [4, 5, "approved"] |
| 99 | + finally: |
| 100 | + if backend is not None: |
| 101 | + backend.stop() |
| 102 | + backend.reset() |
0 commit comments