test: Assert log warnings and errors - #8457
Conversation
| .unwrap() | ||
| .unwrap(); | ||
|
|
||
| t.log_sink.assert_warn("No from in message"); |
There was a problem hiding this comment.
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.
|
After talking to @link2xt, we found some simplifications that are possible, and one bugfix that is needed:
|
8249945 to
2d7664d
Compare
| #[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, | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
(I guess it's just bloat, will change it back to "is_error" bool later.)
66768b3 to
beedff7
Compare
71d806d to
f3df7e8
Compare
fe60a78 to
bbf432d
Compare
|
|
||
| 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yes, I initially implemented it using try_recv, but some tests were flaky. This only adds overhead on failure anyway.
| let log_event = LogEvent::Event(event.clone()); | ||
| print_logevent(&log_event); | ||
| sender.try_send(log_event).ok(); |
There was a problem hiding this comment.
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.
6c2edda to
f892b0b
Compare
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>
f892b0b to
3d6f761
Compare
Adds
assert_warn,assert_errorandassert_manymethods to
TestContext, that let us assert thata certain warning or error is logged during the test.
Also asserts test logs should not contain any other
errors or warnings.
Adjusts tests accordingly.