Skip to content

Commit 2d0676c

Browse files
bnjbvrandybalaam
authored andcommitted
test(event cache): make use of macros to avoid manual timeouts
1 parent cd05871 commit 2d0676c

File tree

1 file changed

+20
-45
lines changed

1 file changed

+20
-45
lines changed

crates/matrix-sdk/tests/integration/event_cache.rs

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::{future::ready, ops::ControlFlow, time::Duration};
22

33
use assert_matches::assert_matches;
4-
use assert_matches2::assert_let;
54
use matrix_sdk::{
5+
assert_let_timeout, assert_next_matches_with_timeout,
66
event_cache::{
7-
paginator::PaginatorState, BackPaginationOutcome, EventCacheError, EventsOrigin,
8-
RoomEventCacheUpdate, TimelineHasBeenResetWhilePaginating,
7+
paginator::PaginatorState, BackPaginationOutcome, EventCacheError, RoomEventCacheUpdate,
8+
TimelineHasBeenResetWhilePaginating,
99
},
1010
test_utils::{assert_event_matches_msg, logged_in_client_with_server, mocks::MatrixMockServer},
1111
};
@@ -15,7 +15,7 @@ use matrix_sdk_test::{
1515
};
1616
use ruma::{event_id, events::AnyTimelineEvent, room_id, serde::Raw, user_id};
1717
use serde_json::json;
18-
use tokio::{spawn, time::timeout};
18+
use tokio::spawn;
1919
use wiremock::{
2020
matchers::{header, method, path_regex, query_param},
2121
Mock, MockServer, ResponseTemplate,
@@ -104,13 +104,11 @@ async fn test_event_cache_receives_events() {
104104
server.reset().await;
105105

106106
// It does receive one update,
107-
let update = timeout(Duration::from_secs(2), subscriber.recv())
108-
.await
109-
.expect("timeout after receiving a sync update")
110-
.expect("should've received a room event cache update");
107+
assert_let_timeout!(
108+
Ok(RoomEventCacheUpdate::AddTimelineEvents { events, .. }) = subscriber.recv()
109+
);
111110

112111
// Which contains the event that was sent beforehand.
113-
assert_let!(RoomEventCacheUpdate::AddTimelineEvents { events, .. } = update);
114112
assert_eq!(events.len(), 1);
115113
assert_event_matches_msg(&events[0], "bonjour monde");
116114

@@ -177,14 +175,8 @@ async fn test_ignored_unignored() {
177175
})
178176
.await;
179177

180-
// It does receive one update,
181-
let update = timeout(Duration::from_secs(2), subscriber.recv())
182-
.await
183-
.expect("timeout after receiving a sync update")
184-
.expect("should've received a room event cache update");
185-
186-
// Which notifies about the clear.
187-
assert_matches!(update, RoomEventCacheUpdate::Clear);
178+
// It does receive one update, which notifies about the clear.
179+
assert_let_timeout!(Ok(RoomEventCacheUpdate::Clear) = subscriber.recv());
188180

189181
// Receiving new events still works.
190182
server
@@ -198,12 +190,9 @@ async fn test_ignored_unignored() {
198190
.await;
199191

200192
// We do receive one update,
201-
let update = timeout(Duration::from_secs(2), subscriber.recv())
202-
.await
203-
.expect("timeout after receiving a sync update")
204-
.expect("should've received a room event cache update");
205-
206-
assert_let!(RoomEventCacheUpdate::AddTimelineEvents { events, .. } = update);
193+
assert_let_timeout!(
194+
Ok(RoomEventCacheUpdate::AddTimelineEvents { events, .. }) = subscriber.recv()
195+
);
207196
assert_eq!(events.len(), 1);
208197
assert_event_matches_msg(&events[0], "i don't like this dexter");
209198

@@ -817,10 +806,7 @@ async fn test_limited_timeline_resets_pagination() {
817806

818807
// And the paginator state delives this as an update, and is internally
819808
// consistent with it:
820-
assert_eq!(
821-
timeout(Duration::from_secs(1), pagination_status.next()).await,
822-
Ok(Some(PaginatorState::Idle))
823-
);
809+
assert_next_matches_with_timeout!(pagination_status, PaginatorState::Idle);
824810
assert!(pagination.hit_timeline_start());
825811

826812
// When a limited sync comes back from the server,
@@ -834,22 +820,15 @@ async fn test_limited_timeline_resets_pagination() {
834820
}
835821

836822
// We receive an update about the limited timeline.
837-
assert_matches!(
838-
timeout(Duration::from_secs(1), room_stream.recv()).await,
839-
Ok(Ok(RoomEventCacheUpdate::Clear))
840-
);
823+
assert_let_timeout!(Ok(RoomEventCacheUpdate::Clear) = room_stream.recv());
841824

842825
// The paginator state is reset: status set to Initial, hasn't hit the timeline
843826
// start.
844827
assert!(!pagination.hit_timeline_start());
845828
assert_eq!(pagination_status.get(), PaginatorState::Initial);
846829

847830
// We receive an update about the paginator status.
848-
let next_state = timeout(Duration::from_secs(1), pagination_status.next())
849-
.await
850-
.expect("timeout")
851-
.expect("no update");
852-
assert_eq!(next_state, PaginatorState::Initial);
831+
assert_next_matches_with_timeout!(pagination_status, PaginatorState::Initial);
853832

854833
assert!(room_stream.is_empty());
855834
}
@@ -906,15 +885,11 @@ async fn test_limited_timeline_with_storage() {
906885
)
907886
.await;
908887

909-
let update = timeout(Duration::from_secs(2), subscriber.recv())
910-
.await
911-
.expect("timeout after receiving a sync update")
912-
.expect("should've received a room event cache update");
913-
914-
assert_matches!(update, RoomEventCacheUpdate::AddTimelineEvents { events, origin: EventsOrigin::Sync } => {
915-
assert_eq!(events.len(), 1);
916-
assert_event_matches_msg(&events[0], "gappy!");
917-
});
888+
assert_let_timeout!(
889+
Ok(RoomEventCacheUpdate::AddTimelineEvents { events, .. }) = subscriber.recv()
890+
);
891+
assert_eq!(events.len(), 1);
892+
assert_event_matches_msg(&events[0], "gappy!");
918893

919894
// That's all, folks!
920895
assert!(subscriber.is_empty());

0 commit comments

Comments
 (0)