fusee 1.0.0

Embeddable durable execution at the speed of the disk: workflows whose steps are durable within microseconds of fdatasync
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
//! The workflow features (docs/features.md), end to end on the embedded
//! engine with real files.

use fusee::{
    Batch, ChildOptions, Config, Ctx, Debounce, Engine, Error, IdPolicy, LimitSpec, OnFinished,
    OnRunning, Overlap, ParentClose, RetryPolicy, ScheduleSpec, StartOptions, StepOptions,
};
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::{Duration, Instant};

fn tmp(name: &str) -> std::path::PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "fusee-feat-{name}-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    let _ = std::fs::remove_dir_all(&dir);
    dir
}

fn cfg(dir: &std::path::Path) -> Config {
    let mut c = Config::local(dir).shards(2);
    c.local.flush = fusee::FlushMethod::Fsync;
    c.keep_history = true;
    c
}

async fn eventually(what: &str, mut f: impl AsyncFnMut() -> bool) {
    let deadline = Instant::now() + Duration::from_secs(10);
    while !f().await {
        assert!(Instant::now() < deadline, "timed out waiting for {what}");
        tokio::time::sleep(Duration::from_millis(5)).await;
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn reset_reruns_from_the_named_step() {
    let dir = tmp("reset");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let calls = Arc::new(AtomicU32::new(0));
    let c = calls.clone();
    let wf = engine.register("pay", move |ctx: Ctx, n: u32| {
        let c = c.clone();
        async move {
            let quote: u32 = ctx
                .step("quote", || async move { Ok::<_, Error>(n) })
                .await?;
            let c2 = c.clone();
            let key: String = ctx
                .step("charge", || {
                    let key = ctx.key();
                    let c2 = c2.clone();
                    async move {
                        c2.fetch_add(1, Ordering::SeqCst);
                        Ok::<_, Error>(key)
                    }
                })
                .await?;
            let go: Option<u32> = ctx.wait_for("go").await?;
            Ok(format!("{quote}/{key}/{}", go.unwrap_or(0)))
        }
    });
    let run = engine.start(&wf, "pay-1", &7).await.unwrap();
    eventually("the wait", async || {
        matches!(
            run.status().await.ok().map(|s| s.status).as_deref(),
            Some("suspended" | "parked")
        )
    })
    .await;
    run.signal("go", &1).await.unwrap();
    let first = run.result().await.unwrap();
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    run.reset("charge").await.unwrap();
    // The new run re-runs charge with a new idempotency key, and gets the
    // signal again (reapplied).
    let second = run.result().await.unwrap();
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    assert_ne!(first, second);
    assert!(
        second.starts_with("7/") && second.ends_with("/1"),
        "{second}"
    );
    let s = run.status().await.unwrap();
    assert_eq!(s.status, "ok");
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn reset_of_a_running_run_hands_over() {
    let dir = tmp("reset-running");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let wf = engine.register("w", |ctx: Ctx, n: u32| async move {
        let a: u32 = ctx.step("a", || async move { Ok::<_, Error>(n) }).await?;
        let b: u32 = ctx
            .step("b", || async move { Ok::<_, Error>(a + 1) })
            .await?;
        let go: Option<u32> = ctx.wait_for("go").await?;
        Ok(a + b + go.unwrap_or(0))
    });
    let run = engine.start(&wf, "r-1", &1).await.unwrap();
    eventually("the wait", async || {
        matches!(
            run.status().await.ok().map(|s| s.status).as_deref(),
            Some("suspended" | "parked")
        )
    })
    .await;
    run.reset("b").await.unwrap();
    let s = run.status().await.unwrap();
    assert_ne!(s.status, "reset", "{s:?}");
    run.signal("go", &10).await.unwrap();
    assert_eq!(run.result().await.unwrap(), 13);
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn queries_and_updates() {
    let dir = tmp("updates");
    let calls = Arc::new(AtomicU32::new(0));
    let register = |engine: &Engine, calls: Arc<AtomicU32>| {
        engine.register("cart", move |ctx: Ctx, _: ()| {
            let calls = calls.clone();
            async move {
                let items = std::rc::Rc::new(std::cell::RefCell::new(Vec::<String>::new()));
                let i2 = items.clone();
                ctx.on_query("items", move |_: ()| i2.borrow().clone());
                let (i3, c2) = (items.clone(), ctx.clone());
                ctx.on_update(
                    "add",
                    |item: &String| {
                        if item.is_empty() {
                            Err("empty item".into())
                        } else {
                            Ok(())
                        }
                    },
                    move |item: String| {
                        let (items, ctx, calls) = (i3.clone(), c2.clone(), calls.clone());
                        async move {
                            let price: u32 = ctx
                                .step("price", || {
                                    let calls = calls.clone();
                                    let n = item.len() as u32;
                                    async move {
                                        calls.fetch_add(1, Ordering::SeqCst);
                                        Ok::<_, Error>(n * 10)
                                    }
                                })
                                .await?;
                            items.borrow_mut().push(item);
                            Ok(price)
                        }
                    },
                );
                let _: Option<()> = ctx.wait_for("checkout").await?;
                Ok(items.borrow().clone())
            }
        })
    };
    {
        let engine = Engine::open(cfg(&dir)).await.unwrap();
        let wf = register(&engine, calls.clone());
        let run = engine.start(&wf, "cart-1", &()).await.unwrap();
        let price: u32 = run.update("add", &"apple").id("u-1").await.unwrap();
        assert_eq!(price, 50);
        // A retried call with the same ID gets the recorded result.
        let again: u32 = run.update("add", &"apple").id("u-1").await.unwrap();
        assert_eq!(again, 50);
        assert_eq!(calls.load(Ordering::SeqCst), 1);
        // The validator rejects; nothing is recorded.
        let err = run.update::<_, u32>("add", &"").await.unwrap_err();
        assert!(
            matches!(err, Error::Rejected(ref m) if m.contains("empty")),
            "{err}"
        );
        let items: Vec<String> = run.query("items", &()).await.unwrap();
        assert_eq!(items, vec!["apple"]);
        let err = run.query::<_, ()>("nope", &()).await.unwrap_err();
        assert!(matches!(err, Error::Rejected(_)), "{err}");
        let j = run.describe().await.unwrap().journal;
        let updates = j
            .iter()
            .filter(|r| matches!(r, fusee::core::Record::Update { .. }))
            .count();
        assert_eq!(updates, 1);
        engine.shutdown().await.unwrap();
    }
    {
        // After a restart the update's dedup and the run's state survive.
        let engine = Engine::open(cfg(&dir)).await.unwrap();
        let _wf = register(&engine, calls.clone());
        let run = engine.run::<Vec<String>>("cart-1");
        let again: u32 = run.update("add", &"apple").id("u-1").await.unwrap();
        assert_eq!(again, 50);
        let p: u32 = run.update("add", &"pear").await.unwrap();
        assert_eq!(p, 40);
        let items: Vec<String> = run.query("items", &()).await.unwrap();
        assert_eq!(items, vec!["apple", "pear"]);
        run.signal("checkout", &()).await.unwrap();
        assert_eq!(run.result().await.unwrap(), vec!["apple", "pear"]);
        assert_eq!(calls.load(Ordering::SeqCst), 2);
        engine.shutdown().await.unwrap();
    }
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn update_with_start_is_one_record() {
    let dir = tmp("uws");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let wf = engine.register("counter", |ctx: Ctx, start: u32| async move {
        let n = std::rc::Rc::new(std::cell::Cell::new(start));
        let n2 = n.clone();
        ctx.on_update(
            "inc",
            |_: &u32| Ok(()),
            move |by: u32| {
                let n = n2.clone();
                async move {
                    n.set(n.get() + by);
                    Ok(n.get())
                }
            },
        );
        let _: Option<()> = ctx.wait_for("stop").await?;
        Ok(n.get())
    });
    let (run, v): (_, u32) = engine
        .update_with_start(&wf, "c-1", &10, "inc", &5)
        .await
        .unwrap();
    assert_eq!(v, 15);
    let j = run.describe().await.unwrap().journal;
    assert!(
        matches!(&j[0], fusee::core::Record::Start { update: Some(u), .. } if &*u.name == "inc"),
        "{j:?}"
    );
    // Running already: the update goes to the running workflow.
    let (_, v): (_, u32) = engine
        .update_with_start(&wf, "c-1", &99, "inc", &1)
        .await
        .unwrap();
    assert_eq!(v, 16);
    run.signal("stop", &()).await.unwrap();
    assert_eq!(run.result().await.unwrap(), 16);
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn heartbeats_resume_after_restart() {
    let dir = tmp("heartbeat");
    let started_from = Arc::new(std::sync::Mutex::new(Vec::<u32>::new()));
    let register = |engine: &Engine, from: Arc<std::sync::Mutex<Vec<u32>>>| {
        engine.register("export", move |ctx: Ctx, _: ()| {
            let from = from.clone();
            async move {
                let c = ctx.clone();
                let opts = StepOptions::default()
                    .retry(RetryPolicy::exponential(
                        3,
                        Duration::from_millis(1),
                        Duration::from_millis(5),
                    ))
                    .heartbeat_timeout(Duration::from_millis(200));
                let pages: u32 = ctx
                    .step("export", move || {
                        let (c, from) = (c.clone(), from.clone());
                        async move {
                            let start = c.progress::<u32>()?.unwrap_or(0);
                            from.lock().unwrap().push(start);
                            for p in start..6 {
                                c.heartbeat(&(p + 1))?;
                                tokio::time::sleep(Duration::from_millis(15)).await;
                            }
                            Ok::<_, Error>(6u32)
                        }
                    })
                    .options(opts)
                    .await?;
                Ok(pages)
            }
        })
    };
    {
        let engine = Engine::open(cfg(&dir)).await.unwrap();
        let wf = register(&engine, started_from.clone());
        engine.start(&wf, "exp-1", &()).await.unwrap();
        tokio::time::sleep(Duration::from_millis(50)).await;
        // The process stops in the middle of the step.
        engine.shutdown().await.unwrap();
    }
    {
        let engine = Engine::open(cfg(&dir)).await.unwrap();
        let _wf = register(&engine, started_from.clone());
        let r: u32 = engine.run("exp-1").result().await.unwrap();
        assert_eq!(r, 6);
        let from = started_from.lock().unwrap().clone();
        assert_eq!(from[0], 0);
        assert!(
            from[1] > 0,
            "the retry resumed from durable progress: {from:?}"
        );
        engine.shutdown().await.unwrap();
    }
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn heartbeat_timeout_retries_a_stuck_step() {
    let dir = tmp("hb-timeout");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let attempts = Arc::new(AtomicU32::new(0));
    let a = attempts.clone();
    let wf = engine.register("w", move |ctx: Ctx, _: ()| {
        let a = a.clone();
        async move {
            let c = ctx.clone();
            let opts = StepOptions::default()
                .retry(RetryPolicy::exponential(
                    3,
                    Duration::from_millis(1),
                    Duration::from_millis(5),
                ))
                .heartbeat_timeout(Duration::from_millis(100));
            ctx.step("slow", move || {
                let (c, a) = (c.clone(), a.clone());
                async move {
                    let n = a.fetch_add(1, Ordering::SeqCst);
                    let start = c.progress::<u32>()?.unwrap_or(0);
                    c.heartbeat(&(start + 1))?;
                    if n == 0 {
                        tokio::time::sleep(Duration::from_secs(30)).await;
                    }
                    Ok::<_, Error>(start)
                }
            })
            .options(opts)
            .await
        }
    });
    let r: u32 = engine
        .start(&wf, "w-1", &())
        .await
        .unwrap()
        .result()
        .await
        .unwrap();
    assert_eq!(attempts.load(Ordering::SeqCst), 2);
    assert_eq!(r, 1, "the second attempt saw the first one's progress");
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn async_completion_with_stale_tokens() {
    let dir = tmp("complete");
    let tokens = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
    let register = |engine: &Engine, tokens: Arc<std::sync::Mutex<Vec<String>>>| {
        engine.register("approve", move |ctx: Ctx, _: ()| {
            let tokens = tokens.clone();
            async move {
                let opts = StepOptions::default().retry(RetryPolicy::exponential(
                    3,
                    Duration::from_millis(1),
                    Duration::from_millis(2),
                ));
                let ok: bool = ctx
                    .await_completion("approval", move |c: fusee::Completion| {
                        let token = c.token;
                        let tokens = tokens.clone();
                        async move {
                            let mut t = tokens.lock().unwrap();
                            t.push(token);
                            // The first attempt to send fails.
                            if t.len() == 1 {
                                Err("mail server down".to_string())
                            } else {
                                Ok(())
                            }
                        }
                    })
                    .options(opts)
                    .await?;
                Ok(ok)
            }
        })
    };
    {
        let engine = Engine::open(cfg(&dir)).await.unwrap();
        let wf = register(&engine, tokens.clone());
        engine.start(&wf, "a-1", &()).await.unwrap();
        eventually("two tokens", async || tokens.lock().unwrap().len() == 2).await;
        engine.shutdown().await.unwrap();
    }
    {
        // Another process (here: after a restart, with the run parked)
        // completes the step with the token.
        let engine = Engine::open(cfg(&dir)).await.unwrap();
        let _wf = register(&engine, tokens.clone());
        let t = tokens.lock().unwrap().clone();
        let err = tokio::time::timeout(Duration::from_secs(5), engine.complete(&t[0], &true))
            .await
            .expect("stale completion answered")
            .unwrap_err();
        assert!(matches!(err, Error::Rejected(_)), "{err}");
        tokio::time::timeout(Duration::from_secs(5), engine.complete(&t[1], &true))
            .await
            .expect("completion answered")
            .unwrap();
        assert!(
            tokio::time::timeout(Duration::from_secs(5), engine.run::<bool>("a-1").result())
                .await
                .expect("result")
                .unwrap()
        );
        let err = engine.complete(&t[1], &false).await.unwrap_err();
        assert!(matches!(err, Error::Rejected(_)), "{err}");
        assert_eq!(
            tokens.lock().unwrap().len(),
            2,
            "the token was not sent again"
        );
        engine.shutdown().await.unwrap();
    }
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn continue_as_new_keeps_the_id() {
    let dir = tmp("can");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let wf = engine.register("loop", |ctx: Ctx, (left, acc): (u32, u32)| async move {
        let v: u32 = ctx
            .step("work", || async move { Ok::<_, Error>(left) })
            .await?;
        assert!(ctx.journal_size() <= 2);
        if left == 0 {
            return Ok(acc + v);
        }
        ctx.continue_as_new(&(left - 1, acc + v)).await
    });
    let run = engine.start(&wf, "loop-1", &(5, 0)).await.unwrap();
    assert_eq!(run.result().await.unwrap(), 15);
    let s = run.status().await.unwrap();
    assert_eq!(s.status, "ok");
    let detail = run.describe().await.unwrap();
    assert!(matches!(
        detail.journal[0],
        fusee::core::Record::Start { from: Some(_), .. }
    ));
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn patching_a_sleeping_workflow() {
    let dir = tmp("patch");
    {
        let engine = Engine::open(cfg(&dir)).await.unwrap();
        let wf = engine.register("refund", |ctx: Ctx, _: ()| async move {
            let a: u32 = ctx.step("a", || async { Ok::<_, Error>(1) }).await?;
            let _: Option<()> = ctx.wait_for("go").await?;
            Ok(a)
        });
        engine.start(&wf, "old", &()).await.unwrap();
        tokio::time::sleep(Duration::from_millis(50)).await;
        engine.shutdown().await.unwrap();
    }
    {
        // New code: the fix applies to new runs and to the old run when it
        // reaches the patch point; replaying its old journal is unaffected.
        let engine = Engine::open(cfg(&dir)).await.unwrap();
        let wf = engine.register("refund", |ctx: Ctx, _: ()| async move {
            let fixed_early = ctx.patched("early")?;
            let a: u32 = ctx.step("a", || async { Ok::<_, Error>(1) }).await?;
            let _: Option<()> = ctx.wait_for("go").await?;
            let fixed = ctx.patched("rounding")?;
            Ok(a + if fixed { 10 } else { 0 } + if fixed_early { 100 } else { 0 })
        });
        let old = engine.run::<u32>("old");
        old.signal("go", &()).await.unwrap();
        assert_eq!(old.result().await.unwrap(), 11);
        let new = engine.start(&wf, "new", &()).await.unwrap();
        new.signal("go", &()).await.unwrap();
        assert_eq!(new.result().await.unwrap(), 111);
        engine.shutdown().await.unwrap();
    }
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn terminate_applies_the_parent_close_policy() {
    let dir = tmp("close");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let _kid = engine.register("kid", |ctx: Ctx, _: ()| async move {
        let _: Option<()> = ctx.wait_for("never").await?;
        Ok(())
    });
    let wf = engine.register("parent", |ctx: Ctx, _: ()| async move {
        ctx.start_child("kid", "kid-terminate", &()).await?;
        ctx.start_child("kid", "kid-cancel", &())
            .options(ChildOptions {
                close: ParentClose::RequestCancel,
                timeout: None,
            })
            .await?;
        ctx.start_child("kid", "kid-abandon", &())
            .options(ChildOptions {
                close: ParentClose::Abandon,
                timeout: None,
            })
            .await?;
        let _: Option<()> = ctx.wait_for("never").await?;
        Ok(())
    });
    let run = engine.start(&wf, "parent-1", &()).await.unwrap();
    eventually("children", async || {
        engine.status("kid-abandon").await.unwrap().is_some()
            && engine.status("kid-cancel").await.unwrap().is_some()
            && engine.status("kid-terminate").await.unwrap().is_some()
    })
    .await;
    run.terminate("stop").await.unwrap();
    assert!(matches!(run.result().await, Err(Error::Terminated)));
    let st = |id: &'static str| {
        let e = engine.clone();
        async move { e.status(id).await.unwrap().unwrap().status }
    };
    eventually("children closed", async || {
        st("kid-terminate").await == "terminated" && st("kid-cancel").await == "cancelled"
    })
    .await;
    assert!(matches!(
        st("kid-abandon").await.as_str(),
        "suspended" | "parked" | "running"
    ));
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn paged_backfill_remembers_instants_after_child_retention() {
    let engine = Engine::open(Config::memory().retention(Duration::from_millis(25)))
        .await
        .unwrap();
    let issued = Arc::new(AtomicU32::new(0));
    let count = issued.clone();
    engine.register("tick", move |ctx: Ctx, _: serde_json::Value| {
        let count = count.clone();
        async move {
            ctx.step("effect", || async {
                Ok::<_, Error>(count.fetch_add(1, Ordering::SeqCst))
            })
            .await
        }
    });
    let spec = ScheduleSpec {
        every_ms: Some(1),
        workflow: "tick".into(),
        paused: true,
        ..Default::default()
    };
    let schedule = engine.schedule("range", spec).await.unwrap();
    schedule.backfill(1, 1001).await.unwrap();
    eventually("entire requested range", async || {
        issued.load(Ordering::SeqCst) == 1001
    })
    .await;
    eventually("expired first child", async || {
        engine
            .status(&fusee::schedule::run_id("range", 1))
            .await
            .unwrap()
            .is_none()
    })
    .await;
    schedule.backfill(1, 1002).await.unwrap();
    eventually("only the new instant", async || {
        issued.load(Ordering::SeqCst) >= 1002
    })
    .await;
    assert_eq!(schedule.describe().await.unwrap()["started"], 1002);
    assert_eq!(issued.load(Ordering::SeqCst), 1002);
    engine.shutdown().await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn schedules_start_one_run_per_time() {
    let dir = tmp("schedule");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let runs = Arc::new(AtomicU32::new(0));
    let r = runs.clone();
    let _report = engine.register("report", move |ctx: Ctx, _: serde_json::Value| {
        let r = r.clone();
        async move {
            r.fetch_add(1, Ordering::SeqCst);
            Ok(ctx.workflow_id().to_owned())
        }
    });
    let spec = ScheduleSpec {
        every_ms: Some(100),
        overlap: Overlap::Skip,
        workflow: "report".into(),
        ..Default::default()
    };
    let s = engine.schedule("nightly", spec.clone()).await.unwrap();
    eventually("three runs", async || runs.load(Ordering::SeqCst) >= 3).await;
    let d = s.describe().await.unwrap();
    assert!(d["started"].as_u64().unwrap() >= 3, "{d}");
    s.pause().await.unwrap();
    tokio::time::sleep(Duration::from_millis(50)).await;
    let n = runs.load(Ordering::SeqCst);
    tokio::time::sleep(Duration::from_millis(300)).await;
    assert_eq!(runs.load(Ordering::SeqCst), n, "paused");
    // A backfill over times that already ran starts nothing twice.
    let now = fusee::local::unix_ms();
    s.backfill(now - 2_000, now).await.unwrap();
    eventually("backfill", async || runs.load(Ordering::SeqCst) > n).await;
    let ids: Vec<String> = engine
        .list_where(&serde_json::json!({"type": "report"}), 1000)
        .await
        .unwrap()
        .into_iter()
        .map(|s| s.id)
        .collect();
    let unique: std::collections::BTreeSet<&String> = ids.iter().collect();
    assert_eq!(unique.len(), ids.len(), "{ids:?}");
    assert!(ids.iter().all(|i| i.starts_with("nightly@")), "{ids:?}");
    // Calling schedule again (an application restart) keeps one schedule.
    engine.schedule("nightly", spec).await.unwrap();
    s.delete().await.unwrap();
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn search_attributes_and_listing() {
    let dir = tmp("attrs");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let wf = engine.register("order", |ctx: Ctx, amount: u32| async move {
        ctx.set_attributes(&serde_json::json!({"customer": "c-1", "amount": amount}))?;
        if amount > 100 {
            return Err(Error::Failed("too big".into()));
        }
        ctx.set_attributes(&serde_json::json!({"stage": "paid"}))?;
        Ok(amount)
    });
    for (i, a) in [50u32, 150, 80].iter().enumerate() {
        let run = engine.start(&wf, &format!("o-{i}"), a).await.unwrap();
        let _ = run.result().await;
    }
    let q = |f: serde_json::Value| {
        let e = engine.clone();
        async move {
            let mut ids: Vec<String> = e
                .list_where(&f, 100)
                .await
                .unwrap()
                .into_iter()
                .map(|s| s.id)
                .collect();
            ids.sort();
            ids
        }
    };
    assert_eq!(
        q(serde_json::json!({"customer": "c-1"})).await,
        vec!["o-0", "o-1", "o-2"]
    );
    assert_eq!(
        q(serde_json::json!({"status": "failed"})).await,
        vec!["o-1"]
    );
    assert_eq!(
        q(serde_json::json!({"amount": {"gte": 60, "lt": 200}, "stage": "paid"})).await,
        vec!["o-2"]
    );
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn limits_bound_concurrent_steps() {
    let dir = tmp("limits");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    engine
        .limit(
            "stripe",
            LimitSpec {
                concurrent: 2,
                per_second: 0.0,
            },
        )
        .unwrap();
    let now = Arc::new(AtomicU32::new(0));
    let max = Arc::new(AtomicU32::new(0));
    let (n2, m2) = (now.clone(), max.clone());
    let wf = engine.register("charge", move |ctx: Ctx, _: ()| {
        let (now, max) = (n2.clone(), m2.clone());
        async move {
            ctx.step("charge", || {
                let (now, max) = (now.clone(), max.clone());
                async move {
                    let n = now.fetch_add(1, Ordering::SeqCst) + 1;
                    max.fetch_max(n, Ordering::SeqCst);
                    tokio::time::sleep(Duration::from_millis(20)).await;
                    now.fetch_sub(1, Ordering::SeqCst);
                    Ok::<_, Error>(())
                }
            })
            .options(StepOptions::default().limit("stripe"))
            .await
        }
    });
    let mut runs = Vec::new();
    for i in 0..10 {
        runs.push(engine.start(&wf, &format!("c-{i}"), &()).await.unwrap());
    }
    for r in runs {
        r.result().await.unwrap();
    }
    assert_eq!(max.load(Ordering::SeqCst), 2);
    assert_eq!(engine.limit_in_use("stripe"), 0);
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn waiting_steps_take_permits_by_priority_then_fairness_key() {
    let dir = tmp("limit-order");
    let mut c = Config::local(&dir).shards(1);
    c.local.flush = fusee::FlushMethod::Fsync;
    let engine = Engine::open(c).await.unwrap();
    engine
        .limit(
            "one",
            LimitSpec {
                concurrent: 1,
                per_second: 0.0,
            },
        )
        .unwrap();
    let open = Arc::new(std::sync::atomic::AtomicBool::new(false));
    let served = Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
    let o2 = open.clone();
    let hold = engine.register("hold", move |ctx: Ctx, _: ()| {
        let open = o2.clone();
        async move {
            ctx.step("hold", || {
                let open = open.clone();
                async move {
                    while !open.load(Ordering::SeqCst) {
                        tokio::time::sleep(Duration::from_millis(2)).await;
                    }
                    Ok::<_, Error>(())
                }
            })
            .options(StepOptions::default().limit("one"))
            .await
        }
    });
    let s2 = served.clone();
    let job = engine.register("job", move |ctx: Ctx, (level, key): (i32, String)| {
        let served = s2.clone();
        async move {
            let opts = StepOptions::default()
                .limit("one")
                .priority(level)
                .fairness(&key);
            ctx.step("work", || {
                let (served, key) = (served.clone(), key.clone());
                async move {
                    served.lock().unwrap().push(format!("{key}@{level}"));
                    Ok::<_, Error>(())
                }
            })
            .options(opts)
            .await
        }
    });
    let blocker = engine.start(&hold, "hold", &()).await.unwrap();
    eventually("the blocker's permit", async || {
        engine.limit_in_use("one") == 1
    })
    .await;
    // Tenant "big" queues six steps, then "a" and "b" two each, then one
    // urgent step: each start waits until its step is queued, so the
    // arrival order is fixed.
    let mut asks: Vec<(i32, &str)> = vec![(0, "big"); 6];
    asks.extend([(0, "a"), (0, "b"), (0, "a"), (0, "b"), (5, "urgent")]);
    let mut runs = Vec::new();
    for (i, (level, key)) in asks.iter().enumerate() {
        runs.push(
            engine
                .start(&job, &format!("job-{i}"), &(*level, key.to_string()))
                .await
                .unwrap(),
        );
        eventually("the step to queue", async || {
            engine.limit_waiting("one") == i + 1
        })
        .await;
    }
    open.store(true, Ordering::SeqCst);
    blocker.result().await.unwrap();
    for r in runs {
        r.result().await.unwrap();
    }
    assert_eq!(
        *served.lock().unwrap(),
        [
            "urgent@5", "big@0", "a@0", "b@0", "big@0", "a@0", "b@0", "big@0", "big@0", "big@0",
            "big@0"
        ]
    );
    assert_eq!(engine.limit_in_use("one"), 0);
    assert_eq!(engine.limit_waiting("one"), 0);
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn encrypted_payloads_never_reach_the_disk() {
    let dir = tmp("codec");
    let key = [3u8; 32];
    let mk = || cfg(&dir).codec(fusee::aes_gcm([("k1", key)]));
    {
        let engine = Engine::open(mk()).await.unwrap();
        let wf = engine.register("secret", |ctx: Ctx, card: String| async move {
            let masked: String = ctx
                .step("mask", || {
                    let c = card.clone();
                    async move { Ok::<_, Error>(format!("{}-SECRETRESULT", &c[..4])) }
                })
                .await?;
            let note: Option<String> = ctx.wait_for("note").await?;
            Ok(format!("{masked}/{}", note.unwrap_or_default()))
        });
        let run = engine
            .start(&wf, "s-1", &"4242SECRETCARD".to_string())
            .await
            .unwrap();
        run.signal("note", &"SECRETSIGNAL").await.unwrap();
        assert_eq!(
            run.result().await.unwrap(),
            "4242-SECRETRESULT/SECRETSIGNAL"
        );
        engine.shutdown().await.unwrap();
    }
    let mut leaked = Vec::new();
    for entry in walk(&dir) {
        let bytes = std::fs::read(&entry).unwrap();
        for needle in ["SECRETCARD", "SECRETRESULT", "SECRETSIGNAL"] {
            if bytes.windows(needle.len()).any(|w| w == needle.as_bytes()) {
                leaked.push(format!("{} holds {needle}", entry.display()));
            }
        }
    }
    assert!(leaked.is_empty(), "{leaked:?}");
    {
        // The same key reads it back; no key cannot.
        let engine = Engine::open(mk()).await.unwrap();
        let r: String = engine.run("s-1").result().await.unwrap();
        assert_eq!(r, "4242-SECRETRESULT/SECRETSIGNAL");
        engine.shutdown().await.unwrap();
    }
    let _ = std::fs::remove_dir_all(&dir);
}

fn walk(dir: &std::path::Path) -> Vec<std::path::PathBuf> {
    let mut out = Vec::new();
    for e in std::fs::read_dir(dir).unwrap().flatten() {
        let p = e.path();
        if p.is_dir() {
            out.extend(walk(&p));
        } else {
            out.push(p);
        }
    }
    out
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn replay_testing_reports_the_first_difference() {
    let dir = tmp("replay");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let wf = engine.register("pay", |ctx: Ctx, _: ()| async move {
        let q: u32 = ctx.step("quote", || async { Ok::<_, Error>(3) }).await?;
        let c: u32 = ctx
            .step("charge", || async move { Ok::<_, Error>(q * 2) })
            .await?;
        Ok(c)
    });
    engine
        .start(&wf, "p-1", &())
        .await
        .unwrap()
        .result()
        .await
        .unwrap();
    let detail = engine.describe("p-1").await.unwrap().unwrap();
    let export = fusee::export::ExportedJournal::from_detail(&detail);
    let file = dir.join(fusee::export::file_name("p-1"));
    export.write(&file).unwrap();
    engine.shutdown().await.unwrap();

    let same = fusee::Replayer::new();
    same.register("pay", |ctx: Ctx, _: ()| async move {
        let q: u32 = ctx.step("quote", || async { Ok::<_, Error>(0) }).await?;
        let c: u32 = ctx
            .step("charge", || async move { Ok::<_, Error>(q) })
            .await?;
        Ok(c)
    });
    let r = same.replay_file(&file).unwrap();
    assert!(r.ok && r.finished, "{r}");

    let changed = fusee::Replayer::new();
    changed.register("pay", |ctx: Ctx, _: ()| async move {
        let q: u32 = ctx.step("quote", || async { Ok::<_, Error>(3) }).await?;
        let c: u32 = ctx
            .step("refund", || async move { Ok::<_, Error>(q) })
            .await?;
        Ok(c)
    });
    let r = changed.replay_file(&file).unwrap();
    assert!(!r.ok, "{r}");
    let e = r.error.unwrap();
    assert!(e.contains("'charge'") && e.contains("'refund'"), "{e}");
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn workflow_id_policies() {
    let dir = tmp("ids");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let wf = engine.register("w", |ctx: Ctx, fail: bool| async move {
        let _: Option<()> = ctx
            .wait_for("go")
            .timeout(Duration::from_millis(50))
            .await?;
        if fail {
            return Err(Error::Failed("no".into()));
        }
        Ok(ctx.run_id())
    });
    let a = engine.start(&wf, "id-1", &false).await.unwrap();
    // Running: fails by default.
    let err = engine.start(&wf, "id-1", &false).await.unwrap_err();
    assert!(matches!(err, Error::AlreadyExists(_)), "{err}");
    // use-existing returns the running workflow.
    let same = engine
        .start(&wf, "id-1", &false)
        .options(StartOptions::default().use_existing())
        .await
        .unwrap();
    let first = a.result().await.unwrap();
    assert_eq!(same.result().await.unwrap(), first);
    // Finished: allowed by default (a new run).
    let b = engine.start(&wf, "id-1", &true).await.unwrap();
    assert!(b.result().await.is_err());
    // allow-if-failed: the last run failed, so a new one starts.
    let only_failed = IdPolicy {
        running: OnRunning::Fail,
        finished: OnFinished::AllowIfFailed,
    };
    let c = engine
        .start(&wf, "id-1", &false)
        .options(StartOptions::default().id_policy(only_failed))
        .await
        .unwrap();
    assert!(c.result().await.unwrap() > first);
    // ...and now it succeeded, so allow-if-failed refuses.
    let err = engine
        .start(&wf, "id-1", &false)
        .options(StartOptions::default().id_policy(only_failed))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::AlreadyExists(_)), "{err}");
    let refuse = IdPolicy {
        running: OnRunning::Fail,
        finished: OnFinished::Refuse,
    };
    assert!(
        engine
            .start(&wf, "id-1", &false)
            .options(StartOptions::default().id_policy(refuse))
            .await
            .is_err()
    );
    // terminate-existing replaces a running workflow.
    let slow = engine.register("slow", |ctx: Ctx, _: ()| async move {
        let _: Option<()> = ctx.wait_for("go").await?;
        Ok(ctx.run_id())
    });
    let old = engine.start(&slow, "id-2", &()).await.unwrap();
    let replace = IdPolicy {
        running: OnRunning::TerminateExisting,
        finished: OnFinished::Allow,
    };
    engine
        .start(&slow, "id-2", &())
        .options(StartOptions::default().id_policy(replace))
        .await
        .unwrap();
    old.signal("go", &()).await.unwrap();
    assert!(
        old.result().await.unwrap() > 0,
        "the new run answers for the ID"
    );
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn export_from_a_data_directory() {
    let dir = tmp("export");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let wf = engine.register("w", |ctx: Ctx, n: u32| async move {
        let a: u32 = ctx.step("a", || async move { Ok::<_, Error>(n) }).await?;
        let _: Option<()> = ctx.wait_for("go").await?;
        Ok(a)
    });
    for i in 0..3 {
        engine.start(&wf, &format!("e-{i}"), &i).await.unwrap();
    }
    tokio::time::sleep(Duration::from_millis(50)).await;
    // While the engine runs.
    let one = fusee::export::export_from_dir(&dir, Some("e-1"), 1).unwrap();
    assert_eq!(one.len(), 1);
    assert_eq!(one[0].workflow_id, "e-1");
    let all = fusee::export::export_from_dir(&dir, None, 10).unwrap();
    assert_eq!(all.len(), 3);
    let r = fusee::Replayer::new();
    r.register("w", |ctx: Ctx, n: u32| async move {
        let a: u32 = ctx.step("a", || async move { Ok::<_, Error>(n) }).await?;
        let _: Option<()> = ctx.wait_for("go").await?;
        Ok(a)
    });
    for j in &all {
        let rep = r.replay(j);
        assert!(rep.ok && !rep.finished, "{rep}");
    }
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn debounced_starts_collapse_into_one_run_with_the_latest_input() {
    let dir = tmp("debounce");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let starts = Arc::new(AtomicU32::new(0));
    let s2 = starts.clone();
    let wf = engine.register("reindex", move |_ctx: Ctx, n: u32| {
        s2.fetch_add(1, Ordering::SeqCst);
        async move { Ok(n) }
    });
    let rule = Debounce::new(Duration::from_millis(150));
    let a = engine.debounce(&wf, "doc-1", &1, &rule).await.unwrap();
    let b = engine.debounce(&wf, "doc-1", &2, &rule).await.unwrap();
    // Another key has its own window.
    let other = engine.debounce(&wf, "doc-2", &20, &rule).await.unwrap();
    let c = engine.debounce(&wf, "doc-1", &3, &rule).await.unwrap();
    assert_eq!(a.id(), b.id());
    assert_eq!(a.id(), c.id());
    assert_ne!(a.id(), other.id());
    assert!(a.id().starts_with("doc-1@"), "{}", a.id());
    // Nothing runs until the window closes.
    assert!(a.status().await.ok().is_none());
    a.started().await.unwrap();
    assert_eq!(a.result().await.unwrap(), 3);
    other.started().await.unwrap();
    assert_eq!(other.result().await.unwrap(), 20);
    assert_eq!(starts.load(Ordering::SeqCst), 2);
    // After the window closed, a start opens a new window and a new run.
    let d = engine.debounce(&wf, "doc-1", &4, &rule).await.unwrap();
    assert_ne!(d.id(), a.id());
    d.started().await.unwrap();
    assert_eq!(d.result().await.unwrap(), 4);
    // A maximum wait bounds a window that starts keep extending.
    let capped = Debounce::new(Duration::from_millis(80)).max_wait(Duration::from_millis(200));
    let mut ids = std::collections::BTreeSet::new();
    let begin = Instant::now();
    let mut n = 100;
    while begin.elapsed() < Duration::from_millis(700) {
        ids.insert(
            engine
                .debounce(&wf, "busy", &n, &capped)
                .await
                .unwrap()
                .id()
                .to_owned(),
        );
        n += 1;
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    assert!(ids.len() >= 3, "{ids:?}");
    for id in &ids {
        let run = engine.run::<u32>(id);
        run.started().await.unwrap();
        run.result().await.unwrap();
    }
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn batches_start_one_run_per_full_or_timed_out_batch() {
    let dir = tmp("batch");
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let wf = engine.register(
        "bulk",
        |_ctx: Ctx, events: Vec<u32>| async move { Ok(events) },
    );
    let rule = Batch::new(3, Duration::from_millis(200));
    let mut handles = Vec::new();
    for i in 0..7u32 {
        handles.push(engine.batch(&wf, "tenant-1", &i, &rule).await.unwrap());
    }
    // A retried event with the same ID is added once.
    let again = engine
        .batch(&wf, "tenant-1", &7, &rule)
        .event_id("evt-7")
        .await
        .unwrap();
    let dup = engine
        .batch(&wf, "tenant-1", &7, &rule)
        .event_id("evt-7")
        .await
        .unwrap();
    assert_eq!(again.id(), dup.id());
    let ids: Vec<&str> = handles.iter().map(|h| h.id()).collect();
    assert_eq!(ids[0], ids[2]);
    assert_ne!(ids[2], ids[3]);
    assert_eq!(ids[3], ids[5]);
    assert_ne!(ids[5], ids[6]);
    assert_eq!(ids[6], again.id());
    let mut results = Vec::new();
    for id in [ids[0], ids[3], ids[6]] {
        let run = engine.run::<Vec<u32>>(id);
        run.started().await.unwrap();
        results.push(run.result().await.unwrap());
    }
    assert_eq!(results, vec![vec![0, 1, 2], vec![3, 4, 5], vec![6, 7]]);
    // A rule the coordinator cannot use is refused before anything is sent.
    let err = engine
        .batch(&wf, "tenant-1", &1, &Batch::new(0, Duration::ZERO))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Rejected(_)), "{err}");
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn an_acknowledged_debounced_start_survives_a_restart() {
    let dir = tmp("debounce-restart");
    let register =
        |engine: &Engine| engine.register("notify", |_ctx: Ctx, n: u32| async move { Ok(n * 10) });
    let rule = Debounce::new(Duration::from_millis(300));
    let id = {
        let engine = Engine::open(cfg(&dir)).await.unwrap();
        let wf = register(&engine);
        let first = engine
            .debounce(&wf, "user-9", &1, &rule)
            .event_id("e-1")
            .await
            .unwrap();
        let second = engine
            .debounce(&wf, "user-9", &2, &rule)
            .event_id("e-2")
            .await
            .unwrap();
        assert_eq!(first.id(), second.id());
        // The process stops before the window closes.
        engine.shutdown().await.unwrap();
        first.id().to_owned()
    };
    let engine = Engine::open(cfg(&dir)).await.unwrap();
    let wf = register(&engine);
    // A retry of an acknowledged event joins the same window once.
    let retried = engine
        .debounce(&wf, "user-9", &2, &rule)
        .event_id("e-2")
        .await
        .unwrap();
    assert_eq!(retried.id(), id);
    let run = engine.run::<u32>(&id);
    run.started().await.unwrap();
    assert_eq!(run.result().await.unwrap(), 20);
    // Once fired, the same event ID still answers with its run.
    let late = engine
        .debounce(&wf, "user-9", &2, &rule)
        .event_id("e-2")
        .await
        .unwrap();
    assert_eq!(late.id(), id);
    let listed = engine.list(100).await.unwrap();
    assert_eq!(
        listed.iter().filter(|r| r.wtype == "notify").count(),
        1,
        "{listed:?}"
    );
    engine.shutdown().await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}