acts 0.25.0

a fast, lightweight, extensiable workflow engine
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
use super::EventAction;
use crate::{
    Workflow,
    event::{Emitter, MessageState},
    scheduler::TaskState,
    utils::{self, test::create_proc},
};
use std::str::FromStr;

#[test]
fn event_message_state_to_string() {
    let state = MessageState::None;
    assert_eq!(state.to_string(), "none");

    let state = MessageState::Created;
    assert_eq!(state.to_string(), "created");

    let state = MessageState::Error;
    assert_eq!(state.to_string(), "error");

    let state = MessageState::Submitted;
    assert_eq!(state.to_string(), "submitted");

    let state = MessageState::Cancelled;
    assert_eq!(state.to_string(), "cancelled");

    let state = MessageState::Backed;
    assert_eq!(state.to_string(), "backed");

    let state = MessageState::Aborted;
    assert_eq!(state.to_string(), "aborted");

    let state = MessageState::Removed;
    assert_eq!(state.to_string(), "removed");

    let state = MessageState::Skipped;
    assert_eq!(state.to_string(), "skipped");
}

#[test]
fn event_message_state_from_string() {
    let state = MessageState::from_str("none").unwrap();
    assert_eq!(state, MessageState::None);

    let state = MessageState::from_str("error").unwrap();
    assert_eq!(state, MessageState::Error);

    let state = MessageState::from_str("aborted").unwrap();
    assert_eq!(state, MessageState::Aborted);

    let state = MessageState::from_str("submitted").unwrap();
    assert_eq!(state, MessageState::Submitted);

    let state = MessageState::from_str("cancelled").unwrap();
    assert_eq!(state, MessageState::Cancelled);

    let state = MessageState::from_str("backed").unwrap();
    assert_eq!(state, MessageState::Backed);

    let state = MessageState::from_str("created").unwrap();
    assert_eq!(state, MessageState::Created);

    let state = MessageState::from_str("skipped").unwrap();
    assert_eq!(state, MessageState::Skipped);

    let state = MessageState::from_str("removed").unwrap();
    assert_eq!(state, MessageState::Removed);
}

#[test]
fn event_message_state_from_task_state() {
    let state: MessageState = TaskState::None.into();
    assert_eq!(state, MessageState::None);

    let state: MessageState = TaskState::Error.into();
    assert_eq!(state, MessageState::Error);

    let state: MessageState = TaskState::Aborted.into();
    assert_eq!(state, MessageState::Aborted);

    let state: MessageState = TaskState::Submitted.into();
    assert_eq!(state, MessageState::Submitted);

    let state: MessageState = TaskState::Cancelled.into();
    assert_eq!(state, MessageState::Cancelled);

    let state: MessageState = TaskState::Backed.into();
    assert_eq!(state, MessageState::Backed);

    let state: MessageState = TaskState::Running.into();
    assert_eq!(state, MessageState::Created);

    let state: MessageState = TaskState::Pending.into();
    assert_eq!(state, MessageState::Created);

    let state: MessageState = TaskState::Interrupt.into();
    assert_eq!(state, MessageState::Created);

    let state: MessageState = TaskState::Skipped.into();
    assert_eq!(state, MessageState::Skipped);

    let state: MessageState = TaskState::Removed.into();
    assert_eq!(state, MessageState::Removed);
}

#[tokio::test]
async fn event_action_parse() {
    let action = EventAction::parse("next").unwrap();
    assert_eq!(action, EventAction::Next);

    let action = EventAction::parse("submit").unwrap();
    assert_eq!(action, EventAction::Submit);

    let action = EventAction::parse("cancel").unwrap();
    assert_eq!(action, EventAction::Cancel);

    let action = EventAction::parse("back").unwrap();
    assert_eq!(action, EventAction::Back);

    let action = EventAction::parse("abort").unwrap();
    assert_eq!(action, EventAction::Abort);

    let action = EventAction::parse("skip").unwrap();
    assert_eq!(action, EventAction::Skip);

    let action = EventAction::parse("error").unwrap();
    assert_eq!(action, EventAction::Error);

    let action = EventAction::parse("aaaaa");
    assert!(action.is_err());
}

#[tokio::test]
async fn event_on_proc() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));

    let (engine, proc) = create_proc(&workflow, &utils::longid()).await;
    let evt = Emitter::new();
    let workflow2 = workflow.clone();
    evt.on_proc(move |e| {
        let workflow2 = workflow2.clone();
        async move {
            assert_eq!(e.inner().state(), TaskState::Running);
            assert_eq!(e.inner().model().id, workflow2.id);
        }
    });
    proc.set_state(TaskState::Running);
    engine.runtime().emitter().emit_proc_event(&proc).await;
}

#[tokio::test]
async fn event_on_task() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));

    let (engine, proc) = create_proc(&workflow, &utils::longid()).await;
    let evt = Emitter::new();
    evt.on_task(move |e| async move {
        assert_eq!(e.inner().state(), TaskState::Running);
    });
    proc.set_state(TaskState::Running);
    let task = proc
        .create_task(proc.tree().root.as_ref().unwrap(), None)
        .unwrap();
    task.set_state(TaskState::Running);
    engine
        .runtime()
        .emitter()
        .emit_task_event(&task)
        .await
        .unwrap();
}

#[tokio::test]
async fn event_start() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));

    let (_, proc) = create_proc(&workflow, &utils::longid()).await;
    let evt = Emitter::new();
    let workflow2 = workflow.clone();
    evt.on_start("k1", move |e| {
        let workflow2 = workflow2.clone();
        async move {
            assert!(e.mid == workflow2.id);
        }
    });
    proc.start().await.unwrap();
    if let Some(root) = proc.root() {
        let message = root.create_message();
        evt.emit_start_event(&message);
    }
}

#[tokio::test]
async fn event_finished() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    let (_, proc) = create_proc(&workflow, &utils::longid()).await;
    let evt = Emitter::new();
    let workflow2 = workflow.clone();
    evt.on_complete("k1", move |e| {
        let workflow2 = workflow2.clone();
        async move {
            assert!(e.mid == workflow2.id);
        }
    });

    proc.start().await.unwrap();
    if let Some(root) = proc.root() {
        let message = root.create_message();
        evt.emit_complete_event(&message);
    }
}

#[tokio::test]
async fn event_error() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    let workflow_id = workflow.id.clone();
    let (_, proc) = create_proc(&workflow, &utils::longid()).await;

    let evt = Emitter::new();
    evt.on_error("k1", move |e| {
        let workflow_id = workflow_id.clone();
        async move {
            assert!(e.mid == workflow_id);
        }
    });

    proc.start().await.unwrap();
    if let Some(root) = proc.root() {
        let message = root.create_message();
        evt.emit_error(&message);
    }
}

#[tokio::test]
async fn event_message_default() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    let workflow_id = workflow.id.clone();
    let (engine, proc) = create_proc(&workflow, &utils::longid()).await;

    let (s1, s2) = engine.signal(false).double();
    let evt = Emitter::new();
    evt.on_message("k1", move |e| {
        let s1 = s1.clone();
        let workflow_id = workflow_id.clone();
        async move {
            s1.send(e.mid == workflow_id);
        }
    });

    proc.start().await.unwrap();
    if let Some(root) = proc.root() {
        let message = root.create_message();
        evt.emit_message(&message);
    }
    let ret = s2.recv().await;
    assert!(ret);
}

#[tokio::test]
async fn event_message_dup_key() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    let workflow_id = workflow.id.clone();
    let (engine, proc) = create_proc(&workflow, &utils::longid()).await;

    let (s1, s2) = engine.signal(false).double();
    let evt = Emitter::new();
    evt.on_message("k1", move |_| async {});
    evt.on_message("k1", move |e| {
        let s1 = s1.clone();
        let workflow_id = workflow_id.clone();
        async move {
            s1.send(e.mid == workflow_id);
        }
    });

    proc.start().await.unwrap();
    if let Some(root) = proc.root() {
        let message = root.create_message();
        evt.emit_message(&message);
    }
    let ret = s2.recv().await;
    assert!(ret);
}

#[tokio::test(flavor = "multi_thread")]
async fn event_message_fifo_order() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    let (engine, proc) = create_proc(&workflow, &utils::longid()).await;

    let (s1, s2) = engine.signal(Vec::new()).double();
    let evt = Emitter::new();
    evt.on_message("k1", move |e| {
        let s1 = s1.clone();
        async move {
            let is_last = e.mid == "mid-2";
            let mid = e.mid.clone();
            s1.update(move |data| data.push(mid.clone()));
            if is_last {
                s1.close();
            }
        }
    });

    proc.start().await.unwrap();
    for i in 0..3 {
        let msg = crate::Message {
            mid: format!("mid-{i}"),
            ..Default::default()
        };
        evt.emit_message(&msg);
    }

    let ret = s2.recv().await;
    assert_eq!(ret, vec!["mid-0", "mid-1", "mid-2"]);
}

/// A worker that exits on its terminal event must not drop an event routed to
/// it while the terminal handler was still running: the exit path re-homes the
/// queued event to a fresh worker instead of dropping it with the receiver.
#[tokio::test]
async fn event_terminal_exit_rehomes_queued_event() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    let (engine, proc) = create_proc(&workflow, &utils::longid()).await;
    let evt = Emitter::new();

    let entered = std::sync::Arc::new(tokio::sync::Notify::new());
    let release = std::sync::Arc::new(tokio::sync::Notify::new());
    {
        let entered = entered.clone();
        let release = release.clone();
        evt.on_error("k1", move |_| {
            let entered = entered.clone();
            let release = release.clone();
            async move {
                entered.notify_one();
                release.notified().await;
            }
        });
    }
    let (s1, s2) = engine.signal(false).double();
    evt.on_message("k1", move |_| {
        let s1 = s1.clone();
        async move {
            s1.send(true);
        }
    });

    proc.start().await.unwrap();
    let root = proc.root().unwrap();
    evt.emit_error(&root.create_message());
    // the terminal handler is now running; queue a follow-up for the same pid
    entered.notified().await;
    evt.emit_message(&root.create_message());
    release.notify_one();

    let received = tokio::time::timeout(std::time::Duration::from_secs(5), s2.recv())
        .await
        .expect("queued event was dropped when the terminal worker exited");
    assert!(received);
}

/// The report's exact scenario: a retry-timer delivery is routed to a process
/// whose terminal event is being handled. The delivery is queued behind the
/// terminal event and must still reach its channel handler when the worker
/// exits on that terminal event — otherwise the row waits out a whole retry
/// window, consuming another attempt of a budget that may already be spent.
///
/// Distinct from `event_terminal_exit_rehomes_queued_event`: a delivery takes
/// its own `KeyEvent` arm and is dispatched to the single handler of its
/// `chan_id` (not to every message handler), so the re-home path is pinned on
/// the delivery route specifically.
#[tokio::test]
async fn event_terminal_exit_rehomes_queued_delivery() {
    use std::sync::atomic::{AtomicUsize, Ordering};

    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    let (_engine, proc) = create_proc(&workflow, &utils::longid()).await;
    let evt = Emitter::new();

    let entered = std::sync::Arc::new(tokio::sync::Notify::new());
    let release = std::sync::Arc::new(tokio::sync::Notify::new());
    {
        let entered = entered.clone();
        let release = release.clone();
        evt.on_error("k1", move |_| {
            let entered = entered.clone();
            let release = release.clone();
            async move {
                entered.notify_one();
                release.notified().await;
            }
        });
    }

    proc.start().await.unwrap();
    let root = proc.root().unwrap();

    // the channel handler the retry timer re-sends to; registered after start
    // so only the delivery can reach it
    let delivered = std::sync::Arc::new(tokio::sync::Notify::new());
    let calls = std::sync::Arc::new(AtomicUsize::new(0));
    {
        let delivered = delivered.clone();
        let calls = calls.clone();
        evt.on_message("client-1", move |_| {
            let delivered = delivered.clone();
            let calls = calls.clone();
            async move {
                calls.fetch_add(1, Ordering::SeqCst);
                delivered.notify_one();
            }
        });
    }

    // terminal event first: its handler holds the worker inside the loop
    evt.emit_error(&root.create_message());
    entered.notified().await;
    // the retry timer re-sends a delivery of the same process; it is queued
    // behind the terminal event and the worker breaks right after it
    evt.emit_delivery("client-1", &root.create_message());
    release.notify_one();

    tokio::time::timeout(std::time::Duration::from_secs(5), delivered.notified())
        .await
        .expect("queued delivery was dropped when the terminal worker exited");
    assert_eq!(
        calls.load(Ordering::SeqCst),
        1,
        "the delivery reached its channel handler exactly once"
    );
}

/// A routing entry whose worker ended without running its re-home exit (its
/// task was dropped, e.g. the runtime aborted it) must not swallow events: the
/// next event replaces the stale sender with a fresh worker instead of being
/// written into a queue nobody reads.
///
/// The event used to create the worker has no registered handler, so only the
/// event routed after the abandonment can reach the handler — the assertion
/// covers delivery, not the orphan's own dispatch.
#[tokio::test]
async fn event_route_replaces_an_abandoned_worker() {
    let workflow = Workflow::new()
        .with_id("m1")
        .with_step(|step| step.with_id("step1"));
    let (engine, proc) = create_proc(&workflow, &utils::longid()).await;
    let evt = Emitter::new();

    let (s1, s2) = engine.signal(Vec::new()).double();
    evt.on_message("k1", move |e| {
        let s1 = s1.clone();
        async move {
            let mid = e.mid.clone();
            s1.update(move |data| data.push(mid.clone()));
            s1.close();
        }
    });

    proc.start().await.unwrap();
    let root = proc.root().unwrap();
    // creates the process's worker; nothing is registered on the start key
    evt.emit_start_event(&root.create_message());
    // the worker task is gone without the exit path that would have replaced
    // its entry — the sender now outlives its receiver
    evt.abandon_worker(proc.id());

    let msg = crate::Message {
        mid: "mid-1".to_string(),
        ..root.create_message()
    };
    evt.emit_message(&msg);

    let received = tokio::time::timeout(std::time::Duration::from_secs(5), s2.recv())
        .await
        .expect("the event was written into the abandoned worker's queue");
    assert_eq!(
        received,
        vec!["mid-1"],
        "the fresh worker delivered the event exactly once"
    );
}