Skip to content

Commit

Permalink
tests: add helper to delay a room /messages response
Browse files Browse the repository at this point in the history
This removes a few manual uses of `ResponseTemplate`, which is sweet and
guarantees some better typing for those responses overall.
  • Loading branch information
bnjbvr committed Jan 13, 2025
1 parent 4228b6c commit bb444d0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
18 changes: 6 additions & 12 deletions crates/matrix-sdk-ui/tests/integration/room_list_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use futures_util::{pin_mut, FutureExt, StreamExt};
use matrix_sdk::{
config::RequestConfig,
test_utils::{
logged_in_client_with_server, mocks::MatrixMockServer, set_client_session,
test_client_builder,
logged_in_client_with_server,
mocks::{MatrixMockServer, RoomMessagesResponse},
set_client_session, test_client_builder,
},
Client,
};
Expand Down Expand Up @@ -2845,16 +2846,9 @@ async fn test_multiple_timeline_init() {
// Send back-pagination responses with a small delay.
server
.mock_room_messages()
.respond_with(
ResponseTemplate::new(200)
.set_body_json(json!({
"start": "unused-start",
"end": null,
"chunk": vec![f.text_msg("hello").into_raw_timeline()],
"state": [],
}))
.set_delay(Duration::from_millis(500)),
)
.ok(RoomMessagesResponse::default()
.events(vec![f.text_msg("hello").into_raw_timeline()])
.delayed(Duration::from_millis(500)))
.mount()
.await;

Expand Down
19 changes: 17 additions & 2 deletions crates/matrix-sdk/src/test_utils/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1698,12 +1698,18 @@ impl<'a> MockEndpoint<'a, RoomMessagesEndpoint> {
/// Note: pass `chunk` in the correct order: topological for forward
/// pagination, reverse topological for backwards pagination.
pub fn ok(self, response: RoomMessagesResponse) -> MatrixMock<'a> {
let mock = self.mock.respond_with(ResponseTemplate::new(200).set_body_json(json!({
let mut template = ResponseTemplate::new(200).set_body_json(json!({
"start": response.start,
"end": response.end,
"chunk": response.chunk,
"state": response.state,
})));
}));

if let Some(delay) = response.delay {
template = template.set_delay(delay);
}

let mock = self.mock.respond_with(template);
MatrixMock { server: self.server, mock }
}
}
Expand All @@ -1719,6 +1725,8 @@ pub struct RoomMessagesResponse {
pub chunk: Vec<Raw<AnyTimelineEvent>>,
/// The set of state events returned by this query.
pub state: Vec<Raw<AnyStateEvent>>,
/// Optional delay to respond to the query.
pub delay: Option<Duration>,
}

impl RoomMessagesResponse {
Expand All @@ -1733,6 +1741,12 @@ impl RoomMessagesResponse {
self.end = Some(token.into());
self
}

/// Respond with a given delay to the query.
pub fn delayed(mut self, delay: Duration) -> Self {
self.delay = Some(delay);
self
}
}

impl Default for RoomMessagesResponse {
Expand All @@ -1742,6 +1756,7 @@ impl Default for RoomMessagesResponse {
end: Default::default(),
chunk: Default::default(),
state: Default::default(),
delay: None,
}
}
}
Expand Down
34 changes: 10 additions & 24 deletions crates/matrix-sdk/tests/integration/event_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use matrix_sdk_test::{
use ruma::{event_id, room_id, user_id};
use serde_json::json;
use tokio::{spawn, sync::broadcast, time::sleep};
use wiremock::ResponseTemplate;

async fn once(
outcome: BackPaginationOutcome,
Expand Down Expand Up @@ -642,15 +641,9 @@ async fn test_reset_while_backpaginating() {
server
.mock_room_messages()
.from("first_backpagination")
.respond_with(
ResponseTemplate::new(200)
.set_body_json(json!({
"chunk": vec![f.text_msg("lalala").into_raw_timeline()],
"start": "t392-516_47314_0_7_1_1_1_11444_1",
}))
// This is why we don't use `server.mock_room_messages()`.
.set_delay(Duration::from_millis(500)),
)
.ok(RoomMessagesResponse::default()
.events(vec![f.text_msg("lalala").into_raw_timeline()])
.delayed(Duration::from_millis(500)))
.mock_once()
.mount()
.await;
Expand Down Expand Up @@ -1065,20 +1058,13 @@ async fn test_backpaginate_with_no_initial_events() {
let wait_time = Duration::from_millis(500);
server
.mock_room_messages()
.respond_with(
ResponseTemplate::new(200)
.set_body_json(json!({
"chunk": vec![
f.text_msg("world").event_id(event_id!("$2")).into_raw_timeline(),
f.text_msg("hello").event_id(event_id!("$3")).into_raw_timeline(),
],
"start": "start-token-unused1",
"end": "prev_batch"
}))
// This is why we don't use `server.mock_room_messages()`.
// This delay has to be greater than the one used to return the sync response.
.set_delay(2 * wait_time),
)
.ok(RoomMessagesResponse::default()
.end_token("prev_batch")
.events(vec![
f.text_msg("world").event_id(event_id!("$2")).into_raw_timeline(),
f.text_msg("hello").event_id(event_id!("$3")).into_raw_timeline(),
])
.delayed(2 * wait_time))
.mock_once()
.mount()
.await;
Expand Down

0 comments on commit bb444d0

Please sign in to comment.