|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Unit tests for delayed-signal datetime normalization and in-memory backend |
| 5 | +timer lifecycle (regression coverage for PR #160 review feedback).""" |
| 6 | + |
| 7 | +import time |
| 8 | +from datetime import datetime, timedelta, timezone |
| 9 | + |
| 10 | +import durabletask.internal.orchestrator_service_pb2 as pb |
| 11 | +from durabletask.entities import EntityInstanceId |
| 12 | +from durabletask.internal import helpers |
| 13 | +from durabletask.internal.client_helpers import build_signal_entity_req |
| 14 | +from durabletask.testing import create_test_backend |
| 15 | + |
| 16 | +from tests.durabletask._port_utils import find_free_port |
| 17 | + |
| 18 | + |
| 19 | +class TestSignalTimeNormalization: |
| 20 | + def test_build_signal_entity_req_naive_matches_aware(self): |
| 21 | + entity_id = EntityInstanceId("Recorder", "k") |
| 22 | + naive = datetime(2030, 1, 1, 12, 0, 0) |
| 23 | + aware = datetime(2030, 1, 1, 12, 0, 0, tzinfo=timezone.utc) |
| 24 | + |
| 25 | + req_naive = build_signal_entity_req(entity_id, "ping", signal_time=naive) |
| 26 | + req_aware = build_signal_entity_req(entity_id, "ping", signal_time=aware) |
| 27 | + |
| 28 | + assert req_naive.scheduledTime.seconds == req_aware.scheduledTime.seconds |
| 29 | + |
| 30 | + def test_new_signal_entity_action_naive_matches_aware(self): |
| 31 | + entity_id = EntityInstanceId("Recorder", "k") |
| 32 | + naive = datetime(2030, 1, 1, 12, 0, 0) |
| 33 | + aware = datetime(2030, 1, 1, 12, 0, 0, tzinfo=timezone.utc) |
| 34 | + |
| 35 | + a_naive = helpers.new_signal_entity_action(1, entity_id, "ping", None, "r1", naive) |
| 36 | + a_aware = helpers.new_signal_entity_action(1, entity_id, "ping", None, "r2", aware) |
| 37 | + |
| 38 | + naive_ts = a_naive.sendEntityMessage.entityOperationSignaled.scheduledTime |
| 39 | + aware_ts = a_aware.sendEntityMessage.entityOperationSignaled.scheduledTime |
| 40 | + assert naive_ts.seconds == aware_ts.seconds |
| 41 | + |
| 42 | + |
| 43 | +class TestDelayedTimerLifecycle: |
| 44 | + def test_delayed_operation_does_not_fire_into_reset_backend(self): |
| 45 | + backend = create_test_backend(port=find_free_port()) |
| 46 | + try: |
| 47 | + entity_id = "@recorder@k" |
| 48 | + event = pb.HistoryEvent( |
| 49 | + eventId=-1, |
| 50 | + entityOperationSignaled=pb.EntityOperationSignaledEvent(operation="ping"), |
| 51 | + ) |
| 52 | + # Schedule the op to fire shortly in the future, then reset before it |
| 53 | + # fires so the timer wakes into a reset (new-generation) backend. |
| 54 | + fire_at = datetime.now(timezone.utc) + timedelta(seconds=0.3) |
| 55 | + backend._schedule_delayed_entity_operation( # pyright: ignore[reportPrivateUsage] |
| 56 | + entity_id, event, fire_at) |
| 57 | + backend.reset() |
| 58 | + |
| 59 | + # Give the timer thread time to wake; it must observe the new |
| 60 | + # generation and refuse to recreate state in the reset backend. |
| 61 | + time.sleep(0.6) |
| 62 | + assert backend._entities == {} # pyright: ignore[reportPrivateUsage] |
| 63 | + finally: |
| 64 | + backend.stop() |
| 65 | + backend.reset() |
0 commit comments