foxtive-supervisor 0.3.0

Foxtive Supervisor - Advanced task supervision for Rust applications
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
mod common;
use foxtive_supervisor::Supervisor;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

#[tokio::test]
async fn test_diamond_dependency_graph() {
    // Creates a diamond-shaped dependency graph:
    //        A
    //       / \
    //      B   C
    //       \ /
    //        D
    
    struct DiamondTask {
        id: &'static str,
        deps: &'static [&'static str],
        setup_order: Arc<std::sync::Mutex<Vec<String>>>,
    }

    #[async_trait::async_trait]
    impl foxtive_supervisor::contracts::SupervisedTask for DiamondTask {
        fn id(&self) -> &'static str { self.id }
        
        fn dependencies(&self) -> &'static [&'static str] {
            self.deps
        }
        
        async fn setup(&self) -> anyhow::Result<()> {
            {
                let mut order = self.setup_order.lock().unwrap();
                order.push(self.id.to_string());
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
            Ok(())
        }
        
        async fn run(&self) -> anyhow::Result<()> {
            tokio::time::sleep(Duration::from_millis(50)).await;
            Ok(())
        }
    }

    let setup_order = Arc::new(std::sync::Mutex::new(Vec::new()));
    
    let supervisor = Supervisor::new()
        .add(DiamondTask { 
            id: "A", 
            deps: &[],  // Root
            setup_order: setup_order.clone() 
        })
        .add(DiamondTask { 
            id: "B", 
            deps: &["A"],  // Depends on A
            setup_order: setup_order.clone() 
        })
        .add(DiamondTask { 
            id: "C", 
            deps: &["A"],  // Depends on A
            setup_order: setup_order.clone() 
        })
        .add(DiamondTask { 
            id: "D", 
            deps: &["B", "C"],  // Depends on both B and C
            setup_order: setup_order.clone() 
        });

    let runtime = supervisor.start().await.unwrap();
    
    tokio::time::sleep(Duration::from_millis(200)).await;
    
    runtime.shutdown().await;
    
    let order = setup_order.lock().unwrap();
    assert_eq!(order.len(), 4);
    
    // A must be first (no dependencies)
    assert_eq!(order[0], "A");
    
    // B and C can be in any order, but both after A
    assert!(order[1] == "B" || order[1] == "C");
    assert!(order[2] == "B" || order[2] == "C");
    
    // D must be last (depends on both B and C)
    assert_eq!(order[3], "D");
}

#[tokio::test]
async fn test_deep_linear_chain() {
    // Creates a deep linear chain: A -> B -> C -> D -> E
    
    struct ChainTask {
        id: &'static str,
        deps: &'static [&'static str],
        execution_order: Arc<std::sync::Mutex<Vec<String>>>,
    }

    #[async_trait::async_trait]
    impl foxtive_supervisor::contracts::SupervisedTask for ChainTask {
        fn id(&self) -> &'static str { self.id }
        
        fn dependencies(&self) -> &'static [&'static str] {
            self.deps
        }
        
        async fn setup(&self) -> anyhow::Result<()> {
            {
                let mut order = self.execution_order.lock().unwrap();
                order.push(format!("{}-setup", self.id));
            }
            Ok(())
        }
        
        async fn run(&self) -> anyhow::Result<()> {
            {
                let mut order = self.execution_order.lock().unwrap();
                order.push(format!("{}-run", self.id));
            }
            tokio::time::sleep(Duration::from_millis(20)).await;
            Ok(())
        }
    }

    let execution_order = Arc::new(std::sync::Mutex::new(Vec::new()));
    
    let supervisor = Supervisor::new()
        .add(ChainTask { id: "A", deps: &[], execution_order: execution_order.clone() })
        .add(ChainTask { id: "B", deps: &["A"], execution_order: execution_order.clone() })
        .add(ChainTask { id: "C", deps: &["B"], execution_order: execution_order.clone() })
        .add(ChainTask { id: "D", deps: &["C"], execution_order: execution_order.clone() })
        .add(ChainTask { id: "E", deps: &["D"], execution_order: execution_order.clone() });

    let runtime = supervisor.start().await.unwrap();
    
    tokio::time::sleep(Duration::from_millis(300)).await;
    
    runtime.shutdown().await;
    
    let order = execution_order.lock().unwrap();
    
    // Verify setup order respects dependencies
    // A-setup must come before B-setup, etc.
    let a_setup_pos = order.iter().position(|x| x == "A-setup").unwrap();
    let b_setup_pos = order.iter().position(|x| x == "B-setup").unwrap();
    let c_setup_pos = order.iter().position(|x| x == "C-setup").unwrap();
    let d_setup_pos = order.iter().position(|x| x == "D-setup").unwrap();
    let e_setup_pos = order.iter().position(|x| x == "E-setup").unwrap();
    
    assert!(a_setup_pos < b_setup_pos);
    assert!(b_setup_pos < c_setup_pos);
    assert!(c_setup_pos < d_setup_pos);
    assert!(d_setup_pos < e_setup_pos);
    
    // All 5 tasks should have executed (setup + run)
    assert_eq!(order.len(), 10);
}

#[tokio::test]
async fn test_multiple_independent_chains() {
    // Creates two independent chains that can run in parallel:
    // Chain 1: A -> B -> C
    // Chain 2: X -> Y -> Z
    
    struct IndependentTask {
        id: &'static str,
        deps: &'static [&'static str],
        completed: Arc<AtomicUsize>,
    }

    #[async_trait::async_trait]
    impl foxtive_supervisor::contracts::SupervisedTask for IndependentTask {
        fn id(&self) -> &'static str { self.id }
        
        fn dependencies(&self) -> &'static [&'static str] {
            self.deps
        }
        
        async fn run(&self) -> anyhow::Result<()> {
            self.completed.fetch_add(1, Ordering::SeqCst);
            tokio::time::sleep(Duration::from_millis(50)).await;
            Ok(())
        }
    }

    let completed = Arc::new(AtomicUsize::new(0));
    
    let supervisor = Supervisor::new()
        // Chain 1
        .add(IndependentTask { id: "A", deps: &[], completed: completed.clone() })
        .add(IndependentTask { id: "B", deps: &["A"], completed: completed.clone() })
        .add(IndependentTask { id: "C", deps: &["B"], completed: completed.clone() })
        // Chain 2
        .add(IndependentTask { id: "X", deps: &[], completed: completed.clone() })
        .add(IndependentTask { id: "Y", deps: &["X"], completed: completed.clone() })
        .add(IndependentTask { id: "Z", deps: &["Y"], completed: completed.clone() });

    let runtime = supervisor.start().await.unwrap();
    
    tokio::time::sleep(Duration::from_millis(300)).await;
    
    runtime.shutdown().await;
    
    // All 6 tasks should have completed
    assert_eq!(completed.load(Ordering::SeqCst), 6);
}

#[tokio::test]
async fn test_complex_dag_with_conditional_deps() {
    // Creates a complex DAG with conditional dependencies:
    //     Base
    //    / | \
    //   S1 S2 S3  (S3 depends on Base conditionally)
    //    \\ | /
    //    Aggregator
    
    struct ComplexTask {
        id: &'static str,
        regular_deps: &'static [&'static str],
        has_conditional_dep: bool,
        execution_count: Arc<AtomicUsize>,
    }

    #[async_trait::async_trait]
    impl foxtive_supervisor::contracts::SupervisedTask for ComplexTask {
        fn id(&self) -> &'static str { self.id }
        
        fn dependencies(&self) -> &'static [&'static str] {
            self.regular_deps
        }
        
        fn conditional_dependencies(&self) -> Vec<(&'static str, Box<dyn Fn() -> bool + Send + Sync>)> {
            if self.has_conditional_dep {
                vec![("Base", Box::new(|| true))]
            } else {
                vec![]
            }
        }
        
        async fn run(&self) -> anyhow::Result<()> {
            self.execution_count.fetch_add(1, Ordering::SeqCst);
            tokio::time::sleep(Duration::from_millis(30)).await;
            Ok(())
        }
    }

    let exec_count = Arc::new(AtomicUsize::new(0));
    
    let supervisor = Supervisor::new()
        .add(ComplexTask { 
            id: "Base", 
            regular_deps: &[], 
            has_conditional_dep: false,
            execution_count: exec_count.clone() 
        })
        .add(ComplexTask { 
            id: "S1", 
            regular_deps: &["Base"], 
            has_conditional_dep: false,
            execution_count: exec_count.clone() 
        })
        .add(ComplexTask { 
            id: "S2", 
            regular_deps: &["Base"], 
            has_conditional_dep: false,
            execution_count: exec_count.clone() 
        })
        .add(ComplexTask { 
            id: "S3", 
            regular_deps: &[], 
            has_conditional_dep: true,  // Conditional dep on Base
            execution_count: exec_count.clone() 
        })
        .add(ComplexTask { 
            id: "Aggregator", 
            regular_deps: &["S1", "S2"], 
            has_conditional_dep: false,  // No conditional deps here for simplicity
            execution_count: exec_count.clone() 
        });

    let runtime = supervisor.start().await.unwrap();
    
    tokio::time::sleep(Duration::from_millis(300)).await;
    
    runtime.shutdown().await;
    
    // All 5 tasks should have executed
    assert_eq!(exec_count.load(Ordering::SeqCst), 5);
}

#[tokio::test]
async fn test_dependency_with_groups() {
    // Tests that dependencies work correctly with task groups
    
    struct GroupedDepTask {
        id: &'static str,
        deps: &'static [&'static str],
        group: Option<&'static str>,
        setup_complete: Arc<AtomicUsize>,
    }

    #[async_trait::async_trait]
    impl foxtive_supervisor::contracts::SupervisedTask for GroupedDepTask {
        fn id(&self) -> &'static str { self.id }
        
        fn dependencies(&self) -> &'static [&'static str] {
            self.deps
        }
        
        fn group_id(&self) -> Option<&'static str> {
            self.group
        }
        
        async fn setup(&self) -> anyhow::Result<()> {
            self.setup_complete.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }
        
        async fn run(&self) -> anyhow::Result<()> {
            tokio::time::sleep(Duration::from_millis(50)).await;
            Ok(())
        }
    }

    let setup_complete = Arc::new(AtomicUsize::new(0));
    
    let supervisor = Supervisor::new()
        .add(GroupedDepTask { 
            id: "db-service", 
            deps: &[], 
            group: Some("infrastructure"),
            setup_complete: setup_complete.clone() 
        })
        .add(GroupedDepTask { 
            id: "cache-service", 
            deps: &[], 
            group: Some("infrastructure"),
            setup_complete: setup_complete.clone() 
        })
        .add(GroupedDepTask { 
            id: "api-server", 
            deps: &["db-service", "cache-service"], 
            group: Some("application"),
            setup_complete: setup_complete.clone() 
        });

    let runtime = supervisor.start().await.unwrap();
    
    tokio::time::sleep(Duration::from_millis(200)).await;
    
    // Verify all tasks are in correct groups
    let infra_tasks = runtime.list_group_tasks("infrastructure");
    assert_eq!(infra_tasks.len(), 2);
    
    let app_tasks = runtime.list_group_tasks("application");
    assert_eq!(app_tasks.len(), 1);
    
    runtime.shutdown().await;
    
    // All 3 tasks should have completed setup
    assert_eq!(setup_complete.load(Ordering::SeqCst), 3);
}

#[tokio::test]
async fn test_fan_out_fan_in_pattern() {
    // Tests fan-out/fan-in pattern:
    //         Source
    //        / | | \
    //      W1 W2 W3 W4  (workers)
    //        \ | | /
    //       Collector
    
    struct FanTask {
        id: &'static str,
        deps: &'static [&'static str],
        processed: Arc<AtomicUsize>,
    }

    #[async_trait::async_trait]
    impl foxtive_supervisor::contracts::SupervisedTask for FanTask {
        fn id(&self) -> &'static str { self.id }
        
        fn dependencies(&self) -> &'static [&'static str] {
            self.deps
        }
        
        async fn run(&self) -> anyhow::Result<()> {
            self.processed.fetch_add(1, Ordering::SeqCst);
            tokio::time::sleep(Duration::from_millis(50)).await;
            Ok(())
        }
    }

    let processed = Arc::new(AtomicUsize::new(0));
    
    let supervisor = Supervisor::new()
        .add(FanTask { 
            id: "Source", 
            deps: &[], 
            processed: processed.clone() 
        })
        .add(FanTask { 
            id: "W1", 
            deps: &["Source"], 
            processed: processed.clone() 
        })
        .add(FanTask { 
            id: "W2", 
            deps: &["Source"], 
            processed: processed.clone() 
        })
        .add(FanTask { 
            id: "W3", 
            deps: &["Source"], 
            processed: processed.clone() 
        })
        .add(FanTask { 
            id: "W4", 
            deps: &["Source"], 
            processed: processed.clone() 
        })
        .add(FanTask { 
            id: "Collector", 
            deps: &["W1", "W2", "W3", "W4"], 
            processed: processed.clone() 
        });

    let runtime = supervisor.start().await.unwrap();
    
    tokio::time::sleep(Duration::from_millis(300)).await;
    
    runtime.shutdown().await;
    
    // All 6 tasks should have processed
    assert_eq!(processed.load(Ordering::SeqCst), 6);
}