From 4140bbd665d6d4ea26b6dcbac3243cad838712b1 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Thu, 19 Sep 2024 12:19:13 +0200 Subject: [PATCH 1/6] Activate share_pos on the room-list sliding sync instance --- .../src/room_list_service/mod.rs | 1 + .../tests/integration/room_list_service.rs | 95 ++++++++++++++++++- .../tests/integration/sliding_sync.rs | 11 +++ 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk-ui/src/room_list_service/mod.rs b/crates/matrix-sdk-ui/src/room_list_service/mod.rs index f6045837f9e..fed288a3098 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/mod.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/mod.rs @@ -116,6 +116,7 @@ impl RoomListService { let mut builder = client .sliding_sync("room-list") .map_err(Error::SlidingSync)? + .share_pos() .with_account_data_extension( assign!(http::request::AccountData::default(), { enabled: Some(true) }), ) diff --git a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs index b37ec389f9c..c6cb498e7d4 100644 --- a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs +++ b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs @@ -6,7 +6,11 @@ use std::{ use assert_matches::assert_matches; use eyeball_im::VectorDiff; use futures_util::{pin_mut, FutureExt, StreamExt}; -use matrix_sdk::{test_utils::logged_in_client_with_server, Client}; +use matrix_sdk::{ + config::RequestConfig, + test_utils::{logged_in_client_with_server, set_client_session, test_client_builder}, + Client, +}; use matrix_sdk_base::{ sliding_sync::http::request::RoomSubscription, sync::UnreadNotificationsCount, }; @@ -27,6 +31,7 @@ use ruma::{ }; use serde_json::json; use stream_assert::{assert_next_matches, assert_pending}; +use tempfile::TempDir; use tokio::{spawn, sync::mpsc::channel, task::yield_now}; use wiremock::{ matchers::{header, method, path}, @@ -42,12 +47,30 @@ async fn new_room_list_service() -> Result<(Client, MockServer, RoomListService) Ok((client, server, room_list)) } +async fn new_persistent_room_list_service( + store_path: impl AsRef, +) -> Result<(MockServer, RoomListService), Error> { + let server = MockServer::start().await; + let client = test_client_builder(Some(server.uri().to_string())) + .request_config(RequestConfig::new().disable_retry()) + .sqlite_store(store_path, None) + .build() + .await + .unwrap(); + set_client_session(&client).await; + + let room_list = RoomListService::new(client.clone()).await?; + + Ok((server, room_list)) +} + // Same macro as in the main, with additional checking that the state // before/after the sync loop match those we expect. macro_rules! sync_then_assert_request_and_fake_response { ( [$server:ident, $room_list:ident, $stream:ident] $( states = $pre_state:pat => $post_state:pat, )? + $( assert pos $pos:expr, )? assert request $assert_request:tt { $( $request_json:tt )* }, respond with = $( ( code $code:expr ) )? { $( $response_json:tt )* } $( , after delay = $response_delay:expr )? @@ -57,6 +80,7 @@ macro_rules! sync_then_assert_request_and_fake_response { [$server, $room_list, $stream] sync matches Some(Ok(_)), $( states = $pre_state => $post_state, )? + $( assert pos $pos, )? assert request $assert_request { $( $request_json )* }, respond with = $( ( code $code ) )? { $( $response_json )* }, $( after delay = $response_delay, )? @@ -67,6 +91,7 @@ macro_rules! sync_then_assert_request_and_fake_response { [$server:ident, $room_list:ident, $stream:ident] sync matches $sync_result:pat, $( states = $pre_state:pat => $post_state:pat, )? + $( assert pos $pos:expr, )? assert request $assert_request:tt { $( $request_json:tt )* }, respond with = $( ( code $code:expr ) )? { $( $response_json:tt )* } $( , after delay = $response_delay:expr )? @@ -84,6 +109,7 @@ macro_rules! sync_then_assert_request_and_fake_response { let next = super::sliding_sync_then_assert_request_and_fake_response! { [$server, $stream] sync matches $sync_result, + $( assert pos $pos, )? assert request $assert_request { $( $request_json )* }, respond with = $( ( code $code ) )? { $( $response_json )* }, $( after delay = $response_delay, )? @@ -479,6 +505,7 @@ async fn test_sync_resumes_from_previous_state() -> Result<(), Error> { sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = Init => SettingUp, + assert pos None::, assert request >= { "lists": { ALL_ROOMS: { @@ -506,6 +533,7 @@ async fn test_sync_resumes_from_previous_state() -> Result<(), Error> { sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = SettingUp => Running, + assert pos Some("0"), assert request >= { "lists": { ALL_ROOMS: { @@ -533,6 +561,7 @@ async fn test_sync_resumes_from_previous_state() -> Result<(), Error> { sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = Running => Running, + assert pos Some("1"), assert request >= { "lists": { ALL_ROOMS: { @@ -555,6 +584,70 @@ async fn test_sync_resumes_from_previous_state() -> Result<(), Error> { Ok(()) } +#[async_test] +async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Error> { + let tmp_dir = TempDir::new().unwrap(); + let store_path = tmp_dir.path(); + + { + let (server, room_list) = new_persistent_room_list_service(&store_path).await?; + let sync = room_list.sync(); + pin_mut!(sync); + + sync_then_assert_request_and_fake_response! { + [server, room_list, sync] + states = Init => SettingUp, + assert pos None::, + assert request >= { + "lists": { + ALL_ROOMS: { + "ranges": [[0, 19]], + }, + }, + }, + respond with = { + "pos": "0", + "lists": { + ALL_ROOMS: { + "count": 10, + }, + }, + "rooms": {}, + }, + }; + } + + { + let (server, room_list) = new_persistent_room_list_service(&store_path).await?; + let sync = room_list.sync(); + pin_mut!(sync); + + sync_then_assert_request_and_fake_response! { + [server, room_list, sync] + states = Init => SettingUp, + assert pos Some("0"), + assert request >= { + "lists": { + ALL_ROOMS: { + "ranges": [[0, 19]], + }, + }, + }, + respond with = { + "pos": "1", + "lists": { + ALL_ROOMS: { + "count": 10, + }, + }, + "rooms": {}, + }, + }; + } + + Ok(()) +} + #[async_test] async fn test_sync_resumes_from_error() -> Result<(), Error> { let (_, server, room_list) = new_room_list_service().await?; diff --git a/crates/matrix-sdk-ui/tests/integration/sliding_sync.rs b/crates/matrix-sdk-ui/tests/integration/sliding_sync.rs index b267fa96bb7..5ec1dda2c00 100644 --- a/crates/matrix-sdk-ui/tests/integration/sliding_sync.rs +++ b/crates/matrix-sdk-ui/tests/integration/sliding_sync.rs @@ -58,6 +58,7 @@ impl Match for SlidingSyncMatcher { macro_rules! sliding_sync_then_assert_request_and_fake_response { ( [$server:ident, $stream:ident] + $( assert pos $pos:expr, )? assert request $sign:tt { $( $request_json:tt )* }, respond with = $( ( code $code:expr ) )? { $( $response_json:tt )* } $( , after delay = $response_delay:expr )? @@ -66,6 +67,7 @@ macro_rules! sliding_sync_then_assert_request_and_fake_response { sliding_sync_then_assert_request_and_fake_response! { [$server, $stream] sync matches Some(Ok(_)), + $( assert pos $pos, )? assert request $sign { $( $request_json )* }, respond with = $( ( code $code ) )? { $( $response_json )* }, $( after delay = $response_delay, )? @@ -75,6 +77,7 @@ macro_rules! sliding_sync_then_assert_request_and_fake_response { ( [$server:ident, $stream:ident] sync matches $sync_result:pat, + $( assert pos $pos:expr, )? assert request $sign:tt { $( $request_json:tt )* }, respond with = $( ( code $code:expr ) )? { $( $response_json:tt )* } $( , after delay = $response_delay:expr )? @@ -117,6 +120,14 @@ macro_rules! sliding_sync_then_assert_request_and_fake_response { root.remove("txn_id"); } + // Validate `pos` from the query parameter if specified. + $( + match $pos { + Some(pos) => assert!(wiremock::matchers::query_param("pos", pos).matches(request)), + None => assert!(wiremock::matchers::query_param_is_missing("pos").matches(request)), + } + )? + if let Err(error) = assert_json_diff::assert_json_matches_no_panic( &json_value, &json!({ $( $request_json )* }), From ac4d99cea4b41d36cb2d8d2b7dc8f1ad5a5a9e55 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Mon, 30 Sep 2024 11:41:37 +0200 Subject: [PATCH 2/6] Address comments --- crates/matrix-sdk-ui/src/room_list_service/mod.rs | 4 +++- crates/matrix-sdk-ui/tests/integration/room_list_service.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk-ui/src/room_list_service/mod.rs b/crates/matrix-sdk-ui/src/room_list_service/mod.rs index fed288a3098..143b8b77b99 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/mod.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/mod.rs @@ -116,7 +116,6 @@ impl RoomListService { let mut builder = client .sliding_sync("room-list") .map_err(Error::SlidingSync)? - .share_pos() .with_account_data_extension( assign!(http::request::AccountData::default(), { enabled: Some(true) }), ) @@ -136,6 +135,9 @@ impl RoomListService { .with_to_device_extension( assign!(http::request::ToDevice::default(), { enabled: Some(true) }), ); + } else { + // TODO: This is racy with encryption, needs cross-process lock + builder = builder.share_pos(); } let sliding_sync = builder diff --git a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs index c6cb498e7d4..4fdf66f28ea 100644 --- a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs +++ b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs @@ -48,7 +48,7 @@ async fn new_room_list_service() -> Result<(Client, MockServer, RoomListService) } async fn new_persistent_room_list_service( - store_path: impl AsRef, + store_path: &std::path::Path, ) -> Result<(MockServer, RoomListService), Error> { let server = MockServer::start().await; let client = test_client_builder(Some(server.uri().to_string())) From 492166a3d6377a363dc86ec50d9718fa29ee8ad6 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Mon, 7 Oct 2024 21:32:45 +0200 Subject: [PATCH 3/6] RoomListLoadingState now yields immediately with current value --- .../src/room_list_service/room_list.rs | 2 +- .../tests/integration/room_list_service.rs | 41 ++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/crates/matrix-sdk-ui/src/room_list_service/room_list.rs b/crates/matrix-sdk-ui/src/room_list_service/room_list.rs index 0cde4bbcd40..4d02b212f6e 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/room_list.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/room_list.rs @@ -115,7 +115,7 @@ impl RoomList { /// Get a subscriber to the room list loading state. pub fn loading_state(&self) -> Subscriber { - self.loading_state.subscribe() + self.loading_state.subscribe_reset() } /// Get all previous rooms, in addition to a [`Stream`] to rooms' updates. diff --git a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs index 4fdf66f28ea..cdf29f0e244 100644 --- a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs +++ b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs @@ -594,6 +594,13 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err let sync = room_list.sync(); pin_mut!(sync); + let all_rooms = room_list.all_rooms().await?; + let mut all_rooms_loading_state = all_rooms.loading_state(); + + // The loading is not loaded. + assert_next_matches!(all_rooms_loading_state, RoomListLoadingState::NotLoaded); + assert_pending!(all_rooms_loading_state); + sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = Init => SettingUp, @@ -622,6 +629,20 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err let sync = room_list.sync(); pin_mut!(sync); + let all_rooms = room_list.all_rooms().await?; + let mut all_rooms_loading_state = all_rooms.loading_state(); + + // Wait on Tokio to run all the tasks. Necessary only when testing. + yield_now().await; + + // We already have a state stored so the list should already be loaded + assert_next_matches!( + all_rooms_loading_state, + RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(10) } + ); + assert_pending!(all_rooms_loading_state); + + // pos has been restored and is used when doing the req sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = Init => SettingUp, @@ -637,12 +658,22 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err "pos": "1", "lists": { ALL_ROOMS: { - "count": 10, + "count": 12, }, }, "rooms": {}, }, }; + + // Wait on Tokio to run all the tasks. Necessary only when testing. + yield_now().await; + + // maximum_number_of_rooms changed so we should get a new loaded state + assert_next_matches!( + all_rooms_loading_state, + RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(12) } + ); + assert_pending!(all_rooms_loading_state); } Ok(()) @@ -1116,8 +1147,7 @@ async fn test_loading_states() -> Result<(), Error> { let mut all_rooms_loading_state = all_rooms.loading_state(); // The loading is not loaded. - assert_matches!(all_rooms_loading_state.get(), RoomListLoadingState::NotLoaded); - assert_pending!(all_rooms_loading_state); + assert_next_matches!(all_rooms_loading_state, RoomListLoadingState::NotLoaded); sync_then_assert_request_and_fake_response! { [server, room_list, sync] @@ -1220,11 +1250,10 @@ async fn test_loading_states() -> Result<(), Error> { pin_mut!(sync); // The loading state is loaded! Indeed, there is data loaded from the cache. - assert_matches!( - all_rooms_loading_state.get(), + assert_next_matches!( + all_rooms_loading_state, RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(12) } ); - assert_pending!(all_rooms_loading_state); sync_then_assert_request_and_fake_response! { [server, room_list, sync] From 7a3197da6f37b3e8d0c7c32487aa58bdf17d7c90 Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Mon, 7 Oct 2024 21:42:49 +0200 Subject: [PATCH 4/6] Fix clippy --- crates/matrix-sdk-ui/src/room_list_service/room_list.rs | 2 ++ crates/matrix-sdk-ui/tests/integration/room_list_service.rs | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk-ui/src/room_list_service/room_list.rs b/crates/matrix-sdk-ui/src/room_list_service/room_list.rs index 4d02b212f6e..81e3f0ee1fd 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/room_list.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/room_list.rs @@ -114,6 +114,8 @@ impl RoomList { } /// Get a subscriber to the room list loading state. + /// + /// This method will send out the current loading state as the first update. pub fn loading_state(&self) -> Subscriber { self.loading_state.subscribe_reset() } diff --git a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs index cdf29f0e244..75377dc1eb4 100644 --- a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs +++ b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs @@ -590,7 +590,7 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err let store_path = tmp_dir.path(); { - let (server, room_list) = new_persistent_room_list_service(&store_path).await?; + let (server, room_list) = new_persistent_room_list_service(store_path).await?; let sync = room_list.sync(); pin_mut!(sync); @@ -625,7 +625,7 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err } { - let (server, room_list) = new_persistent_room_list_service(&store_path).await?; + let (server, room_list) = new_persistent_room_list_service(store_path).await?; let sync = room_list.sync(); pin_mut!(sync); From 8d4984a756fb81db1b48c779cc56d4ae815fd67e Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Tue, 8 Oct 2024 17:49:30 +0200 Subject: [PATCH 5/6] Adress some comments --- .../tests/integration/room_list_service.rs | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs index 9c934254725..d28c8613d13 100644 --- a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs +++ b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs @@ -7,6 +7,7 @@ use assert_matches::assert_matches; use eyeball_im::VectorDiff; use futures_util::{pin_mut, FutureExt, StreamExt}; use matrix_sdk::{ + assert_next_matches_with_timeout, config::RequestConfig, test_utils::{logged_in_client_with_server, set_client_session, test_client_builder}, Client, @@ -30,7 +31,7 @@ use ruma::{ mxc_uri, room_id, uint, }; use serde_json::json; -use stream_assert::{assert_next_matches, assert_pending}; +use stream_assert::assert_pending; use tempfile::TempDir; use tokio::{spawn, sync::mpsc::channel, task::yield_now}; use wiremock::{ @@ -48,10 +49,10 @@ async fn new_room_list_service() -> Result<(Client, MockServer, RoomListService) } async fn new_persistent_room_list_service( + homeserver_url: String, store_path: &std::path::Path, -) -> Result<(MockServer, RoomListService), Error> { - let server = MockServer::start().await; - let client = test_client_builder(Some(server.uri().to_string())) +) -> Result { + let client = test_client_builder(Some(homeserver_url)) .request_config(RequestConfig::new().disable_retry()) .sqlite_store(store_path, None) .build() @@ -61,7 +62,7 @@ async fn new_persistent_room_list_service( let room_list = RoomListService::new(client.clone()).await?; - Ok((server, room_list)) + Ok(room_list) } // Same macro as in the main, with additional checking that the state @@ -596,8 +597,10 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err let tmp_dir = TempDir::new().unwrap(); let store_path = tmp_dir.path(); + let server = MockServer::start().await; + { - let (server, room_list) = new_persistent_room_list_service(store_path).await?; + let room_list = new_persistent_room_list_service(server.uri(), store_path).await?; let sync = room_list.sync(); pin_mut!(sync); @@ -605,7 +608,7 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err let mut all_rooms_loading_state = all_rooms.loading_state(); // The loading is not loaded. - assert_next_matches!(all_rooms_loading_state, RoomListLoadingState::NotLoaded); + assert_next_matches_with_timeout!(all_rooms_loading_state, RoomListLoadingState::NotLoaded); assert_pending!(all_rooms_loading_state); sync_then_assert_request_and_fake_response! { @@ -632,7 +635,7 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err } { - let (server, room_list) = new_persistent_room_list_service(store_path).await?; + let room_list = new_persistent_room_list_service(server.uri(), store_path).await?; let sync = room_list.sync(); pin_mut!(sync); @@ -643,7 +646,7 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err yield_now().await; // We already have a state stored so the list should already be loaded - assert_next_matches!( + assert_next_matches_with_timeout!( all_rooms_loading_state, RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(10) } ); @@ -676,7 +679,7 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err yield_now().await; // maximum_number_of_rooms changed so we should get a new loaded state - assert_next_matches!( + assert_next_matches_with_timeout!( all_rooms_loading_state, RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(12) } ); @@ -1172,7 +1175,8 @@ async fn test_loading_states() -> Result<(), Error> { let mut all_rooms_loading_state = all_rooms.loading_state(); // The loading is not loaded. - assert_next_matches!(all_rooms_loading_state, RoomListLoadingState::NotLoaded); + assert_next_matches_with_timeout!(all_rooms_loading_state, RoomListLoadingState::NotLoaded); + assert_pending!(all_rooms_loading_state); sync_then_assert_request_and_fake_response! { [server, room_list, sync] @@ -1200,10 +1204,11 @@ async fn test_loading_states() -> Result<(), Error> { yield_now().await; // There is a loading state update, it's loaded now! - assert_next_matches!( + assert_next_matches_with_timeout!( all_rooms_loading_state, RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(10) } ); + assert_pending!(all_rooms_loading_state); sync_then_assert_request_and_fake_response! { [server, room_list, sync] @@ -1231,10 +1236,11 @@ async fn test_loading_states() -> Result<(), Error> { yield_now().await; // There is a loading state update because the number of rooms has been updated. - assert_next_matches!( + assert_next_matches_with_timeout!( all_rooms_loading_state, RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(12) } ); + assert_pending!(all_rooms_loading_state); sync_then_assert_request_and_fake_response! { [server, room_list, sync] @@ -1278,10 +1284,11 @@ async fn test_loading_states() -> Result<(), Error> { pin_mut!(sync); // The loading state is loaded! Indeed, there is data loaded from the cache. - assert_next_matches!( + assert_next_matches_with_timeout!( all_rooms_loading_state, RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(12) } ); + assert_pending!(all_rooms_loading_state); sync_then_assert_request_and_fake_response! { [server, room_list, sync] @@ -1309,10 +1316,11 @@ async fn test_loading_states() -> Result<(), Error> { yield_now().await; // The loading state has been updated. - assert_next_matches!( + assert_next_matches_with_timeout!( all_rooms_loading_state, RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(13) } ); + assert_pending!(all_rooms_loading_state); } Ok(()) From 11a38960048e62aaadd9c9599782494b88a9192e Mon Sep 17 00:00:00 2001 From: Mathieu Velten Date: Wed, 9 Oct 2024 00:59:54 +0200 Subject: [PATCH 6/6] Adress comments --- .../tests/integration/room_list_service.rs | 24 +++++++------------ .../tests/integration/sliding_sync.rs | 11 +++++---- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs index d28c8613d13..e7ebf4b8a97 100644 --- a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs +++ b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs @@ -71,7 +71,7 @@ macro_rules! sync_then_assert_request_and_fake_response { ( [$server:ident, $room_list:ident, $stream:ident] $( states = $pre_state:pat => $post_state:pat, )? - $( assert pos $pos:expr, )? + $( assert pos is $pos:tt, )? assert request $assert_request:tt { $( $request_json:tt )* }, respond with = $( ( code $code:expr ) )? { $( $response_json:tt )* } $( , after delay = $response_delay:expr )? @@ -81,7 +81,7 @@ macro_rules! sync_then_assert_request_and_fake_response { [$server, $room_list, $stream] sync matches Some(Ok(_)), $( states = $pre_state => $post_state, )? - $( assert pos $pos, )? + $( assert pos is $pos, )? assert request $assert_request { $( $request_json )* }, respond with = $( ( code $code ) )? { $( $response_json )* }, $( after delay = $response_delay, )? @@ -92,7 +92,7 @@ macro_rules! sync_then_assert_request_and_fake_response { [$server:ident, $room_list:ident, $stream:ident] sync matches $sync_result:pat, $( states = $pre_state:pat => $post_state:pat, )? - $( assert pos $pos:expr, )? + $( assert pos is $pos:tt, )? assert request $assert_request:tt { $( $request_json:tt )* }, respond with = $( ( code $code:expr ) )? { $( $response_json:tt )* } $( , after delay = $response_delay:expr )? @@ -110,7 +110,7 @@ macro_rules! sync_then_assert_request_and_fake_response { let next = super::sliding_sync_then_assert_request_and_fake_response! { [$server, $stream] sync matches $sync_result, - $( assert pos $pos, )? + $( assert pos is $pos, )? assert request $assert_request { $( $request_json )* }, respond with = $( ( code $code ) )? { $( $response_json )* }, $( after delay = $response_delay, )? @@ -510,7 +510,7 @@ async fn test_sync_resumes_from_previous_state() -> Result<(), Error> { sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = Init => SettingUp, - assert pos None::, + assert pos is none, assert request >= { "lists": { ALL_ROOMS: { @@ -539,7 +539,7 @@ async fn test_sync_resumes_from_previous_state() -> Result<(), Error> { sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = SettingUp => Running, - assert pos Some("0"), + assert pos is "0", assert request >= { "lists": { ALL_ROOMS: { @@ -568,7 +568,7 @@ async fn test_sync_resumes_from_previous_state() -> Result<(), Error> { sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = Running => Running, - assert pos Some("1"), + assert pos is "1", assert request >= { "lists": { ALL_ROOMS: { @@ -614,7 +614,7 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = Init => SettingUp, - assert pos None::, + assert pos is none, assert request >= { "lists": { ALL_ROOMS: { @@ -642,9 +642,6 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err let all_rooms = room_list.all_rooms().await?; let mut all_rooms_loading_state = all_rooms.loading_state(); - // Wait on Tokio to run all the tasks. Necessary only when testing. - yield_now().await; - // We already have a state stored so the list should already be loaded assert_next_matches_with_timeout!( all_rooms_loading_state, @@ -656,7 +653,7 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err sync_then_assert_request_and_fake_response! { [server, room_list, sync] states = Init => SettingUp, - assert pos Some("0"), + assert pos is "0", assert request >= { "lists": { ALL_ROOMS: { @@ -675,9 +672,6 @@ async fn test_sync_resumes_from_previous_state_after_restart() -> Result<(), Err }, }; - // Wait on Tokio to run all the tasks. Necessary only when testing. - yield_now().await; - // maximum_number_of_rooms changed so we should get a new loaded state assert_next_matches_with_timeout!( all_rooms_loading_state, diff --git a/crates/matrix-sdk-ui/tests/integration/sliding_sync.rs b/crates/matrix-sdk-ui/tests/integration/sliding_sync.rs index 5ec1dda2c00..740811e862c 100644 --- a/crates/matrix-sdk-ui/tests/integration/sliding_sync.rs +++ b/crates/matrix-sdk-ui/tests/integration/sliding_sync.rs @@ -58,7 +58,7 @@ impl Match for SlidingSyncMatcher { macro_rules! sliding_sync_then_assert_request_and_fake_response { ( [$server:ident, $stream:ident] - $( assert pos $pos:expr, )? + $( assert pos is $pos:tt, )? assert request $sign:tt { $( $request_json:tt )* }, respond with = $( ( code $code:expr ) )? { $( $response_json:tt )* } $( , after delay = $response_delay:expr )? @@ -67,7 +67,7 @@ macro_rules! sliding_sync_then_assert_request_and_fake_response { sliding_sync_then_assert_request_and_fake_response! { [$server, $stream] sync matches Some(Ok(_)), - $( assert pos $pos, )? + $( assert pos is $pos, )? assert request $sign { $( $request_json )* }, respond with = $( ( code $code ) )? { $( $response_json )* }, $( after delay = $response_delay, )? @@ -77,7 +77,7 @@ macro_rules! sliding_sync_then_assert_request_and_fake_response { ( [$server:ident, $stream:ident] sync matches $sync_result:pat, - $( assert pos $pos:expr, )? + $( assert pos is $pos:tt, )? assert request $sign:tt { $( $request_json:tt )* }, respond with = $( ( code $code:expr ) )? { $( $response_json:tt )* } $( , after delay = $response_delay:expr )? @@ -122,7 +122,7 @@ macro_rules! sliding_sync_then_assert_request_and_fake_response { // Validate `pos` from the query parameter if specified. $( - match $pos { + match $crate::sliding_sync_then_assert_request_and_fake_response!(@assertion_pos $pos) { Some(pos) => assert!(wiremock::matchers::query_param("pos", pos).matches(request)), None => assert!(wiremock::matchers::query_param_is_missing("pos").matches(request)), } @@ -150,4 +150,7 @@ macro_rules! sliding_sync_then_assert_request_and_fake_response { (@assertion_config >=) => { assert_json_diff::Config::new(assert_json_diff::CompareMode::Inclusive) }; (@assertion_config =) => { assert_json_diff::Config::new(assert_json_diff::CompareMode::Strict) }; + + (@assertion_pos none) => { None:: }; + (@assertion_pos $val:expr) => { Some($val) }; }