forked from matrix-org/sliding-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_test.go
709 lines (685 loc) · 22.1 KB
/
connection_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
package syncv3
import (
"context"
"encoding/json"
"fmt"
"sync"
"testing"
"time"
slidingsync "github.com/matrix-org/sliding-sync"
"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/gjson"
)
// Test that if you hit /sync and give up, we only start 1 connection.
// Relevant for when large accounts hit /sync for the first time and then time-out locally, and then
// hit /sync again without a ?pos=
func TestMultipleConnsAtStartup(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)
var res *sync3.Response
var wg sync.WaitGroup
wg.Add(1)
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer wg.Done()
_, body, _ := v3.doV3Request(t, ctx, aliceToken, "", sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 10,
},
}},
})
if body != nil {
// we got sent a response but should've got nothing as we knifed the connection
t.Errorf("got unexpected response: %s", string(body))
}
}()
// wait until the proxy is waiting on v2. As this is the initial sync, we won't have a conn yet.
v2.waitUntilEmpty(t, alice)
// interrupt the connection
cancel()
wg.Wait()
// respond to sync v2
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{}{}),
},
}),
},
})
// do another /sync
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}),
)))
}
// Regression test for running the proxy server behind a reverse proxy.
// The problem: when using a reverse proxy and a client cancels a request, the reverse proxy may
// not terminate the upstream connection. If this happens, subsequent requests from the client will
// stack up in Conn behind a mutex until the cancelled request is processed. In reality, we want the
// cancelled request to be stopped entirely. Whilst we cannot force reverse proxies to terminate the
// connection, we _can_ check if there is an outstanding request holding the mutex, and if so, interrupt
// it at the application layer. This test ensures we do that.
func TestOutstandingRequestsGetCancelled(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
// Rooms A,B gets sent to the client initially, then we will send a 2nd blocking request with a 3s timeout.
// During this time, we will send a 3rd request with modified sort operations to ensure that the proxy can
// return an immediate response. We _should_ see the response promptly as the blocking request gets cancelled.
// If it takes seconds to process this request, that implies it got stacked up behind a previous request,
// failing the test.
roomA := "!a:localhost" // name is A, older timestamp
roomB := "!b:localhost" // name is B, newer timestamp
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomA,
state: createRoomState(t, alice, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": "A"}),
},
}, roomEvents{
roomID: roomB,
state: createRoomState(t, alice, time.Now().Add(time.Hour)),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": "B"}, testutils.WithTimestamp(time.Now().Add(time.Hour))),
},
}),
},
})
// first request to get some data
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1},
},
Sort: []string{sync3.SortByName}, // A,B
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(2), m.MatchV3Ops(
m.MatchV3SyncOp(0, 1, []string{roomA, roomB}),
)))
// now we do a blocking request, and a few ms later do another request which can be satisfied
// using the same pos
pos := res.Pos
waitTimeMS := 3000
startTime := time.Now()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(100 * time.Millisecond)
res2 := v3.mustDoV3RequestWithPos(t, aliceToken, pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1},
},
Sort: []string{sync3.SortByRecency}, // B,A
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
}},
})
// this will be the response for the cancelled request initially
m.MatchResponse(t, res2, m.MatchNoV3Ops())
// retry request with new pos and we should see the new data
res2 = v3.mustDoV3RequestWithPos(t, aliceToken, res2.Pos, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1},
},
Sort: []string{sync3.SortByRecency}, // B,A
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
}},
})
m.MatchResponse(t, res2, m.MatchList("a", m.MatchV3Count(2), m.MatchV3Ops(
m.MatchV3InvalidateOp(0, 1),
m.MatchV3SyncOp(0, 1, []string{roomB, roomA}),
)))
if time.Since(startTime) > time.Second {
t.Errorf("took >1s to process request which should have been processed instantly, took %v", time.Since(startTime))
}
}()
req := sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 1},
},
}},
}
req.SetTimeoutMSecs(waitTimeMS)
res = v3.mustDoV3RequestWithPos(t, aliceToken, pos, req)
if time.Since(startTime) > time.Second {
t.Errorf("took >1s to process request which should have been interrupted before timing out, took %v", time.Since(startTime))
}
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(2)), m.MatchNoV3Ops())
wg.Wait()
}
// Regression test to ensure that ?timeout= isn't reset when live events come in.
func TestConnectionTimeoutNotReset(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
// One room gets sent v2 updates, one room does not. Room B gets updates, which, because
// we are tracking alphabetically, causes those updates to not trigger a v3 response. This
// used to reset the timeout though, so we will check to make sure it doesn't.
roomA := "!a:localhost"
roomB := "!b:localhost"
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomA,
state: createRoomState(t, alice, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": "A"}),
},
},
roomEvents{
roomID: roomB,
state: createRoomState(t, alice, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": "B"}),
},
}),
},
})
// first request to get some data
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 0}, // first room only -> roomID
},
Sort: []string{sync3.SortByName},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(2), m.MatchV3Ops(
m.MatchV3SyncOp(0, 0, []string{roomA}),
)))
// 2nd request with a 1s timeout
req := sync3.Request{
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 0},
},
}},
}
req.SetTimeoutMSecs(1000) // 1s
// inject 4 events 500ms apart - if we reset the timeout each time then we will return late
done := make(chan struct{})
go func() {
time.Sleep(10 * time.Millisecond)
ticker := time.NewTicker(500 * time.Millisecond)
i := 0
for range ticker.C {
if i > 3 {
t.Logf("stopping update")
break
}
t.Logf("sending update")
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomB,
events: []json.RawMessage{
testutils.NewEvent(
t, "m.room.message", alice, map[string]interface{}{"old": "msg"}, testutils.WithTimestamp(time.Now().Add(-2*time.Hour)),
),
},
}),
},
})
i++
}
done <- struct{}{}
}()
startTime := time.Now()
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, req)
dur := time.Since(startTime)
if dur > (1500 * time.Millisecond) { // 0.5s leeway
t.Fatalf("request took %v to complete, expected ~1s", dur)
}
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(2)), m.MatchNoV3Ops())
// Wait for all the responses before closing the v2 server.
<-done
}
// Test that the txn_id is echoed back
func TestTxnIDEcho(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"
txnID := "hi"
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{})
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
TxnID: txnID,
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 5,
},
},
})
m.MatchResponse(t, res, m.MatchTxnID(txnID))
txnID2 := "goodbyte"
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
TxnID: txnID2,
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 15,
},
},
})
m.MatchResponse(t, res, m.MatchTxnID(txnID2))
// retry same request
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
TxnID: txnID2,
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 15,
},
},
})
m.MatchResponse(t, res, m.MatchTxnID(txnID2))
}
// Test that we implement https://github.com/matrix-org/matrix-spec-proposals/blob/kegan/sync-v3/proposals/3575-sync.md#sticky-request-parameters
// correctly server-side. We do this by:
// - Create rooms A, B and C.
// - Send a request with room_name_like = C. Get back pos=1
// - Send a request with pos=1 to filter for room_name_like = A . Discard the response.
// - Send a request with pos=1 to filter for room_name_like = B. Ensure we see both A,B and the txn_id is set correctly for both.
func TestTxnIDResponseBuffering(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
roomA := "!a:localhost"
roomB := "!b:localhost"
roomC := "!c:localhost"
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomA,
state: createRoomState(t, alice, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": "A"}),
},
},
roomEvents{
roomID: roomB,
state: createRoomState(t, alice, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": "B"}),
},
},
roomEvents{
roomID: roomC,
state: createRoomState(t, alice, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": "C"}),
},
}),
},
})
// Send a request with room_name_like = C. Get back pos=1
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
TxnID: "c",
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
Sort: []string{sync3.SortByName},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
Filters: &sync3.RequestFilters{
RoomNameFilter: "C",
},
}},
})
m.MatchResponse(t, res, m.MatchTxnID("c"), m.MatchList("a", m.MatchV3Count(1), m.MatchV3Ops(
m.MatchV3SyncOp(0, 0, []string{roomC}),
)))
// Send a request with pos=1 to filter for room_name_like = A . Discard the response.
v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
TxnID: "a",
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
Sort: []string{sync3.SortByName},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
Filters: &sync3.RequestFilters{
RoomNameFilter: "A",
},
}},
})
// Send a request with pos=1 to filter for room_name_like = B. Ensure we see both A,B and the txn_id is set correctly for both.
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{
TxnID: "b",
Lists: map[string]sync3.RequestList{"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, 10},
},
Sort: []string{sync3.SortByName},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: 1,
},
Filters: &sync3.RequestFilters{
RoomNameFilter: "B",
},
}},
})
// this response should be the one for A
m.MatchResponse(t, res, m.MatchTxnID("a"), m.MatchList("a", m.MatchV3Count(1), m.MatchV3Ops(
m.MatchV3InvalidateOp(0, 0),
m.MatchV3SyncOp(0, 0, []string{roomA}),
)))
// poll again
res = v3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{})
// now we get the response for B
m.MatchResponse(t, res, m.MatchTxnID("b"), m.MatchList("a", m.MatchV3Count(1), m.MatchV3Ops(
m.MatchV3InvalidateOp(0, 0),
m.MatchV3SyncOp(0, 0, []string{roomB}),
)))
}
// Regression test to make sure that if Alice does an initial sync followed by Bob, that Bob actually
// makes the request and can be serviced before Alice even though she was first. Early proxy impls had
// this behaviour but it regressed when we converted to a pubsub model as a single goroutine would handle
// EnsurePolling requests, rather than the HTTP goroutine.
func TestEnsurePollingDoesntQueue(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v2.timeToWaitForV2Response = 5 * time.Second
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
roomA := "!a:localhost"
roomB := "!b:localhost"
v2.addAccount(t, alice, aliceToken)
v2.addAccount(t, bob, bobToken)
v2.queueResponse(bob, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomB,
state: createRoomState(t, bob, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", bob, map[string]interface{}{"name": "B"}),
},
}),
},
})
var mu sync.Mutex
aliceReturned := false
// wait until alice makes the v2 /sync request, then start bob's v3 request
go func() {
t.Logf("waiting for alice's v2 poller to start")
v2.waitUntilEmpty(t, alice) // alice's poller is making the v2 request
t.Logf("alice's v2 poller is waiting, doing bob's v3 request")
startTime := time.Now()
res := v3.mustDoV3Request(t, bobToken, sync3.Request{ // start bob's v3 request
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomB: {
TimelineLimit: 1,
},
},
})
t.Logf("bob's v3 response returned")
if time.Since(startTime) > 4*time.Second {
t.Errorf("took too long to process bob's v3 request, it probably stacked behind alice")
}
mu.Lock()
if aliceReturned {
t.Errorf("Alice's /sync request returned before Bob's, expected Bob's to return first")
}
mu.Unlock()
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomB: {
m.MatchJoinCount(1),
m.MatchRoomName("B"),
},
}))
// now send alice's response to unblock her
t.Logf("sending alice's v2 response")
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomA,
state: createRoomState(t, alice, time.Now()),
events: []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": "A"}),
},
}),
},
})
}()
t.Logf("starting alice's v3 request")
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomA: {
TimelineLimit: 1,
},
},
})
t.Logf("alice's v3 response returned")
mu.Lock()
aliceReturned = true
mu.Unlock()
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomA: {
m.MatchJoinCount(1),
m.MatchRoomName("A"),
},
}))
}
// Test to ensure that we send back a spec-compliant error message when the session is expired.
func TestSessionExpiry(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
v2 := runTestV2Server(t)
v2.addAccount(t, alice, aliceToken)
v3 := runTestServer(t, v2, pqString)
roomID := "!doesnt:matter"
res1 := v3.mustDoV3Request(t, aliceToken, sync3.Request{
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 1,
},
},
})
req := sync3.Request{}
req.SetTimeoutMSecs(1)
res2 := v3.mustDoV3RequestWithPos(t, aliceToken, res1.Pos, req)
_ = v3.mustDoV3RequestWithPos(t, aliceToken, res2.Pos, req)
// now use an earlier ?pos= to expire the session
_, body, code := v3.doV3Request(t, context.Background(), aliceToken, res1.Pos, req)
if code != 400 {
t.Errorf("got HTTP %d want 400", code)
}
if gjson.ParseBytes(body).Get("errcode").Str != "M_UNKNOWN_POS" {
t.Errorf("got %v want errcode=M_UNKNOWN_POS", string(body))
}
}
func TestSessionExpiryOnBufferFill(t *testing.T) {
roomID := "!doesnt:matter"
maxPendingEventUpdates := 3
pqString := testutils.PrepareDBConnectionString()
v2 := runTestV2Server(t)
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.name", "", alice, map[string]interface{}{"name": "B"}),
},
}),
},
})
v3 := runTestServer(t, v2, pqString, slidingsync.Opts{
MaxPendingEventUpdates: maxPendingEventUpdates,
})
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 1,
},
},
})
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomID: {
m.MatchJoinCount(1),
},
}))
// inject maxPendingEventUpdates+1 events to expire the session
events := make([]json.RawMessage, maxPendingEventUpdates+1)
for i := range events {
events[i] = testutils.NewEvent(t, "m.room.message", alice, map[string]interface{}{
"msgtype": "m.text",
"body": fmt.Sprintf("Test %d", i),
}, testutils.WithTimestamp(time.Now().Add(time.Duration(i)*time.Second)))
}
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(roomEvents{
roomID: roomID,
events: events,
}),
},
})
v2.waitUntilEmpty(t, aliceToken)
_, body, code := v3.doV3Request(t, context.Background(), aliceToken, res.Pos, sync3.Request{})
if code != 400 {
t.Errorf("got HTTP %d want 400", code)
}
if gjson.ParseBytes(body).Get("errcode").Str != "M_UNKNOWN_POS" {
t.Errorf("got %v want errcode=M_UNKNOWN_POS", string(body))
}
// make sure we can sync from fresh (regression for when we deadlocked after this point)
res = v3.mustDoV3Request(t, aliceToken, sync3.Request{
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 1,
},
},
})
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomID: {
m.MatchJoinCount(1),
},
}))
}
func TestExpiredAccessToken(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
v2 := runTestV2Server(t)
v2.addAccount(t, alice, aliceToken)
v3 := runTestServer(t, v2, pqString)
roomID := "!doesnt:matter"
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 1,
},
},
})
// now expire the token
v2.invalidateToken(aliceToken)
// now do another request, this should 401
req := sync3.Request{}
req.SetTimeoutMSecs(1)
_, body, statusCode := v3.doV3Request(t, context.Background(), aliceToken, res.Pos, req)
if statusCode != 401 {
t.Fatalf("got %d want 401 : %v", statusCode, string(body))
}
}
func TestExpiredAccessTokenMultipleConns(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
v2 := runTestV2Server(t)
v2.addAccount(t, alice, aliceToken)
v3 := runTestServer(t, v2, pqString)
roomID := "!doesnt:matter"
resA := v3.mustDoV3Request(t, aliceToken, sync3.Request{
ConnID: "A",
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 1,
},
},
})
resB := v3.mustDoV3Request(t, aliceToken, sync3.Request{
ConnID: "B",
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 1,
},
},
})
// now expire the token
v2.invalidateToken(aliceToken)
// now do another request for each conn, this should 401
testCases := []struct {
ConnID string
Res *sync3.Response
}{
{ConnID: "A", Res: resA},
{ConnID: "B", Res: resB},
}
for _, tc := range testCases {
req := sync3.Request{ConnID: tc.ConnID}
req.SetTimeoutMSecs(1)
_, body, statusCode := v3.doV3Request(t, context.Background(), aliceToken, tc.Res.Pos, req)
if statusCode != 401 {
t.Fatalf("got %d want 401 : %v", statusCode, string(body))
}
}
}