summaryrefslogtreecommitdiffstats
path: root/vendor/futures/tests/future_shared.rs
blob: bd69c1d7c14e40f2c918f124cdec999bf1743c36 (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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use futures::channel::oneshot;
use futures::executor::{block_on, LocalPool};
use futures::future::{self, FutureExt, LocalFutureObj, TryFutureExt};
use futures::task::LocalSpawn;
use std::cell::{Cell, RefCell};
use std::panic::AssertUnwindSafe;
use std::rc::Rc;
use std::task::Poll;
use std::thread;

struct CountClone(Rc<Cell<i32>>);

impl Clone for CountClone {
    fn clone(&self) -> Self {
        self.0.set(self.0.get() + 1);
        Self(self.0.clone())
    }
}

fn send_shared_oneshot_and_wait_on_multiple_threads(threads_number: u32) {
    let (tx, rx) = oneshot::channel::<i32>();
    let f = rx.shared();
    let join_handles = (0..threads_number)
        .map(|_| {
            let cloned_future = f.clone();
            thread::spawn(move || {
                assert_eq!(block_on(cloned_future).unwrap(), 6);
            })
        })
        .collect::<Vec<_>>();

    tx.send(6).unwrap();

    assert_eq!(block_on(f).unwrap(), 6);
    for join_handle in join_handles {
        join_handle.join().unwrap();
    }
}

#[test]
fn one_thread() {
    send_shared_oneshot_and_wait_on_multiple_threads(1);
}

#[test]
fn two_threads() {
    send_shared_oneshot_and_wait_on_multiple_threads(2);
}

#[test]
fn many_threads() {
    send_shared_oneshot_and_wait_on_multiple_threads(1000);
}

#[test]
fn drop_on_one_task_ok() {
    let (tx, rx) = oneshot::channel::<u32>();
    let f1 = rx.shared();
    let f2 = f1.clone();

    let (tx2, rx2) = oneshot::channel::<u32>();

    let t1 = thread::spawn(|| {
        let f = future::try_select(f1.map_err(|_| ()), rx2.map_err(|_| ()));
        drop(block_on(f));
    });

    let (tx3, rx3) = oneshot::channel::<u32>();

    let t2 = thread::spawn(|| {
        let _ = block_on(f2.map_ok(|x| tx3.send(x).unwrap()).map_err(|_| ()));
    });

    tx2.send(11).unwrap(); // cancel `f1`
    t1.join().unwrap();

    tx.send(42).unwrap(); // Should cause `f2` and then `rx3` to get resolved.
    let result = block_on(rx3).unwrap();
    assert_eq!(result, 42);
    t2.join().unwrap();
}

#[test]
fn drop_in_poll() {
    let slot1 = Rc::new(RefCell::new(None));
    let slot2 = slot1.clone();

    let future1 = future::lazy(move |_| {
        slot2.replace(None); // Drop future
        1
    })
    .shared();

    let future2 = LocalFutureObj::new(Box::new(future1.clone()));
    slot1.replace(Some(future2));

    assert_eq!(block_on(future1), 1);
}

#[test]
fn peek() {
    let mut local_pool = LocalPool::new();
    let spawn = &mut local_pool.spawner();

    let (tx0, rx0) = oneshot::channel::<i32>();
    let f1 = rx0.shared();
    let f2 = f1.clone();

    // Repeated calls on the original or clone do not change the outcome.
    for _ in 0..2 {
        assert!(f1.peek().is_none());
        assert!(f2.peek().is_none());
    }

    // Completing the underlying future has no effect, because the value has not been `poll`ed in.
    tx0.send(42).unwrap();
    for _ in 0..2 {
        assert!(f1.peek().is_none());
        assert!(f2.peek().is_none());
    }

    // Once the Shared has been polled, the value is peekable on the clone.
    spawn.spawn_local_obj(LocalFutureObj::new(Box::new(f1.map(|_| ())))).unwrap();
    local_pool.run();
    for _ in 0..2 {
        assert_eq!(*f2.peek().unwrap(), Ok(42));
    }
}

#[test]
fn downgrade() {
    let (tx, rx) = oneshot::channel::<i32>();
    let shared = rx.shared();
    // Since there are outstanding `Shared`s, we can get a `WeakShared`.
    let weak = shared.downgrade().unwrap();
    // It should upgrade fine right now.
    let mut shared2 = weak.upgrade().unwrap();

    tx.send(42).unwrap();
    assert_eq!(block_on(shared).unwrap(), 42);

    // We should still be able to get a new `WeakShared` and upgrade it
    // because `shared2` is outstanding.
    assert!(shared2.downgrade().is_some());
    assert!(weak.upgrade().is_some());

    assert_eq!(block_on(&mut shared2).unwrap(), 42);
    // Now that all `Shared`s have been exhausted, we should not be able
    // to get a new `WeakShared` or upgrade an existing one.
    assert!(weak.upgrade().is_none());
    assert!(shared2.downgrade().is_none());
}

#[test]
fn ptr_eq() {
    use future::FusedFuture;
    use std::collections::hash_map::DefaultHasher;
    use std::hash::Hasher;

    let (tx, rx) = oneshot::channel::<i32>();
    let shared = rx.shared();
    let mut shared2 = shared.clone();
    let mut hasher = DefaultHasher::new();
    let mut hasher2 = DefaultHasher::new();

    // Because these two futures share the same underlying future,
    // `ptr_eq` should return true.
    assert!(shared.ptr_eq(&shared2));
    // Equivalence relations are symmetric
    assert!(shared2.ptr_eq(&shared));

    // If `ptr_eq` returns true, they should hash to the same value.
    shared.ptr_hash(&mut hasher);
    shared2.ptr_hash(&mut hasher2);
    assert_eq!(hasher.finish(), hasher2.finish());

    tx.send(42).unwrap();
    assert_eq!(block_on(&mut shared2).unwrap(), 42);

    // Now that `shared2` has completed, `ptr_eq` should return false.
    assert!(shared2.is_terminated());
    assert!(!shared.ptr_eq(&shared2));

    // `ptr_eq` should continue to work for the other `Shared`.
    let shared3 = shared.clone();
    let mut hasher3 = DefaultHasher::new();
    assert!(shared.ptr_eq(&shared3));

    shared3.ptr_hash(&mut hasher3);
    assert_eq!(hasher.finish(), hasher3.finish());

    let (_tx, rx) = oneshot::channel::<i32>();
    let shared4 = rx.shared();

    // And `ptr_eq` should return false for two futures that don't share
    // the underlying future.
    assert!(!shared.ptr_eq(&shared4));
}

#[test]
fn dont_clone_in_single_owner_shared_future() {
    let counter = CountClone(Rc::new(Cell::new(0)));
    let (tx, rx) = oneshot::channel();

    let rx = rx.shared();

    tx.send(counter).ok().unwrap();

    assert_eq!(block_on(rx).unwrap().0.get(), 0);
}

#[test]
fn dont_do_unnecessary_clones_on_output() {
    let counter = CountClone(Rc::new(Cell::new(0)));
    let (tx, rx) = oneshot::channel();

    let rx = rx.shared();

    tx.send(counter).ok().unwrap();

    assert_eq!(block_on(rx.clone()).unwrap().0.get(), 1);
    assert_eq!(block_on(rx.clone()).unwrap().0.get(), 2);
    assert_eq!(block_on(rx).unwrap().0.get(), 2);
}

#[test]
fn shared_future_that_wakes_itself_until_pending_is_returned() {
    let proceed = Cell::new(false);
    let fut = futures::future::poll_fn(|cx| {
        if proceed.get() {
            Poll::Ready(())
        } else {
            cx.waker().wake_by_ref();
            Poll::Pending
        }
    })
    .shared();

    // The join future can only complete if the second future gets a chance to run after the first
    // has returned pending
    assert_eq!(block_on(futures::future::join(fut, async { proceed.set(true) })), ((), ()));
}

#[test]
#[should_panic(expected = "inner future panicked during poll")]
fn panic_while_poll() {
    let fut = futures::future::poll_fn::<i8, _>(|_cx| panic!("test")).shared();

    let fut_captured = fut.clone();
    std::panic::catch_unwind(AssertUnwindSafe(|| {
        block_on(fut_captured);
    }))
    .unwrap_err();

    block_on(fut);
}

#[test]
#[should_panic(expected = "test_marker")]
fn poll_while_panic() {
    struct S;

    impl Drop for S {
        fn drop(&mut self) {
            let fut = futures::future::ready(1).shared();
            assert_eq!(block_on(fut.clone()), 1);
            assert_eq!(block_on(fut), 1);
        }
    }

    let _s = S {};
    panic!("test_marker");
}