From c9e2db1fe548416de32b61d6fe2ebb3209307eba Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 16 Jul 2024 11:35:14 +0200 Subject: [PATCH] test(sdk): make `test_room_info_notable_update_deduplication` better --- .../src/tests/sliding_sync/room.rs | 115 +++++------------- 1 file changed, 28 insertions(+), 87 deletions(-) diff --git a/testing/matrix-sdk-integration-testing/src/tests/sliding_sync/room.rs b/testing/matrix-sdk-integration-testing/src/tests/sliding_sync/room.rs index 61c531e82..c6d8a6aae 100644 --- a/testing/matrix-sdk-integration-testing/src/tests/sliding_sync/room.rs +++ b/testing/matrix-sdk-integration-testing/src/tests/sliding_sync/room.rs @@ -796,7 +796,7 @@ async fn test_delayed_decryption_latest_event() -> Result<()> { assert_matches!( &diffs[0], VectorDiff::Reset { values: rooms } => { - assert_eq!(rooms.len(), 1); + assert_eq!(rooms.len(), 1, "{rooms:?}"); assert_eq!(rooms[0].room_id(), room.room_id()); } ); @@ -818,6 +818,8 @@ async fn test_delayed_decryption_latest_event() -> Result<()> { } ); + assert_pending!(alice_room_list_stream); + // Now we allow the key to come through *CUSTOM_RESPONDER.drop_todevice.lock().unwrap() = false; @@ -924,51 +926,13 @@ async fn test_room_info_notable_update_deduplication() -> Result<()> { let alice = TestClientBuilder::new("alice").use_sqlite().build().await?; let bob = TestClientBuilder::new("bob").use_sqlite().build().await?; + // Set up sliding sync and encryption for Alice. let alice_sync_service = SyncService::builder(alice.clone()).build().await.unwrap(); alice_sync_service.start().await; - // Set up sliding sync for alice. - let sliding_alice = alice - .sliding_sync("main")? - .with_all_extensions() - .poll_timeout(Duration::from_secs(2)) - .network_timeout(Duration::from_secs(2)) - .add_list( - SlidingSyncList::builder("all") - .sync_mode(SlidingSyncMode::new_selective().add_range(0..=20)), - ) - .build() - .await?; // Set up sliding sync for bob. - let sliding_bob = bob - .sliding_sync("main")? - .with_all_extensions() - .poll_timeout(Duration::from_secs(2)) - .network_timeout(Duration::from_secs(2)) - .add_list( - SlidingSyncList::builder("all") - .sync_mode(SlidingSyncMode::new_selective().add_range(0..=20)), - ) - .build() - .await?; - - let s = sliding_alice.clone(); - spawn(async move { - let stream = s.sync(); - pin_mut!(stream); - while let Some(up) = stream.next().await { - warn!("alice received update: {up:?}"); - } - }); - - let s = sliding_bob.clone(); - spawn(async move { - let stream = s.sync(); - pin_mut!(stream); - while let Some(up) = stream.next().await { - warn!("bob received update: {up:?}"); - } - }); + let bob_sync_service = SyncService::builder(bob.clone()).build().await.unwrap(); + bob_sync_service.start().await; // alice creates a room and invites bob. let alice_room = alice @@ -978,31 +942,37 @@ async fn test_room_info_notable_update_deduplication() -> Result<()> { preset: Some(RoomPreset::TrustedPrivateChat), })) .await?; + alice_room.enable_encryption().await.unwrap(); - let alice_all_rooms = alice_sync_service.room_list_service().all_rooms().await.unwrap(); - let (stream, entries) = alice_all_rooms + let alice_room_list = alice_sync_service.room_list_service().all_rooms().await.unwrap(); + let (alice_rooms, alice_room_controller) = alice_room_list .entries_with_dynamic_adapters(10, alice.room_info_notable_update_receiver()); - entries.set_filter(Box::new(new_filter_all(vec![]))); - pin_mut!(stream); + alice_room_controller.set_filter(Box::new(new_filter_all(vec![]))); - // Stream only has the initial Reset entry. - assert_let!(Some(diffs) = stream.next().await); + pin_mut!(alice_rooms); + + // First, we observe the initial reset. + assert_let!(Ok(Some(diffs)) = timeout(Duration::from_secs(3), alice_rooms.next()).await); assert_eq!(diffs.len(), 1); assert_matches!( &diffs[0], - VectorDiff::Reset { values: rooms } => { + VectorDiff::Reset { values: rooms } => { assert_eq!(rooms.len(), 1); assert_eq!(rooms[0].room_id(), alice_room.room_id()); } ); - assert_pending!(stream); + assert_pending!(alice_rooms); + + // Alice sees the room. let alice_room = alice.get_room(alice_room.room_id()).unwrap(); assert_eq!(alice_room.state(), RoomState::Joined); + assert!(alice_room.is_encrypted().await.unwrap()); + // Bob sees and joins the room. let bob_room = bob.get_room(alice_room.room_id()).unwrap(); bob_room.join().await.unwrap(); @@ -1010,41 +980,19 @@ async fn test_room_info_notable_update_deduplication() -> Result<()> { sleep(Duration::from_secs(2)).await; assert_eq!(bob_room.state(), RoomState::Joined); - // Room update for join - assert_let!(Some(diffs) = stream.next().await); - assert_eq!(diffs.len(), 1); - assert_matches!( - &diffs[0], - VectorDiff::Set { index: 0, value: room } => { - assert_eq!(room.room_id(), alice_room.room_id()); - } - ); + assert_pending!(alice_rooms); - // Sometimes Synapse sends the same message twice. Let's consume useless `Set`… - // if they arrived before 3s. - if let Ok(Some(diffs)) = timeout(Duration::from_secs(3), stream.next()).await { - assert_eq!(diffs.len(), 1); - assert_matches!( - &diffs[0], - VectorDiff::Set { index: 0, value: room } => { - assert_eq!(room.room_id(), alice_room.room_id()); - } - ); - } - - assert_pending!(stream); - - // Send a message, it should arrive + // Send a message, it should arrive. let event = bob_room.send(RoomMessageEventContent::text_plain("hello world")).await?; // Wait the message from Bob to be sent. sleep(Duration::from_secs(2)).await; - // Latest event is set now + // Latest event is set now. assert_eq!(alice_room.latest_event().unwrap().event_id(), Some(event.event_id)); - // Room has been updated. - assert_let!(Some(diffs) = stream.next().await); + // Room has been updated because of the latest event. + assert_let!(Ok(Some(diffs)) = timeout(Duration::from_secs(3), alice_rooms.next()).await); assert_eq!(diffs.len(), 1); assert_matches!( &diffs[0], @@ -1053,16 +1001,9 @@ async fn test_room_info_notable_update_deduplication() -> Result<()> { } ); - assert_let!(Some(diffs) = stream.next().await); - assert_eq!(diffs.len(), 1); - assert_matches!( - &diffs[0], - VectorDiff::Set { index: 0, value: room } => { - assert_eq!(room.room_id(), alice_room.room_id()); - } - ); - - assert_pending!(stream); + // And there are no subsequent room updates. + sleep(Duration::from_secs(2)).await; + assert_pending!(alice_rooms); Ok(()) }