Skip to content

Commit

Permalink
test: keep a single sync in `test_delayed_invite_response_and_sent_me…
Browse files Browse the repository at this point in the history
…ssage_decryption()`

This removes one sync that happens in the background, because it's
likely spurious and may be confusing the server about what's been seen
by the current client.
  • Loading branch information
bnjbvr committed Jan 14, 2025
1 parent e9487b0 commit b9014a5
Showing 1 changed file with 58 additions and 42 deletions.
100 changes: 58 additions & 42 deletions testing/matrix-sdk-integration-testing/src/tests/sliding_sync/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use matrix_sdk::{
},
sliding_sync::VersionBuilder,
test_utils::{logged_in_client_with_server, mocks::MatrixMockServer},
Client, RoomInfo, RoomMemberships, RoomState, SlidingSyncList, SlidingSyncMode,
Client, Room, RoomInfo, RoomMemberships, RoomState, SlidingSyncList, SlidingSyncMode,
};
use matrix_sdk_base::{
ruma::{owned_room_id, room_alias_id},
Expand Down Expand Up @@ -835,10 +835,28 @@ async fn test_delayed_decryption_latest_event() -> Result<()> {
Ok(())
}

async fn get_or_wait_for_room(client: &Client, room_id: &RoomId) -> Room {
let (mut rooms, mut room_stream) = client.rooms_stream();

loop {
if let Some(room) = rooms.iter().find(|room| room.room_id() == room_id) {
return room.clone();
}

if let Ok(Some(diffs)) = timeout(Duration::from_secs(3), room_stream.next()).await {
for diff in diffs {
diff.apply(&mut rooms);
}
} else {
panic!("bob never founds out about the room");
}
}
}

#[tokio::test]
async fn test_delayed_invite_response_and_sent_message_decryption() -> Result<()> {
let alice = TestClientBuilder::new("alice").use_sqlite().build().await?;
let bob = TestClientBuilder::new("bob").use_sqlite().build().await?;
async fn test_delayed_invite_response_and_sent_message_decryption() {
let alice = TestClientBuilder::new("alice").use_sqlite().build().await.unwrap();
let bob = TestClientBuilder::new("bob").use_sqlite().build().await.unwrap();

let alice_sync_service = SyncService::builder(alice.clone()).build().await.unwrap();
alice_sync_service.start().await;
Expand All @@ -853,74 +871,72 @@ async fn test_delayed_invite_response_and_sent_message_decryption() -> Result<()
is_direct: true,
preset: Some(RoomPreset::PrivateChat),
}))
.await?;
alice_room.enable_encryption().await?;
.await
.unwrap();
alice_room.enable_encryption().await.unwrap();

// Initial message to make sure any lazy /members call is performed before the
// test actually starts
alice_room
.send(RoomMessageEventContent::text_plain("dummy message to make members call"))
.await?;
.await
.unwrap();

// Send the invite to Bob and a message to reproduce the edge case
alice_room.invite_user_by_id(bob.user_id().unwrap()).await.unwrap();
alice_room.send(RoomMessageEventContent::text_plain("hello world")).await?;
alice_room.send(RoomMessageEventContent::text_plain("hello world")).await.unwrap();

// Wait until Bob receives the invite
let bob_sync_stream = bob.sync_stream(SyncSettings::new()).await;
pin_mut!(bob_sync_stream);
let room_id = alice_room.room_id();

while let Some(Ok(response)) =
timeout(Duration::from_secs(3), bob_sync_stream.next()).await.expect("Room sync timed out")
{
if response.rooms.invite.contains_key(alice_room.room_id()) {
break;
}
}
// Wait until Bob receives the invite.
let bob_room = get_or_wait_for_room(&bob, room_id).await;

// Join the room from Bob's client.
let bob_timeline = bob_room.timeline().await.unwrap();
let (_, timeline_stream) = bob_timeline.subscribe().await;
pin_mut!(timeline_stream);

// Join the room from Bob's client
let bob_room = bob.get_room(alice_room.room_id()).unwrap();
bob_room.join().await?;
info!("Bob joins the room.");
bob_room.join().await.unwrap();

assert_eq!(alice_room.state(), RoomState::Joined);
assert!(alice_room.is_encrypted().await.unwrap());
assert_eq!(bob_room.state(), RoomState::Joined);
assert!(bob_room.is_encrypted().await.unwrap());

let bob_timeline = bob_room.timeline().await?;
let (_, timeline_stream) = bob_timeline.subscribe().await;
pin_mut!(timeline_stream);

// Get previous events, including the sent messages
bob_timeline.paginate_backwards(3).await?;

// Look for the sent message, which should not be an UTD event
loop {
let diff = timeout(Duration::from_millis(300), timeline_stream.next())
.await
.expect("Failed to receive the decrypted sent message")
.unwrap();
// Get previous events, including the sent messages.
bob_timeline.paginate_backwards(3).await.unwrap();

// Look for the sent message, which should not be an UTD event.
while let Ok(Some(diff)) = timeout(Duration::from_secs(3), timeline_stream.next()).await {
trace!(?diff, "Received diff from Bob's room");

match diff {
VectorDiff::PushBack { value: event }
VectorDiff::PushFront { value: event }
| VectorDiff::PushBack { value: event }
| VectorDiff::Insert { value: event, .. }
| VectorDiff::Set { value: event, .. } => {
if let Some(content) = event.as_event().map(|e| e.content()) {
if let Some(message) = content.as_message() {
if message.body() == "hello world" {
return Ok(());
}
let Some(event) = event.as_event() else {
continue;
};

panic!("Unexpected message event found");
}
let content = event.content();

if content.as_unable_to_decrypt().is_some() {
info!("Observed UTD for {}", event.event_id().unwrap());
}

if let Some(message) = content.as_message() {
assert_eq!(message.body(), "hello world");
return;
}
}

_ => {}
}
}

panic!("We never received the decrypted event!");
}

#[tokio::test]
Expand Down

0 comments on commit b9014a5

Please sign in to comment.