@@ -60,106 +60,93 @@ 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- 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" )
63+ remoteUserID := "@charlie:hs2"
64+ remoteCharlie := deployment .Client (t , "hs2" , remoteUserID )
6765
6866 virtualUserLocalpart := "maria"
6967 virtualUserID := fmt .Sprintf ("@%s:hs1" , virtualUserLocalpart )
7068
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-
15669 t .Run ("parallel" , func (t * testing.T ) {
15770 // Final timeline output: ( [n] = historical chunk )
15871 // (oldest) A, B, [insertion, c, d, e] [insertion, f, g, h, insertion], I, J (newest)
15972 // chunk 1 chunk 0
16073 t .Run ("Backfilled historical events resolve with proper state in correct order" , func (t * testing.T ) {
16174 t .Parallel ()
16275
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+
163150 messagesRes := alice .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
164151 "dir" : []string {"b" },
165152 "limit" : []string {"100" },
@@ -197,18 +184,49 @@ func TestBackfillingHistory(t *testing.T) {
197184 }
198185 })
199186
200- t .Run ("Backfilled historical events do not come down in an incremental sync" , func (t * testing.T ) {
187+ t .Run ("Backfilled historical events with m.historical do not come down in an incremental sync" , func (t * testing.T ) {
201188 t .Parallel ()
202189
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+
203221 // Sync until we find the eventIdAfterBackfill. If we're able to see the eventIdAfterBackfill
204222 // that occurs after the backfilledEventId without seeing eventIdAfterBackfill in between,
205223 // we're probably safe to assume it won't sync
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 )
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 )
209227 }
210228
211- return containsItem ( eventIDsAfter , r .Get ("event_id" ).Str )
229+ return r .Get ("event_id" ).Str == eventIdAfterBackfill
212230 })
213231 })
214232
@@ -237,6 +255,13 @@ func TestBackfillingHistory(t *testing.T) {
237255 t .Run ("Normal users aren't allowed to backfill messages" , func (t * testing.T ) {
238256 t .Parallel ()
239257
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+
240265 backfillBatchHistoricalMessages (
241266 t ,
242267 alice ,
@@ -261,6 +286,33 @@ func TestBackfillingHistory(t *testing.T) {
261286 t .Skip ("Skipping until federation is implemented" )
262287 t .Parallel ()
263288
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+
264316 // Join the room from a remote homeserver after the backfilled messages were sent
265317 remoteCharlie .JoinRoom (t , roomID , []string {"hs1" })
266318
@@ -282,6 +334,43 @@ func TestBackfillingHistory(t *testing.T) {
282334 t .Skip ("Skipping until federation is implemented" )
283335 t .Parallel ()
284336
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+
285374 messagesRes := remoteCharlie .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
286375 "dir" : []string {"b" },
287376 "limit" : []string {"100" },
@@ -300,7 +389,45 @@ func TestBackfillingHistory(t *testing.T) {
300389 t .Skip ("Skipping until federation is implemented" )
301390 t .Parallel ()
302391
303- messagesRes := remoteCharlieWithFullScrollback .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
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 {
304431 "dir" : []string {"b" },
305432 "limit" : []string {"100" },
306433 }))
@@ -324,15 +451,6 @@ func reversed(in []string) []string {
324451 return out
325452}
326453
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-
336454func getRelevantEventDebugStringsFromMessagesResponse (t * testing.T , body []byte ) (eventIDsFromResponse []string ) {
337455 t .Helper ()
338456
0 commit comments