executor.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2023 The ChromiumOS Authors
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. use cros_async::sys::ExecutorKindSys;
  5. use cros_async::Executor;
  6. use cros_async::ExecutorKind;
  7. #[cfg(any(target_os = "android", target_os = "linux"))]
  8. fn all_kinds() -> Vec<ExecutorKind> {
  9. let mut kinds = vec![ExecutorKindSys::Fd.into()];
  10. if cros_async::is_uring_stable() {
  11. kinds.push(ExecutorKindSys::Uring.into());
  12. }
  13. kinds
  14. }
  15. #[cfg(windows)]
  16. fn all_kinds() -> Vec<ExecutorKind> {
  17. vec![ExecutorKindSys::Handle.into()]
  18. }
  19. #[test]
  20. fn cancel_pending_task() {
  21. for kind in all_kinds() {
  22. let ex = Executor::with_executor_kind(kind).unwrap();
  23. let task = ex.spawn(std::future::pending::<()>());
  24. assert_eq!(ex.run_until(task.cancel()).unwrap(), None);
  25. }
  26. }
  27. // Testing a completed task without relying on implementation details is tricky. We create a future
  28. // that signals a channel when it is polled so that we can delay the `task.cancel()` call until we
  29. // know the task has been executed.
  30. #[test]
  31. fn cancel_ready_task() {
  32. for kind in all_kinds() {
  33. let ex = Executor::with_executor_kind(kind).unwrap();
  34. let (s, r) = futures::channel::oneshot::channel();
  35. let mut s = Some(s);
  36. let task = ex.spawn(futures::future::poll_fn(move |_| {
  37. s.take().unwrap().send(()).unwrap();
  38. std::task::Poll::Ready(5)
  39. }));
  40. assert_eq!(
  41. ex.run_until(async {
  42. r.await.unwrap();
  43. task.cancel().await
  44. })
  45. .unwrap(),
  46. Some(5)
  47. );
  48. }
  49. }