Skip to content

Commit 5c25b0b

Browse files
committed
Test non-async-def sync operations
1 parent 0a3e5bd commit 5c25b0b

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

temporalio/nexus/handler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,17 @@ def to_workflow_handle(token: str, client: Client) -> WorkflowHandle[Any, O]:
119119
return client.get_workflow_handle(workflow_id, run_id=run_id)
120120

121121

122+
# TODO(dan): naming, visibility, make this less awkward
122123
def get_input_and_output_types_from_async_start_method(
123124
start_method: Callable[
124125
[S, I, nexusrpc.handler.StartOperationOptions],
125126
Awaitable[StartWorkflowOperationResult[O]],
126127
],
127128
) -> tuple[Type[I], Type[O]]:
128129
input_type, output_type = (
129-
nexusrpc.handler.get_input_and_output_types_from_sync_start_method(start_method)
130+
nexusrpc.handler.get_input_and_output_types_from_sync_operation_start_method(
131+
start_method
132+
)
130133
)
131134
origin_type = typing.get_origin(output_type)
132135
if not origin_type or not issubclass(origin_type, StartWorkflowOperationResult):

tests/worker/test_nexus.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class OpDefinitionType(IntEnum):
5050
@dataclass
5151
class SyncResponse:
5252
op_definition_type: OpDefinitionType
53+
use_async_def: bool
5354

5455

5556
@dataclass
@@ -86,6 +87,7 @@ class OpOutput:
8687
class ServiceInterface:
8788
sync_or_async_operation: nexusrpc.interface.Operation[OpInput, OpOutput]
8889
sync_operation: nexusrpc.interface.Operation[OpInput, OpOutput]
90+
non_async_sync_operation: nexusrpc.interface.Operation[OpInput, OpOutput]
8991
async_operation: nexusrpc.interface.Operation[OpInput, OpOutput]
9092

9193

@@ -176,6 +178,16 @@ async def sync_operation(
176178
start_options_received_by_handler=options,
177179
)
178180

181+
@nexusrpc.handler.sync_operation
182+
def non_async_sync_operation(
183+
self, input: OpInput, options: nexusrpc.handler.StartOperationOptions
184+
) -> OpOutput:
185+
assert isinstance(input.response_type, SyncResponse)
186+
return OpOutput(
187+
value="sync response",
188+
start_options_received_by_handler=options,
189+
)
190+
179191
@temporalio.nexus.handler.workflow_run_operation
180192
async def async_operation(
181193
self, input: OpInput, options: nexusrpc.handler.StartOperationOptions
@@ -276,48 +288,73 @@ def _get_operation(
276288
SyncResponse,
277289
OpDefinitionType.SHORTHAND,
278290
CallerReference.IMPL_WITH_INTERFACE,
291+
True,
279292
): ServiceImpl.sync_operation,
280293
(
281294
SyncResponse,
282295
OpDefinitionType.SHORTHAND,
283296
CallerReference.INTERFACE,
297+
True,
284298
): ServiceInterface.sync_operation,
299+
(
300+
SyncResponse,
301+
OpDefinitionType.SHORTHAND,
302+
CallerReference.IMPL_WITH_INTERFACE,
303+
False,
304+
): ServiceImpl.non_async_sync_operation,
305+
(
306+
SyncResponse,
307+
OpDefinitionType.SHORTHAND,
308+
CallerReference.INTERFACE,
309+
False,
310+
): ServiceInterface.non_async_sync_operation,
285311
(
286312
SyncResponse,
287313
OpDefinitionType.LONGHAND,
288314
CallerReference.IMPL_WITH_INTERFACE,
315+
True,
289316
): ServiceImpl.sync_or_async_operation,
290317
(
291318
SyncResponse,
292319
OpDefinitionType.LONGHAND,
293320
CallerReference.INTERFACE,
321+
True,
294322
): ServiceInterface.sync_or_async_operation,
295323
(
296324
AsyncResponse,
297325
OpDefinitionType.SHORTHAND,
298326
CallerReference.IMPL_WITH_INTERFACE,
327+
True,
299328
): ServiceImpl.async_operation,
300329
(
301330
AsyncResponse,
302331
OpDefinitionType.SHORTHAND,
303332
CallerReference.INTERFACE,
333+
True,
304334
): ServiceInterface.async_operation,
305335
(
306336
AsyncResponse,
307337
OpDefinitionType.LONGHAND,
308338
CallerReference.IMPL_WITH_INTERFACE,
339+
True,
309340
): ServiceImpl.sync_or_async_operation,
310341
(
311342
AsyncResponse,
312343
OpDefinitionType.LONGHAND,
313344
CallerReference.INTERFACE,
345+
True,
314346
): ServiceInterface.sync_or_async_operation,
315347
}[
316348
{True: SyncResponse, False: AsyncResponse}[
317349
isinstance(op_input.response_type, SyncResponse)
318350
],
319351
op_input.response_type.op_definition_type,
320352
op_input.caller_reference,
353+
(
354+
op_input.response_type.use_async_def
355+
if isinstance(op_input.response_type, SyncResponse)
356+
else True
357+
),
321358
]
322359

323360

@@ -400,7 +437,7 @@ async def test_sync_response(
400437
args=[
401438
CallerWfInput(
402439
op_input=OpInput(
403-
response_type=SyncResponse(op_definition_type),
440+
response_type=SyncResponse(op_definition_type, True),
404441
start_options=nexusrpc.handler.StartOperationOptions(
405442
headers={"header-key": "header-value"},
406443
),
@@ -579,7 +616,7 @@ async def test_untyped_caller(
579616
args=[
580617
CallerWfInput(
581618
op_input=OpInput(
582-
response_type=SyncResponse(op_definition_type),
619+
response_type=SyncResponse(op_definition_type, True),
583620
start_options=nexusrpc.handler.StartOperationOptions(),
584621
caller_reference=caller_reference,
585622
),

0 commit comments

Comments
 (0)