Skip to content

test: Assert log warnings and errors - #8457

Open
j-g00da wants to merge 1 commit into
mainfrom
j-g00da/log_asserts
Open

test: Assert log warnings and errors#8457
j-g00da wants to merge 1 commit into
mainfrom
j-g00da/log_asserts

Conversation

@j-g00da

@j-g00da j-g00da commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Adds assert_warn, assert_error and assert_many
methods to TestContext, that let us assert that
a certain warning or error is logged during the test.
Also asserts test logs should not contain any other
errors or warnings.

Adjusts tests accordingly.

Comment thread src/receive_imf/receive_imf_tests.rs Outdated
.unwrap()
.unwrap();

t.log_sink.assert_warn("No from in message");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage example, all logged errors and warnings will cause tests to fail unless we explicitly assert (maybe this should be renamed to expect) that error/warn should be logged.

I will probably expose these methods directly in TestContext and TestContextManager instead of exposing log_sink.

@Hocuri

Hocuri commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

After talking to @link2xt, we found some simplifications that are possible, and one bugfix that is needed:

  • The whole printing-logs-only-when-LogSink-is-dropped logic is historical (from before cargo nextest existed) and not needed anymore. Instead, print_logevent() can be called directly from the loop in fn subscribe. Then, we don't need the collected field.
  • With the PR as-is, calling assert_warn() on one TestContext will find also react to warnings on other TestContexts, because they share the same LogSink. Instead, each TestContext should get its own LogSink. This also is a simplification, because TestContextManager doesn't need a LogSink anymore, then.

@j-g00da
j-g00da force-pushed the j-g00da/log_asserts branch 4 times, most recently from 8249945 to 2d7664d Compare July 30, 2026 12:24
Comment thread src/test_utils.rs Outdated
Comment on lines +1364 to +1389
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum LogAssertType {
Warn,
Error,
}

impl Display for LogAssertType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
Self::Warn => "a warning".to_string(),
Self::Error => "an error".to_string(),
};
write!(f, "{}", str)
}
}

impl Not for LogAssertType {
type Output = Self;

fn not(self) -> Self::Output {
match self {
LogAssertType::Warn => LogAssertType::Error,
LogAssertType::Error => LogAssertType::Warn,
}
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I guess it's just bloat, will change it back to "is_error" bool later.)

@j-g00da
j-g00da force-pushed the j-g00da/log_asserts branch 7 times, most recently from 66768b3 to beedff7 Compare August 3, 2026 15:28
@j-g00da
j-g00da marked this pull request as ready for review August 3, 2026 15:28
@j-g00da j-g00da changed the title WIP: test: Assert log warnings and errors test: Assert log warnings and errors Aug 3, 2026
@j-g00da
j-g00da force-pushed the j-g00da/log_asserts branch 3 times, most recently from 71d806d to f3df7e8 Compare August 3, 2026 16:16
@j-g00da
j-g00da requested review from Hocuri and link2xt August 3, 2026 16:20
@j-g00da
j-g00da force-pushed the j-g00da/log_asserts branch 4 times, most recently from fe60a78 to bbf432d Compare August 4, 2026 08:07
Comment thread src/test_utils.rs Outdated
Comment thread src/test_utils.rs Outdated
Comment thread src/test_utils.rs

async fn assert(&self, is_error: bool, pat: &str) {
while let Ok(Ok(event)) =
tokio::time::timeout(Duration::from_secs(1), self.events.recv()).await

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this timeout needed? Should work with plain try_recv(), since the events were sent on the same task, or do some tests fail / become flaky without it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I initially implemented it using try_recv, but some tests were flaky. This only adds overhead on failure anyway.

Comment thread src/events/payload.rs Outdated
Comment thread src/test_utils.rs Outdated
Comment thread src/test_utils.rs Outdated
Comment on lines +1417 to +1419
let log_event = LogEvent::Event(event.clone());
print_logevent(&log_event);
sender.try_send(log_event).ok();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I'm wondering if it's a problem that the test might end before this task got a chance to run and send all the events into the channel. Though if it is a problem, then it existed before this PR already, and I can't remember any problems with missing test output.

Comment thread src/test_utils.rs Outdated
Comment thread src/events.rs Outdated
@j-g00da
j-g00da force-pushed the j-g00da/log_asserts branch 2 times, most recently from 6c2edda to f892b0b Compare August 4, 2026 12:04
Adds `assert_warn`, `assert_error` and `assert_many`
methods to `TestContext`, that let us assert that
a certain warning or error is logged during the test.
Also asserts test logs should not contain any other
errors or warnings.

Adjusts tests accordingly.

Signed-off-by: Jagoda 艢l膮zak <jslazak@jslazak.com>
@j-g00da
j-g00da force-pushed the j-g00da/log_asserts branch from f892b0b to 3d6f761 Compare August 4, 2026 13:33
@j-g00da
j-g00da requested a review from Hocuri August 4, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants