@@ -60,93 +60,106 @@ func TestBackfillingHistory(t *testing.T) {
6060 alice := deployment .Client (t , "hs1" , userID )
6161
6262 // Create the federated user which will fetch the messages from a remote homeserver
63- remoteUserID := "@charlie:hs2"
64- remoteCharlie := deployment .Client (t , "hs2" , remoteUserID )
63+ remoteCharlieUserID := "@charlie:hs2"
64+ remoteCharlie := deployment .Client (t , "hs2" , remoteCharlieUserID )
65+ remoteCharlieAlreadyJoined := deployment .RegisterUser (t , "hs2" , "remoteCharlieAlready" , "123" )
66+ remoteCharlieWithFullScrollback := deployment .RegisterUser (t , "hs2" , "remoteCharlieWithFullScrollback" , "123" )
6567
6668 virtualUserLocalpart := "maria"
6769 virtualUserID := fmt .Sprintf ("@%s:hs1" , virtualUserLocalpart )
6870
71+ roomID := as .CreateRoom (t , map [string ]interface {}{
72+ "preset" : "public_chat" ,
73+ "name" : "the hangout spot" ,
74+ })
75+ alice .JoinRoom (t , roomID , nil )
76+
77+ // Join the room from a remote homeserver before any backfilled messages are sent
78+ remoteCharlieAlreadyJoined .JoinRoom (t , roomID , []string {"hs1" })
79+
80+ // Create some normal messages in the timeline. We're creating them in
81+ // two batches so we can create some time in between where we are going
82+ // to backfill.
83+ //
84+ // Create the first batch including the "live" event we are going to
85+ // insert our backfilled events next to.
86+ eventIDsBefore := createMessagesInRoom (t , alice , roomID , 2 )
87+ eventIdBefore := eventIDsBefore [len (eventIDsBefore )- 1 ]
88+ timeAfterEventBefore := time .Now ()
89+
90+ // wait X number of ms to ensure that the timestamp changes enough for
91+ // each of the messages we try to backfill later
92+ numHistoricalMessages := 6
93+ time .Sleep (time .Duration (numHistoricalMessages ) * timeBetweenMessages )
94+
95+ // Create the second batch of events.
96+ // This will also fill up the buffer so we have to scrollback to the
97+ // inserted history later.
98+ eventIDsAfter := createMessagesInRoom (t , alice , roomID , 2 )
99+
100+ // Mimic scrollback to all of the messages
101+ // scrollbackMessagesRes
102+ remoteCharlieWithFullScrollback .JoinRoom (t , roomID , []string {"hs1" })
103+ remoteCharlieWithFullScrollback .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
104+ "dir" : []string {"b" },
105+ "limit" : []string {"100" },
106+ }))
107+
108+ // Register and join the virtual user
109+ ensureVirtualUserRegistered (t , as , virtualUserLocalpart )
110+
111+ // Insert the most recent chunk of backfilled history
112+ backfillRes := backfillBatchHistoricalMessages (
113+ t ,
114+ as ,
115+ virtualUserID ,
116+ roomID ,
117+ eventIdBefore ,
118+ timeAfterEventBefore .Add (timeBetweenMessages * 3 ),
119+ "" ,
120+ 3 ,
121+ // Status
122+ 200 ,
123+ )
124+ historicalEventIDs := getEventsFromBatchSendResponse (t , backfillRes )
125+ nextChunkID := getNextChunkIdFromBatchSendResponse (t , backfillRes )
126+
127+ // Insert another older chunk of backfilled history from the same user.
128+ // Make sure the meta data and joins still work on the subsequent chunk
129+ backfillRes2 := backfillBatchHistoricalMessages (
130+ t ,
131+ as ,
132+ virtualUserID ,
133+ roomID ,
134+ eventIdBefore ,
135+ timeAfterEventBefore ,
136+ nextChunkID ,
137+ 3 ,
138+ // Status
139+ 200 ,
140+ )
141+ historicalEventIDs2 := getEventsFromBatchSendResponse (t , backfillRes2 )
142+
143+ var expectedEventIDOrder []string
144+ expectedEventIDOrder = append (expectedEventIDOrder , eventIDsBefore ... )
145+ expectedEventIDOrder = append (expectedEventIDOrder , historicalEventIDs2 ... )
146+ expectedEventIDOrder = append (expectedEventIDOrder , historicalEventIDs ... )
147+ expectedEventIDOrder = append (expectedEventIDOrder , eventIDsAfter ... )
148+ // Order events from newest to oldest
149+ expectedEventIDOrder = reversed (expectedEventIDOrder )
150+
151+ // 2 eventIDsBefore + 6 historical events + 3 insertion events + 2 eventIDsAfter
152+ if len (expectedEventIDOrder ) != 13 {
153+ t .Fatalf ("Expected eventID list should be length 13 but saw %d: %s" , len (expectedEventIDOrder ), expectedEventIDOrder )
154+ }
155+
69156 t .Run ("parallel" , func (t * testing.T ) {
70157 // Final timeline output: ( [n] = historical chunk )
71158 // (oldest) A, B, [insertion, c, d, e] [insertion, f, g, h, insertion], I, J (newest)
72159 // chunk 1 chunk 0
73160 t .Run ("Backfilled historical events resolve with proper state in correct order" , func (t * testing.T ) {
74161 t .Parallel ()
75162
76- roomID := as .CreateRoom (t , map [string ]interface {}{
77- "preset" : "public_chat" ,
78- "name" : "the hangout spot" ,
79- })
80- alice .JoinRoom (t , roomID , nil )
81-
82- // Create some normal messages in the timeline. We're creating them in
83- // two batches so we can create some time in between where we are going
84- // to backfill.
85- //
86- // Create the first batch including the "live" event we are going to
87- // insert our backfilled events next to.
88- eventIDsBefore := createMessagesInRoom (t , alice , roomID , 2 )
89- eventIdBefore := eventIDsBefore [len (eventIDsBefore )- 1 ]
90- timeAfterEventBefore := time .Now ()
91-
92- // wait X number of ms to ensure that the timestamp changes enough for
93- // each of the messages we try to backfill later
94- numHistoricalMessages := 6
95- time .Sleep (time .Duration (numHistoricalMessages ) * timeBetweenMessages )
96-
97- // Create the second batch of events.
98- // This will also fill up the buffer so we have to scrollback to the
99- // inserted history later.
100- eventIDsAfter := createMessagesInRoom (t , alice , roomID , 2 )
101-
102- // Register and join the virtual user
103- ensureVirtualUserRegistered (t , as , virtualUserLocalpart )
104-
105- // Insert the most recent chunk of backfilled history
106- backfillRes := backfillBatchHistoricalMessages (
107- t ,
108- as ,
109- virtualUserID ,
110- roomID ,
111- eventIdBefore ,
112- timeAfterEventBefore .Add (timeBetweenMessages * 3 ),
113- "" ,
114- 3 ,
115- // Status
116- 200 ,
117- )
118- historicalEventIDs := getEventsFromBatchSendResponse (t , backfillRes )
119- nextChunkID := getNextChunkIdFromBatchSendResponse (t , backfillRes )
120-
121- // Insert another older chunk of backfilled history from the same user.
122- // Make sure the meta data and joins still work on the subsequent chunk
123- backfillRes2 := backfillBatchHistoricalMessages (
124- t ,
125- as ,
126- virtualUserID ,
127- roomID ,
128- eventIdBefore ,
129- timeAfterEventBefore ,
130- nextChunkID ,
131- 3 ,
132- // Status
133- 200 ,
134- )
135- historicalEventIDs2 := getEventsFromBatchSendResponse (t , backfillRes2 )
136-
137- var expectedEventIDOrder []string
138- expectedEventIDOrder = append (expectedEventIDOrder , eventIDsBefore ... )
139- expectedEventIDOrder = append (expectedEventIDOrder , historicalEventIDs2 ... )
140- expectedEventIDOrder = append (expectedEventIDOrder , historicalEventIDs ... )
141- expectedEventIDOrder = append (expectedEventIDOrder , eventIDsAfter ... )
142- // Order events from newest to oldest
143- expectedEventIDOrder = reversed (expectedEventIDOrder )
144-
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-
150163 messagesRes := alice .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
151164 "dir" : []string {"b" },
152165 "limit" : []string {"100" },
@@ -184,49 +197,18 @@ func TestBackfillingHistory(t *testing.T) {
184197 }
185198 })
186199
187- t .Run ("Backfilled historical events with m.historical do not come down in an incremental sync" , func (t * testing.T ) {
200+ t .Run ("Backfilled historical events do not come down in an incremental sync" , func (t * testing.T ) {
188201 t .Parallel ()
189202
190- roomID := as .CreateRoom (t , struct {}{})
191- alice .JoinRoom (t , roomID , nil )
192-
193- // Create the "live" event we are going to insert our backfilled events next to
194- eventIDsBefore := createMessagesInRoom (t , alice , roomID , 1 )
195- eventIdBefore := eventIDsBefore [0 ]
196- timeAfterEventBefore := time .Now ()
197-
198- // Create some "live" events to saturate and fill up the /sync response
199- createMessagesInRoom (t , alice , roomID , 5 )
200-
201- // Insert a backfilled event
202- backfillRes := backfillBatchHistoricalMessages (
203- t ,
204- as ,
205- virtualUserID ,
206- roomID ,
207- eventIdBefore ,
208- timeAfterEventBefore ,
209- "" ,
210- 1 ,
211- // Status
212- 200 ,
213- )
214- historicalEventIDs := getEventsFromBatchSendResponse (t , backfillRes )
215- backfilledEventId := historicalEventIDs [0 ]
216-
217- // This is just a dummy event we search for after the backfilledEventId
218- eventIDsAfterBackfill := createMessagesInRoom (t , alice , roomID , 1 )
219- eventIdAfterBackfill := eventIDsAfterBackfill [0 ]
220-
221203 // Sync until we find the eventIdAfterBackfill. If we're able to see the eventIdAfterBackfill
222204 // that occurs after the backfilledEventId without seeing eventIdAfterBackfill in between,
223205 // we're probably safe to assume it won't sync
224- alice .SyncUntil (t , "" , `{ "room": { "timeline": { "limit": 3 } } }` , "rooms.join." + client .GjsonEscape (roomID )+ ".timeline.events" , func (r gjson.Result ) bool {
225- if r .Get ("event_id" ).Str == backfilledEventId {
226- t .Fatalf ("We should not see the %s backfilled event in /sync response but it was present" , backfilledEventId )
206+ alice .SyncUntil (t , "" , `{ "room": { "timeline": { "limit": 2 } } }` , "rooms.join." + client .GjsonEscape (roomID )+ ".timeline.events" , func (r gjson.Result ) bool {
207+ if containsItem ( historicalEventIDs , r .Get ("event_id" ).Str ) || containsItem ( historicalEventIDs2 , r . Get ( "event_id" ). Str ) {
208+ t .Fatalf ("We should not see the %s backfilled event in /sync response but it was present" , r . Get ( "event_id" ). Str )
227209 }
228210
229- return r .Get ("event_id" ).Str == eventIdAfterBackfill
211+ return containsItem ( eventIDsAfter , r .Get ("event_id" ).Str )
230212 })
231213 })
232214
@@ -255,13 +237,6 @@ func TestBackfillingHistory(t *testing.T) {
255237 t .Run ("Normal users aren't allowed to backfill messages" , func (t * testing.T ) {
256238 t .Parallel ()
257239
258- roomID := as .CreateRoom (t , struct {}{})
259- alice .JoinRoom (t , roomID , nil )
260-
261- eventIDsBefore := createMessagesInRoom (t , alice , roomID , 1 )
262- eventIdBefore := eventIDsBefore [0 ]
263- timeAfterEventBefore := time .Now ()
264-
265240 backfillBatchHistoricalMessages (
266241 t ,
267242 alice ,
@@ -286,33 +261,6 @@ func TestBackfillingHistory(t *testing.T) {
286261 t .Skip ("Skipping until federation is implemented" )
287262 t .Parallel ()
288263
289- roomID := as .CreateRoom (t , struct {}{})
290- alice .JoinRoom (t , roomID , nil )
291-
292- eventIDsBefore := createMessagesInRoom (t , alice , roomID , 1 )
293- eventIdBefore := eventIDsBefore [0 ]
294- timeAfterEventBefore := time .Now ()
295-
296- // eventIDsAfter
297- createMessagesInRoom (t , alice , roomID , 3 )
298-
299- // Register and join the virtual user
300- ensureVirtualUserRegistered (t , as , virtualUserLocalpart )
301-
302- backfillRes := backfillBatchHistoricalMessages (
303- t ,
304- as ,
305- virtualUserID ,
306- roomID ,
307- eventIdBefore ,
308- timeAfterEventBefore ,
309- "" ,
310- 2 ,
311- // Status
312- 200 ,
313- )
314- historicalEventIDs := getEventsFromBatchSendResponse (t , backfillRes )
315-
316264 // Join the room from a remote homeserver after the backfilled messages were sent
317265 remoteCharlie .JoinRoom (t , roomID , []string {"hs1" })
318266
@@ -334,43 +282,6 @@ func TestBackfillingHistory(t *testing.T) {
334282 t .Skip ("Skipping until federation is implemented" )
335283 t .Parallel ()
336284
337- roomID := as .CreateRoom (t , struct {}{})
338- alice .JoinRoom (t , roomID , nil )
339-
340- // Join the room from a remote homeserver before any backfilled messages are sent
341- remoteCharlie .JoinRoom (t , roomID , []string {"hs1" })
342-
343- eventIDsBefore := createMessagesInRoom (t , alice , roomID , 1 )
344- eventIdBefore := eventIDsBefore [0 ]
345- timeAfterEventBefore := time .Now ()
346-
347- // eventIDsAfter
348- createMessagesInRoom (t , alice , roomID , 10 )
349-
350- // Mimic scrollback just through the latest messages
351- remoteCharlie .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
352- "dir" : []string {"b" },
353- // Limited so we can only see a few of the latest messages
354- "limit" : []string {"5" },
355- }))
356-
357- // Register and join the virtual user
358- ensureVirtualUserRegistered (t , as , virtualUserLocalpart )
359-
360- backfillRes := backfillBatchHistoricalMessages (
361- t ,
362- as ,
363- virtualUserID ,
364- roomID ,
365- eventIdBefore ,
366- timeAfterEventBefore ,
367- "" ,
368- 2 ,
369- // Status
370- 200 ,
371- )
372- historicalEventIDs := getEventsFromBatchSendResponse (t , backfillRes )
373-
374285 messagesRes := remoteCharlie .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
375286 "dir" : []string {"b" },
376287 "limit" : []string {"100" },
@@ -389,45 +300,7 @@ func TestBackfillingHistory(t *testing.T) {
389300 t .Skip ("Skipping until federation is implemented" )
390301 t .Parallel ()
391302
392- roomID := as .CreateRoom (t , struct {}{})
393- alice .JoinRoom (t , roomID , nil )
394-
395- // Join the room from a remote homeserver before any backfilled messages are sent
396- remoteCharlie .JoinRoom (t , roomID , []string {"hs1" })
397-
398- eventIDsBefore := createMessagesInRoom (t , alice , roomID , 1 )
399- eventIdBefore := eventIDsBefore [0 ]
400- timeAfterEventBefore := time .Now ()
401-
402- // eventIDsAfter
403- createMessagesInRoom (t , alice , roomID , 3 )
404-
405- // Register and join the virtual user
406- ensureVirtualUserRegistered (t , as , virtualUserLocalpart )
407-
408- // Mimic scrollback to all of the messages
409- // scrollbackMessagesRes
410- remoteCharlie .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
411- "dir" : []string {"b" },
412- "limit" : []string {"100" },
413- }))
414-
415- // Historical messages are inserted where we have already scrolled back to
416- backfillRes := backfillBatchHistoricalMessages (
417- t ,
418- as ,
419- virtualUserID ,
420- roomID ,
421- eventIdBefore ,
422- timeAfterEventBefore ,
423- "" ,
424- 2 ,
425- // Status
426- 200 ,
427- )
428- historicalEventIDs := getEventsFromBatchSendResponse (t , backfillRes )
429-
430- messagesRes := remoteCharlie .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
303+ messagesRes := remoteCharlieWithFullScrollback .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
431304 "dir" : []string {"b" },
432305 "limit" : []string {"100" },
433306 }))
@@ -451,6 +324,15 @@ func reversed(in []string) []string {
451324 return out
452325}
453326
327+ func containsItem (slice []string , item string ) bool {
328+ for _ , a := range slice {
329+ if a == item {
330+ return true
331+ }
332+ }
333+ return false
334+ }
335+
454336func getRelevantEventDebugStringsFromMessagesResponse (t * testing.T , body []byte ) (eventIDsFromResponse []string ) {
455337 t .Helper ()
456338
0 commit comments