-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtest_invoke.py
More file actions
1072 lines (776 loc) · 34.8 KB
/
test_invoke.py
File metadata and controls
1072 lines (776 loc) · 34.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for the invoke callback group."""
import threading
import time
from statemachine.invoke import IInvoke
from statemachine.invoke import InvokeContext
from statemachine.invoke import invoke_group
from statemachine import Event
from statemachine import State
from statemachine import StateChart
class TestInvokeSimpleCallable:
"""Simple callable invoke — function runs in background, done.invoke fires."""
async def test_simple_callable_invoke(self, sm_runner):
results = []
class SM(StateChart):
loading = State(initial=True, invoke=lambda: 42)
ready = State(final=True)
done_invoke_loading = loading.to(ready)
def on_enter_ready(self, data=None, **kwargs):
results.append(data)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
assert results == [42]
async def test_invoke_return_value_in_done_event(self, sm_runner):
results = []
class SM(StateChart):
loading = State(initial=True, invoke=lambda: {"key": "value"})
ready = State(final=True)
done_invoke_loading = loading.to(ready)
def on_enter_ready(self, data=None, **kwargs):
results.append(data)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
assert results == [{"key": "value"}]
class TestInvokeNamingConvention:
"""Naming convention — on_invoke_<state>() method is discovered and invoked."""
async def test_naming_convention(self, sm_runner):
invoked = []
class SM(StateChart):
loading = State(initial=True)
ready = State(final=True)
done_invoke_loading = loading.to(ready)
def on_invoke_loading(self, **kwargs):
invoked.append(True)
return "done"
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert invoked == [True]
assert "ready" in sm.configuration_values
class TestInvokeDecorator:
"""Decorator — @state.invoke handler."""
async def test_decorator_invoke(self, sm_runner):
invoked = []
class SM(StateChart):
loading = State(initial=True)
ready = State(final=True)
done_invoke_loading = loading.to(ready)
@loading.invoke
def do_work(self, **kwargs):
invoked.append(True)
return "result"
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert invoked == [True]
assert "ready" in sm.configuration_values
class TestInvokeIInvokeProtocol:
"""IInvoke protocol — class with run(ctx) method."""
async def test_iinvoke_class(self, sm_runner):
"""Pass an IInvoke class — engine instantiates per SM instance."""
results = []
class MyInvoker:
def run(self, ctx: InvokeContext):
results.append(ctx.state_id)
return "invoker_result"
def on_cancel(self):
pass # no-op: only verifying the protocol is satisfied
assert isinstance(MyInvoker(), IInvoke)
class SM(StateChart):
loading = State(initial=True, invoke=MyInvoker)
ready = State(final=True)
done_invoke_loading = loading.to(ready)
def on_enter_ready(self, data=None, **kwargs):
results.append(data)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "loading" in results
assert "invoker_result" in results
assert "ready" in sm.configuration_values
async def test_each_sm_instance_gets_own_handler(self, sm_runner):
"""Each StateChart instance must get a fresh IInvoke instance."""
handler_ids = []
class TrackingInvoker:
def run(self, ctx: InvokeContext):
handler_ids.append(id(self))
return None
class SM(StateChart):
loading = State(initial=True, invoke=TrackingInvoker)
ready = State(final=True)
done_invoke_loading = loading.to(ready)
sm1 = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm1)
sm2 = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm2)
assert len(handler_ids) == 2
assert handler_ids[0] != handler_ids[1], "Each SM must get its own handler instance"
class TestInvokeCancelOnExit:
"""Cancel on exit — ctx.cancelled is set when state is exited."""
async def test_cancel_on_exit_sync(self):
"""Test cancel in sync mode only — uses threading.Event.wait()."""
from tests.conftest import SMRunner
sm_runner = SMRunner(is_async=False)
cancel_observed = []
class SM(StateChart):
loading = State(initial=True)
cancelled_state = State(final=True)
cancel = loading.to(cancelled_state)
def on_invoke_loading(self, ctx=None, **kwargs):
if ctx is None:
return
ctx.cancelled.wait(timeout=5.0)
cancel_observed.append(ctx.cancelled.is_set())
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.05)
await sm_runner.send(sm, "cancel")
await sm_runner.sleep(0.1)
assert cancel_observed == [True]
assert "cancelled_state" in sm.configuration_values
async def test_cancel_on_exit_with_on_cancel(self, sm_runner):
"""Test that on_cancel() is called when state is exited."""
cancel_called = []
class CancelTracker:
def run(self, ctx):
while not ctx.cancelled.is_set():
ctx.cancelled.wait(0.01)
def on_cancel(self):
cancel_called.append(True)
class SM(StateChart):
loading = State(initial=True, invoke=CancelTracker)
cancelled_state = State(final=True)
cancel = loading.to(cancelled_state)
sm = await sm_runner.start(SM)
# Give the invoke handler time to start in its background thread
await sm_runner.sleep(0.15)
await sm_runner.send(sm, "cancel")
await sm_runner.sleep(0.15)
assert cancel_called == [True]
assert "cancelled_state" in sm.configuration_values
class TestInvokeErrorHandling:
"""Error in invoker → error.execution event."""
async def test_error_in_invoke(self, sm_runner):
errors = []
class SM(StateChart):
loading = State(initial=True, invoke=lambda: 1 / 0)
error_state = State(final=True)
error_execution = loading.to(error_state)
def on_enter_error_state(self, **kwargs):
errors.append(True)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert errors == [True]
assert "error_state" in sm.configuration_values
class TestInvokeMultiple:
"""Multiple invokes per state — all run concurrently."""
async def test_multiple_invokes(self, sm_runner):
results = []
lock = threading.Lock()
def task_a():
with lock:
results.append("a")
return "a"
def task_b():
with lock:
results.append("b")
return "b"
class SM(StateChart):
loading = State(initial=True, invoke=invoke_group(task_a, task_b))
ready = State(final=True)
done_invoke_loading = loading.to(ready)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.2)
await sm_runner.processing_loop(sm)
assert sorted(results) == ["a", "b"]
class TestInvokeStateChartChild:
"""StateChart as invoker — child machine runs, completion fires done event."""
async def test_statechart_invoker(self, sm_runner):
class ChildMachine(StateChart):
start = State(initial=True)
end = State(final=True)
go = start.to(end)
def on_enter_start(self, **kwargs):
self.send("go")
class SM(StateChart):
loading = State(initial=True, invoke=ChildMachine)
ready = State(final=True)
done_invoke_loading = loading.to(ready)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
class TestDoneInvokeTransition:
"""done_invoke_<state> transition — naming convention works."""
async def test_done_invoke_transition(self, sm_runner):
class SM(StateChart):
loading = State(initial=True, invoke=lambda: "hello")
ready = State(final=True)
done_invoke_loading = loading.to(ready)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
class TestDoneInvokeEventFormat:
"""done.invoke event name must be done.invoke.<state_id>.<platform_id> (no duplication)."""
async def test_done_invoke_event_has_no_duplicate_state_id(self, sm_runner):
received_events = []
class SM(StateChart):
loading = State(initial=True, invoke=lambda: "ok")
ready = State(final=True)
done_invoke_loading = loading.to(ready)
def on_enter_ready(self, event=None, **kwargs):
if event is not None:
received_events.append(str(event))
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert len(received_events) == 1
event_name = received_events[0]
# Must be "done.invoke.loading.<hex>" — NOT "done.invoke.loading.loading.<hex>"
assert event_name.startswith("done.invoke.loading.")
parts = event_name.split(".")
# ["done", "invoke", "loading", "<hex>"] — exactly 4 parts
assert len(parts) == 4, f"Expected 4 parts, got {parts}"
class TestInvokeGroup:
"""invoke_group() — runs multiple callables concurrently, returns list of results."""
async def test_group_returns_ordered_results(self, sm_runner):
"""Results are returned in the same order as the input callables."""
results = []
def slow():
time.sleep(0.05)
return "slow"
def fast():
return "fast"
class SM(StateChart):
loading = State(initial=True, invoke=invoke_group(slow, fast))
ready = State(final=True)
done_invoke_loading = loading.to(ready)
def on_enter_ready(self, data=None, **kwargs):
results.append(data)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.2)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
assert results == [["slow", "fast"]]
async def test_group_with_file_io(self, sm_runner, tmp_path):
"""Real I/O: read two files concurrently and get both results."""
file_a = tmp_path / "a.txt"
file_b = tmp_path / "b.txt"
file_a.write_text("hello")
file_b.write_text("world")
results = []
class SM(StateChart):
loading = State(
initial=True,
invoke=invoke_group(
lambda: file_a.read_text(),
lambda: file_b.read_text(),
),
)
ready = State(final=True)
done_invoke_loading = loading.to(ready)
def on_enter_ready(self, data=None, **kwargs):
results.append(data)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.2)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
assert results == [["hello", "world"]]
async def test_group_error_cancels_remaining(self, sm_runner):
"""If one callable raises, error.execution is sent."""
errors = []
def ok():
time.sleep(0.1)
return "ok"
def fail():
raise ValueError("boom")
class SM(StateChart):
loading = State(initial=True, invoke=invoke_group(ok, fail))
error_state = State(final=True)
error_execution = loading.to(error_state)
def on_enter_error_state(self, **kwargs):
errors.append(True)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.3)
await sm_runner.processing_loop(sm)
assert "error_state" in sm.configuration_values
assert errors == [True]
async def test_group_cancel_on_exit(self, sm_runner):
"""Cancellation propagates: exiting state stops the group."""
cancel_flag = threading.Event()
def slow_task():
# Use interruptible wait so thread can exit promptly on cancellation.
cancel_flag.wait(timeout=5.0)
return "should not complete"
class SM(StateChart):
loading = State(initial=True, invoke=invoke_group(slow_task))
stopped = State(final=True)
cancel = loading.to(stopped)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.05)
await sm_runner.send(sm, "cancel")
cancel_flag.set() # Unblock the slow_task thread
await sm_runner.sleep(0.1)
assert "stopped" in sm.configuration_values
async def test_group_single_callable(self, sm_runner):
"""Edge case: group with a single callable still returns a list."""
results = []
class SM(StateChart):
loading = State(initial=True, invoke=invoke_group(lambda: 42))
ready = State(final=True)
done_invoke_loading = loading.to(ready)
def on_enter_ready(self, data=None, **kwargs):
results.append(data)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.2)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
assert results == [[42]]
async def test_each_sm_instance_gets_own_group(self, sm_runner):
"""Each SM instance must get its own InvokeGroup — no shared state."""
all_results = []
counter = {"value": 0}
lock = threading.Lock()
def counting_task():
with lock:
counter["value"] += 1
return counter["value"]
class SM(StateChart):
loading = State(initial=True, invoke=invoke_group(counting_task))
ready = State(final=True)
done_invoke_loading = loading.to(ready)
def on_enter_ready(self, data=None, **kwargs):
all_results.append(data)
sm1 = await sm_runner.start(SM)
await sm_runner.sleep(0.2)
await sm_runner.processing_loop(sm1)
sm2 = await sm_runner.start(SM)
await sm_runner.sleep(0.2)
await sm_runner.processing_loop(sm2)
assert len(all_results) == 2
assert all_results[0] == [1]
assert all_results[1] == [2]
class TestInvokeEventKwargs:
"""Event kwargs from send() are forwarded to invoke handlers."""
async def test_plain_callable_receives_event_kwargs(self, sm_runner):
"""Plain callable invoke handler receives event kwargs via SignatureAdapter."""
received = []
class SM(StateChart):
idle = State(initial=True)
loading = State()
ready = State(final=True)
start = idle.to(loading)
done_invoke_loading = loading.to(ready)
def on_invoke_loading(self, file_name=None, **kwargs):
received.append(file_name)
return f"loaded:{file_name}"
def on_enter_ready(self, data=None, **kwargs):
received.append(data)
sm = await sm_runner.start(SM)
await sm_runner.send(sm, "start", file_name="config.json")
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
assert received == ["config.json", "loaded:config.json"]
async def test_iinvoke_handler_receives_event_kwargs_via_ctx(self, sm_runner):
"""IInvoke handler receives event kwargs via ctx.kwargs."""
received = []
class FileLoader:
def run(self, ctx: InvokeContext):
received.append(ctx.kwargs.get("file_name"))
return f"loaded:{ctx.kwargs['file_name']}"
class SM(StateChart):
idle = State(initial=True)
loading = State(invoke=FileLoader)
ready = State(final=True)
start = idle.to(loading)
done_invoke_loading = loading.to(ready)
def on_enter_ready(self, data=None, **kwargs):
received.append(data)
sm = await sm_runner.start(SM)
await sm_runner.send(sm, "start", file_name="data.csv")
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
assert received == ["data.csv", "loaded:data.csv"]
async def test_initial_state_invoke_has_empty_kwargs(self, sm_runner):
"""Invoke on initial state gets empty kwargs (no triggering event)."""
class SM(StateChart):
loading = State(initial=True, invoke=lambda: 42)
ready = State(final=True)
done_invoke_loading = loading.to(ready)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
class TestInvokeNotTriggeredOnNonInvokeState:
"""States without invoke handlers should not be affected."""
async def test_no_invoke_on_plain_state(self, sm_runner):
class SM(StateChart):
idle = State(initial=True)
active = State()
done = State(final=True)
go = idle.to(active)
finish = active.to(done)
sm = await sm_runner.start(SM)
await sm_runner.send(sm, "go")
assert "active" in sm.configuration_values
await sm_runner.send(sm, "finish")
assert "done" in sm.configuration_values
class TestInvokeManagerCancelAll:
"""InvokeManager.cancel_all() cancels every active invocation."""
async def test_cancel_all(self, sm_runner):
class SlowHandler:
def run(self, ctx):
ctx.cancelled.wait(timeout=5.0)
class SM(StateChart):
loading = State(initial=True, invoke=SlowHandler)
stopped = State(final=True)
cancel = loading.to(stopped)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
sm._engine._invoke_manager.cancel_all()
await sm_runner.sleep(0.15)
# All invocations should be terminated
for inv in sm._engine._invoke_manager._active.values():
assert inv.terminated
class TestInvokeCancelAlreadyTerminated:
"""Cancelling an already-terminated invocation is a no-op."""
async def test_cancel_terminated_invocation(self, sm_runner):
class SM(StateChart):
loading = State(initial=True, invoke=lambda: 42)
ready = State(final=True)
done_invoke_loading = loading.to(ready)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
# All invocations should be terminated by now
manager = sm._engine._invoke_manager
for inv in manager._active.values():
assert inv.terminated
# Calling cancel on terminated invocations should be a safe no-op
for inv_id in list(manager._active.keys()):
manager._cancel(inv_id)
class TestInvokeOnCancelException:
"""Exception in on_cancel() is caught and logged, not propagated."""
async def test_on_cancel_exception_is_suppressed(self, sm_runner):
class BadCancelHandler:
def run(self, ctx):
ctx.cancelled.wait(timeout=5.0)
def on_cancel(self):
raise RuntimeError("on_cancel exploded")
class SM(StateChart):
loading = State(initial=True, invoke=BadCancelHandler)
stopped = State(final=True)
cancel = loading.to(stopped)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
# This should NOT raise even though on_cancel() raises
await sm_runner.send(sm, "cancel")
await sm_runner.sleep(0.15)
assert "stopped" in sm.configuration_values
class TestStateChartInvokerOnCancel:
"""StateChartInvoker.on_cancel() cleans up the child reference."""
def test_on_cancel_clears_child(self):
from statemachine.invoke import StateChartInvoker
class ChildMachine(StateChart):
start = State(initial=True, final=True)
invoker = StateChartInvoker(ChildMachine)
ctx = InvokeContext(
invokeid="test.123",
state_id="test",
send=lambda *a, **kw: None,
machine=None,
)
invoker.run(ctx)
assert invoker._child is not None
invoker.on_cancel()
assert invoker._child is None
class TestNormalizeInvokeCallbacks:
"""normalize_invoke_callbacks handles edge cases."""
def test_string_passes_through(self):
from statemachine.invoke import normalize_invoke_callbacks
result = normalize_invoke_callbacks("some_method_name")
assert result == ["some_method_name"]
def test_already_wrapped_passes_through(self):
from statemachine.invoke import _InvokeCallableWrapper
from statemachine.invoke import normalize_invoke_callbacks
class MyHandler:
def run(self, ctx):
pass
wrapper = _InvokeCallableWrapper(MyHandler)
result = normalize_invoke_callbacks(wrapper)
assert len(result) == 1
assert result[0] is wrapper
def test_iinvoke_class_with_run_method(self):
"""IInvoke-compatible class gets wrapped."""
from statemachine.invoke import _InvokeCallableWrapper
from statemachine.invoke import normalize_invoke_callbacks
class CustomHandler:
def run(self, ctx):
return "result"
# CustomHandler satisfies IInvoke protocol (has run method)
assert isinstance(CustomHandler(), IInvoke)
result = normalize_invoke_callbacks(CustomHandler)
assert len(result) == 1
assert isinstance(result[0], _InvokeCallableWrapper)
def test_plain_callable_passes_through(self):
from statemachine.invoke import _InvokeCallableWrapper
from statemachine.invoke import normalize_invoke_callbacks
def my_func():
return 42
result = normalize_invoke_callbacks(my_func)
assert len(result) == 1
assert result[0] is my_func
assert not isinstance(result[0], _InvokeCallableWrapper)
def test_non_invoke_class_passes_through(self):
"""A class without run() (not IInvoke, not StateChart) passes through unwrapped."""
from statemachine.invoke import _InvokeCallableWrapper
from statemachine.invoke import normalize_invoke_callbacks
class PlainClass:
pass
result = normalize_invoke_callbacks(PlainClass)
assert len(result) == 1
assert result[0] is PlainClass
assert not isinstance(result[0], _InvokeCallableWrapper)
class TestResolveHandler:
"""InvokeManager._resolve_handler edge cases."""
def test_bare_iinvoke_instance(self):
from statemachine.invoke import InvokeManager
class MyHandler:
def run(self, ctx):
return "result"
handler = MyHandler()
assert isinstance(handler, IInvoke)
resolved = InvokeManager._resolve_handler(handler)
assert resolved is handler
def test_bare_statechart_class(self):
from statemachine.invoke import InvokeManager
from statemachine.invoke import StateChartInvoker
class ChildMachine(StateChart):
start = State(initial=True, final=True)
resolved = InvokeManager._resolve_handler(ChildMachine)
assert isinstance(resolved, StateChartInvoker)
def test_plain_callable_returns_none(self):
from statemachine.invoke import InvokeManager
def my_func():
return 42
assert InvokeManager._resolve_handler(my_func) is None
class TestInvokeCallableWrapperOnCancel:
"""_InvokeCallableWrapper.on_cancel() edge cases."""
def test_on_cancel_non_class_instance_with_on_cancel(self):
"""Non-class handler (already instantiated) delegates on_cancel."""
from statemachine.invoke import _InvokeCallableWrapper
cancel_called = []
class MyHandler:
def run(self, ctx):
return "result"
def on_cancel(self):
cancel_called.append(True)
handler = MyHandler()
wrapper = _InvokeCallableWrapper(handler)
# _instance is None, _is_class is False → falls through to _invoke_handler
wrapper.on_cancel()
assert cancel_called == [True]
def test_on_cancel_class_not_yet_instantiated(self):
"""Class handler not yet instantiated — on_cancel is a no-op."""
from statemachine.invoke import _InvokeCallableWrapper
class MyHandler:
def run(self, ctx):
return "result"
def on_cancel(self):
raise RuntimeError("should not be called")
wrapper = _InvokeCallableWrapper(MyHandler)
# _instance is None, _is_class is True → early return
wrapper.on_cancel() # should not raise
def test_callable_wrapper_call_returns_handler(self):
"""__call__ returns the original handler (used by callback system for resolution)."""
from statemachine.invoke import _InvokeCallableWrapper
class MyHandler:
def run(self, ctx):
return "result"
wrapper = _InvokeCallableWrapper(MyHandler)
assert wrapper() is MyHandler
class TestInvokeGroupOnCancelBeforeRun:
"""InvokeGroup.on_cancel() before run() is a safe no-op."""
def test_on_cancel_before_run(self):
group = invoke_group(lambda: 1)
# on_cancel before run — executor is None, no futures
group.on_cancel()
class TestDoneInvokeEventFactory:
"""done_invoke_ prefix works with both TransitionList and Event."""
async def test_done_invoke_with_event_object(self, sm_runner):
"""Event() object with done_invoke_ prefix should match done.invoke events."""
class SM(StateChart):
loading = State(initial=True, invoke=lambda: "result")
ready = State(final=True)
done_invoke_loading = Event(loading.to(ready))
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.15)
await sm_runner.processing_loop(sm)
assert "ready" in sm.configuration_values
class TestVisitNoCallbacks:
"""visit/async_visit with no registered callbacks is a no-op."""
def test_visit_missing_key(self):
from statemachine.callbacks import CallbacksRegistry
registry = CallbacksRegistry()
# Should not raise — just returns
registry.visit("nonexistent_key", lambda cb, **kw: None)
async def test_async_visit_missing_key(self):
from statemachine.callbacks import CallbacksRegistry
registry = CallbacksRegistry()
await registry.async_visit("nonexistent_key", lambda cb, **kw: None)
class TestAsyncVisitAwaitable:
"""async_visit should await the visitor_fn result when it is awaitable."""
async def test_async_visitor_fn_is_awaited(self):
from statemachine.callbacks import CallbackGroup
from statemachine.callbacks import CallbacksExecutor
from statemachine.callbacks import CallbackSpec
visited = []
async def async_visitor(callback, **kwargs):
visited.append(str(callback))
executor = CallbacksExecutor()
spec = CallbackSpec("dummy", group=CallbackGroup.INVOKE, is_convention=True)
executor.add("test_key", spec, lambda: lambda **kw: True)
await executor.async_visit(async_visitor)
assert visited == ["dummy"]
class TestIInvokeProtocolRun:
"""IInvoke.run() protocol method can be called on a concrete implementation."""
def test_protocol_run_is_callable(self):
"""Verify that calling run() on a concrete IInvoke instance works."""
class ConcreteInvoker:
def run(self, ctx):
return "concrete_result"
invoker: IInvoke = ConcreteInvoker()
result = invoker.run(None)
assert result == "concrete_result"
class TestSpawnPendingAsyncEmpty:
"""spawn_pending_async with nothing pending is a no-op."""
async def test_spawn_pending_async_no_pending(self, sm_runner):
class SM(StateChart):
idle = State(initial=True)
active = State(final=True)
go = idle.to(active)
sm = await sm_runner.start(SM)
# Directly call spawn_pending_async with empty pending list
await sm._engine._invoke_manager.spawn_pending_async()
class TestInvokeAsyncCancelledDuringExecution:
"""Async handler completes or errors after state was already exited."""
async def test_success_after_cancel(self):
"""Handler returns successfully but ctx.cancelled is already set."""
from tests.conftest import SMRunner
class SM(StateChart):
loading = State(initial=True)
stopped = State(final=True)
cancel = loading.to(stopped)
def on_invoke_loading(self, ctx=None, **kwargs):
if ctx is None:
return
# Simulate: cancelled is set during execution but we still return
ctx.cancelled.set()
return "should_be_ignored"
sm_runner = SMRunner(is_async=True)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.2)
await sm_runner.processing_loop(sm)
# The done.invoke event should NOT have been sent (cancelled)
assert "loading" in sm.configuration_values
async def test_error_after_cancel(self):
"""Handler raises but ctx.cancelled is already set — error is swallowed."""
from tests.conftest import SMRunner
class SM(StateChart):
loading = State(initial=True)
error_state = State(final=True)
error_execution = loading.to(error_state)
def on_invoke_loading(self, ctx=None, **kwargs):
if ctx is None:
return
# Simulate: cancelled during execution, then error
ctx.cancelled.set()
raise ValueError("should be ignored")
sm_runner = SMRunner(is_async=True)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.2)
await sm_runner.processing_loop(sm)
# The error.execution event should NOT have been sent (cancelled)
assert "loading" in sm.configuration_values
class TestSyncInvokeErrorAfterCancel:
"""Sync handler errors after state was already exited."""
async def test_sync_error_after_cancel(self):
"""Sync handler raises but ctx.cancelled is set — error.execution not sent."""
from tests.conftest import SMRunner
class SM(StateChart):
loading = State(initial=True)
error_state = State(final=True)
error_execution = loading.to(error_state)
def on_invoke_loading(self, ctx=None, **kwargs):
if ctx is None:
return
ctx.cancelled.set()
raise ValueError("should be ignored")
sm_runner = SMRunner(is_async=False)
sm = await sm_runner.start(SM)
await sm_runner.sleep(0.2)
await sm_runner.processing_loop(sm)
assert "loading" in sm.configuration_values
class TestInvokeManagerUnit:
"""Unit tests for InvokeManager methods not exercised by integration tests."""
def test_send_to_child_not_found(self):
"""send_to_child returns False when invokeid is not in _active."""
from unittest.mock import Mock