|
| 1 | +"""Tests for the timeout contrib module.""" |
| 2 | + |
| 3 | +import threading |
| 4 | + |
| 5 | +import pytest |
| 6 | +from statemachine.contrib.timeout import _Timeout |
| 7 | +from statemachine.contrib.timeout import timeout |
| 8 | + |
| 9 | +from statemachine import State |
| 10 | +from statemachine import StateChart |
| 11 | + |
| 12 | + |
| 13 | +class TestTimeoutValidation: |
| 14 | + def test_positive_duration(self): |
| 15 | + t = timeout(5) |
| 16 | + assert isinstance(t, _Timeout) |
| 17 | + assert t.duration == 5 |
| 18 | + |
| 19 | + def test_zero_duration_raises(self): |
| 20 | + with pytest.raises(ValueError, match="must be positive"): |
| 21 | + timeout(0) |
| 22 | + |
| 23 | + def test_negative_duration_raises(self): |
| 24 | + with pytest.raises(ValueError, match="must be positive"): |
| 25 | + timeout(-1) |
| 26 | + |
| 27 | + def test_repr_without_on(self): |
| 28 | + assert repr(timeout(5)) == "timeout(5)" |
| 29 | + |
| 30 | + def test_repr_with_on(self): |
| 31 | + assert repr(timeout(3.5, on="expired")) == "timeout(3.5, on='expired')" |
| 32 | + |
| 33 | + |
| 34 | +class TestTimeoutBasic: |
| 35 | + """Timeout fires done.invoke.<state> when no custom event is specified.""" |
| 36 | + |
| 37 | + async def test_timeout_fires_done_invoke(self, sm_runner): |
| 38 | + class SM(StateChart): |
| 39 | + waiting = State(initial=True, invoke=timeout(0.05)) |
| 40 | + done = State(final=True) |
| 41 | + done_invoke_waiting = waiting.to(done) |
| 42 | + |
| 43 | + sm = await sm_runner.start(SM) |
| 44 | + await sm_runner.sleep(0.15) |
| 45 | + await sm_runner.processing_loop(sm) |
| 46 | + |
| 47 | + assert "done" in sm.configuration_values |
| 48 | + |
| 49 | + async def test_timeout_cancelled_on_early_exit(self, sm_runner): |
| 50 | + """If the machine transitions out before the timeout, nothing fires.""" |
| 51 | + |
| 52 | + class SM(StateChart): |
| 53 | + waiting = State(initial=True, invoke=timeout(10)) |
| 54 | + other = State(final=True) |
| 55 | + go = waiting.to(other) |
| 56 | + # No done_invoke_waiting — would fail if timeout fired unexpectedly |
| 57 | + done_invoke_waiting = waiting.to(waiting) |
| 58 | + |
| 59 | + sm = await sm_runner.start(SM) |
| 60 | + await sm_runner.send(sm, "go") |
| 61 | + |
| 62 | + assert "other" in sm.configuration_values |
| 63 | + |
| 64 | + |
| 65 | +class TestTimeoutCustomEvent: |
| 66 | + """Timeout fires a custom event via the `on` parameter.""" |
| 67 | + |
| 68 | + async def test_custom_event_fires(self, sm_runner): |
| 69 | + class SM(StateChart): |
| 70 | + waiting = State(initial=True, invoke=timeout(0.05, on="expired")) |
| 71 | + timed_out = State(final=True) |
| 72 | + expired = waiting.to(timed_out) |
| 73 | + |
| 74 | + sm = await sm_runner.start(SM) |
| 75 | + await sm_runner.sleep(0.15) |
| 76 | + await sm_runner.processing_loop(sm) |
| 77 | + |
| 78 | + assert "timed_out" in sm.configuration_values |
| 79 | + |
| 80 | + async def test_custom_event_cancelled_on_early_exit(self, sm_runner): |
| 81 | + class SM(StateChart): |
| 82 | + waiting = State(initial=True, invoke=timeout(10, on="expired")) |
| 83 | + other = State(final=True) |
| 84 | + go = waiting.to(other) |
| 85 | + expired = waiting.to(waiting) |
| 86 | + |
| 87 | + sm = await sm_runner.start(SM) |
| 88 | + await sm_runner.send(sm, "go") |
| 89 | + |
| 90 | + assert "other" in sm.configuration_values |
| 91 | + |
| 92 | + |
| 93 | +class TestTimeoutComposition: |
| 94 | + """Timeout combined with other invoke handlers — first to complete wins.""" |
| 95 | + |
| 96 | + async def test_invoke_completes_before_timeout(self, sm_runner): |
| 97 | + """A fast invoke handler transitions out, cancelling the timeout.""" |
| 98 | + |
| 99 | + def fast_handler(): |
| 100 | + return "fast_result" |
| 101 | + |
| 102 | + class SM(StateChart): |
| 103 | + loading = State(initial=True, invoke=[fast_handler, timeout(10, on="too_slow")]) |
| 104 | + ready = State(final=True) |
| 105 | + stuck = State(final=True) |
| 106 | + done_invoke_loading = loading.to(ready) |
| 107 | + too_slow = loading.to(stuck) |
| 108 | + |
| 109 | + sm = await sm_runner.start(SM) |
| 110 | + await sm_runner.sleep(0.15) |
| 111 | + await sm_runner.processing_loop(sm) |
| 112 | + |
| 113 | + assert "ready" in sm.configuration_values |
| 114 | + |
| 115 | + async def test_timeout_fires_before_slow_invoke(self, sm_runner): |
| 116 | + """Timeout fires while a slow invoke handler is still running.""" |
| 117 | + handler_cancelled = threading.Event() |
| 118 | + |
| 119 | + class SlowHandler: |
| 120 | + def run(self, ctx): |
| 121 | + # Wait until cancelled (state exit) — simulates long-running work |
| 122 | + ctx.cancelled.wait() |
| 123 | + handler_cancelled.set() |
| 124 | + |
| 125 | + class SM(StateChart): |
| 126 | + loading = State(initial=True, invoke=[SlowHandler(), timeout(0.05, on="too_slow")]) |
| 127 | + ready = State(final=True) |
| 128 | + stuck = State(final=True) |
| 129 | + done_invoke_loading = loading.to(ready) |
| 130 | + too_slow = loading.to(stuck) |
| 131 | + |
| 132 | + sm = await sm_runner.start(SM) |
| 133 | + await sm_runner.sleep(0.15) |
| 134 | + await sm_runner.processing_loop(sm) |
| 135 | + |
| 136 | + assert "stuck" in sm.configuration_values |
| 137 | + # The slow handler should have been cancelled when the state exited |
| 138 | + handler_cancelled.wait(timeout=2) |
| 139 | + assert handler_cancelled.is_set() |
0 commit comments