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
|
extern crate futures;
extern crate tokio_mock_task;
extern crate tokio_sync;
use tokio_mock_task::*;
use tokio_sync::oneshot;
use futures::prelude::*;
macro_rules! assert_ready {
($e:expr) => {{
match $e {
Ok(futures::Async::Ready(v)) => v,
Ok(_) => panic!("not ready"),
Err(e) => panic!("error = {:?}", e),
}
}};
}
macro_rules! assert_not_ready {
($e:expr) => {{
match $e {
Ok(futures::Async::NotReady) => {}
Ok(futures::Async::Ready(v)) => panic!("ready; value = {:?}", v),
Err(e) => panic!("error = {:?}", e),
}
}};
}
trait AssertSend: Send {}
impl AssertSend for oneshot::Sender<i32> {}
impl AssertSend for oneshot::Receiver<i32> {}
#[test]
fn send_recv() {
let (tx, mut rx) = oneshot::channel();
let mut task = MockTask::new();
task.enter(|| {
assert_not_ready!(rx.poll());
});
assert!(tx.send(1).is_ok());
assert!(task.is_notified());
let val = assert_ready!(rx.poll());
assert_eq!(val, 1);
}
#[test]
fn close_tx() {
let (tx, mut rx) = oneshot::channel::<i32>();
let mut task = MockTask::new();
task.enter(|| {
assert_not_ready!(rx.poll());
});
drop(tx);
assert!(task.is_notified());
assert!(rx.poll().is_err());
}
#[test]
fn close_rx() {
// First, without checking poll_close()
//
let (tx, _) = oneshot::channel();
assert!(tx.send(1).is_err());
// Second, via poll_close();
let (mut tx, rx) = oneshot::channel();
let mut task = MockTask::new();
task.enter(|| assert_not_ready!(tx.poll_close()));
drop(rx);
assert!(task.is_notified());
assert!(tx.is_closed());
assert_ready!(tx.poll_close());
assert!(tx.send(1).is_err());
}
#[test]
fn explicit_close_poll() {
// First, with message sent
let (tx, mut rx) = oneshot::channel();
assert!(tx.send(1).is_ok());
rx.close();
let value = assert_ready!(rx.poll());
assert_eq!(value, 1);
println!("~~~~~~~~~ TWO ~~~~~~~~~~");
// Second, without the message sent
let (mut tx, mut rx) = oneshot::channel::<i32>();
let mut task = MockTask::new();
task.enter(|| assert_not_ready!(tx.poll_close()));
rx.close();
assert!(task.is_notified());
assert!(tx.is_closed());
assert_ready!(tx.poll_close());
assert!(tx.send(1).is_err());
assert!(rx.poll().is_err());
// Again, but without sending the value this time
let (mut tx, mut rx) = oneshot::channel::<i32>();
let mut task = MockTask::new();
task.enter(|| assert_not_ready!(tx.poll_close()));
rx.close();
assert!(task.is_notified());
assert!(tx.is_closed());
assert_ready!(tx.poll_close());
assert!(rx.poll().is_err());
}
#[test]
fn explicit_close_try_recv() {
// First, with message sent
let (tx, mut rx) = oneshot::channel();
assert!(tx.send(1).is_ok());
rx.close();
assert_eq!(rx.try_recv().unwrap(), 1);
println!("~~~~~~~~~ TWO ~~~~~~~~~~");
// Second, without the message sent
let (mut tx, mut rx) = oneshot::channel::<i32>();
let mut task = MockTask::new();
task.enter(|| assert_not_ready!(tx.poll_close()));
rx.close();
assert!(task.is_notified());
assert!(tx.is_closed());
assert_ready!(tx.poll_close());
assert!(rx.try_recv().is_err());
}
#[test]
#[should_panic]
fn close_try_recv_poll() {
let (_tx, mut rx) = oneshot::channel::<i32>();
let mut task = MockTask::new();
rx.close();
assert!(rx.try_recv().is_err());
task.enter(|| {
let _ = rx.poll();
});
}
#[test]
fn drops_tasks() {
let (mut tx, mut rx) = oneshot::channel::<i32>();
let mut tx_task = MockTask::new();
let mut rx_task = MockTask::new();
tx_task.enter(|| {
assert_not_ready!(tx.poll_close());
});
rx_task.enter(|| {
assert_not_ready!(rx.poll());
});
drop(tx);
drop(rx);
assert_eq!(1, tx_task.notifier_ref_count());
assert_eq!(1, rx_task.notifier_ref_count());
}
#[test]
fn receiver_changes_task() {
let (tx, mut rx) = oneshot::channel();
let mut task1 = MockTask::new();
let mut task2 = MockTask::new();
task1.enter(|| {
assert_not_ready!(rx.poll());
});
assert_eq!(2, task1.notifier_ref_count());
assert_eq!(1, task2.notifier_ref_count());
task2.enter(|| {
assert_not_ready!(rx.poll());
});
assert_eq!(1, task1.notifier_ref_count());
assert_eq!(2, task2.notifier_ref_count());
tx.send(1).unwrap();
assert!(!task1.is_notified());
assert!(task2.is_notified());
assert_ready!(rx.poll());
}
#[test]
fn sender_changes_task() {
let (mut tx, rx) = oneshot::channel::<i32>();
let mut task1 = MockTask::new();
let mut task2 = MockTask::new();
task1.enter(|| {
assert_not_ready!(tx.poll_close());
});
assert_eq!(2, task1.notifier_ref_count());
assert_eq!(1, task2.notifier_ref_count());
task2.enter(|| {
assert_not_ready!(tx.poll_close());
});
assert_eq!(1, task1.notifier_ref_count());
assert_eq!(2, task2.notifier_ref_count());
drop(rx);
assert!(!task1.is_notified());
assert!(task2.is_notified());
assert_ready!(tx.poll_close());
}
|