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