Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(subscriber): add test for tasks being kept open #490

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 28 additions & 65 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ members = [
"xtask"
]
resolver = "2"

[patch.crates-io]
tokio = { git = "https://github.com/tokio-rs/tokio.git", branch = "master" }
4 changes: 2 additions & 2 deletions console-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ env-filter = ["tracing-subscriber/env-filter"]
[dependencies]

crossbeam-utils = "0.8.7"
tokio = { version = "^1.21", features = ["sync", "time", "macros", "tracing"] }
tokio = { version = "1.34", features = ["sync", "time", "macros", "tracing"] }
tokio-stream = { version = "0.1", features = ["net"] }
thread_local = "1.1.3"
console-api = { version = "0.6.0", path = "../console-api", features = ["transport"] }
Expand All @@ -54,7 +54,7 @@ serde_json = "1"
crossbeam-channel = "0.5"

[dev-dependencies]
tokio = { version = "^1.21", features = ["full", "rt-multi-thread"] }
tokio = { version = "1.34", features = ["full", "rt-multi-thread"] }
tower = { version = "0.4", default-features = false }
futures = "0.3"

Expand Down
50 changes: 49 additions & 1 deletion console-subscriber/tests/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use futures::future;
use tokio::{task, time::sleep};

mod support;
use support::{assert_task, assert_tasks, ExpectedTask};
use support::{assert_task, assert_tasks, ExpectedTask, TaskState};

#[test]
fn expect_present() {
Expand Down Expand Up @@ -198,6 +198,54 @@ fn fail_polls() {
assert_task(expected_task, future);
}

#[test]
fn main_task_completes() {
let expected_task = ExpectedTask::default()
.match_default_name()
.expect_state(TaskState::Completed);

let future = async {};

assert_task(expected_task, future);
}

#[test]
#[should_panic(expected = "Test failed: Task validation failed:
- Task { name=task }: expected `state` to be Idle, but actual was Completed")]
fn fail_completed_task_is_idle() {
let expected_task = ExpectedTask::default()
.match_name("task".into())
.expect_state(TaskState::Idle);

let future = async {
_ = task::Builder::new()
.name("task")
.spawn(futures::future::ready(()))
.unwrap()
.await;
};

assert_task(expected_task, future);
}

#[test]
#[should_panic(expected = "Test failed: Task validation failed:
- Task { name=task }: expected `state` to be Completed, but actual was Idle")]
fn fail_idle_task_is_completed() {
let expected_task = ExpectedTask::default()
.match_name("task".into())
.expect_state(TaskState::Completed);

let future = async {
_ = task::Builder::new()
.name("task")
.spawn(futures::future::pending::<()>())
.unwrap();
};

assert_task(expected_task, future);
}

async fn yield_to_runtime() {
// There is a race condition that can occur when tests are run in parallel,
// caused by tokio-rs/tracing#2743. It tends to cause test failures only
Expand Down
27 changes: 26 additions & 1 deletion console-subscriber/tests/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;
use tokio::time::sleep;

mod support;
use support::{assert_tasks, spawn_named, ExpectedTask};
use support::{assert_tasks, spawn_named, ExpectedTask, TaskState};

/// This test asserts the behavior that was fixed in #440. Before that fix,
/// the polls of a child were also counted towards the parent (the task which
Expand Down Expand Up @@ -34,3 +34,28 @@ fn child_polls_dont_count_towards_parent_polls() {

assert_tasks(expected_tasks, future);
}

/// This test asserts that the lifetime of a task is not affected by the
/// lifetimes of tasks that it spawns. The test will pass when #345 is
/// fixed.
#[test]
fn spawner_task_with_running_children_completes() {
let expected_tasks = vec![
ExpectedTask::default()
.match_name("parent".into())
.expect_state(TaskState::Completed),
ExpectedTask::default()
.match_name("child".into())
.expect_state(TaskState::Idle),
];

let future = async {
spawn_named("parent", async {
spawn_named("child", futures::future::pending::<()>());
})
.await
.expect("joining parent failed");
};

assert_tasks(expected_tasks, future);
}
2 changes: 2 additions & 0 deletions console-subscriber/tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use subscriber::run_test;

pub(crate) use subscriber::MAIN_TASK_NAME;
pub(crate) use task::ExpectedTask;
#[allow(unused_imports)]
pub(crate) use task::TaskState;
use tokio::task::JoinHandle;

/// Assert that an `expected_task` is recorded by a console-subscriber
Expand Down
Loading
Loading