Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 1 | use std::cell::Cell; |
| 2 | use std::future::Future; |
| 3 | use std::panic::catch_unwind; |
| 4 | use std::pin::Pin; |
| 5 | use std::task::Waker; |
| 6 | use std::task::{Context, Poll}; |
| 7 | use std::thread; |
| 8 | use std::time::Duration; |
| 9 | |
| 10 | use async_task::Task; |
| 11 | use crossbeam::atomic::AtomicCell; |
| 12 | use crossbeam::channel; |
| 13 | use lazy_static::lazy_static; |
| 14 | |
| 15 | // Creates a future with event counters. |
| 16 | // |
| 17 | // Usage: `future!(f, waker, POLL, DROP)` |
| 18 | // |
| 19 | // The future `f` always sleeps for 200 ms, and panics the second time it is polled. |
| 20 | // When it gets polled, `POLL` is incremented. |
| 21 | // When it gets dropped, `DROP` is incremented. |
| 22 | // |
| 23 | // Every time the future is run, it stores the waker into a global variable. |
| 24 | // This waker can be extracted using the `waker` function. |
| 25 | macro_rules! future { |
| 26 | ($name:pat, $waker:pat, $poll:ident, $drop:ident) => { |
| 27 | lazy_static! { |
| 28 | static ref $poll: AtomicCell<usize> = AtomicCell::new(0); |
| 29 | static ref $drop: AtomicCell<usize> = AtomicCell::new(0); |
| 30 | static ref WAKER: AtomicCell<Option<Waker>> = AtomicCell::new(None); |
| 31 | } |
| 32 | |
| 33 | let ($name, $waker) = { |
| 34 | struct Fut(Cell<bool>, Box<i32>); |
| 35 | |
| 36 | impl Future for Fut { |
| 37 | type Output = (); |
| 38 | |
| 39 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 40 | WAKER.store(Some(cx.waker().clone())); |
| 41 | $poll.fetch_add(1); |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 42 | thread::sleep(ms(400)); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 43 | |
| 44 | if self.0.get() { |
| 45 | panic!() |
| 46 | } else { |
| 47 | self.0.set(true); |
| 48 | Poll::Pending |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl Drop for Fut { |
| 54 | fn drop(&mut self) { |
| 55 | $drop.fetch_add(1); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | (Fut(Cell::new(false), Box::new(0)), || { |
| 60 | WAKER.swap(None).unwrap() |
| 61 | }) |
| 62 | }; |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | // Creates a schedule function with event counters. |
| 67 | // |
| 68 | // Usage: `schedule!(s, chan, SCHED, DROP)` |
| 69 | // |
| 70 | // The schedule function `s` pushes the task into `chan`. |
| 71 | // When it gets invoked, `SCHED` is incremented. |
| 72 | // When it gets dropped, `DROP` is incremented. |
| 73 | // |
| 74 | // Receiver `chan` extracts the task when it is scheduled. |
| 75 | macro_rules! schedule { |
| 76 | ($name:pat, $chan:pat, $sched:ident, $drop:ident) => { |
| 77 | lazy_static! { |
| 78 | static ref $sched: AtomicCell<usize> = AtomicCell::new(0); |
| 79 | static ref $drop: AtomicCell<usize> = AtomicCell::new(0); |
| 80 | } |
| 81 | |
| 82 | let ($name, $chan) = { |
| 83 | let (s, r) = channel::unbounded(); |
| 84 | |
| 85 | struct Guard(Box<i32>); |
| 86 | |
| 87 | impl Drop for Guard { |
| 88 | fn drop(&mut self) { |
| 89 | $drop.fetch_add(1); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | let guard = Guard(Box::new(0)); |
| 94 | let sched = move |task: Task<_>| { |
| 95 | &guard; |
| 96 | $sched.fetch_add(1); |
| 97 | s.send(task).unwrap(); |
| 98 | }; |
| 99 | |
| 100 | (sched, r) |
| 101 | }; |
| 102 | }; |
| 103 | } |
| 104 | |
| 105 | // Creates a task with event counters. |
| 106 | // |
| 107 | // Usage: `task!(task, handle f, s, DROP)` |
| 108 | // |
| 109 | // A task with future `f` and schedule function `s` is created. |
| 110 | // The `Task` and `JoinHandle` are bound to `task` and `handle`, respectively. |
| 111 | // When the tag inside the task gets dropped, `DROP` is incremented. |
| 112 | macro_rules! task { |
| 113 | ($task:pat, $handle: pat, $future:expr, $schedule:expr, $drop:ident) => { |
| 114 | lazy_static! { |
| 115 | static ref $drop: AtomicCell<usize> = AtomicCell::new(0); |
| 116 | } |
| 117 | |
| 118 | let ($task, $handle) = { |
| 119 | struct Tag(Box<i32>); |
| 120 | |
| 121 | impl Drop for Tag { |
| 122 | fn drop(&mut self) { |
| 123 | $drop.fetch_add(1); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | async_task::spawn($future, $schedule, Tag(Box::new(0))) |
| 128 | }; |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | fn ms(ms: u64) -> Duration { |
| 133 | Duration::from_millis(ms) |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn wake_during_run() { |
| 138 | future!(f, waker, POLL, DROP_F); |
| 139 | schedule!(s, chan, SCHEDULE, DROP_S); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 140 | task!(task, handle, f, s, DROP_T); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 141 | |
| 142 | task.run(); |
| 143 | let w = waker(); |
| 144 | w.wake_by_ref(); |
| 145 | let task = chan.recv().unwrap(); |
| 146 | |
| 147 | crossbeam::scope(|scope| { |
| 148 | scope.spawn(|_| { |
| 149 | assert!(catch_unwind(|| task.run()).is_err()); |
| 150 | drop(waker()); |
| 151 | assert_eq!(POLL.load(), 2); |
| 152 | assert_eq!(SCHEDULE.load(), 1); |
| 153 | assert_eq!(DROP_F.load(), 1); |
| 154 | assert_eq!(DROP_S.load(), 1); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 155 | assert_eq!(DROP_T.load(), 1); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 156 | assert_eq!(chan.len(), 0); |
| 157 | }); |
| 158 | |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 159 | thread::sleep(ms(200)); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 160 | |
| 161 | w.wake(); |
| 162 | drop(handle); |
| 163 | assert_eq!(POLL.load(), 2); |
| 164 | assert_eq!(SCHEDULE.load(), 1); |
| 165 | assert_eq!(DROP_F.load(), 0); |
| 166 | assert_eq!(DROP_S.load(), 0); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 167 | assert_eq!(DROP_T.load(), 0); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 168 | assert_eq!(chan.len(), 0); |
| 169 | |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 170 | thread::sleep(ms(400)); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 171 | |
| 172 | assert_eq!(POLL.load(), 2); |
| 173 | assert_eq!(SCHEDULE.load(), 1); |
| 174 | assert_eq!(DROP_F.load(), 1); |
| 175 | assert_eq!(DROP_S.load(), 1); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 176 | assert_eq!(DROP_T.load(), 1); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 177 | assert_eq!(chan.len(), 0); |
| 178 | }) |
| 179 | .unwrap(); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn cancel_during_run() { |
| 184 | future!(f, waker, POLL, DROP_F); |
| 185 | schedule!(s, chan, SCHEDULE, DROP_S); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 186 | task!(task, handle, f, s, DROP_T); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 187 | |
| 188 | task.run(); |
| 189 | let w = waker(); |
| 190 | w.wake(); |
| 191 | let task = chan.recv().unwrap(); |
| 192 | |
| 193 | crossbeam::scope(|scope| { |
| 194 | scope.spawn(|_| { |
| 195 | assert!(catch_unwind(|| task.run()).is_err()); |
| 196 | drop(waker()); |
| 197 | assert_eq!(POLL.load(), 2); |
| 198 | assert_eq!(SCHEDULE.load(), 1); |
| 199 | assert_eq!(DROP_F.load(), 1); |
| 200 | assert_eq!(DROP_S.load(), 1); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 201 | assert_eq!(DROP_T.load(), 1); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 202 | assert_eq!(chan.len(), 0); |
| 203 | }); |
| 204 | |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 205 | thread::sleep(ms(200)); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 206 | |
| 207 | handle.cancel(); |
| 208 | assert_eq!(POLL.load(), 2); |
| 209 | assert_eq!(SCHEDULE.load(), 1); |
| 210 | assert_eq!(DROP_F.load(), 0); |
| 211 | assert_eq!(DROP_S.load(), 0); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 212 | assert_eq!(DROP_T.load(), 0); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 213 | assert_eq!(chan.len(), 0); |
| 214 | |
| 215 | drop(handle); |
| 216 | assert_eq!(POLL.load(), 2); |
| 217 | assert_eq!(SCHEDULE.load(), 1); |
| 218 | assert_eq!(DROP_F.load(), 0); |
| 219 | assert_eq!(DROP_S.load(), 0); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 220 | assert_eq!(DROP_T.load(), 0); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 221 | assert_eq!(chan.len(), 0); |
| 222 | |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 223 | thread::sleep(ms(400)); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 224 | |
| 225 | assert_eq!(POLL.load(), 2); |
| 226 | assert_eq!(SCHEDULE.load(), 1); |
| 227 | assert_eq!(DROP_F.load(), 1); |
| 228 | assert_eq!(DROP_S.load(), 1); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 229 | assert_eq!(DROP_T.load(), 1); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 230 | assert_eq!(chan.len(), 0); |
| 231 | }) |
| 232 | .unwrap(); |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn wake_and_cancel_during_run() { |
| 237 | future!(f, waker, POLL, DROP_F); |
| 238 | schedule!(s, chan, SCHEDULE, DROP_S); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 239 | task!(task, handle, f, s, DROP_T); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 240 | |
| 241 | task.run(); |
| 242 | let w = waker(); |
| 243 | w.wake_by_ref(); |
| 244 | let task = chan.recv().unwrap(); |
| 245 | |
| 246 | crossbeam::scope(|scope| { |
| 247 | scope.spawn(|_| { |
| 248 | assert!(catch_unwind(|| task.run()).is_err()); |
| 249 | drop(waker()); |
| 250 | assert_eq!(POLL.load(), 2); |
| 251 | assert_eq!(SCHEDULE.load(), 1); |
| 252 | assert_eq!(DROP_F.load(), 1); |
| 253 | assert_eq!(DROP_S.load(), 1); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 254 | assert_eq!(DROP_T.load(), 1); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 255 | assert_eq!(chan.len(), 0); |
| 256 | }); |
| 257 | |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 258 | thread::sleep(ms(200)); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 259 | |
| 260 | w.wake(); |
| 261 | assert_eq!(POLL.load(), 2); |
| 262 | assert_eq!(SCHEDULE.load(), 1); |
| 263 | assert_eq!(DROP_F.load(), 0); |
| 264 | assert_eq!(DROP_S.load(), 0); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 265 | assert_eq!(DROP_T.load(), 0); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 266 | assert_eq!(chan.len(), 0); |
| 267 | |
| 268 | handle.cancel(); |
| 269 | assert_eq!(POLL.load(), 2); |
| 270 | assert_eq!(SCHEDULE.load(), 1); |
| 271 | assert_eq!(DROP_F.load(), 0); |
| 272 | assert_eq!(DROP_S.load(), 0); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 273 | assert_eq!(DROP_T.load(), 0); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 274 | assert_eq!(chan.len(), 0); |
| 275 | |
| 276 | drop(handle); |
| 277 | assert_eq!(POLL.load(), 2); |
| 278 | assert_eq!(SCHEDULE.load(), 1); |
| 279 | assert_eq!(DROP_F.load(), 0); |
| 280 | assert_eq!(DROP_S.load(), 0); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 281 | assert_eq!(DROP_T.load(), 0); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 282 | assert_eq!(chan.len(), 0); |
| 283 | |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 284 | thread::sleep(ms(400)); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 285 | |
| 286 | assert_eq!(POLL.load(), 2); |
| 287 | assert_eq!(SCHEDULE.load(), 1); |
| 288 | assert_eq!(DROP_F.load(), 1); |
| 289 | assert_eq!(DROP_S.load(), 1); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 290 | assert_eq!(DROP_T.load(), 1); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 291 | assert_eq!(chan.len(), 0); |
| 292 | }) |
| 293 | .unwrap(); |
| 294 | } |
| 295 | |
| 296 | #[test] |
| 297 | fn cancel_and_wake_during_run() { |
| 298 | future!(f, waker, POLL, DROP_F); |
| 299 | schedule!(s, chan, SCHEDULE, DROP_S); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 300 | task!(task, handle, f, s, DROP_T); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 301 | |
| 302 | task.run(); |
| 303 | let w = waker(); |
| 304 | w.wake_by_ref(); |
| 305 | let task = chan.recv().unwrap(); |
| 306 | |
| 307 | crossbeam::scope(|scope| { |
| 308 | scope.spawn(|_| { |
| 309 | assert!(catch_unwind(|| task.run()).is_err()); |
| 310 | drop(waker()); |
| 311 | assert_eq!(POLL.load(), 2); |
| 312 | assert_eq!(SCHEDULE.load(), 1); |
| 313 | assert_eq!(DROP_F.load(), 1); |
| 314 | assert_eq!(DROP_S.load(), 1); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 315 | assert_eq!(DROP_T.load(), 1); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 316 | assert_eq!(chan.len(), 0); |
| 317 | }); |
| 318 | |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 319 | thread::sleep(ms(200)); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 320 | |
| 321 | handle.cancel(); |
| 322 | assert_eq!(POLL.load(), 2); |
| 323 | assert_eq!(SCHEDULE.load(), 1); |
| 324 | assert_eq!(DROP_F.load(), 0); |
| 325 | assert_eq!(DROP_S.load(), 0); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 326 | assert_eq!(DROP_T.load(), 0); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 327 | assert_eq!(chan.len(), 0); |
| 328 | |
| 329 | drop(handle); |
| 330 | assert_eq!(POLL.load(), 2); |
| 331 | assert_eq!(SCHEDULE.load(), 1); |
| 332 | assert_eq!(DROP_F.load(), 0); |
| 333 | assert_eq!(DROP_S.load(), 0); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 334 | assert_eq!(DROP_T.load(), 0); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 335 | assert_eq!(chan.len(), 0); |
| 336 | |
| 337 | w.wake(); |
| 338 | assert_eq!(POLL.load(), 2); |
| 339 | assert_eq!(SCHEDULE.load(), 1); |
| 340 | assert_eq!(DROP_F.load(), 0); |
| 341 | assert_eq!(DROP_S.load(), 0); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 342 | assert_eq!(DROP_T.load(), 0); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 343 | assert_eq!(chan.len(), 0); |
| 344 | |
Stjepan Glavina | a94d2f4 | 2020-01-25 00:14:33 +0100 | [diff] [blame] | 345 | thread::sleep(ms(400)); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 346 | |
| 347 | assert_eq!(POLL.load(), 2); |
| 348 | assert_eq!(SCHEDULE.load(), 1); |
| 349 | assert_eq!(DROP_F.load(), 1); |
| 350 | assert_eq!(DROP_S.load(), 1); |
Stjepan Glavina | 45a2c1f | 2019-12-31 01:38:58 +0100 | [diff] [blame] | 351 | assert_eq!(DROP_T.load(), 1); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 352 | assert_eq!(chan.len(), 0); |
| 353 | }) |
| 354 | .unwrap(); |
| 355 | } |