aboutsummaryrefslogtreecommitdiff
path: root/src/sync/tests/loom_oneshot.rs
blob: c5f797207943cdb0b061397a6b6616206e154dc3 (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
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
use crate::sync::oneshot;

use futures::future::poll_fn;
use loom::future::block_on;
use loom::thread;
use std::task::Poll::{Pending, Ready};

#[test]
fn smoke() {
    loom::model(|| {
        let (tx, rx) = oneshot::channel();

        thread::spawn(move || {
            tx.send(1).unwrap();
        });

        let value = block_on(rx).unwrap();
        assert_eq!(1, value);
    });
}

#[test]
fn changing_rx_task() {
    loom::model(|| {
        let (tx, mut rx) = oneshot::channel();

        thread::spawn(move || {
            tx.send(1).unwrap();
        });

        let rx = thread::spawn(move || {
            let ready = block_on(poll_fn(|cx| match Pin::new(&mut rx).poll(cx) {
                Ready(Ok(value)) => {
                    assert_eq!(1, value);
                    Ready(true)
                }
                Ready(Err(_)) => unimplemented!(),
                Pending => Ready(false),
            }));

            if ready {
                None
            } else {
                Some(rx)
            }
        })
        .join()
        .unwrap();

        if let Some(rx) = rx {
            // Previous task parked, use a new task...
            let value = block_on(rx).unwrap();
            assert_eq!(1, value);
        }
    });
}

#[test]
fn try_recv_close() {
    // reproduces https://github.com/tokio-rs/tokio/issues/4225
    loom::model(|| {
        let (tx, mut rx) = oneshot::channel();
        thread::spawn(move || {
            let _ = tx.send(());
        });

        rx.close();
        let _ = rx.try_recv();
    })
}

#[test]
fn recv_closed() {
    // reproduces https://github.com/tokio-rs/tokio/issues/4225
    loom::model(|| {
        let (tx, mut rx) = oneshot::channel();

        thread::spawn(move || {
            let _ = tx.send(1);
        });

        rx.close();
        let _ = block_on(rx);
    });
}

// TODO: Move this into `oneshot` proper.

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

struct OnClose<'a> {
    tx: &'a mut oneshot::Sender<i32>,
}

impl<'a> OnClose<'a> {
    fn new(tx: &'a mut oneshot::Sender<i32>) -> Self {
        OnClose { tx }
    }
}

impl Future for OnClose<'_> {
    type Output = bool;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<bool> {
        let fut = self.get_mut().tx.closed();
        crate::pin!(fut);

        Ready(fut.poll(cx).is_ready())
    }
}

#[test]
fn changing_tx_task() {
    loom::model(|| {
        let (mut tx, rx) = oneshot::channel::<i32>();

        thread::spawn(move || {
            drop(rx);
        });

        let tx = thread::spawn(move || {
            let t1 = block_on(OnClose::new(&mut tx));

            if t1 {
                None
            } else {
                Some(tx)
            }
        })
        .join()
        .unwrap();

        if let Some(mut tx) = tx {
            // Previous task parked, use a new task...
            block_on(OnClose::new(&mut tx));
        }
    });
}