Skip to content

Commit df4dc04

Browse files
committed
test(sdk): Fix test_reset_while_backpaginating.
The test `test_reset_while_backpaginating` was expecting a race-condition, which no longer exists. It first initially tried to assert a workaround about this race-condition. It doesn't hold anymore. Rewrite the test to assert the (correct) new behaviour.
1 parent 6b86e1f commit df4dc04

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,12 @@ async fn test_reset_while_backpaginating() {
428428
JoinedRoomBuilder::new(room_id)
429429
// Note to self: a timeline must have at least single event to be properly
430430
// serialized.
431-
.add_timeline_event(event_builder.make_sync_message_event(
431+
.add_timeline_event(event_builder.make_sync_message_event_with_id(
432432
user_id!("@a:b.c"),
433+
event_id!("$from_first_sync"),
433434
RoomMessageEventContent::text_plain("heyo"),
434435
))
435-
.set_timeline_prev_batch("first_backpagination".to_owned()),
436+
.set_timeline_prev_batch("first_prev_batch_token".to_owned()),
436437
);
437438
let response_body = sync_builder.build_json_sync_response();
438439

@@ -471,25 +472,27 @@ async fn test_reset_while_backpaginating() {
471472
JoinedRoomBuilder::new(room_id)
472473
// Note to self: a timeline must have at least single event to be properly
473474
// serialized.
474-
.add_timeline_event(event_builder.make_sync_message_event(
475+
.add_timeline_event(event_builder.make_sync_message_event_with_id(
475476
user_id!("@a:b.c"),
477+
event_id!("$from_second_sync"),
476478
RoomMessageEventContent::text_plain("heyo"),
477479
))
478-
.set_timeline_prev_batch("second_backpagination".to_owned())
480+
.set_timeline_prev_batch("second_prev_batch_token_from_sync".to_owned())
479481
.set_timeline_limited(),
480482
);
481483
let sync_response_body = sync_builder.build_json_sync_response();
482484

483485
// First back-pagination request:
484-
let chunk = non_sync_events!(event_builder, [ (room_id, "$2": "lalala") ]);
486+
let chunk = non_sync_events!(event_builder, [ (room_id, "$from_backpagination": "lalala") ]);
485487
let response_json = json!({
486488
"chunk": chunk,
487489
"start": "t392-516_47314_0_7_1_1_1_11444_1",
490+
"end": "second_prev_batch_token_from_backpagination"
488491
});
489492
Mock::given(method("GET"))
490493
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/messages$"))
491494
.and(header("authorization", "Bearer 1234"))
492-
.and(query_param("from", "first_backpagination"))
495+
.and(query_param("from", "first_prev_batch_token"))
493496
.respond_with(
494497
ResponseTemplate::new(200)
495498
.set_body_json(response_json.clone())
@@ -506,22 +509,33 @@ async fn test_reset_while_backpaginating() {
506509

507510
let rec = room_event_cache.clone();
508511
let first_token_clone = first_token.clone();
509-
let backpagination = spawn(async move { rec.backpaginate(20, first_token_clone).await });
512+
let backpagination = spawn(async move {
513+
let ret = rec.backpaginate(20, first_token_clone).await;
514+
515+
ret
516+
});
510517

511518
// Receive the sync response (which clears the timeline).
512519
mock_sync(&server, sync_response_body, None).await;
513520
client.sync_once(Default::default()).await.unwrap();
514521

515522
let outcome = backpagination.await.expect("join failed").unwrap();
516523

517-
// Backpagination should be confused, and the operation should result in an
518-
// unknown token.
519-
assert_matches!(outcome, BackPaginationOutcome::UnknownBackpaginationToken);
524+
// Backpagination should have been executed before the sync, despite the
525+
// concurrency here. The backpagination should have acquired a write lock before
526+
// the sync.
527+
{
528+
assert_let!(BackPaginationOutcome::Success { events, reached_start } = outcome);
529+
assert!(!reached_start);
530+
assert_event_matches_msg(&events[0].clone().into(), "lalala");
531+
assert_eq!(events.len(), 1);
532+
}
520533

521534
// Now if we retrieve the earliest token, it's not the one we had before.
535+
// Even better, it's the one from the sync, NOT from the backpagination!
522536
let second_token = room_event_cache.oldest_backpagination_token(None).await.unwrap().unwrap();
523537
assert!(first_token.unwrap() != second_token);
524-
assert_eq!(second_token.0, "second_backpagination");
538+
assert_eq!(second_token.0, "second_prev_batch_token_from_sync");
525539
}
526540

527541
#[async_test]

0 commit comments

Comments
 (0)