| 1 |
#![warn(rust_2018_idioms)] |
| 2 |
|
| 3 |
use tokio::pin; |
| 4 |
use tokio_util::sync::{CancellationToken, WaitForCancellationFuture}; |
| 5 |
|
| 6 |
use core::future::Future; |
| 7 |
use core::task::{Context, Poll}; |
| 8 |
use futures_test::task::new_count_waker; |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn cancel_token() { |
| 12 |
let (waker, wake_counter) = new_count_waker(); |
| 13 |
let token = CancellationToken::new(); |
| 14 |
assert!(!token.is_cancelled()); |
| 15 |
|
| 16 |
let wait_fut = token.cancelled(); |
| 17 |
pin!(wait_fut); |
| 18 |
|
| 19 |
assert_eq!( |
| 20 |
Poll::Pending, |
| 21 |
wait_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 22 |
); |
| 23 |
assert_eq!(wake_counter, 0); |
| 24 |
|
| 25 |
let wait_fut_2 = token.cancelled(); |
| 26 |
pin!(wait_fut_2); |
| 27 |
|
| 28 |
token.cancel(); |
| 29 |
assert_eq!(wake_counter, 1); |
| 30 |
assert!(token.is_cancelled()); |
| 31 |
|
| 32 |
assert_eq!( |
| 33 |
Poll::Ready(()), |
| 34 |
wait_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 35 |
); |
| 36 |
assert_eq!( |
| 37 |
Poll::Ready(()), |
| 38 |
wait_fut_2.as_mut().poll(&mut Context::from_waker(&waker)) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn cancel_child_token_through_parent() { |
| 44 |
let (waker, wake_counter) = new_count_waker(); |
| 45 |
let token = CancellationToken::new(); |
| 46 |
|
| 47 |
let child_token = token.child_token(); |
| 48 |
assert!(!child_token.is_cancelled()); |
| 49 |
|
| 50 |
let child_fut = child_token.cancelled(); |
| 51 |
pin!(child_fut); |
| 52 |
let parent_fut = token.cancelled(); |
| 53 |
pin!(parent_fut); |
| 54 |
|
| 55 |
assert_eq!( |
| 56 |
Poll::Pending, |
| 57 |
child_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 58 |
); |
| 59 |
assert_eq!( |
| 60 |
Poll::Pending, |
| 61 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 62 |
); |
| 63 |
assert_eq!(wake_counter, 0); |
| 64 |
|
| 65 |
token.cancel(); |
| 66 |
assert_eq!(wake_counter, 2); |
| 67 |
assert!(token.is_cancelled()); |
| 68 |
assert!(child_token.is_cancelled()); |
| 69 |
|
| 70 |
assert_eq!( |
| 71 |
Poll::Ready(()), |
| 72 |
child_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 73 |
); |
| 74 |
assert_eq!( |
| 75 |
Poll::Ready(()), |
| 76 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn cancel_grandchild_token_through_parent_if_child_was_dropped() { |
| 82 |
let (waker, wake_counter) = new_count_waker(); |
| 83 |
let token = CancellationToken::new(); |
| 84 |
|
| 85 |
let intermediate_token = token.child_token(); |
| 86 |
let child_token = intermediate_token.child_token(); |
| 87 |
drop(intermediate_token); |
| 88 |
assert!(!child_token.is_cancelled()); |
| 89 |
|
| 90 |
let child_fut = child_token.cancelled(); |
| 91 |
pin!(child_fut); |
| 92 |
let parent_fut = token.cancelled(); |
| 93 |
pin!(parent_fut); |
| 94 |
|
| 95 |
assert_eq!( |
| 96 |
Poll::Pending, |
| 97 |
child_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 98 |
); |
| 99 |
assert_eq!( |
| 100 |
Poll::Pending, |
| 101 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 102 |
); |
| 103 |
assert_eq!(wake_counter, 0); |
| 104 |
|
| 105 |
token.cancel(); |
| 106 |
assert_eq!(wake_counter, 2); |
| 107 |
assert!(token.is_cancelled()); |
| 108 |
assert!(child_token.is_cancelled()); |
| 109 |
|
| 110 |
assert_eq!( |
| 111 |
Poll::Ready(()), |
| 112 |
child_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 113 |
); |
| 114 |
assert_eq!( |
| 115 |
Poll::Ready(()), |
| 116 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn cancel_child_token_without_parent() { |
| 122 |
let (waker, wake_counter) = new_count_waker(); |
| 123 |
let token = CancellationToken::new(); |
| 124 |
|
| 125 |
let child_token_1 = token.child_token(); |
| 126 |
|
| 127 |
let child_fut = child_token_1.cancelled(); |
| 128 |
pin!(child_fut); |
| 129 |
let parent_fut = token.cancelled(); |
| 130 |
pin!(parent_fut); |
| 131 |
|
| 132 |
assert_eq!( |
| 133 |
Poll::Pending, |
| 134 |
child_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 135 |
); |
| 136 |
assert_eq!( |
| 137 |
Poll::Pending, |
| 138 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 139 |
); |
| 140 |
assert_eq!(wake_counter, 0); |
| 141 |
|
| 142 |
child_token_1.cancel(); |
| 143 |
assert_eq!(wake_counter, 1); |
| 144 |
assert!(!token.is_cancelled()); |
| 145 |
assert!(child_token_1.is_cancelled()); |
| 146 |
|
| 147 |
assert_eq!( |
| 148 |
Poll::Ready(()), |
| 149 |
child_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 150 |
); |
| 151 |
assert_eq!( |
| 152 |
Poll::Pending, |
| 153 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 154 |
); |
| 155 |
|
| 156 |
let child_token_2 = token.child_token(); |
| 157 |
let child_fut_2 = child_token_2.cancelled(); |
| 158 |
pin!(child_fut_2); |
| 159 |
|
| 160 |
assert_eq!( |
| 161 |
Poll::Pending, |
| 162 |
child_fut_2.as_mut().poll(&mut Context::from_waker(&waker)) |
| 163 |
); |
| 164 |
assert_eq!( |
| 165 |
Poll::Pending, |
| 166 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 167 |
); |
| 168 |
|
| 169 |
token.cancel(); |
| 170 |
assert_eq!(wake_counter, 3); |
| 171 |
assert!(token.is_cancelled()); |
| 172 |
assert!(child_token_2.is_cancelled()); |
| 173 |
|
| 174 |
assert_eq!( |
| 175 |
Poll::Ready(()), |
| 176 |
child_fut_2.as_mut().poll(&mut Context::from_waker(&waker)) |
| 177 |
); |
| 178 |
assert_eq!( |
| 179 |
Poll::Ready(()), |
| 180 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn create_child_token_after_parent_was_cancelled() { |
| 186 |
for drop_child_first in [true, false].iter().cloned() { |
| 187 |
let (waker, wake_counter) = new_count_waker(); |
| 188 |
let token = CancellationToken::new(); |
| 189 |
token.cancel(); |
| 190 |
|
| 191 |
let child_token = token.child_token(); |
| 192 |
assert!(child_token.is_cancelled()); |
| 193 |
|
| 194 |
{ |
| 195 |
let child_fut = child_token.cancelled(); |
| 196 |
pin!(child_fut); |
| 197 |
let parent_fut = token.cancelled(); |
| 198 |
pin!(parent_fut); |
| 199 |
|
| 200 |
assert_eq!( |
| 201 |
Poll::Ready(()), |
| 202 |
child_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 203 |
); |
| 204 |
assert_eq!( |
| 205 |
Poll::Ready(()), |
| 206 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 207 |
); |
| 208 |
assert_eq!(wake_counter, 0); |
| 209 |
|
| 210 |
drop(child_fut); |
| 211 |
drop(parent_fut); |
| 212 |
} |
| 213 |
|
| 214 |
if drop_child_first { |
| 215 |
drop(child_token); |
| 216 |
drop(token); |
| 217 |
} else { |
| 218 |
drop(token); |
| 219 |
drop(child_token); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn drop_multiple_child_tokens() { |
| 226 |
for drop_first_child_first in &[true, false] { |
| 227 |
let token = CancellationToken::new(); |
| 228 |
let mut child_tokens = [None, None, None]; |
| 229 |
for child in &mut child_tokens { |
| 230 |
*child = Some(token.child_token()); |
| 231 |
} |
| 232 |
|
| 233 |
assert!(!token.is_cancelled()); |
| 234 |
assert!(!child_tokens[0].as_ref().unwrap().is_cancelled()); |
| 235 |
|
| 236 |
for i in 0..child_tokens.len() { |
| 237 |
if *drop_first_child_first { |
| 238 |
child_tokens[i] = None; |
| 239 |
} else { |
| 240 |
child_tokens[child_tokens.len() - 1 - i] = None; |
| 241 |
} |
| 242 |
assert!(!token.is_cancelled()); |
| 243 |
} |
| 244 |
|
| 245 |
drop(token); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn cancel_only_all_descendants() { |
| 251 |
// ARRANGE |
| 252 |
let (waker, wake_counter) = new_count_waker(); |
| 253 |
|
| 254 |
let parent_token = CancellationToken::new(); |
| 255 |
let token = parent_token.child_token(); |
| 256 |
let sibling_token = parent_token.child_token(); |
| 257 |
let child1_token = token.child_token(); |
| 258 |
let child2_token = token.child_token(); |
| 259 |
let grandchild_token = child1_token.child_token(); |
| 260 |
let grandchild2_token = child1_token.child_token(); |
| 261 |
let grandgrandchild_token = grandchild_token.child_token(); |
| 262 |
|
| 263 |
assert!(!parent_token.is_cancelled()); |
| 264 |
assert!(!token.is_cancelled()); |
| 265 |
assert!(!sibling_token.is_cancelled()); |
| 266 |
assert!(!child1_token.is_cancelled()); |
| 267 |
assert!(!child2_token.is_cancelled()); |
| 268 |
assert!(!grandchild_token.is_cancelled()); |
| 269 |
assert!(!grandchild2_token.is_cancelled()); |
| 270 |
assert!(!grandgrandchild_token.is_cancelled()); |
| 271 |
|
| 272 |
let parent_fut = parent_token.cancelled(); |
| 273 |
let fut = token.cancelled(); |
| 274 |
let sibling_fut = sibling_token.cancelled(); |
| 275 |
let child1_fut = child1_token.cancelled(); |
| 276 |
let child2_fut = child2_token.cancelled(); |
| 277 |
let grandchild_fut = grandchild_token.cancelled(); |
| 278 |
let grandchild2_fut = grandchild2_token.cancelled(); |
| 279 |
let grandgrandchild_fut = grandgrandchild_token.cancelled(); |
| 280 |
|
| 281 |
pin!(parent_fut); |
| 282 |
pin!(fut); |
| 283 |
pin!(sibling_fut); |
| 284 |
pin!(child1_fut); |
| 285 |
pin!(child2_fut); |
| 286 |
pin!(grandchild_fut); |
| 287 |
pin!(grandchild2_fut); |
| 288 |
pin!(grandgrandchild_fut); |
| 289 |
|
| 290 |
assert_eq!( |
| 291 |
Poll::Pending, |
| 292 |
parent_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 293 |
); |
| 294 |
assert_eq!( |
| 295 |
Poll::Pending, |
| 296 |
fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 297 |
); |
| 298 |
assert_eq!( |
| 299 |
Poll::Pending, |
| 300 |
sibling_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 301 |
); |
| 302 |
assert_eq!( |
| 303 |
Poll::Pending, |
| 304 |
child1_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 305 |
); |
| 306 |
assert_eq!( |
| 307 |
Poll::Pending, |
| 308 |
child2_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 309 |
); |
| 310 |
assert_eq!( |
| 311 |
Poll::Pending, |
| 312 |
grandchild_fut |
| 313 |
.as_mut() |
| 314 |
.poll(&mut Context::from_waker(&waker)) |
| 315 |
); |
| 316 |
assert_eq!( |
| 317 |
Poll::Pending, |
| 318 |
grandchild2_fut |
| 319 |
.as_mut() |
| 320 |
.poll(&mut Context::from_waker(&waker)) |
| 321 |
); |
| 322 |
assert_eq!( |
| 323 |
Poll::Pending, |
| 324 |
grandgrandchild_fut |
| 325 |
.as_mut() |
| 326 |
.poll(&mut Context::from_waker(&waker)) |
| 327 |
); |
| 328 |
assert_eq!(wake_counter, 0); |
| 329 |
|
| 330 |
// ACT |
| 331 |
token.cancel(); |
| 332 |
|
| 333 |
// ASSERT |
| 334 |
assert_eq!(wake_counter, 6); |
| 335 |
assert!(!parent_token.is_cancelled()); |
| 336 |
assert!(token.is_cancelled()); |
| 337 |
assert!(!sibling_token.is_cancelled()); |
| 338 |
assert!(child1_token.is_cancelled()); |
| 339 |
assert!(child2_token.is_cancelled()); |
| 340 |
assert!(grandchild_token.is_cancelled()); |
| 341 |
assert!(grandchild2_token.is_cancelled()); |
| 342 |
assert!(grandgrandchild_token.is_cancelled()); |
| 343 |
|
| 344 |
assert_eq!( |
| 345 |
Poll::Ready(()), |
| 346 |
fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 347 |
); |
| 348 |
assert_eq!( |
| 349 |
Poll::Ready(()), |
| 350 |
child1_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 351 |
); |
| 352 |
assert_eq!( |
| 353 |
Poll::Ready(()), |
| 354 |
child2_fut.as_mut().poll(&mut Context::from_waker(&waker)) |
| 355 |
); |
| 356 |
assert_eq!( |
| 357 |
Poll::Ready(()), |
| 358 |
grandchild_fut |
| 359 |
.as_mut() |
| 360 |
.poll(&mut Context::from_waker(&waker)) |
| 361 |
); |
| 362 |
assert_eq!( |
| 363 |
Poll::Ready(()), |
| 364 |
grandchild2_fut |
| 365 |
.as_mut() |
| 366 |
.poll(&mut Context::from_waker(&waker)) |
| 367 |
); |
| 368 |
assert_eq!( |
| 369 |
Poll::Ready(()), |
| 370 |
grandgrandchild_fut |
| 371 |
.as_mut() |
| 372 |
.poll(&mut Context::from_waker(&waker)) |
| 373 |
); |
| 374 |
assert_eq!(wake_counter, 6); |
| 375 |
} |
| 376 |
|
| 377 |
#[test] |
| 378 |
fn drop_parent_before_child_tokens() { |
| 379 |
let token = CancellationToken::new(); |
| 380 |
let child1 = token.child_token(); |
| 381 |
let child2 = token.child_token(); |
| 382 |
|
| 383 |
drop(token); |
| 384 |
assert!(!child1.is_cancelled()); |
| 385 |
|
| 386 |
drop(child1); |
| 387 |
drop(child2); |
| 388 |
} |
| 389 |
|
| 390 |
#[test] |
| 391 |
fn derives_send_sync() { |
| 392 |
fn assert_send<T: Send>() {} |
| 393 |
fn assert_sync<T: Sync>() {} |
| 394 |
|
| 395 |
assert_send::<CancellationToken>(); |
| 396 |
assert_sync::<CancellationToken>(); |
| 397 |
|
| 398 |
assert_send::<WaitForCancellationFuture<'static>>(); |
| 399 |
assert_sync::<WaitForCancellationFuture<'static>>(); |
| 400 |
} |