@@ -60,106 +60,93 @@ func TestBackfillingHistory(t *testing.T) {
60
60
alice := deployment .Client (t , "hs1" , userID )
61
61
62
62
// 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 )
67
65
68
66
virtualUserLocalpart := "maria"
69
67
virtualUserID := fmt .Sprintf ("@%s:hs1" , virtualUserLocalpart )
70
68
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
-
156
69
t .Run ("parallel" , func (t * testing.T ) {
157
70
// Final timeline output: ( [n] = historical chunk )
158
71
// (oldest) A, B, [insertion, c, d, e] [insertion, f, g, h, insertion], I, J (newest)
159
72
// chunk 1 chunk 0
160
73
t .Run ("Backfilled historical events resolve with proper state in correct order" , func (t * testing.T ) {
161
74
t .Parallel ()
162
75
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
+
163
150
messagesRes := alice .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
164
151
"dir" : []string {"b" },
165
152
"limit" : []string {"100" },
@@ -197,18 +184,49 @@ func TestBackfillingHistory(t *testing.T) {
197
184
}
198
185
})
199
186
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 ) {
201
188
t .Parallel ()
202
189
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
+
203
221
// Sync until we find the eventIdAfterBackfill. If we're able to see the eventIdAfterBackfill
204
222
// that occurs after the backfilledEventId without seeing eventIdAfterBackfill in between,
205
223
// 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 )
209
227
}
210
228
211
- return containsItem ( eventIDsAfter , r .Get ("event_id" ).Str )
229
+ return r .Get ("event_id" ).Str == eventIdAfterBackfill
212
230
})
213
231
})
214
232
@@ -237,6 +255,13 @@ func TestBackfillingHistory(t *testing.T) {
237
255
t .Run ("Normal users aren't allowed to backfill messages" , func (t * testing.T ) {
238
256
t .Parallel ()
239
257
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
+
240
265
backfillBatchHistoricalMessages (
241
266
t ,
242
267
alice ,
@@ -261,6 +286,33 @@ func TestBackfillingHistory(t *testing.T) {
261
286
t .Skip ("Skipping until federation is implemented" )
262
287
t .Parallel ()
263
288
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
+
264
316
// Join the room from a remote homeserver after the backfilled messages were sent
265
317
remoteCharlie .JoinRoom (t , roomID , []string {"hs1" })
266
318
@@ -282,6 +334,43 @@ func TestBackfillingHistory(t *testing.T) {
282
334
t .Skip ("Skipping until federation is implemented" )
283
335
t .Parallel ()
284
336
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
+
285
374
messagesRes := remoteCharlie .MustDoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "rooms" , roomID , "messages" }, client .WithContentType ("application/json" ), client .WithQueries (url.Values {
286
375
"dir" : []string {"b" },
287
376
"limit" : []string {"100" },
@@ -300,7 +389,45 @@ func TestBackfillingHistory(t *testing.T) {
300
389
t .Skip ("Skipping until federation is implemented" )
301
390
t .Parallel ()
302
391
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 {
304
431
"dir" : []string {"b" },
305
432
"limit" : []string {"100" },
306
433
}))
@@ -324,15 +451,6 @@ func reversed(in []string) []string {
324
451
return out
325
452
}
326
453
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
-
336
454
func getRelevantEventDebugStringsFromMessagesResponse (t * testing.T , body []byte ) (eventIDsFromResponse []string ) {
337
455
t .Helper ()
338
456
0 commit comments