Skip to content

Commit 42fb829

Browse files
committed
test(subscriber): add initial integration tests
The `console-subscriber` crate has no integration tests. There are some unit tests, but without very high coverage of features. Recently, we've found or fixed a few errors which probably could have been caught by a medium level of integration testing. However, testing `console-subscriber` isn't straight forward. It is effectively a tracing subscriber (or layer) on one end, and a gRPC server on the other end. This change adds enough of a testing framework to write some initial integration tests. It is the first step towards closing #450. Each test comprises 2 parts: - One or more "expcted tasks" - A future which will be driven to completion on a dedicated Tokio runtime. Behind the scenes, a console subscriber layer is created and it's server part is connected to a duplex stream. The client of the duplex stream then records incoming updates and reconstructs "actual tasks". The layer itself is set as the default subscriber for the duration of `block_on` which is used to drive the provided future to completioin. The expected tasks have a set of "matches", which is how we find the actual task that we want to validate against. Currently, the only value we match on is the task's name. The expected tasks also have a set of expectations. These are other fields on the actual task which are validated once a matching task is found. Currently, the two fields which can have expectations set on them are the `wakes` and `self_wakes` fields. So, to construct an expected task, which will match a task with the name `"my-task"` and then validate that the matched task gets woken once, the code would be: ```rust ExpectedTask::default() .match_name("my-task") .expect_wakes(1); ``` A future which passes this test could be: ```rust async { task::Builder::new() .name("my-task") .spawn(async { tokio::time::sleep(std::time::Duration::ZERO).await }) } ``` The full test would then look like: ```rust fn wakes_once() { let expected_task = ExpectedTask::default() .match_name("my-task") .expect_wakes(1); let future = async { task::Builder::new() .name("my-task") .spawn(async { tokio::time::sleep(std::time::Duration::ZERO).await }) }; assert_task(expected_task, future); } ``` The PR depends on 2 others: - #447 which fixes an error in the logic that determines whether a task is retained in the aggregator or not. - #451 which exposes the server parts and is necessary to allow us to connect the instrument server and client via a duplex channel. This change contains some initial tests for wakes and self wakes which would have caught the error fixed in #430. Additionally there are tests for the functionality of the testing framework itself.
1 parent 639e03b commit 42fb829

File tree

9 files changed

+971
-4
lines changed

9 files changed

+971
-4
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

console-subscriber/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ crossbeam-channel = "0.5"
5555

5656
[dev-dependencies]
5757
tokio = { version = "^1.21", features = ["full", "rt-multi-thread"] }
58+
tower = "0.4"
5859
futures = "0.3"
5960

6061
[package.metadata.docs.rs]

console-subscriber/src/aggregator/id_data.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,18 @@ impl<T: Unsent> IdData<T> {
104104
if let Some(dropped_at) = stats.dropped_at() {
105105
let dropped_for = now.checked_duration_since(dropped_at).unwrap_or_default();
106106
let dirty = stats.is_unsent();
107-
let should_drop =
107+
let should_retain =
108108
// if there are any clients watching, retain all dirty tasks regardless of age
109109
(dirty && has_watchers)
110-
|| dropped_for > retention;
110+
|| dropped_for <= retention;
111111
tracing::trace!(
112112
stats.id = ?id,
113113
stats.dropped_at = ?dropped_at,
114114
stats.dropped_for = ?dropped_for,
115115
stats.dirty = dirty,
116-
should_drop,
116+
should_retain,
117117
);
118-
return !should_drop;
118+
return should_retain;
119119
}
120120

121121
true

console-subscriber/tests/framework.rs

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
//! Framework tests
2+
//!
3+
//! The tests in this module are here to verify the testing framework itself.
4+
//! As such, some of these tests may be repeated elsewhere (where we wish to
5+
//! actually test the functionality of `console-subscriber`) and others are
6+
//! negative tests that should panic.
7+
8+
use std::time::Duration;
9+
10+
use tokio::{task, time::sleep};
11+
12+
mod support;
13+
use support::{assert_task, assert_tasks, ExpectedTask};
14+
15+
#[test]
16+
fn expect_present() {
17+
let expected_task = ExpectedTask::default()
18+
.match_default_name()
19+
.expect_present();
20+
21+
let future = async {
22+
sleep(Duration::ZERO).await;
23+
};
24+
25+
assert_task(expected_task, future);
26+
}
27+
28+
#[test]
29+
#[should_panic(expected = "Test failed: Task validation failed:
30+
- Task<name=main>: no expectations set, if you want to just expect that a matching task is present, use `expect_present()`
31+
")]
32+
fn fail_no_expectations() {
33+
let expected_task = ExpectedTask::default().match_default_name();
34+
35+
let future = async {
36+
sleep(Duration::ZERO).await;
37+
};
38+
39+
assert_task(expected_task, future);
40+
}
41+
42+
#[test]
43+
fn wakes() {
44+
let expected_task = ExpectedTask::default().match_default_name().expect_wakes(1);
45+
46+
let future = async {
47+
sleep(Duration::ZERO).await;
48+
};
49+
50+
assert_task(expected_task, future);
51+
}
52+
53+
#[test]
54+
#[should_panic(expected = "Test failed: Task validation failed:
55+
- Task<name=main>: expected `wakes` to be 5, but actual was 1
56+
")]
57+
fn fail_wakes() {
58+
let expected_task = ExpectedTask::default().match_default_name().expect_wakes(5);
59+
60+
let future = async {
61+
sleep(Duration::ZERO).await;
62+
};
63+
64+
assert_task(expected_task, future);
65+
}
66+
67+
#[test]
68+
fn self_wakes() {
69+
let expected_task = ExpectedTask::default()
70+
.match_default_name()
71+
.expect_self_wakes(1);
72+
73+
let future = async { task::yield_now().await };
74+
75+
assert_task(expected_task, future);
76+
}
77+
78+
#[test]
79+
#[should_panic(expected = "Test failed: Task validation failed:
80+
- Task<name=main>: expected `self_wakes` to be 1, but actual was 0
81+
")]
82+
fn fail_self_wake() {
83+
let expected_task = ExpectedTask::default()
84+
.match_default_name()
85+
.expect_self_wakes(1);
86+
87+
let future = async {
88+
sleep(Duration::ZERO).await;
89+
};
90+
91+
assert_task(expected_task, future);
92+
}
93+
94+
#[test]
95+
fn test_spawned_task() {
96+
let expected_task = ExpectedTask::default()
97+
.match_name("another-name".into())
98+
.expect_present();
99+
100+
let future = async {
101+
task::Builder::new()
102+
.name("another-name")
103+
.spawn(async { task::yield_now().await })
104+
};
105+
106+
assert_task(expected_task, future);
107+
}
108+
109+
#[test]
110+
#[should_panic(expected = "Test failed: Task validation failed:
111+
- Task<name=wrong-name>: no matching actual task was found
112+
")]
113+
fn fail_wrong_task_name() {
114+
let expected_task = ExpectedTask::default().match_name("wrong-name".into());
115+
116+
let future = async { task::yield_now().await };
117+
118+
assert_task(expected_task, future);
119+
}
120+
121+
#[test]
122+
fn multiple_tasks() {
123+
let expected_tasks = vec![
124+
ExpectedTask::default()
125+
.match_name("task-1".into())
126+
.expect_wakes(1),
127+
ExpectedTask::default()
128+
.match_name("task-2".into())
129+
.expect_wakes(1),
130+
];
131+
132+
let future = async {
133+
let task1 = task::Builder::new()
134+
.name("task-1")
135+
.spawn(async { task::yield_now().await })
136+
.unwrap();
137+
let task2 = task::Builder::new()
138+
.name("task-2")
139+
.spawn(async { task::yield_now().await })
140+
.unwrap();
141+
142+
tokio::try_join! {
143+
task1,
144+
task2,
145+
}
146+
.unwrap();
147+
};
148+
149+
assert_tasks(expected_tasks, future);
150+
}
151+
152+
#[test]
153+
#[should_panic(expected = "Test failed: Task validation failed:
154+
- Task<name=task-2>: expected `wakes` to be 2, but actual was 1
155+
")]
156+
fn fail_1_of_2_expected_tasks() {
157+
let expected_tasks = vec![
158+
ExpectedTask::default()
159+
.match_name("task-1".into())
160+
.expect_wakes(1),
161+
ExpectedTask::default()
162+
.match_name("task-2".into())
163+
.expect_wakes(2),
164+
];
165+
166+
let future = async {
167+
let task1 = task::Builder::new()
168+
.name("task-1")
169+
.spawn(async { task::yield_now().await })
170+
.unwrap();
171+
let task2 = task::Builder::new()
172+
.name("task-2")
173+
.spawn(async { task::yield_now().await })
174+
.unwrap();
175+
176+
tokio::try_join! {
177+
task1,
178+
task2,
179+
}
180+
.unwrap();
181+
};
182+
183+
assert_tasks(expected_tasks, future);
184+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use futures::Future;
2+
3+
mod state;
4+
mod subscriber;
5+
mod task;
6+
7+
use subscriber::run_test;
8+
9+
pub(crate) use subscriber::MAIN_TASK_NAME;
10+
pub(crate) use task::ExpectedTask;
11+
12+
/// Assert that an `expected_task` is recorded by a console-subscriber
13+
/// when driving the provided `future` to completion.
14+
///
15+
/// This function is equivalent to calling [`assert_tasks`] with a vector
16+
/// containing a single task.
17+
///
18+
/// # Panics
19+
///
20+
/// This function will panic if the expectations on the expected task are not
21+
/// met or if a matching task is not recorded.
22+
#[track_caller]
23+
#[allow(dead_code)]
24+
pub(crate) fn assert_task<Fut>(expected_task: ExpectedTask, future: Fut)
25+
where
26+
Fut: Future + Send + 'static,
27+
Fut::Output: Send + 'static,
28+
{
29+
run_test(vec![expected_task], future)
30+
}
31+
32+
/// Assert that the `expected_tasks` are recorded by a console-subscriber
33+
/// when driving the provided `future` to completion.
34+
///
35+
/// # Panics
36+
///
37+
/// This function will panic if the expectations on any of the expected tasks
38+
/// are not met or if matching tasks are not recorded for all expected tasks.
39+
#[track_caller]
40+
#[allow(dead_code)]
41+
pub(crate) fn assert_tasks<Fut>(expected_tasks: Vec<ExpectedTask>, future: Fut)
42+
where
43+
Fut: Future + Send + 'static,
44+
Fut::Output: Send + 'static,
45+
{
46+
run_test(expected_tasks, future)
47+
}

0 commit comments

Comments
 (0)