summaryrefslogtreecommitdiffstats
path: root/third_party/rust/futures-0.1.31/tests/eager_drop.rs
blob: 79f94d5ddc7dc54c4fa63ed27f59b4b7c87dd591 (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
extern crate futures;

use std::sync::mpsc::channel;

use futures::prelude::*;
use futures::sync::oneshot;
use futures::future::{err, ok};

mod support;
use support::*;

#[test]
fn map() {
    // Whatever runs after a `map` should have dropped the closure by that
    // point.
    let (tx, rx) = channel::<()>();
    let (tx2, rx2) = channel();
    err::<i32, i32>(1).map(move |a| { drop(tx); a }).map_err(move |_| {
        assert!(rx.recv().is_err());
        tx2.send(()).unwrap()
    }).forget();
    rx2.recv().unwrap();
}

#[test]
fn map_err() {
    // Whatever runs after a `map_err` should have dropped the closure by that
    // point.
    let (tx, rx) = channel::<()>();
    let (tx2, rx2) = channel();
    ok::<i32, i32>(1).map_err(move |a| { drop(tx); a }).map(move |_| {
        assert!(rx.recv().is_err());
        tx2.send(()).unwrap()
    }).forget();
    rx2.recv().unwrap();
}

struct FutureData<F, T> {
    _data: T,
    future: F,
}

impl<F: Future, T: Send + 'static> Future for FutureData<F, T> {
    type Item = F::Item;
    type Error = F::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.future.poll()
    }
}

#[test]
fn and_then_drops_eagerly() {
    let (c, p) = oneshot::channel::<()>();
    let (tx, rx) = channel::<()>();
    let (tx2, rx2) = channel();
    FutureData { _data: tx, future: p }.and_then(move |_| {
        assert!(rx.recv().is_err());
        tx2.send(()).unwrap();
        ok(1)
    }).forget();
    assert!(rx2.try_recv().is_err());
    c.send(()).unwrap();
    rx2.recv().unwrap();
}

// #[test]
// fn or_else_drops_eagerly() {
//     let (p1, c1) = oneshot::<(), ()>();
//     let (p2, c2) = oneshot::<(), ()>();
//     let (tx, rx) = channel::<()>();
//     let (tx2, rx2) = channel();
//     p1.map(move |a| { drop(tx); a }).or_else(move |_| {
//         assert!(rx.recv().is_err());
//         p2
//     }).map(move |_| tx2.send(()).unwrap()).forget();
//     assert!(rx2.try_recv().is_err());
//     c1.fail(());
//     assert!(rx2.try_recv().is_err());
//     c2.finish(());
//     rx2.recv().unwrap();
// }