Skip to content

Commit 991e91c

Browse files
committed
Make federation tests more robust
1 parent 53f5347 commit 991e91c

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

tests/msc2716_test.go

+54-4
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,15 @@ func TestBackfillingHistory(t *testing.T) {
377377
// Join the room from a remote homeserver after the backfilled messages were sent
378378
remoteCharlie.JoinRoom(t, roomID, []string{"hs1"})
379379

380-
// TODO: I think we need to update this to be similar to
381-
// SyncUntilTimelineHas but going back in time because this can be flakey
380+
// Make sure all of the events have been backfilled
381+
fetchUntilMessagesResponseHas(t, remoteCharlie, roomID, func(ev gjson.Result) bool {
382+
if ev.Get("event_id").Str == eventIdBefore {
383+
return true
384+
}
385+
386+
return false
387+
})
388+
382389
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
383390
"dir": []string{"b"},
384391
"limit": []string{"100"},
@@ -446,8 +453,15 @@ func TestBackfillingHistory(t *testing.T) {
446453
// Join the room from a remote homeserver after the backfilled messages were sent
447454
remoteCharlie.JoinRoom(t, roomID, []string{"hs1"})
448455

449-
// TODO: I think we need to update this to be similar to
450-
// SyncUntilTimelineHas but going back in time because this can be flakey
456+
// Make sure all of the events have been backfilled
457+
fetchUntilMessagesResponseHas(t, remoteCharlie, roomID, func(ev gjson.Result) bool {
458+
if ev.Get("event_id").Str == eventIdBefore {
459+
return true
460+
}
461+
462+
return false
463+
})
464+
451465
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
452466
"dir": []string{"b"},
453467
"limit": []string{"100"},
@@ -647,6 +661,42 @@ func reversed(in []string) []string {
647661
return out
648662
}
649663

664+
func fetchUntilMessagesResponseHas(t *testing.T, c *client.CSAPI, roomID string, check func(gjson.Result) bool) {
665+
t.Helper()
666+
start := time.Now()
667+
checkCounter := 0
668+
for {
669+
if time.Since(start) > c.SyncUntilTimeout {
670+
t.Fatalf("fetchMessagesUntilResponseHas timed out. Called check function %d times", checkCounter)
671+
}
672+
673+
messagesRes := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
674+
"dir": []string{"b"},
675+
"limit": []string{"100"},
676+
}))
677+
messsageResBody := client.ParseJSON(t, messagesRes)
678+
wantKey := "chunk"
679+
keyRes := gjson.GetBytes(messsageResBody, wantKey)
680+
if !keyRes.Exists() {
681+
t.Fatalf("missing key '%s'", wantKey)
682+
}
683+
if !keyRes.IsArray() {
684+
t.Fatalf("key '%s' is not an array (was %s)", wantKey, keyRes.Type)
685+
}
686+
687+
events := keyRes.Array()
688+
for _, ev := range events {
689+
if check(ev) {
690+
return
691+
}
692+
}
693+
694+
checkCounter++
695+
// Add a slight delay so we don't hammmer the messages endpoint
696+
time.Sleep(500 * time.Millisecond)
697+
}
698+
}
699+
650700
func isRelevantEvent(r gjson.Result) bool {
651701
return len(r.Get("content").Get("body").Str) > 0 || r.Get("type").Str == insertionEventType || r.Get("type").Str == markerEventType
652702
}

0 commit comments

Comments
 (0)