Skip to content

Commit

Permalink
add failing test for #5377
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Laux committed Mar 25, 2024
1 parent b95d582 commit 7dd715b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ use crate::tools::{
};
use crate::{chat, stock_str};


/// Time during which a contact is considered as seen recently.
#[cfg(not(test))]
const SEEN_RECENTLY_SECONDS: i64 = 600;
#[cfg(test)]
const SEEN_RECENTLY_SECONDS: i64 = 4;

/// Valid contact address.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -2811,6 +2815,52 @@ Hi."#;

Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_was_seen_recently_event() -> Result<()> {
let mut tcm = TestContextManager::new();
let alice = tcm.alice().await;
let bob = tcm.bob().await;

let chat = alice.create_chat(&bob).await;
let sent_msg = alice.send_text(chat.id, "moin").await;

let chat = bob.create_chat(&alice).await;
let contacts = chat::get_chat_contacts(&bob, chat.id).await?;
let contact = Contact::get_by_id(&bob, *contacts.first().unwrap()).await?;
assert!(!contact.was_seen_recently());

bob.recv_msg(&sent_msg).await;
let contact = Contact::get_by_id(&bob, *contacts.first().unwrap()).await?;
assert!(contact.was_seen_recently());

// wait for recently seen to be done
tokio::time::sleep(Duration::from_secs((SEEN_RECENTLY_SECONDS + 2) as u64)).await;
let contact = Contact::get_by_id(&bob, *contacts.first().unwrap()).await?;
assert!(!contact.was_seen_recently());

// first contact establishing will always emit events.
while bob.evtracker.try_recv().is_ok() {}

let chat = alice.create_chat(&bob).await;
let sent_msg = alice.send_text(chat.id, "moin2").await;
let contact = Contact::get_by_id(&bob, *contacts.first().unwrap()).await?;
assert!(!contact.was_seen_recently());
bob.recv_msg(&sent_msg).await;
let contact = Contact::get_by_id(&bob, *contacts.first().unwrap()).await?;
assert!(contact.was_seen_recently());
println!("wait for event from alice turning to recently seen");
bob.evtracker.get_matching(|evt| matches!(evt, EventType::ContactsChanged{..})).await;

// wait for recently seen to be done
tokio::time::sleep(Duration::from_secs((SEEN_RECENTLY_SECONDS + 2) as u64)).await;
let contact = Contact::get_by_id(&bob, *contacts.first().unwrap()).await?;
assert!(!contact.was_seen_recently());
println!("wait for event from alice turning to not recently seen");
bob.evtracker.get_matching(|evt| matches!(evt, EventType::ContactsChanged{..})).await;

Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_verified_by_none() -> Result<()> {
Expand Down

0 comments on commit 7dd715b

Please sign in to comment.