forked from matrix-org/sliding-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline_test.go
1406 lines (1332 loc) · 47.3 KB
/
timeline_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package syncv3
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/jmoiron/sqlx"
slidingsync "github.com/matrix-org/sliding-sync"
"github.com/matrix-org/sliding-sync/sqlutil"
"github.com/matrix-org/sliding-sync/state"
"github.com/matrix-org/sliding-sync/sync2"
"github.com/matrix-org/sliding-sync/sync3"
"github.com/matrix-org/sliding-sync/testutils"
"github.com/matrix-org/sliding-sync/testutils/m"
"github.com/tidwall/sjson"
)
// Inject 20 rooms with A,B,C as the most recent events. Then do a v3 request [0,3] with a timeline limit of 3
// and make sure we get scrolback for the 4 rooms we care about. Then, restart the server (so it repopulates caches)
// and attempt the same request again, making sure we get the same results. Then add in some "live" v2 events
// and make sure the initial scrollback includes these new live events.
func TestTimelines(t *testing.T) {
// setup code
pqString := testutils.PrepareDBConnectionString()
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
// make 20 rooms, last room is most recent, and send A,B,C into each room
allRooms := make([]roomEvents, 20)
for i := 0; i < len(allRooms); i++ {
ts := time.Now().Add(time.Duration(i) * time.Minute)
roomName := fmt.Sprintf("My Room %d", i)
allRooms[i] = roomEvents{
roomID: fmt.Sprintf("!TestTimelines_%d:localhost", i),
name: roomName,
events: append(createRoomState(t, alice, ts), []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": roomName}, testutils.WithTimestamp(ts.Add(3*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "A"}, testutils.WithTimestamp(ts.Add(4*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "B"}, testutils.WithTimestamp(ts.Add(5*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "C"}, testutils.WithTimestamp(ts.Add(6*time.Second))),
}...),
}
}
latestTimestamp := time.Now().Add(10 * time.Hour)
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(allRooms...),
},
})
// most recent 4 rooms
var wantRooms []roomEvents
i := 0
for len(wantRooms) < 4 {
wantRooms = append(wantRooms, allRooms[len(allRooms)-i-1])
i++
}
numTimelineEventsPerRoom := 3
t.Run("timelines load initially", testTimelineLoadInitialEvents(v3, aliceToken, len(allRooms), wantRooms, numTimelineEventsPerRoom))
// restart the server
v3.restart(t, v2, pqString)
t.Run("timelines load initially after restarts", testTimelineLoadInitialEvents(v3, aliceToken, len(allRooms), wantRooms, numTimelineEventsPerRoom))
// inject some live events
liveEvents := []roomEvents{
{
roomID: allRooms[0].roomID,
events: []json.RawMessage{
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "ping"}, testutils.WithTimestamp(latestTimestamp.Add(1*time.Minute))),
},
},
{
roomID: allRooms[1].roomID,
events: []json.RawMessage{
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "ping2"}, testutils.WithTimestamp(latestTimestamp.Add(2*time.Minute))),
},
},
}
v2.waitUntilEmpty(t, alice)
// add these live events to the global view of the timeline
allRooms[0].events = append(allRooms[0].events, liveEvents[0].events...)
allRooms[1].events = append(allRooms[1].events, liveEvents[1].events...)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(liveEvents...),
},
})
v2.waitUntilEmpty(t, alice)
// now we want the new live rooms and then the most recent 2 rooms from before
wantRooms = append([]roomEvents{
allRooms[1], allRooms[0],
}, wantRooms[0:2]...)
t.Run("live events are added to the timeline initially", testTimelineLoadInitialEvents(v3, aliceToken, len(allRooms), wantRooms, numTimelineEventsPerRoom))
}
// Create 20 rooms and send A,B,C into each. Then bump various rooms "live streamed" from v2 and ensure
// the correct delta operations are sent e.g DELETE/INSERT/UPDATE.
func TestTimelinesLiveStream(t *testing.T) {
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, "")
defer v2.close()
defer v3.close()
// make 20 rooms, last room is most recent, and send A,B,C into each room
allRooms := make([]roomEvents, 20)
latestTimestamp := time.Now()
for i := 0; i < len(allRooms); i++ {
ts := time.Now().Add(time.Duration(i) * time.Minute)
roomName := fmt.Sprintf("My Room %d", i)
allRooms[i] = roomEvents{
roomID: fmt.Sprintf("!TestTimelinesLiveStream_%d:localhost", i),
name: roomName,
events: append(createRoomState(t, alice, ts), []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": roomName}, testutils.WithTimestamp(ts.Add(3*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "A"}, testutils.WithTimestamp(ts.Add(4*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "B"}, testutils.WithTimestamp(ts.Add(5*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "C"}, testutils.WithTimestamp(ts.Add(6*time.Second))),
}...),
}
if ts.After(latestTimestamp) {
latestTimestamp = ts.Add(10 * time.Second)
}
}
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(allRooms...),
},
})
numTimelineEventsPerRoom := 3
// send a live event in allRooms[i] (always 1s newer)
bumpRoom := func(i int) {
latestTimestamp = latestTimestamp.Add(1 * time.Second)
ev := testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": fmt.Sprintf("bump %d", i)}, testutils.WithTimestamp(latestTimestamp))
allRooms[i].events = append(allRooms[i].events, ev)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: allRooms[i].roomID,
events: []json.RawMessage{ev},
}),
},
})
v2.waitUntilEmpty(t, alice)
}
// most recent 4 rooms
var wantRooms []roomEvents
i := 0
for len(wantRooms) < 4 {
wantRooms = append(wantRooms, allRooms[len(allRooms)-i-1])
i++
}
// first request => rooms 19,18,17,16
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, int64(len(wantRooms) - 1)}, // first N rooms
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: int64(numTimelineEventsPerRoom),
},
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(len(allRooms)), m.MatchV3Ops(
m.MatchV3SyncOpFn(func(op *sync3.ResponseOpRange) error {
if len(op.RoomIDs) != len(wantRooms) {
return fmt.Errorf("want %d rooms, got %d", len(wantRooms), len(op.RoomIDs))
}
for i := range wantRooms {
err := wantRooms[i].MatchRoom(op.RoomIDs[i],
res.Rooms[op.RoomIDs[i]],
m.MatchRoomName(wantRooms[i].name),
m.MatchRoomTimelineMostRecent(numTimelineEventsPerRoom, wantRooms[i].events),
)
if err != nil {
return err
}
}
return nil
}),
)))
bumpRoom(7)
// next request, DELETE 3; INSERT 0 7;
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, int64(len(wantRooms) - 1)}, // first N rooms
},
// sticky remember the timeline_limit
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(len(allRooms)), m.MatchV3Ops(
m.MatchV3DeleteOp(3),
m.MatchV3InsertOp(0, allRooms[7].roomID),
)), m.MatchRoomSubscription(
allRooms[7].roomID, m.MatchRoomName(allRooms[7].name), m.MatchRoomTimelineMostRecent(numTimelineEventsPerRoom, allRooms[7].events),
))
bumpRoom(7)
// next request, UPDATE 0 7;
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, int64(len(wantRooms) - 1)}, // first N rooms
},
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(len(allRooms))), m.MatchNoV3Ops())
bumpRoom(18)
// next request, DELETE 2; INSERT 0 18;
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, int64(len(wantRooms) - 1)}, // first N rooms
},
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(len(allRooms)), m.MatchV3Ops(
m.MatchV3DeleteOp(2),
m.MatchV3InsertOp(0, allRooms[18].roomID),
)), m.MatchRoomSubscription(allRooms[18].roomID, m.MatchRoomTimelineMostRecent(1, []json.RawMessage{allRooms[18].events[len(allRooms[18].events)-1]})))
}
func TestMultipleWindows(t *testing.T) {
// setup code
pqString := testutils.PrepareDBConnectionString()
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
// make 20 rooms, first room is most recent, and send A,B,C into each room
allRooms := make([]roomEvents, 20)
for i := 0; i < len(allRooms); i++ {
ts := time.Now().Add(time.Duration(i) * -1 * time.Minute)
roomName := fmt.Sprintf("My Room %d", i)
allRooms[i] = roomEvents{
roomID: fmt.Sprintf("!TestMultipleWindows_%d:localhost", i),
name: roomName,
events: append(createRoomState(t, alice, ts), []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": roomName}, testutils.WithTimestamp(ts.Add(3*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "A"}, testutils.WithTimestamp(ts.Add(4*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "B"}, testutils.WithTimestamp(ts.Add(5*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "C"}, testutils.WithTimestamp(ts.Add(6*time.Second))),
}...),
}
}
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(allRooms...),
},
})
numTimelineEventsPerRoom := 2
// request 3 windows
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 2}, // first 3 rooms
[2]int64{10, 12}, // 3 rooms in the middle
[2]int64{17, 19}, // last 3 rooms
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: int64(numTimelineEventsPerRoom),
},
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(len(allRooms)), m.MatchV3Ops(
m.MatchV3SyncOp(0, 2, []string{allRooms[0].roomID, allRooms[1].roomID, allRooms[2].roomID}),
m.MatchV3SyncOp(10, 12, []string{allRooms[10].roomID, allRooms[11].roomID, allRooms[12].roomID}),
m.MatchV3SyncOp(17, 19, []string{allRooms[17].roomID, allRooms[18].roomID, allRooms[19].roomID}),
)))
// bump room 18 to position 0
latestTimestamp := time.Now().Add(time.Hour)
bumpRoom := func(i int) {
latestTimestamp = latestTimestamp.Add(1 * time.Second)
ev := testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": fmt.Sprintf("bump %d", i)}, testutils.WithTimestamp(latestTimestamp))
allRooms[i].events = append(allRooms[i].events, ev)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: allRooms[i].roomID,
events: []json.RawMessage{ev},
}),
},
})
v2.waitUntilEmpty(t, alice)
}
bumpRoom(18)
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 2}, // first 3 rooms
[2]int64{10, 12}, // 3 rooms in the middle
[2]int64{17, 19}, // last 3 rooms
},
}},
})
// Range A Range B Range C
// _____ ________ ________
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// DELETE 2 DELETE 12 DELETE 18
// INSERT 0,18 INSERT 10,9 INSERT 17,16
m.MatchResponse(t, res, m.MatchList("a",
m.MatchV3Count(len(allRooms)),
m.MatchV3Ops(
m.MatchV3DeleteOp(18),
m.MatchV3InsertOp(17, allRooms[16].roomID),
m.MatchV3DeleteOp(2),
m.MatchV3InsertOp(0, allRooms[18].roomID),
m.MatchV3DeleteOp(12),
m.MatchV3InsertOp(10, allRooms[9].roomID),
),
))
}
func TestInitialFlag(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
roomID := "!a:localhost"
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
events: createRoomState(t, alice, time.Now()),
}),
},
})
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 10,
},
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Ops(
m.MatchV3SyncOp(0, 0, []string{roomID}),
)), m.MatchRoomSubscription(roomID, m.MatchRoomInitial(true)))
// send an update
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(
roomEvents{
roomID: roomID,
events: []json.RawMessage{
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{}),
},
},
),
},
})
v2.waitUntilEmpty(t, alice)
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
}},
})
m.MatchResponse(t, res, m.MatchNoV3Ops(), m.MatchRoomSubscription(roomID, m.MatchRoomInitial(false)))
}
// Regression test for in-the-wild bug:
//
// ERR missing events in database!
// ERR V2: failed to accumulate room error="failed to extract nids from inserted events, asked for 9 got 8"
//
// We should be able to gracefully handle duplicate events in the timeline.
func TestDuplicateEventsInTimeline(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
roomID := "!a:localhost"
dupeEvent := testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{})
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
state: createRoomState(t, alice, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.topic", "", alice, map[string]interface{}{}),
dupeEvent, dupeEvent,
},
}),
},
})
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 10,
},
}},
})
m.MatchResponse(t, res, m.MatchList("a",
m.MatchV3Ops(m.MatchV3SyncOp(0, 0, []string{roomID})),
), m.MatchRoomSubscription(roomID, m.MatchRoomTimelineMostRecent(1, []json.RawMessage{dupeEvent})))
}
// Regression test for https://github.com/matrix-org/sliding-sync/commit/39d6e99f967e55b609f8ef8b4271c04ebb053d37
// Request a timeline_limit of 0 for the room list. Sometimes when a new event arrives it causes an
// unrelated room to be sent to the client (e.g tracking rooms [5,10] and room 15 bumps to room 2,
// causing all the rooms to shift so you're now actually tracking [4,9] - the client knows 5-9 but
// room 4 is new, so you notify about that room and not the one which had a new event (room 15).
// Ensure that room 4 is given to the client. In the past, this would panic when timeline limit = 0
// as the timeline was loaded using the timeline limit of the client, and an unchecked array access
// into the timeline
func TestTimelineMiddleWindowZeroTimelineLimit(t *testing.T) {
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, "")
defer v2.close()
defer v3.close()
// make 20 rooms, first room is most recent, and send A,B,C into each room
allRooms := make([]roomEvents, 20)
for i := 0; i < len(allRooms); i++ {
ts := time.Now().Add(-1 * time.Duration(i) * time.Minute)
roomName := fmt.Sprintf("My Room %d", i)
allRooms[i] = roomEvents{
roomID: fmt.Sprintf("!TestTimelineMiddleWindowZeroTimelineLimit_%d:localhost", i),
name: roomName,
events: append(createRoomState(t, alice, ts), []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": roomName}, testutils.WithTimestamp(ts.Add(3*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "A"}, testutils.WithTimestamp(ts.Add(4*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "B"}, testutils.WithTimestamp(ts.Add(5*time.Second))),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "C"}, testutils.WithTimestamp(ts.Add(6*time.Second))),
}...),
}
}
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(allRooms...),
},
})
// Request rooms 5-10 with a 0 timeline limit
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{5, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 0,
},
}},
})
wantRooms := allRooms[5:11]
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(len(allRooms)), m.MatchV3Ops(
m.MatchV3SyncOpFn(func(op *sync3.ResponseOpRange) error {
if len(op.RoomIDs) != len(wantRooms) {
return fmt.Errorf("want %d rooms, got %d", len(wantRooms), len(op.RoomIDs))
}
for i := range wantRooms {
err := wantRooms[i].MatchRoom(op.RoomIDs[i],
res.Rooms[op.RoomIDs[i]],
m.MatchRoomName(wantRooms[i].name),
m.MatchRoomTimeline(nil),
)
if err != nil {
return err
}
}
return nil
}),
)))
// bump room 15 to 2
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: allRooms[15].roomID,
events: []json.RawMessage{
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "bump"}),
},
}),
},
})
v2.waitUntilEmpty(t, alice)
// should see room 4, the server should not panic
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{5, 10},
},
}},
})
m.MatchResponse(t, res, m.MatchLists(map[string][]m.ListMatcher{
"a": {
m.MatchV3Count(len(allRooms)),
m.MatchV3Ops(
m.MatchV3DeleteOp(10),
m.MatchV3InsertOp(5, allRooms[4].roomID),
),
},
}))
}
// Regression test to ensure that the 'state' block NEVER appears when requesting a high timeline_limit.
// In the past, the proxy treated state/timeline sections as the 'same' in that they were inserted into the
// events table and had stream positions associated with them. This could cause ancient state events to appear
// in the timeline if the timeline_limit was greatert than the number of genuine timeline events received via
// v2 sync.
func TestHistoryDoesntIncludeState(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
roomID := "!a:localhost"
prevBatch := "P_B"
room := roomEvents{
roomID: roomID,
// these events should NEVER appear in the timeline
state: createRoomState(t, alice, time.Now()),
// these events are the timeline and should appear
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.topic", "", alice, map[string]interface{}{"topic": "boo"}),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "hello"}),
},
prevBatch: prevBatch,
}
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
})
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 10,
},
}},
})
m.MatchResponse(t, res, m.MatchList("a",
m.MatchV3Ops(m.MatchV3SyncOp(0, 0, []string{roomID})),
), m.MatchRoomSubscription(roomID, m.MatchRoomTimeline(room.events), m.MatchRoomPrevBatch(prevBatch)))
}
// Test that transaction IDs come down the user's stream correctly in the case where 2 clients are
// in the same room.
func TestTimelineTxnID(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
roomID := "!a:localhost"
latestTimestamp := time.Now()
// Alice and Bob are in the same room
room := roomEvents{
roomID: roomID,
events: append(
createRoomState(t, alice, latestTimestamp),
testutils.NewJoinEvent(t, bob),
),
}
v2.addAccount(t, alice, aliceToken)
v2.addAccount(t, bob, bobToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
})
v2.queueResponse(bob, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
})
aliceRes := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 2,
},
},
},
})
bobRes := v3.mustDoV3Request(t, bobToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 2,
},
},
},
})
// Alice has sent a message but it arrives down Bob's poller first, so it has no txn_id
txnID := "m1234567890"
newEvent := testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "hi"}, testutils.WithUnsigned(map[string]interface{}{
"transaction_id": txnID,
}))
newEventNoUnsigned, err := sjson.DeleteBytes(newEvent, "unsigned")
if err != nil {
t.Fatalf("failed to delete bytes: %s", err)
}
v2.queueResponse(bob, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
events: []json.RawMessage{newEventNoUnsigned},
}),
},
})
v2.waitUntilEmpty(t, bob)
// now it arrives down Alice's poller, but the event has already been persisted at this point!
// We need a txn ID cache to remember it.
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
events: []json.RawMessage{newEvent},
}),
},
})
v2.waitUntilEmpty(t, alice)
// now Alice syncs, she should see the event with the txn ID
aliceRes = v3.mustDoV3RequestWithPos(t, aliceToken, aliceRes.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
},
},
})
m.MatchResponse(t, aliceRes, m.MatchList("a", m.MatchV3Count(1)), m.MatchNoV3Ops(), m.MatchRoomSubscription(
roomID, m.MatchRoomTimelineMostRecent(1, []json.RawMessage{newEvent}),
))
// now Bob syncs, he should see the event without the txn ID
bobRes = v3.mustDoV3RequestWithPos(t, bobToken, bobRes.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
},
},
})
m.MatchResponse(t, bobRes, m.MatchList("a", m.MatchV3Count(1)), m.MatchNoV3Ops(), m.MatchRoomSubscription(
roomID, m.MatchRoomTimelineMostRecent(1, []json.RawMessage{newEventNoUnsigned}),
))
}
// TestTimelineTxnID checks that Alice sees her transaction_id if
// - Bob's poller sees Alice's event,
// - Alice's poller sees Alice's event with txn_id, and
// - Alice syncs.
//
// This test is similar but not identical. It checks that Alice sees her transaction_id if
// - Bob's poller sees Alice's event,
// - Alice does an incremental sync, which should omit her event,
// - Alice's poller sees Alice's event with txn_id, and
// - Alice syncs, seeing her event with txn_id.
func TestTimelineTxnIDBuffersForTxnID(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString, slidingsync.Opts{
// This needs to be greater than the request timeout, which is hardcoded to a
// minimum of 100ms in connStateLive.liveUpdate. This ensures that the
// liveUpdate call finishes before the TxnIDWaiter publishes the update,
// meaning that Alice doesn't see her event before the txn ID is known.
MaxTransactionIDDelay: 200 * time.Millisecond,
})
defer v2.close()
defer v3.close()
roomID := "!a:localhost"
latestTimestamp := time.Now()
t.Log("Alice and Bob are in the same room")
room := roomEvents{
roomID: roomID,
events: append(
createRoomState(t, alice, latestTimestamp),
testutils.NewJoinEvent(t, bob),
),
}
v2.addAccount(t, alice, aliceToken)
v2.addAccount(t, bob, bobToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
NextBatch: "alice_after_initial_poll",
})
v2.queueResponse(bob, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
NextBatch: "bob_after_initial_poll",
})
t.Log("Alice and Bob make initial sliding syncs.")
aliceRes := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 2,
},
},
},
})
bobRes := v3.mustDoV3Request(t, bobToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 2,
},
},
},
})
t.Log("Alice has sent a message... but it arrives down Bob's poller first, without a transaction_id")
txnID := "m1234567890"
newEvent := testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "hi"}, testutils.WithUnsigned(map[string]interface{}{
"transaction_id": txnID,
}))
newEventNoUnsigned, err := sjson.DeleteBytes(newEvent, "unsigned")
if err != nil {
t.Fatalf("failed to delete bytes: %s", err)
}
v2.queueResponse(bob, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
events: []json.RawMessage{newEventNoUnsigned},
}),
},
})
t.Log("Bob's poller sees the message.")
v2.waitUntilEmpty(t, bob)
t.Log("Bob makes an incremental sliding sync")
bobRes = v3.mustDoV3RequestWithPos(t, bobToken, bobRes.Pos, sync3.Request{})
t.Log("Bob should see the message without a transaction_id")
m.MatchResponse(t, bobRes, m.MatchList("a", m.MatchV3Count(1)), m.MatchNoV3Ops(), m.MatchRoomSubscription(
roomID, m.MatchRoomTimelineMostRecent(1, []json.RawMessage{newEventNoUnsigned}),
))
t.Log("Alice requests an incremental sliding sync with no request changes.")
aliceRes = v3.mustDoV3RequestWithPos(t, aliceToken, aliceRes.Pos, sync3.Request{})
t.Log("Alice should see no messages.")
m.MatchResponse(t, aliceRes, m.MatchRoomSubscriptionsStrict(nil))
// Now the message arrives down Alice's poller.
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
events: []json.RawMessage{newEvent},
}),
},
})
t.Log("Alice's poller sees the message with transaction_id.")
v2.waitUntilEmpty(t, alice)
t.Log("Alice makes another incremental sync request.")
aliceRes = v3.mustDoV3RequestWithPos(t, aliceToken, aliceRes.Pos, sync3.Request{})
t.Log("Alice's sync response includes the message with the txn ID.")
m.MatchResponse(t, aliceRes, m.MatchList("a", m.MatchV3Count(1)), m.MatchNoV3Ops(), m.MatchRoomSubscription(
roomID, m.MatchRoomTimelineMostRecent(1, []json.RawMessage{newEvent}),
))
}
// Similar to TestTimelineTxnIDBuffersForTxnID, this test checks:
// - Bob's poller sees Alice's event,
// - Alice does an incremental sync, which should omit her event,
// - Alice's poller sees Alice's event without a txn_id, and
// - Alice syncs, seeing her event without txn_id.
// I.e. we're checking that the "all clear" empties out the buffer of events.
func TestTimelineTxnIDRespectsAllClear(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString, slidingsync.Opts{
// This needs to be greater than the request timeout, which is hardcoded to a
// minimum of 100ms in connStateLive.liveUpdate. This ensures that the
// liveUpdate call finishes before the TxnIDWaiter publishes the update,
// meaning that Alice doesn't see her event before the txn ID is known.
MaxTransactionIDDelay: 200 * time.Millisecond,
})
defer v2.close()
defer v3.close()
roomID := "!a:localhost"
latestTimestamp := time.Now()
t.Log("Alice and Bob are in the same room")
room := roomEvents{
roomID: roomID,
events: append(
createRoomState(t, alice, latestTimestamp),
testutils.NewJoinEvent(t, bob),
),
}
v2.addAccount(t, alice, aliceToken)
v2.addAccount(t, bob, bobToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
NextBatch: "alice_after_initial_poll",
})
v2.queueResponse(bob, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(room),
},
NextBatch: "bob_after_initial_poll",
})
t.Log("Alice and Bob make initial sliding syncs.")
aliceRes := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 2,
},
},
},
})
bobRes := v3.mustDoV3Request(t, bobToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 2,
},
},
},
})
t.Log("Alice has sent a message... but it arrives down Bob's poller first, without a transaction_id")
newEventNoTxn := testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "hi"})
v2.queueResponse(bob, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
events: []json.RawMessage{newEventNoTxn},
}),
},
})
t.Log("Bob's poller sees the message.")
v2.waitUntilEmpty(t, bob)
t.Log("Bob makes an incremental sliding sync")
bobRes = v3.mustDoV3RequestWithPos(t, bobToken, bobRes.Pos, sync3.Request{})
t.Log("Bob should see the message without a transaction_id")
m.MatchResponse(t, bobRes, m.MatchList("a", m.MatchV3Count(1)), m.MatchNoV3Ops(), m.MatchRoomSubscription(
roomID, m.MatchRoomTimelineMostRecent(1, []json.RawMessage{newEventNoTxn}),
))
t.Log("Alice requests an incremental sliding sync with no request changes.")
aliceRes = v3.mustDoV3RequestWithPos(t, aliceToken, aliceRes.Pos, sync3.Request{})
t.Log("Alice should see no messages.")
m.MatchResponse(t, aliceRes, m.MatchRoomSubscriptionsStrict(nil))
// Now the message arrives down Alice's poller.
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
events: []json.RawMessage{newEventNoTxn},
}),
},
})
t.Log("Alice's poller sees the message without transaction_id.")
v2.waitUntilEmpty(t, alice)
t.Log("Alice makes another incremental sync request.")
aliceRes = v3.mustDoV3RequestWithPos(t, aliceToken, aliceRes.Pos, sync3.Request{})
t.Log("Alice's sync response includes the event without a txn ID.")
m.MatchResponse(t, aliceRes, m.MatchList("a", m.MatchV3Count(1)), m.MatchNoV3Ops(), m.MatchRoomSubscription(
roomID, m.MatchRoomTimelineMostRecent(1, []json.RawMessage{newEventNoTxn}),
))
}
// Executes a sync v3 request without a ?pos and asserts that the count, rooms and timeline events m.Match the inputs given.
func testTimelineLoadInitialEvents(v3 *testV3Server, token string, count int, wantRooms []roomEvents, numTimelineEventsPerRoom int) func(t *testing.T) {
return func(t *testing.T) {
t.Helper()
res := v3.mustDoV3Request(t, token, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, int64(len(wantRooms) - 1)}, // first N rooms
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: int64(numTimelineEventsPerRoom),
},
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(count), m.MatchV3Ops(
m.MatchV3SyncOpFn(func(op *sync3.ResponseOpRange) error {
if len(op.RoomIDs) != len(wantRooms) {
return fmt.Errorf("want %d rooms, got %d", len(wantRooms), len(op.RoomIDs))
}
for i := range wantRooms {
err := wantRooms[i].MatchRoom(op.RoomIDs[i],
res.Rooms[op.RoomIDs[i]],
m.MatchRoomName(wantRooms[i].name),
m.MatchRoomTimelineMostRecent(numTimelineEventsPerRoom, wantRooms[i].events),
)
if err != nil {
return err
}
}
return nil
}),
)))
}
}
// Test that prev batch tokens appear correctly.
// 1: When there is no newer prev_batch, none is present.
// 2: When there is a newer prev_batch, it is present.
func TestPrevBatchInTimeline(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
roomID := "!a:localhost"
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
prevBatch: "create",
roomID: roomID,
state: createRoomState(t, alice, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.topic", "", alice, map[string]interface{}{}),
testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{"body": "hello"}),
},
}),
},
})
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {