Skip to content

Commit 9a3da4e

Browse files
committed
Fix assertion when no events are returned and other nits
1 parent b6b615d commit 9a3da4e

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

tests/msc2716_test.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package tests
88

99
import (
1010
"bytes"
11-
"encoding/json"
1211
"fmt"
1312
"io/ioutil"
1413
"net/http"
@@ -69,8 +68,8 @@ func TestBackfillingHistory(t *testing.T) {
6968

7069
t.Run("parallel", func(t *testing.T) {
7170
// Final timeline output: ( [n] = historical chunk )
72-
// (oldest) A, B, [c, d, e] [f, g, h], I, J (newest)
73-
// chunk 1 chunk 0
71+
// (oldest) A, B, [insertion, c, d, e] [insertion, f, g, h, insertion], I, J (newest)
72+
// chunk 1 chunk 0
7473
t.Run("Backfilled historical events resolve with proper state in correct order", func(t *testing.T) {
7574
t.Parallel()
7675

@@ -116,7 +115,7 @@ func TestBackfillingHistory(t *testing.T) {
116115
// Status
117116
200,
118117
)
119-
_, historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
118+
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
120119
nextChunkID := getNextChunkIdFromBatchSendResponse(t, backfillRes)
121120

122121
// Insert another older chunk of backfilled history from the same user.
@@ -133,7 +132,7 @@ func TestBackfillingHistory(t *testing.T) {
133132
// Status
134133
200,
135134
)
136-
_, historicalEventIDs2 := getEventsFromBatchSendResponse(t, backfillRes2)
135+
historicalEventIDs2 := getEventsFromBatchSendResponse(t, backfillRes2)
137136

138137
var expectedEventIDOrder []string
139138
expectedEventIDOrder = append(expectedEventIDOrder, eventIDsBefore...)
@@ -143,16 +142,21 @@ func TestBackfillingHistory(t *testing.T) {
143142
// Order events from newest to oldest
144143
expectedEventIDOrder = reversed(expectedEventIDOrder)
145144

145+
// 2 eventIDsBefore + 6 historical events + 3 insertion events + 2 eventIDsAfter
146+
if len(expectedEventIDOrder) != 13 {
147+
t.Fatalf("Expected eventID list should be length 13 but saw %d: %s", len(expectedEventIDOrder), expectedEventIDOrder)
148+
}
149+
146150
messagesRes := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
147151
"dir": []string{"b"},
148152
"limit": []string{"100"},
149153
}))
150154
messsageResBody := client.ParseJSON(t, messagesRes)
151-
eventIDsFromResponse := getEventIDsFromResponseBody(t, messsageResBody)
155+
eventDebugStringsFromResponse := getRelevantEventDebugStringsFromMessagesResponse(t, messsageResBody)
152156
// Since the original body can only be read once, create a new one from the body bytes we just read
153157
messagesRes.Body = ioutil.NopCloser(bytes.NewBuffer(messsageResBody))
154158

155-
// Copy the array by value so we can modify it as we iterate in the foreach loop.
159+
// Copy the array by slice so we can modify it as we iterate in the foreach loop.
156160
// We save the full untouched `expectedEventIDOrder` for use in the log messages
157161
workingExpectedEventIDOrder := expectedEventIDOrder
158162

@@ -166,14 +170,18 @@ func TestBackfillingHistory(t *testing.T) {
166170
workingExpectedEventIDOrder = workingExpectedEventIDOrder[1:]
167171

168172
if r.Get("event_id").Str != nextEventIdInOrder {
169-
return fmt.Errorf("Next event found was %s but expected %s\nActualEvents (%d): %v\nExpectedEvents (%d): %v", r.Get("event_id").Str, nextEventIdInOrder, len(eventIDsFromResponse), eventIDsFromResponse, len(expectedEventIDOrder), expectedEventIDOrder)
173+
return fmt.Errorf("Next event found was %s but expected %s\nActualEvents (%d): %v\nExpectedEvents (%d): %v", r.Get("event_id").Str, nextEventIdInOrder, len(eventDebugStringsFromResponse), eventDebugStringsFromResponse, len(expectedEventIDOrder), expectedEventIDOrder)
170174
}
171175
}
172176

173177
return nil
174178
}),
175179
},
176180
})
181+
182+
if len(workingExpectedEventIDOrder) != 0 {
183+
t.Fatalf("Expected all events to be matched in message response but there were some left-over events: %s", workingExpectedEventIDOrder)
184+
}
177185
})
178186

179187
t.Run("Backfilled historical events with m.historical do not come down in an incremental sync", func(t *testing.T) {
@@ -203,7 +211,7 @@ func TestBackfillingHistory(t *testing.T) {
203211
// Status
204212
200,
205213
)
206-
_, historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
214+
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
207215
backfilledEventId := historicalEventIDs[0]
208216

209217
// This is just a dummy event we search for after the backfilledEventId
@@ -303,7 +311,7 @@ func TestBackfillingHistory(t *testing.T) {
303311
// Status
304312
200,
305313
)
306-
_, historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
314+
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
307315

308316
// Join the room from a remote homeserver after the backfilled messages were sent
309317
remoteCharlie.JoinRoom(t, roomID, []string{"hs1"})
@@ -361,7 +369,7 @@ func TestBackfillingHistory(t *testing.T) {
361369
// Status
362370
200,
363371
)
364-
_, historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
372+
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
365373

366374
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
367375
"dir": []string{"b"},
@@ -417,7 +425,7 @@ func TestBackfillingHistory(t *testing.T) {
417425
// Status
418426
200,
419427
)
420-
_, historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
428+
historicalEventIDs := getEventsFromBatchSendResponse(t, backfillRes)
421429

422430
messagesRes := remoteCharlie.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "messages"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
423431
"dir": []string{"b"},
@@ -443,7 +451,9 @@ func reversed(in []string) []string {
443451
return out
444452
}
445453

446-
func getEventIDsFromResponseBody(t *testing.T, body []byte) (eventIDsFromResponse []string) {
454+
func getRelevantEventDebugStringsFromMessagesResponse(t *testing.T, body []byte) (eventIDsFromResponse []string) {
455+
t.Helper()
456+
447457
wantKey := "chunk"
448458
res := gjson.GetBytes(body, wantKey)
449459
if !res.Exists() {
@@ -470,7 +480,7 @@ func ensureVirtualUserRegistered(t *testing.T, c *client.CSAPI, virtualUserLocal
470480
t,
471481
"POST",
472482
[]string{"_matrix", "client", "r0", "register"},
473-
client.WithRawBody(json.RawMessage(fmt.Sprintf(`{ "type": "m.login.application_service", "username": "%s" }`, virtualUserLocalpart))),
483+
client.WithJSONBody(t, map[string]interface{}{"type": "m.login.application_service", "username": virtualUserLocalpart}),
474484
client.WithContentType("application/json"),
475485
)
476486

@@ -558,19 +568,14 @@ func backfillBatchHistoricalMessages(
558568
query.Add("chunk_id", chunkID)
559569
}
560570

561-
b, err := json.Marshal(map[string]interface{}{
562-
"events": evs,
563-
"state_events_at_start": []map[string]interface{}{joinEvent},
564-
})
565-
if err != nil {
566-
t.Fatalf("msc2716.backfillBatchHistoricalMessages failed to marshal JSON body: %s", err)
567-
}
568-
569571
res = c.DoFunc(
570572
t,
571573
"POST",
572574
[]string{"_matrix", "client", "unstable", "org.matrix.msc2716", "rooms", roomID, "batch_send"},
573-
client.WithRawBody(b),
575+
client.WithJSONBody(t, map[string]interface{}{
576+
"events": evs,
577+
"state_events_at_start": []map[string]interface{}{joinEvent},
578+
}),
574579
client.WithContentType("application/json"),
575580
client.WithQueries(query),
576581
)
@@ -590,15 +595,14 @@ func backfillBatchHistoricalMessages(
590595
return res
591596
}
592597

593-
func getEventsFromBatchSendResponse(t *testing.T, res *http.Response) (stateEventsIDs []string, eventIDs []string) {
598+
func getEventsFromBatchSendResponse(t *testing.T, res *http.Response) (eventIDs []string) {
594599
body := client.ParseJSON(t, res)
595600
// Since the original body can only be read once, create a new one from the body bytes we just read
596601
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
597602

598-
stateEventsIDs = client.GetJSONFieldStringArray(t, body, "state_events")
599603
eventIDs = client.GetJSONFieldStringArray(t, body, "events")
600604

601-
return stateEventsIDs, eventIDs
605+
return eventIDs
602606
}
603607

604608
func getNextChunkIdFromBatchSendResponse(t *testing.T, res *http.Response) (nextChunkID string) {

0 commit comments

Comments
 (0)