Skip to content

Commit 4933197

Browse files
hdshawkw
andcommitted
test(subscriber): add initial integration tests (#452)
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 "expected tasks" - A future which will be driven to completion on a dedicated Tokio runtime. Behind the scenes, a console subscriber layer is created and its 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 `wakes` and `self_wakes`. 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. Co-authored-by: Eliza Weisman <[email protected]>
1 parent 01b2875 commit 4933197

File tree

8 files changed

+1005
-0
lines changed

8 files changed

+1005
-0
lines changed

Cargo.lock

+1
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 = { version = "0.4", default-features = false }
5859
futures = "0.3"
5960

6061
[package.metadata.docs.rs]

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=console-test::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=console-test::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=console-test::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+
}
+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
use std::fmt;
2+
3+
use tokio::sync::broadcast::{
4+
self,
5+
error::{RecvError, TryRecvError},
6+
};
7+
8+
/// A step in the running of the test
9+
#[derive(Clone, Debug, PartialEq, PartialOrd)]
10+
pub(super) enum TestStep {
11+
/// The overall test has begun
12+
Start,
13+
/// The instrument server has been started
14+
ServerStarted,
15+
/// The client has connected to the instrument server
16+
ClientConnected,
17+
/// The future being driven has completed
18+
TestFinished,
19+
/// The client has finished recording updates
20+
UpdatesRecorded,
21+
}
22+
23+
impl fmt::Display for TestStep {
24+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25+
(self as &dyn fmt::Debug).fmt(f)
26+
}
27+
}
28+
29+
/// The state of the test.
30+
///
31+
/// This struct is used by various parts of the test framework to wait until
32+
/// a specific test step has been reached and advance the test state to a new
33+
/// step.
34+
pub(super) struct TestState {
35+
receiver: broadcast::Receiver<TestStep>,
36+
sender: broadcast::Sender<TestStep>,
37+
step: TestStep,
38+
}
39+
40+
impl TestState {
41+
pub(super) fn new() -> Self {
42+
let (sender, receiver) = broadcast::channel(1);
43+
Self {
44+
receiver,
45+
sender,
46+
step: TestStep::Start,
47+
}
48+
}
49+
50+
/// Wait asynchronously until the desired step has been reached.
51+
///
52+
/// # Panics
53+
///
54+
/// This function will panic if the underlying channel gets closed.
55+
pub(super) async fn wait_for_step(&mut self, desired_step: TestStep) {
56+
while self.step < desired_step {
57+
match self.receiver.recv().await {
58+
Ok(step) => self.step = step,
59+
Err(RecvError::Lagged(_)) => {
60+
// we don't mind being lagged, we'll just get the latest state
61+
}
62+
Err(RecvError::Closed) => panic!(
63+
"console-test error: failed to receive current step, \
64+
waiting for step: {desired_step}. This shouldn't happen, \
65+
did the test abort?"
66+
),
67+
}
68+
}
69+
}
70+
71+
/// Returns `true` if the current step is `desired_step` or later.
72+
pub(super) fn is_step(&mut self, desired_step: TestStep) -> bool {
73+
self.update_step();
74+
75+
self.step == desired_step
76+
}
77+
78+
/// Advance to the next step.
79+
///
80+
/// The test must be at the step prior to the next step before starting.
81+
/// Being in a different step is likely to indicate a logic error in the
82+
/// test framework.
83+
///
84+
/// # Panics
85+
///
86+
/// This method will panic if the test state is not at the step prior to
87+
/// `next_step`, or if the underlying channel is closed.
88+
#[track_caller]
89+
pub(super) fn advance_to_step(&mut self, next_step: TestStep) {
90+
self.update_step();
91+
92+
assert!(
93+
self.step < next_step,
94+
"console-test error: cannot advance to previous or current step! \
95+
current step: {current}, next step: {next_step}. This shouldn't \
96+
happen.",
97+
current = self.step,
98+
);
99+
100+
match (&self.step, &next_step) {
101+
(TestStep::Start, TestStep::ServerStarted)
102+
| (TestStep::ServerStarted, TestStep::ClientConnected)
103+
| (TestStep::ClientConnected, TestStep::TestFinished)
104+
| (TestStep::TestFinished, TestStep::UpdatesRecorded) => {}
105+
(current, _) => panic!(
106+
"console-test error: test cannot advance more than one step! \
107+
current step: {current}, next step: {next_step}. This \
108+
shouldn't happen."
109+
),
110+
}
111+
112+
self.sender.send(next_step).expect(
113+
"console-test error: failed to send the next test step. \
114+
This shouldn't happen, did the test abort?",
115+
);
116+
}
117+
118+
fn update_step(&mut self) {
119+
loop {
120+
match self.receiver.try_recv() {
121+
Ok(step) => self.step = step,
122+
Err(TryRecvError::Lagged(_)) => {
123+
// we don't mind being lagged, we'll just get the latest state
124+
}
125+
Err(TryRecvError::Closed) => panic!(
126+
"console-test error: failed to update current step, did \
127+
the test abort?"
128+
),
129+
Err(TryRecvError::Empty) => break,
130+
}
131+
}
132+
}
133+
}
134+
135+
impl Clone for TestState {
136+
fn clone(&self) -> Self {
137+
Self {
138+
receiver: self.receiver.resubscribe(),
139+
sender: self.sender.clone(),
140+
step: self.step.clone(),
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)