|
15 | 15 | import temporalio.converter |
16 | 16 | import temporalio.nexus.system as nexus_system |
17 | 17 | from temporalio import workflow |
| 18 | +from temporalio.bridge._visitor import PayloadVisitor |
| 19 | +from temporalio.bridge.proto.workflow_completion.workflow_completion_pb2 import ( |
| 20 | + WorkflowActivationCompletion, |
| 21 | +) |
18 | 22 | from temporalio.client import Client |
19 | 23 | from temporalio.converter import ExternalStorage, PayloadCodec |
20 | 24 | from temporalio.testing import WorkflowEnvironment |
@@ -139,6 +143,89 @@ def _assert_start_nexus_operation_interceptor_trace() -> None: |
139 | 143 | assert request.workflow_type.name == "test-workflow" |
140 | 144 |
|
141 | 145 |
|
| 146 | +class _MarkingPayloadVisitor: |
| 147 | + def __init__(self) -> None: |
| 148 | + self.visited_payload_count = 0 |
| 149 | + self.system_envelope_count = 0 |
| 150 | + |
| 151 | + async def visit_payload(self, payload: temporalio.api.common.v1.Payload) -> None: |
| 152 | + self.visited_payload_count += 1 |
| 153 | + payload.metadata["visited"] = b"true" |
| 154 | + |
| 155 | + async def visit_payloads( |
| 156 | + self, payloads: Sequence[temporalio.api.common.v1.Payload] |
| 157 | + ) -> None: |
| 158 | + for payload in payloads: |
| 159 | + await self.visit_payload(payload) |
| 160 | + |
| 161 | + async def visit_system_nexus_envelope( |
| 162 | + self, payload: temporalio.api.common.v1.Payload |
| 163 | + ) -> None: |
| 164 | + _ = payload |
| 165 | + self.system_envelope_count += 1 |
| 166 | + |
| 167 | + |
| 168 | +def _new_schedule_nexus_completion( |
| 169 | + endpoint: str, payload: temporalio.api.common.v1.Payload |
| 170 | +) -> WorkflowActivationCompletion: |
| 171 | + completion = WorkflowActivationCompletion() |
| 172 | + command = completion.successful.commands.add() |
| 173 | + schedule = command.schedule_nexus_operation |
| 174 | + schedule.seq = 1 |
| 175 | + schedule.endpoint = endpoint |
| 176 | + schedule.service = "not-a-registered-system-service" |
| 177 | + schedule.operation = "NotARegisteredSystemOperation" |
| 178 | + schedule.input.CopyFrom(payload) |
| 179 | + return completion |
| 180 | + |
| 181 | + |
| 182 | +def _new_system_nexus_request_payload() -> temporalio.api.common.v1.Payload: |
| 183 | + nested_payload = temporalio.converter.PayloadConverter.default.to_payload( |
| 184 | + "workflow-input" |
| 185 | + ) |
| 186 | + assert nested_payload is not None |
| 187 | + request = workflowservice_pb2.SignalWithStartWorkflowExecutionRequest() |
| 188 | + request.input.payloads.add().CopyFrom(nested_payload) |
| 189 | + payload = nexus_system.get_payload_converter().to_payload(request) |
| 190 | + assert payload is not None |
| 191 | + return payload |
| 192 | + |
| 193 | + |
| 194 | +async def test_schedule_system_nexus_endpoint_ignores_operation_registry() -> None: |
| 195 | + completion = _new_schedule_nexus_completion( |
| 196 | + nexus_system.TEMPORAL_SYSTEM_ENDPOINT, |
| 197 | + _new_system_nexus_request_payload(), |
| 198 | + ) |
| 199 | + visitor = _MarkingPayloadVisitor() |
| 200 | + |
| 201 | + await PayloadVisitor().visit(visitor, completion) |
| 202 | + |
| 203 | + schedule = completion.successful.commands[0].schedule_nexus_operation |
| 204 | + decoded = nexus_system.get_payload_converter().from_payload(schedule.input) |
| 205 | + assert isinstance( |
| 206 | + decoded, workflowservice_pb2.SignalWithStartWorkflowExecutionRequest |
| 207 | + ) |
| 208 | + assert decoded.input.payloads[0].metadata["visited"] == b"true" |
| 209 | + assert "visited" not in schedule.input.metadata |
| 210 | + assert visitor.visited_payload_count == 1 |
| 211 | + assert visitor.system_envelope_count == 1 |
| 212 | + |
| 213 | + |
| 214 | +async def test_schedule_non_system_nexus_visits_input_as_regular_payload() -> None: |
| 215 | + completion = _new_schedule_nexus_completion( |
| 216 | + "not-the-system-endpoint", |
| 217 | + _new_system_nexus_request_payload(), |
| 218 | + ) |
| 219 | + visitor = _MarkingPayloadVisitor() |
| 220 | + |
| 221 | + await PayloadVisitor().visit(visitor, completion) |
| 222 | + |
| 223 | + schedule = completion.successful.commands[0].schedule_nexus_operation |
| 224 | + assert schedule.input.metadata["visited"] == b"true" |
| 225 | + assert visitor.visited_payload_count == 1 |
| 226 | + assert visitor.system_envelope_count == 0 |
| 227 | + |
| 228 | + |
142 | 229 | def _build_proto_sample(message_type: type[Message]) -> Message: |
143 | 230 | message = message_type() |
144 | 231 | _populate_proto_sample(message) |
|
0 commit comments