|
| 1 | +# |
| 2 | +# Copyright (c) 2023-2025 - Restate Software, Inc., Restate GmbH |
| 3 | +# |
| 4 | +# This file is part of the Restate SDK for Python, |
| 5 | +# which is released under the MIT license. |
| 6 | +# |
| 7 | +# You can find a copy of the license in file LICENSE in the root |
| 8 | +# directory of this repository or package, or at |
| 9 | +# https://github.com/restatedev/sdk-typescript/blob/main/LICENSE |
| 10 | +# |
| 11 | + |
| 12 | +import uuid |
| 13 | +import restate |
| 14 | +from restate import ( |
| 15 | + Context, |
| 16 | + Service, |
| 17 | + HarnessEnvironment, |
| 18 | + VirtualObject, |
| 19 | + ObjectContext, |
| 20 | + ObjectSharedContext, |
| 21 | + Workflow, |
| 22 | + WorkflowContext, |
| 23 | + getLogger, |
| 24 | + WorkflowSharedContext, |
| 25 | +) |
| 26 | +import pytest |
| 27 | +import asyncio |
| 28 | + |
| 29 | +# ----- Asyncio fixtures |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(scope="session") |
| 33 | +def anyio_backend(): |
| 34 | + return "asyncio" |
| 35 | + |
| 36 | + |
| 37 | +pytestmark = [ |
| 38 | + pytest.mark.anyio, |
| 39 | +] |
| 40 | + |
| 41 | +# -------- Restate services and restate fixture |
| 42 | + |
| 43 | +greeter = Service("greeter") |
| 44 | + |
| 45 | + |
| 46 | +@greeter.handler() |
| 47 | +async def greet(ctx: Context, name: str) -> str: |
| 48 | + return f"Hello {name}!" |
| 49 | + |
| 50 | + |
| 51 | +counter = VirtualObject("counter") |
| 52 | + |
| 53 | + |
| 54 | +@counter.handler() |
| 55 | +async def increment(ctx: ObjectContext, value: int) -> int: |
| 56 | + n = await ctx.get("counter", type_hint=int) or 0 |
| 57 | + n += value |
| 58 | + ctx.set("counter", n) |
| 59 | + return n |
| 60 | + |
| 61 | + |
| 62 | +@counter.handler(kind="shared") |
| 63 | +async def count(ctx: ObjectSharedContext) -> int: |
| 64 | + return await ctx.get("counter") or 0 |
| 65 | + |
| 66 | + |
| 67 | +payment = Workflow("payment") |
| 68 | +payment_logger = getLogger("payment") |
| 69 | + |
| 70 | + |
| 71 | +@payment.main() |
| 72 | +async def pay(ctx: WorkflowContext): |
| 73 | + ctx.set("status", "verifying payment") |
| 74 | + |
| 75 | + def payment_gateway(): |
| 76 | + payment_logger.info("Doing payment work") |
| 77 | + |
| 78 | + await ctx.run_typed("payment", payment_gateway) |
| 79 | + |
| 80 | + ctx.set("status", "waiting for the payment provider to approve") |
| 81 | + |
| 82 | + # Wait for the payment to be verified |
| 83 | + result = await ctx.promise("verify.payment", type_hint=str).value() |
| 84 | + return f"Verified {result}!" |
| 85 | + |
| 86 | + |
| 87 | +@payment.handler() |
| 88 | +async def payment_verified(ctx: WorkflowSharedContext, result: str): |
| 89 | + promise = ctx.promise("verify.payment", type_hint=str) |
| 90 | + await promise.resolve(result) |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture(scope="session") |
| 94 | +async def restate_test_harness(): |
| 95 | + async with restate.create_test_harness( |
| 96 | + restate.app([greeter, counter, payment]), restate_image="ghcr.io/restatedev/restate:latest" |
| 97 | + ) as harness: |
| 98 | + yield harness |
| 99 | + |
| 100 | + |
| 101 | +# ----- Tests |
| 102 | + |
| 103 | + |
| 104 | +async def test_greeter(restate_test_harness: HarnessEnvironment): |
| 105 | + greeting = await restate_test_harness.client.service_call(greet, arg="Pippo") |
| 106 | + |
| 107 | + assert greeting == "Hello Pippo!" |
| 108 | + |
| 109 | + |
| 110 | +async def test_counter(restate_test_harness: HarnessEnvironment): |
| 111 | + random_key = str(uuid.uuid4()) |
| 112 | + initial_count = await restate_test_harness.client.object_call(count, key=random_key, arg=None) |
| 113 | + await restate_test_harness.client.object_call(increment, key=random_key, arg=1) |
| 114 | + new_count = await restate_test_harness.client.object_call(count, key=random_key, arg=None) |
| 115 | + |
| 116 | + assert new_count == initial_count + 1 |
| 117 | + |
| 118 | + |
| 119 | +async def test_idempotency_key(restate_test_harness: HarnessEnvironment): |
| 120 | + random_key = str(uuid.uuid4()) |
| 121 | + initial_count = await restate_test_harness.client.object_call(count, key=random_key, arg=None) |
| 122 | + await restate_test_harness.client.object_call(increment, key=random_key, arg=1, idempotency_key=random_key) |
| 123 | + await restate_test_harness.client.object_call(increment, key=random_key, arg=1, idempotency_key=random_key) |
| 124 | + new_count = await restate_test_harness.client.object_call(count, key=random_key, arg=None) |
| 125 | + |
| 126 | + assert new_count == initial_count + 1 |
| 127 | + |
| 128 | + |
| 129 | +async def test_workflow(restate_test_harness: HarnessEnvironment): |
| 130 | + random_key = str(uuid.uuid4()) |
| 131 | + call_task = asyncio.create_task(restate_test_harness.client.workflow_call(pay, key=random_key, arg=None)) |
| 132 | + |
| 133 | + await restate_test_harness.client.workflow_call(payment_verified, key=random_key, arg="Done") |
| 134 | + |
| 135 | + assert await call_task == "Verified Done!" |
| 136 | + |
| 137 | + |
| 138 | +async def test_send(restate_test_harness: HarnessEnvironment): |
| 139 | + invocation_handle = await restate_test_harness.client.service_send(greet, arg="Pippo") |
| 140 | + |
| 141 | + assert invocation_handle.status_code == 200 |
| 142 | + assert len(invocation_handle.invocation_id) > 0 |
0 commit comments