crosvm/cros_async/tests/executor.rs
A. Cody Schuffelen b7ca74d516 cros_async: Combine platform-specific Executors
Instead of a dispatch enum defined for windows and a dispatch enum
defined for linux, there is now one dispatch enum with branches defined
for the two platforms. This makes it so defining the tokio executor
later only has to be added in one place.

Bug: b/320603688
Test: tools/dev_container tools/presubmit
Test: tools/dev_container tools/presubmit clippy
Change-Id: I6eaf46710bbb54a9684cb1622da36c3026906a5c
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5208332
Commit-Queue: Cody Schuffelen <schuffelen@google.com>
Reviewed-by: Frederick Mayle <fmayle@google.com>
2024-03-26 16:54:35 +00:00

53 lines
1.6 KiB
Rust

// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use cros_async::sys::ExecutorKindSys;
use cros_async::Executor;
use cros_async::ExecutorKind;
#[cfg(any(target_os = "android", target_os = "linux"))]
fn all_kinds() -> Vec<ExecutorKind> {
let mut kinds = vec![ExecutorKindSys::Fd.into()];
if cros_async::is_uring_stable() {
kinds.push(ExecutorKindSys::Uring.into());
}
kinds
}
#[cfg(windows)]
fn all_kinds() -> Vec<ExecutorKind> {
vec![ExecutorKindSys::Handle.into()]
}
#[test]
fn cancel_pending_task() {
for kind in all_kinds() {
let ex = Executor::with_executor_kind(kind).unwrap();
let task = ex.spawn(std::future::pending::<()>());
assert_eq!(ex.run_until(task.cancel()).unwrap(), None);
}
}
// Testing a completed task without relying on implementation details is tricky. We create a future
// that signals a channel when it is polled so that we can delay the `task.cancel()` call until we
// know the task has been executed.
#[test]
fn cancel_ready_task() {
for kind in all_kinds() {
let ex = Executor::with_executor_kind(kind).unwrap();
let (s, r) = futures::channel::oneshot::channel();
let mut s = Some(s);
let task = ex.spawn(futures::future::poll_fn(move |_| {
s.take().unwrap().send(()).unwrap();
std::task::Poll::Ready(5)
}));
assert_eq!(
ex.run_until(async {
r.await.unwrap();
task.cancel().await
})
.unwrap(),
Some(5)
);
}
}