dag_scheduler/
lib.rs

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
pub mod scheduler;
pub mod nodes;
pub mod pipeline;
pub mod utils;


#[cfg(test)]
mod tests {
    use std::thread;

    use crossbeam::channel::{Receiver, Sender};

    use crate::nodes::CommonNode;

    use super::*;

    #[test]
    fn test_pipeline() {
        struct CtxV {
            pub a: i32
        } 

        fn init(_: Vec<Receiver<CtxV>>, outputs: Vec<Sender<CtxV>>) {
            for _ in 0..10000 {
                outputs[0].send(CtxV { a: 1 }).unwrap();
            }
        }

        fn add_one(inputs: Vec<Receiver<CtxV>>, outputs: Vec<Sender<CtxV>>)  {
            for mut ctx in inputs[0].iter() {
                ctx.a += 1;
                outputs[0].send(ctx).unwrap();
            }
        }

        fn add_two(inputs: Vec<Receiver<CtxV>>, outputs: Vec<Sender<CtxV>>)  {
            for mut ctx in inputs[0].iter() {
                ctx.a += 2;
                outputs[0].send(ctx).unwrap();
            }
        }

        let mut sche = scheduler::Scheduler::new();
        
        sche.add_node(false, &vec![], Box::new(CommonNode::new("first", 2, &[10], init)));
        sche.add_node(false, &vec![("first", 0)], Box::new(CommonNode::new("second", 2, &[10], add_one)));
        let outputs = sche.add_node(true, &vec![("second", 0)], Box::new(CommonNode::new("third", 2, &[10], add_two))).unwrap();

        let handler = thread::spawn(move|| {
            let mut result = vec![];
            for ctx in outputs[0].iter() {
                result.push(ctx.a);
            }
            return result;
        });

        sche.start();
        let result = handler.join().unwrap();
        println!("result:{result:?}");

    }




    #[test]
    fn test_lined_printer() {
        println!("a");
        println!("b");
        println!("c");
        print!("\x1b[3A\x1b[K");
        println!("A");
        println!("B");
        println!("C");
        
    }
}