crosvm/cros_async/tests/executor.rs
A. Cody Schuffelen 97dff044f8 Replace #[cfg(unix)] with #[cfg(any(target_os = "android", target_os = "linux"))]
Updates are made to source and documentation.

This more accurately represents the currently supported platforms of
Android/Linux and Windows, without unexpectedly including other
unix-like operating systems.

Command to reproduce:
$ find . -type f -not -path '*/\.git/*' | xargs -I {} sed -i 's/cfg(unix)/cfg(any(target_os = "android", target_os = "linux"))/g' {}
$ cargo fmt

md files manually updated to fix line lengths.

Renaming `unix` modules to `linux` will be done in a later CL.

Test: ./tools/dev_container ./tools/presubmit
Bug: b/298269162
Change-Id: I42c1bf0abf80b9a0df25551613910293217c7295
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4909059
Commit-Queue: Cody Schuffelen <schuffelen@google.com>
Reviewed-by: Frederick Mayle <fmayle@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Noah Gold <nkgold@google.com>
2023-10-11 00:43:29 +00:00

52 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::Executor;
use cros_async::ExecutorKind;
#[cfg(any(target_os = "android", target_os = "linux"))]
fn all_kinds() -> Vec<ExecutorKind> {
let mut kinds = vec![ExecutorKind::Fd];
if cros_async::is_uring_stable() {
kinds.push(ExecutorKind::Uring);
}
kinds
}
#[cfg(windows)]
fn all_kinds() -> Vec<ExecutorKind> {
vec![ExecutorKind::Handle]
}
#[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)
);
}
}