summaryrefslogtreecommitdiffstats
path: root/tokio/src/runtime/tests/mock_park.rs
blob: 0fe28b36107699850858ad719dd2a76657128ab2 (plain)
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
#![allow(warnings)]

use crate::runtime::{Park, Unpark};

use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
use std::sync::Arc;
use std::time::Duration;

pub struct MockPark {
    parks: HashMap<usize, Arc<Inner>>,
}

#[derive(Clone)]
struct ParkImpl(Arc<Inner>);

struct Inner {
    unparked: AtomicBool,
}

impl MockPark {
    pub fn new() -> MockPark {
        MockPark {
            parks: HashMap::new(),
        }
    }

    pub fn is_unparked(&self, index: usize) -> bool {
        self.parks[&index].unparked.load(SeqCst)
    }

    pub fn clear(&self, index: usize) {
        self.parks[&index].unparked.store(false, SeqCst);
    }

    pub fn mk_park(&mut self, index: usize) -> impl Park {
        let inner = Arc::new(Inner {
            unparked: AtomicBool::new(false),
        });
        self.parks.insert(index, inner.clone());
        ParkImpl(inner)
    }
}

impl Park for ParkImpl {
    type Unpark = ParkImpl;
    type Error = ();

    fn unpark(&self) -> Self::Unpark {
        self.clone()
    }

    fn park(&mut self) -> Result<(), Self::Error> {
        unimplemented!();
    }

    fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error> {
        unimplemented!();
    }
}

impl Unpark for ParkImpl {
    fn unpark(&self) {
        self.0.unparked.store(true, SeqCst);
    }
}