1
use std::convert::Infallible;
2
use std::fmt::Debug;
3
use std::hash::Hash;
4

            
5
use super::Manager;
6
use crate::tasks::{Job, Keyed};
7

            
8
#[derive(Debug)]
9
struct Echo<T>(T);
10

            
11
impl<T> Job for Echo<T>
12
where
13
    T: Clone + Eq + Hash + Debug + Send + Sync + 'static,
14
{
15
    type Error = Infallible;
16
    type Output = T;
17

            
18
2
    fn execute(&mut self) -> Result<Self::Output, Self::Error> {
19
2
        Ok(self.0.clone())
20
2
    }
21
}
22

            
23
impl<T> Keyed<T> for Echo<T>
24
where
25
    T: Clone + Eq + Hash + Debug + Send + Sync + 'static,
26
{
27
3
    fn key(&self) -> T {
28
3
        self.0.clone()
29
3
    }
30
}
31

            
32
1
#[test]
33
1
fn simple() -> Result<(), flume::RecvError> {
34
1
    let manager = Manager::<usize>::default();
35
1
    manager.spawn_worker();
36
1
    let handle = manager.enqueue(Echo(1));
37
1
    if let Ok(value) = handle.receive()? {
38
1
        assert_eq!(value, 1);
39

            
40
1
        Ok(())
41
    } else {
42
        unreachable!()
43
    }
44
1
}
45

            
46
1
#[test]
47
1
fn keyed_simple() {
48
1
    let manager = Manager::<usize>::default();
49
1
    let handle = manager.lookup_or_enqueue(Echo(1));
50
1
    let handle2 = manager.lookup_or_enqueue(Echo(1));
51
1
    // Tests that they received the same job id
52
1
    assert_eq!(handle.id, handle2.id);
53
1
    let handle3 = manager.lookup_or_enqueue(Echo(1));
54
1
    assert_eq!(handle3.id, handle.id);
55

            
56
1
    manager.spawn_worker();
57
1

            
58
1
    let result1 = handle.receive().unwrap();
59
1
    let result2 = handle2.receive().unwrap();
60
1
    // Because they're all the same handle, if those have returned, this one
61
1
    // should be available without blocking.
62
1
    let result3 = handle3.receive().unwrap();
63

            
64
3
    for result in [result1, result2, result3] {
65
3
        assert_eq!(result.unwrap(), 1);
66
    }
67
1
}