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
// Copyright 2020-2021 Gnosis Ltd.
// SPDX-License-Identifier: Apache-2.0

use super::async_queue::AsyncQueue;
use std::{sync::Arc, thread};

/// Creates thread and uses AsyncQueue to queue and execute
/// items that are received in queue function.
/// Queue is restricted by max_items and will return false if
/// we want enqueue items on full queue.
pub struct ExecutionQueue<ITEM>
where
    ITEM: 'static + Send,
{
    async_queue: Arc<AsyncQueue<ITEM>>,
    thread_handle: Option<thread::JoinHandle<()>>,
}

impl<ITEM> ExecutionQueue<ITEM>
where
    ITEM: 'static + Send,
{
    /// Create and spawn one thread that executed `exec` every time item is queue-ed.
    pub fn new<EXEC: FnMut(Vec<ITEM>) + Send + 'static>(
        max_items: usize,
        batch_size: usize,
        mut exec: EXEC,
        symbolic_name: &str,
    ) -> Self {
        let mut queue = ExecutionQueue {
            async_queue: Arc::new(AsyncQueue::new(max_items, batch_size)),
            thread_handle: None,
        };

        let async_queue = queue.async_queue.clone();
        // main thread logic is in here
        queue.thread_handle = Some(
            thread::Builder::new()
                .name(symbolic_name.to_string())
                .spawn(move || {
                    while let Some(item) = async_queue.wait_for_batch() {
                        exec(item)
                    }
                })
                .expect("Expect to run thread"),
        );
        queue
    }

    /// Add item to queue
    pub fn enqueue(&self, item: ITEM) -> bool {
        self.async_queue.enqueue(item)
    }

    /// End execution and close thread. Pending items will be abandoned.
    pub fn end(&mut self) {
        self.async_queue.end();
        if let Some(handle) = self.thread_handle.take() {
            handle.join().expect("Join handle should not panic");
        }
    }

    /// Items currently in queue.
    pub fn len(&self) -> usize {
        self.async_queue.len()
    }
}

#[cfg(test)]
mod tests {
    use std::{
        sync::{
            mpsc,
            mpsc::{Receiver, Sender},
        },
        time::Duration,
    };

    use super::*;

    #[test]
    fn simple_example_execution_queue() {
        let (tx, rx): (Sender<()>, Receiver<()>) = mpsc::channel();
        let ev = vec![0, 1, 2, 5, 10, 12, 13, 34, 45];
        let iv = ev.clone();
        let mut index = 0;
        let mut q = ExecutionQueue::new(
            10,
            1,
            move |item: Vec<u32>| {
                assert!(ev[index] == item[0]);
                index += 1;
                if index == ev.len() {
                    tx.send(()).expect("Expect to work for testing");
                }
            },
            "symb_name",
        );
        for i in iv.iter() {
            assert!(q.enqueue(*i))
        }
        rx.recv_timeout(Duration::from_secs(3))
            .expect("Expect to work for testing");
        q.end()
    }

    #[test]
    fn execute_batches_execution_queue() {
        let (tx, rx): (Sender<()>, Receiver<()>) = mpsc::channel();
        let ev = vec![0, 1, 2, 5, 10, 12, 13, 34, 45, 1, 2, 3, 4, 5, 6, 7];
        let iv = ev.clone();
        let mut index = 0;
        let mut at_least_one_3batch = false;
        let mut q = ExecutionQueue::new(
            20,
            3,
            move |item: Vec<u32>| {
                if !at_least_one_3batch {
                    at_least_one_3batch = item.len() == 3;
                }
                for i in item {
                    assert!(ev[index] == i);
                    index += 1;
                }
                if index == ev.len() {
                    assert!(
                        at_least_one_3batch,
                        "We expect at least one batch of 3 items"
                    );
                    tx.send(()).expect("Expect to work for testing");
                }
            },
            "symb_name",
        );
        for i in iv.iter() {
            assert!(q.enqueue(*i), "should insert all items")
        }
        rx.recv_timeout(Duration::from_secs(3))
            .expect("Expect to work for testing");
        q.end()
    }

    #[test]
    fn start_stop_execution_queue() {
        let mut q = ExecutionQueue::new(10, 1, move |_: Vec<u32>| {}, "");
        q.end()
    }

    #[test]
    fn overfill_execution_queue() {
        let (tx, rx): (Sender<()>, Receiver<()>) = mpsc::channel();

        let mut q = ExecutionQueue::new(
            1,
            1,
            move |_: Vec<u32>| {
                rx.recv_timeout(Duration::from_secs(3))
                    .expect("Expect to work for testing");
            },
            "",
        );

        assert_eq!(q.enqueue(10), true); // one to be currently executed
        thread::sleep(Duration::from_millis(100)); // wait for queue to take that item for execution
        assert_eq!(q.enqueue(15), true); // one queue in VecQueue
        assert_eq!(q.enqueue(15), false); // queue is full
        tx.send(()).expect("Expect to work for testing"); // process one item
        thread::sleep(Duration::from_millis(100)); // sleep so that queue continue processing current item and take next one.
        assert_eq!(q.enqueue(15), true); // one item added
        assert_eq!(q.enqueue(15), false); // queue is full

        // clean queue so that it does not panic in closure.
        tx.send(()).expect("Expect to work for testing");
        tx.send(()).expect("Expect to work for testing");
        tx.send(()).expect("Expect to work for testing");

        // close thread
        q.end()
    }
}