|
1 | 1 | import re |
2 | 2 |
|
3 | 3 | import pytest |
| 4 | +from statemachine.exceptions import InvalidDefinition |
4 | 5 | from statemachine.exceptions import InvalidStateValue |
5 | 6 |
|
6 | 7 | from statemachine import State |
| 8 | +from statemachine import StateChart |
7 | 9 | from statemachine import StateMachine |
8 | 10 |
|
9 | 11 |
|
@@ -119,3 +121,219 @@ async def test_async_state_should_be_initialized(async_order_control_machine): |
119 | 121 |
|
120 | 122 | await sm.activate_initial_state() |
121 | 123 | assert sm.current_state == sm.waiting_for_payment |
| 124 | + |
| 125 | + |
| 126 | +@pytest.mark.timeout(5) |
| 127 | +async def test_async_error_on_execution_in_condition(): |
| 128 | + """Async engine catches errors in conditions with error_on_execution.""" |
| 129 | + |
| 130 | + class SM(StateChart): |
| 131 | + s1 = State(initial=True) |
| 132 | + s2 = State() |
| 133 | + error_state = State(final=True) |
| 134 | + |
| 135 | + go = s1.to(s2, cond="bad_cond") |
| 136 | + error_execution = s1.to(error_state) |
| 137 | + |
| 138 | + def bad_cond(self, **kwargs): |
| 139 | + raise RuntimeError("Condition boom") |
| 140 | + |
| 141 | + sm = SM() |
| 142 | + sm.send("go") |
| 143 | + assert sm.configuration == {sm.error_state} |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.timeout(5) |
| 147 | +async def test_async_error_on_execution_in_transition(): |
| 148 | + """Async engine catches errors in transition callbacks with error_on_execution.""" |
| 149 | + |
| 150 | + class SM(StateChart): |
| 151 | + s1 = State(initial=True) |
| 152 | + s2 = State() |
| 153 | + error_state = State(final=True) |
| 154 | + |
| 155 | + go = s1.to(s2, on="bad_action") |
| 156 | + error_execution = s1.to(error_state) |
| 157 | + |
| 158 | + def bad_action(self, **kwargs): |
| 159 | + raise RuntimeError("Transition boom") |
| 160 | + |
| 161 | + sm = SM() |
| 162 | + sm.send("go") |
| 163 | + assert sm.configuration == {sm.error_state} |
| 164 | + |
| 165 | + |
| 166 | +@pytest.mark.timeout(5) |
| 167 | +async def test_async_error_on_execution_in_after(): |
| 168 | + """Async engine catches errors in after callbacks with error_on_execution.""" |
| 169 | + |
| 170 | + class SM(StateChart): |
| 171 | + s1 = State(initial=True) |
| 172 | + s2 = State() |
| 173 | + error_state = State(final=True) |
| 174 | + |
| 175 | + go = s1.to(s2) |
| 176 | + error_execution = s2.to(error_state) |
| 177 | + |
| 178 | + def after_go(self, **kwargs): |
| 179 | + raise RuntimeError("After boom") |
| 180 | + |
| 181 | + sm = SM() |
| 182 | + sm.send("go") |
| 183 | + assert sm.configuration == {sm.error_state} |
| 184 | + |
| 185 | + |
| 186 | +@pytest.mark.timeout(5) |
| 187 | +async def test_async_invalid_definition_in_transition_propagates(): |
| 188 | + """InvalidDefinition in async transition propagates.""" |
| 189 | + |
| 190 | + class SM(StateChart): |
| 191 | + s1 = State(initial=True) |
| 192 | + s2 = State() |
| 193 | + |
| 194 | + go = s1.to(s2, on="bad_action") |
| 195 | + |
| 196 | + def bad_action(self, **kwargs): |
| 197 | + raise InvalidDefinition("Bad async") |
| 198 | + |
| 199 | + sm = SM() |
| 200 | + with pytest.raises(InvalidDefinition, match="Bad async"): |
| 201 | + sm.send("go") |
| 202 | + |
| 203 | + |
| 204 | +@pytest.mark.timeout(5) |
| 205 | +async def test_async_invalid_definition_in_after_propagates(): |
| 206 | + """InvalidDefinition in async after callback propagates.""" |
| 207 | + |
| 208 | + class SM(StateChart): |
| 209 | + s1 = State(initial=True) |
| 210 | + s2 = State(final=True) |
| 211 | + |
| 212 | + go = s1.to(s2) |
| 213 | + |
| 214 | + def after_go(self, **kwargs): |
| 215 | + raise InvalidDefinition("Bad async after") |
| 216 | + |
| 217 | + sm = SM() |
| 218 | + with pytest.raises(InvalidDefinition, match="Bad async after"): |
| 219 | + sm.send("go") |
| 220 | + |
| 221 | + |
| 222 | +@pytest.mark.timeout(5) |
| 223 | +async def test_async_runtime_error_in_after_without_error_on_execution(): |
| 224 | + """RuntimeError in async after callback without error_on_execution propagates.""" |
| 225 | + |
| 226 | + class SM(StateMachine): |
| 227 | + s1 = State(initial=True) |
| 228 | + s2 = State(final=True) |
| 229 | + |
| 230 | + go = s1.to(s2) |
| 231 | + |
| 232 | + def after_go(self, **kwargs): |
| 233 | + raise RuntimeError("Async after boom") |
| 234 | + |
| 235 | + sm = SM() |
| 236 | + with pytest.raises(RuntimeError, match="Async after boom"): |
| 237 | + sm.send("go") |
| 238 | + |
| 239 | + |
| 240 | +# --- Actual async engine tests (async callbacks trigger AsyncEngine) --- |
| 241 | +# Note: async engine error_on_execution with async callbacks has a known limitation: |
| 242 | +# _send_error_execution calls sm.send() which returns an unawaited coroutine. |
| 243 | +# The tests below cover the paths that DO work in the async engine. |
| 244 | + |
| 245 | + |
| 246 | +@pytest.mark.timeout(5) |
| 247 | +async def test_async_engine_invalid_definition_in_condition_propagates(): |
| 248 | + """AsyncEngine: InvalidDefinition in async condition always propagates.""" |
| 249 | + |
| 250 | + class SM(StateChart): |
| 251 | + s1 = State(initial=True) |
| 252 | + s2 = State() |
| 253 | + |
| 254 | + go = s1.to(s2, cond="bad_cond") |
| 255 | + |
| 256 | + async def bad_cond(self, **kwargs): |
| 257 | + raise InvalidDefinition("Async bad definition") |
| 258 | + |
| 259 | + sm = SM() |
| 260 | + await sm.activate_initial_state() |
| 261 | + with pytest.raises(InvalidDefinition, match="Async bad definition"): |
| 262 | + await sm.send("go") |
| 263 | + |
| 264 | + |
| 265 | +@pytest.mark.timeout(5) |
| 266 | +async def test_async_engine_invalid_definition_in_transition_propagates(): |
| 267 | + """AsyncEngine: InvalidDefinition in async transition execution always propagates.""" |
| 268 | + |
| 269 | + class SM(StateChart): |
| 270 | + s1 = State(initial=True) |
| 271 | + s2 = State() |
| 272 | + |
| 273 | + go = s1.to(s2, on="bad_action") |
| 274 | + |
| 275 | + async def bad_action(self, **kwargs): |
| 276 | + raise InvalidDefinition("Async bad transition") |
| 277 | + |
| 278 | + sm = SM() |
| 279 | + await sm.activate_initial_state() |
| 280 | + with pytest.raises(InvalidDefinition, match="Async bad transition"): |
| 281 | + await sm.send("go") |
| 282 | + |
| 283 | + |
| 284 | +@pytest.mark.timeout(5) |
| 285 | +async def test_async_engine_invalid_definition_in_after_propagates(): |
| 286 | + """AsyncEngine: InvalidDefinition in async after callback propagates.""" |
| 287 | + |
| 288 | + class SM(StateChart): |
| 289 | + s1 = State(initial=True) |
| 290 | + s2 = State(final=True) |
| 291 | + |
| 292 | + go = s1.to(s2) |
| 293 | + |
| 294 | + async def after_go(self, **kwargs): |
| 295 | + raise InvalidDefinition("Async bad after") |
| 296 | + |
| 297 | + sm = SM() |
| 298 | + await sm.activate_initial_state() |
| 299 | + with pytest.raises(InvalidDefinition, match="Async bad after"): |
| 300 | + await sm.send("go") |
| 301 | + |
| 302 | + |
| 303 | +@pytest.mark.timeout(5) |
| 304 | +async def test_async_engine_runtime_error_in_after_without_error_on_execution_propagates(): |
| 305 | + """AsyncEngine: RuntimeError in async after callback without error_on_execution raises.""" |
| 306 | + |
| 307 | + class SM(StateMachine): |
| 308 | + s1 = State(initial=True) |
| 309 | + s2 = State(final=True) |
| 310 | + |
| 311 | + go = s1.to(s2) |
| 312 | + |
| 313 | + async def after_go(self, **kwargs): |
| 314 | + raise RuntimeError("Async after boom no catch") |
| 315 | + |
| 316 | + sm = SM() |
| 317 | + await sm.activate_initial_state() |
| 318 | + with pytest.raises(RuntimeError, match="Async after boom no catch"): |
| 319 | + await sm.send("go") |
| 320 | + |
| 321 | + |
| 322 | +@pytest.mark.timeout(5) |
| 323 | +async def test_async_engine_start_noop_when_already_initialized(): |
| 324 | + """BaseEngine.start() is a no-op when state machine is already initialized.""" |
| 325 | + |
| 326 | + class SM(StateMachine): |
| 327 | + s1 = State(initial=True) |
| 328 | + s2 = State(final=True) |
| 329 | + |
| 330 | + go = s1.to(s2) |
| 331 | + |
| 332 | + async def on_go(self): |
| 333 | + pass |
| 334 | + |
| 335 | + sm = SM() |
| 336 | + await sm.activate_initial_state() |
| 337 | + assert sm.current_state_value is not None |
| 338 | + sm._engine.start() # Should return early |
| 339 | + assert sm.s1.is_active |
0 commit comments