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

DO NOT MERGE test(subscriber): additional testing of integration tests on CI #458

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions console-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ crossbeam-channel = "0.5"

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

[package.metadata.docs.rs]
Expand Down
8 changes: 4 additions & 4 deletions console-subscriber/src/aggregator/id_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ impl<T: Unsent> IdData<T> {
if let Some(dropped_at) = stats.dropped_at() {
let dropped_for = now.checked_duration_since(dropped_at).unwrap_or_default();
let dirty = stats.is_unsent();
let should_drop =
let should_retain =
// if there are any clients watching, retain all dirty tasks regardless of age
(dirty && has_watchers)
|| dropped_for > retention;
|| dropped_for <= retention;
tracing::trace!(
stats.id = ?id,
stats.dropped_at = ?dropped_at,
stats.dropped_for = ?dropped_for,
stats.dirty = dirty,
should_drop,
should_retain,
);
return !should_drop;
return should_retain;
}

true
Expand Down
2 changes: 1 addition & 1 deletion console-subscriber/src/aggregator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl Aggregator {
// to be woken when the flush interval has elapsed, or when the
// channel is almost full.
let mut drained = false;
while let Some(event) = self.events.recv().now_or_never() {
while let Some(event) = tokio::task::unconstrained(self.events.recv()).now_or_never() {
match event {
Some(event) => {
self.update_state(event);
Expand Down
184 changes: 184 additions & 0 deletions console-subscriber/tests/framework.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
//! Framework tests
//!
//! The tests in this module are here to verify the testing framework itself.
//! As such, some of these tests may be repeated elsewhere (where we wish to
//! actually test the functionality of `console-subscriber`) and others are
//! negative tests that should panic.

use std::time::Duration;

use tokio::{task, time::sleep};

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

#[test]
fn expect_present() {
let expected_task = ExpectedTask::default()
.match_default_name()
.expect_present();

let future = async {
sleep(Duration::ZERO).await;
};

assert_task(expected_task, future);
}

#[test]
#[should_panic(expected = "Test failed: Task validation failed:
- Task<name=main>: no expectations set, if you want to just expect that a matching task is present, use `expect_present()`
")]
fn fail_no_expectations() {
let expected_task = ExpectedTask::default().match_default_name();

let future = async {
sleep(Duration::ZERO).await;
};

assert_task(expected_task, future);
}

#[test]
fn wakes() {
let expected_task = ExpectedTask::default().match_default_name().expect_wakes(1);

let future = async {
sleep(Duration::ZERO).await;
};

assert_task(expected_task, future);
}

#[test]
#[should_panic(expected = "Test failed: Task validation failed:
- Task<name=main>: expected `wakes` to be 5, but actual was 1
")]
fn fail_wakes() {
let expected_task = ExpectedTask::default().match_default_name().expect_wakes(5);

let future = async {
sleep(Duration::ZERO).await;
};

assert_task(expected_task, future);
}

#[test]
fn self_wakes() {
let expected_task = ExpectedTask::default()
.match_default_name()
.expect_self_wakes(1);

let future = async { task::yield_now().await };

assert_task(expected_task, future);
}

#[test]
#[should_panic(expected = "Test failed: Task validation failed:
- Task<name=main>: expected `self_wakes` to be 1, but actual was 0
")]
fn fail_self_wake() {
let expected_task = ExpectedTask::default()
.match_default_name()
.expect_self_wakes(1);

let future = async {
sleep(Duration::ZERO).await;
};

assert_task(expected_task, future);
}

#[test]
fn test_spawned_task() {
let expected_task = ExpectedTask::default()
.match_name("another-name".into())
.expect_present();

let future = async {
task::Builder::new()
.name("another-name")
.spawn(async { task::yield_now().await })
};

assert_task(expected_task, future);
}

#[test]
#[should_panic(expected = "Test failed: Task validation failed:
- Task<name=wrong-name>: no matching actual task was found
")]
fn fail_wrong_task_name() {
let expected_task = ExpectedTask::default().match_name("wrong-name".into());

let future = async { task::yield_now().await };

assert_task(expected_task, future);
}

#[test]
fn multiple_tasks() {
let expected_tasks = vec![
ExpectedTask::default()
.match_name("task-1".into())
.expect_wakes(1),
ExpectedTask::default()
.match_name("task-2".into())
.expect_wakes(1),
];

let future = async {
let task1 = task::Builder::new()
.name("task-1")
.spawn(async { task::yield_now().await })
.unwrap();
let task2 = task::Builder::new()
.name("task-2")
.spawn(async { task::yield_now().await })
.unwrap();

tokio::try_join! {
task1,
task2,
}
.unwrap();
};

assert_tasks(expected_tasks, future);
}

#[test]
#[should_panic(expected = "Test failed: Task validation failed:
- Task<name=task-2>: expected `wakes` to be 2, but actual was 1
")]
fn fail_1_of_2_expected_tasks() {
let expected_tasks = vec![
ExpectedTask::default()
.match_name("task-1".into())
.expect_wakes(1),
ExpectedTask::default()
.match_name("task-2".into())
.expect_wakes(2),
];

let future = async {
let task1 = task::Builder::new()
.name("task-1")
.spawn(async { task::yield_now().await })
.unwrap();
let task2 = task::Builder::new()
.name("task-2")
.spawn(async { task::yield_now().await })
.unwrap();

tokio::try_join! {
task1,
task2,
}
.unwrap();
};

assert_tasks(expected_tasks, future);
}
47 changes: 47 additions & 0 deletions console-subscriber/tests/support/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use futures::Future;

mod state;
mod subscriber;
mod task;

use subscriber::run_test;

pub(crate) use subscriber::MAIN_TASK_NAME;
pub(crate) use task::ExpectedTask;

/// Assert that an `expected_task` is recorded by a console-subscriber
/// when driving the provided `future` to completion.
///
/// This function is equivalent to calling [`assert_tasks`] with a vector
/// containing a single task.
///
/// # Panics
///
/// This function will panic if the expectations on the expected task are not
/// met or if a matching task is not recorded.
#[track_caller]
#[allow(dead_code)]
pub(crate) fn assert_task<Fut>(expected_task: ExpectedTask, future: Fut)
where
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
run_test(vec![expected_task], future)
}

/// Assert that the `expected_tasks` are recorded by a console-subscriber
/// when driving the provided `future` to completion.
///
/// # Panics
///
/// This function will panic if the expectations on any of the expected tasks
/// are not met or if matching tasks are not recorded for all expected tasks.
#[track_caller]
#[allow(dead_code)]
pub(crate) fn assert_tasks<Fut>(expected_tasks: Vec<ExpectedTask>, future: Fut)
where
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
run_test(expected_tasks, future)
}
Loading