Skip to content

Commit 39be591

Browse files
authored
test: Notifiy more prominently & in more tests about false positives when running cargo test (#6308)
This PR: - Moves the note about the false positive to the end of the test output, where it is more likely to be noticed - Also notes in test_modify_chat_disordered() and test_setup_contact_*(), in addition to the existing note in test_was_seen_recently()
1 parent f03dc6a commit 39be591

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

src/chat.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4730,7 +4730,7 @@ mod tests {
47304730
use crate::headerdef::HeaderDef;
47314731
use crate::message::delete_msgs;
47324732
use crate::receive_imf::receive_imf;
4733-
use crate::test_utils::{sync, TestContext, TestContextManager};
4733+
use crate::test_utils::{sync, TestContext, TestContextManager, TimeShiftFalsePositiveNote};
47344734
use strum::IntoEnumIterator;
47354735
use tokio::fs;
47364736

@@ -5274,6 +5274,8 @@ mod tests {
52745274

52755275
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
52765276
async fn test_modify_chat_disordered() -> Result<()> {
5277+
let _n = TimeShiftFalsePositiveNote;
5278+
52775279
// Alice creates a group with Bob, Claire and Daisy and then removes Claire and Daisy
52785280
// (sleep() is needed as otherwise smeared time from Alice looks to Bob like messages from the future which are all set to "now" then)
52795281
let alice = TestContext::new_alice().await;

src/contact.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,7 @@ mod tests {
19841984
use crate::chat::{get_chat_contacts, send_text_msg, Chat};
19851985
use crate::chatlist::Chatlist;
19861986
use crate::receive_imf::receive_imf;
1987-
use crate::test_utils::{self, TestContext, TestContextManager};
1987+
use crate::test_utils::{self, TestContext, TestContextManager, TimeShiftFalsePositiveNote};
19881988

19891989
#[test]
19901990
fn test_contact_id_values() {
@@ -2915,6 +2915,8 @@ Hi."#;
29152915

29162916
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
29172917
async fn test_was_seen_recently() -> Result<()> {
2918+
let _n = TimeShiftFalsePositiveNote;
2919+
29182920
let mut tcm = TestContextManager::new();
29192921
let alice = tcm.alice().await;
29202922
let bob = tcm.bob().await;
@@ -2930,18 +2932,7 @@ Hi."#;
29302932
bob.recv_msg(&sent_msg).await;
29312933
let contact = Contact::get_by_id(&bob, *contacts.first().unwrap()).await?;
29322934

2933-
let green = nu_ansi_term::Color::Green.normal();
2934-
assert!(
2935-
contact.was_seen_recently(),
2936-
"{}",
2937-
green.paint(
2938-
"\nNOTE: This test failure is probably a false-positive, caused by tests running in parallel.
2939-
The issue is that `SystemTime::shift()` (a utility function for tests) changes the time for all threads doing tests, and not only for the running test.
2940-
Until the false-positive is fixed:
2941-
- Use `cargo test -- --test-threads 1` instead of `cargo test`
2942-
- Or use `cargo nextest run` (install with `cargo install cargo-nextest --locked`)\n"
2943-
)
2944-
);
2935+
assert!(contact.was_seen_recently());
29452936

29462937
let self_contact = Contact::get_by_id(&bob, ContactId::SELF).await?;
29472938
assert!(!self_contact.was_seen_recently());

src/securejoin.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ mod tests {
751751
use crate::imex::{imex, ImexMode};
752752
use crate::receive_imf::receive_imf;
753753
use crate::stock_str::{self, chat_protection_enabled};
754-
use crate::test_utils::get_chat_msg;
754+
use crate::test_utils::{get_chat_msg, TimeShiftFalsePositiveNote};
755755
use crate::test_utils::{TestContext, TestContextManager};
756756
use crate::tools::SystemTime;
757757
use std::collections::HashSet;
@@ -798,6 +798,8 @@ mod tests {
798798
}
799799

800800
async fn test_setup_contact_ex(case: SetupContactCase) {
801+
let _n = TimeShiftFalsePositiveNote;
802+
801803
let mut tcm = TestContextManager::new();
802804
let alice = tcm.alice().await;
803805
let alice_addr = &alice.get_config(Config::Addr).await.unwrap().unwrap();

src/test_utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,24 @@ async fn write_msg(context: &Context, prefix: &str, msg: &Message, buf: &mut Str
13511351
.unwrap();
13521352
}
13531353

1354+
/// When dropped after a test failure,
1355+
/// prints a note about a possible false-possible caused by SystemTime::shift().
1356+
pub(crate) struct TimeShiftFalsePositiveNote;
1357+
impl Drop for TimeShiftFalsePositiveNote {
1358+
fn drop(&mut self) {
1359+
if std::thread::panicking() {
1360+
let green = nu_ansi_term::Color::Green.normal();
1361+
println!("{}", green.paint(
1362+
"\nNOTE: This test failure may be a false-positive, caused by tests running in parallel.
1363+
The issue is that `SystemTime::shift()` (a utility function for tests) changes the time for all threads doing tests, and not only for the running test.
1364+
Until the false-positive is fixed:
1365+
- Use `cargo test -- --test-threads 1` instead of `cargo test`
1366+
- Or use `cargo nextest run` (install with `cargo install cargo-nextest --locked`)\n")
1367+
);
1368+
}
1369+
}
1370+
}
1371+
13541372
#[cfg(test)]
13551373
mod tests {
13561374
use super::*;

0 commit comments

Comments
 (0)