|
9 | 9 |
|
10 | 10 | import durabletask.internal.helpers as helpers |
11 | 11 | import durabletask.internal.orchestrator_service_pb2 as pb |
12 | | -from durabletask import task, worker |
| 12 | +from durabletask import task, worker, entities |
13 | 13 |
|
14 | 14 | logging.basicConfig( |
15 | 15 | format='%(asctime)s.%(msecs)03d %(name)s %(levelname)s: %(message)s', |
@@ -1183,6 +1183,77 @@ def orchestrator(ctx: task.OrchestrationContext, _): |
1183 | 1183 | assert str(ex) in complete_action.failureDetails.errorMessage |
1184 | 1184 |
|
1185 | 1185 |
|
| 1186 | +def test_orchestrator_completed_no_effect(): |
| 1187 | + def dummy_activity(ctx, _): |
| 1188 | + pass |
| 1189 | + |
| 1190 | + def orchestrator(ctx: task.OrchestrationContext, orchestrator_input): |
| 1191 | + yield ctx.call_activity(dummy_activity, input=orchestrator_input) |
| 1192 | + |
| 1193 | + registry = worker._Registry() |
| 1194 | + name = registry.add_orchestrator(orchestrator) |
| 1195 | + |
| 1196 | + encoded_input = json.dumps(42) |
| 1197 | + new_events = [ |
| 1198 | + helpers.new_orchestrator_started_event(), |
| 1199 | + helpers.new_execution_started_event(name, TEST_INSTANCE_ID, encoded_input), |
| 1200 | + helpers.new_orchestrator_completed_event()] |
| 1201 | + executor = worker._OrchestrationExecutor(registry, TEST_LOGGER) |
| 1202 | + result = executor.execute(TEST_INSTANCE_ID, [], new_events) |
| 1203 | + actions = result.actions |
| 1204 | + |
| 1205 | + assert len(actions) == 1 |
| 1206 | + assert type(actions[0]) is pb.OrchestratorAction |
| 1207 | + assert actions[0].id == 1 |
| 1208 | + assert actions[0].HasField("scheduleTask") |
| 1209 | + assert actions[0].scheduleTask.name == task.get_name(dummy_activity) |
| 1210 | + assert actions[0].scheduleTask.input.value == encoded_input |
| 1211 | + |
| 1212 | + |
| 1213 | +def test_entity_lock_created_as_event(): |
| 1214 | + test_entity_id = entities.EntityInstanceId("Counter", "myCounter") |
| 1215 | + |
| 1216 | + def orchestrator(ctx: task.OrchestrationContext, _): |
| 1217 | + entity_id = test_entity_id |
| 1218 | + with (yield ctx.lock_entities([entity_id])): |
| 1219 | + return (yield ctx.call_entity(entity_id, "set", 1)) |
| 1220 | + |
| 1221 | + registry = worker._Registry() |
| 1222 | + name = registry.add_orchestrator(orchestrator) |
| 1223 | + |
| 1224 | + new_events = [ |
| 1225 | + helpers.new_orchestrator_started_event(), |
| 1226 | + helpers.new_execution_started_event(name, TEST_INSTANCE_ID, None), |
| 1227 | + ] |
| 1228 | + |
| 1229 | + executor = worker._OrchestrationExecutor(registry, TEST_LOGGER) |
| 1230 | + result1 = executor.execute(TEST_INSTANCE_ID, [], new_events) |
| 1231 | + actions = result1.actions |
| 1232 | + assert len(actions) == 1 |
| 1233 | + assert type(actions[0]) is pb.OrchestratorAction |
| 1234 | + assert actions[0].id == 1 |
| 1235 | + assert actions[0].HasField("sendEntityMessage") |
| 1236 | + assert actions[0].sendEntityMessage.HasField("entityLockRequested") |
| 1237 | + |
| 1238 | + old_events = new_events |
| 1239 | + event_sent_input = { |
| 1240 | + "id": actions[0].sendEntityMessage.entityLockRequested.criticalSectionId, |
| 1241 | + } |
| 1242 | + new_events = [ |
| 1243 | + helpers.new_event_sent_event(1, str(test_entity_id), json.dumps(event_sent_input)), |
| 1244 | + helpers.new_event_raised_event(event_sent_input["id"], None), |
| 1245 | + ] |
| 1246 | + result = executor.execute(TEST_INSTANCE_ID, old_events, new_events) |
| 1247 | + actions = result.actions |
| 1248 | + |
| 1249 | + assert len(actions) == 1 |
| 1250 | + assert type(actions[0]) is pb.OrchestratorAction |
| 1251 | + assert actions[0].id == 2 |
| 1252 | + assert actions[0].HasField("sendEntityMessage") |
| 1253 | + assert actions[0].sendEntityMessage.HasField("entityOperationCalled") |
| 1254 | + assert actions[0].sendEntityMessage.entityOperationCalled.targetInstanceId.value == str(test_entity_id) |
| 1255 | + |
| 1256 | + |
1186 | 1257 | def get_and_validate_complete_orchestration_action_list(expected_action_count: int, actions: list[pb.OrchestratorAction]) -> pb.CompleteOrchestrationAction: |
1187 | 1258 | assert len(actions) == expected_action_count |
1188 | 1259 | assert type(actions[-1]) is pb.OrchestratorAction |
|
0 commit comments