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
pub mod async_channel;
pub mod execution_context;
pub mod executor;
pub mod executor_address;
pub mod memory_tracker;
pub mod objects_pool;
pub mod packet;
pub mod thread_pool;
pub mod units_io;

#[cfg(test)]
mod tests {
    use crate::execution_manager::execution_context::{ExecutionContext, PoolAllocMode};
    use crate::execution_manager::executor::{AsyncExecutor, ExecutorReceiver};
    use crate::execution_manager::memory_tracker::MemoryTracker;
    use crate::execution_manager::objects_pool::PoolObjectTrait;
    use crate::execution_manager::packet::PacketTrait;
    use crate::execution_manager::thread_pool::ExecThreadPool;
    use crate::execution_manager::units_io::{ExecutorInput, ExecutorInputAddressMode};
    use std::future::Future;
    use std::ops::Deref;
    use std::sync::Arc;
    use std::time::Duration;

    struct TestExecutor {}

    impl PoolObjectTrait for usize {
        type InitData = ();

        fn allocate_new(_init_data: &Self::InitData) -> Self {
            0
        }

        fn reset(&mut self) {}
    }

    impl PacketTrait for usize {
        fn get_size(&self) -> usize {
            0
        }
    }

    impl AsyncExecutor for TestExecutor {
        type InputPacket = usize;
        type OutputPacket = usize;
        type GlobalParams = ();
        type InitData = ();
        type AsyncExecutorFuture<'a> = impl Future<Output = ()> + 'a;

        fn new() -> Self {
            Self {}
        }

        fn async_executor_main<'a>(
            &'a mut self,
            _global_params: &'a Self::GlobalParams,
            mut receiver: ExecutorReceiver<Self>,
            _memory_tracker: MemoryTracker<Self>,
        ) -> Self::AsyncExecutorFuture<'a> {
            async move {
                while let Ok((addr, _init_data)) = receiver.obtain_address().await {
                    let pool = addr.pool_alloc_await(1000).await;

                    while let Some(packet) = addr.receive_packet().await {
                        let mut x = *packet.deref();
                        for i in 0..100000000 {
                            x += i * x + i;
                        }
                        println!("X: {}", x);

                        tokio::time::sleep(Duration::from_millis(1000)).await;

                        drop(packet);
                        for exec in 0..2 {
                            let address = TestExecutor::generate_new_address(());
                            addr.declare_addresses(vec![address.clone()], 0);

                            let mut packet = pool.alloc_packet().await;
                            *packet = exec + x;
                            println!("Push packet {}", *packet.deref() * 2 + exec);
                            addr.packet_send(address.clone(), packet);
                        }
                    }
                }
                println!("Ended executor!");
            }
        }
    }

    #[test]
    #[ignore]
    fn test_executors() {
        let context = ExecutionContext::new();

        let readers_pool = ExecThreadPool::new(&context, 16, "readers-pool");

        readers_pool.register_executors::<TestExecutor>(
            640000,
            PoolAllocMode::Shared { capacity: 1024 },
            (),
            &Arc::new(()),
        );

        let strings = vec![1]; //, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

        let mut test_input =
            ExecutorInput::from_iter(strings.into_iter(), ExecutorInputAddressMode::Multiple);

        test_input.set_output_executor::<TestExecutor>(&context, (), 0);

        // readers_pool
        loop {
            std::thread::sleep(Duration::from_millis(1000));
        }
    }
}