@@ -13,6 +13,7 @@ import (
13
13
"github.com/matrix-org/complement/match"
14
14
"github.com/matrix-org/complement/must"
15
15
"github.com/matrix-org/complement/runtime"
16
+ "github.com/matrix-org/complement/should"
16
17
"github.com/tidwall/gjson"
17
18
)
18
19
@@ -40,12 +41,7 @@ func TestDelayedEvents(t *testing.T) {
40
41
user2 .MustJoinRoom (t , roomID , nil )
41
42
42
43
t .Run ("delayed events are empty on startup" , func (t * testing.T ) {
43
- res := getDelayedEvents (t , user )
44
- must .MatchResponse (t , res , match.HTTPResponse {
45
- JSON : []match.JSON {
46
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
47
- },
48
- })
44
+ matchDelayedEvents (t , user , 0 )
49
45
})
50
46
51
47
t .Run ("delayed message events are sent on timeout" , func (t * testing.T ) {
@@ -87,30 +83,15 @@ func TestDelayedEvents(t *testing.T) {
87
83
})
88
84
}
89
85
90
- res = getDelayedEvents (t , user )
91
86
countExpected = 0
92
- must .MatchResponse (t , res , match.HTTPResponse {
93
- JSON : []match.JSON {
94
- match .JSONKeyArrayOfSize ("delayed_events" , numEvents ),
95
- },
96
- })
87
+ matchDelayedEvents (t , user , numEvents )
97
88
98
89
t .Run ("cannot get delayed events of another user" , func (t * testing.T ) {
99
- res := getDelayedEvents (t , user2 )
100
- must .MatchResponse (t , res , match.HTTPResponse {
101
- JSON : []match.JSON {
102
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
103
- },
104
- })
90
+ matchDelayedEvents (t , user2 , 0 )
105
91
})
106
92
107
93
time .Sleep (1 * time .Second )
108
- res = getDelayedEvents (t , user )
109
- must .MatchResponse (t , res , match.HTTPResponse {
110
- JSON : []match.JSON {
111
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
112
- },
113
- })
94
+ matchDelayedEvents (t , user , 0 )
114
95
queryParams := url.Values {}
115
96
queryParams .Set ("dir" , "f" )
116
97
queryParams .Set ("from" , token )
@@ -151,10 +132,12 @@ func TestDelayedEvents(t *testing.T) {
151
132
}),
152
133
getDelayQueryParam ("900" ),
153
134
)
135
+
136
+ matchDelayedEvents (t , user , 1 )
137
+
154
138
res = getDelayedEvents (t , user )
155
139
must .MatchResponse (t , res , match.HTTPResponse {
156
140
JSON : []match.JSON {
157
- match .JSONKeyArrayOfSize ("delayed_events" , 1 ),
158
141
match .JSONArrayEach ("delayed_events" , func (val gjson.Result ) error {
159
142
content := val .Get ("content" ).Map ()
160
143
if l := len (content ); l != 1 {
@@ -173,12 +156,7 @@ func TestDelayedEvents(t *testing.T) {
173
156
})
174
157
175
158
time .Sleep (1 * time .Second )
176
- res = getDelayedEvents (t , user )
177
- must .MatchResponse (t , res , match.HTTPResponse {
178
- JSON : []match.JSON {
179
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
180
- },
181
- })
159
+ matchDelayedEvents (t , user , 0 )
182
160
res = user .MustDo (t , "GET" , getPathForState (roomID , eventType , stateKey ))
183
161
must .MatchResponse (t , res , match.HTTPResponse {
184
162
JSON : []match.JSON {
@@ -274,12 +252,7 @@ func TestDelayedEvents(t *testing.T) {
274
252
delayID := client .GetJSONFieldStr (t , client .ParseJSON (t , res ), "delay_id" )
275
253
276
254
time .Sleep (1 * time .Second )
277
- res = getDelayedEvents (t , user )
278
- must .MatchResponse (t , res , match.HTTPResponse {
279
- JSON : []match.JSON {
280
- match .JSONKeyArrayOfSize ("delayed_events" , 1 ),
281
- },
282
- })
255
+ matchDelayedEvents (t , user , 1 )
283
256
res = user .Do (t , "GET" , getPathForState (roomID , eventType , stateKey ))
284
257
must .MatchResponse (t , res , match.HTTPResponse {
285
258
StatusCode : 404 ,
@@ -293,12 +266,7 @@ func TestDelayedEvents(t *testing.T) {
293
266
"action" : "cancel" ,
294
267
}),
295
268
)
296
- res = getDelayedEvents (t , user )
297
- must .MatchResponse (t , res , match.HTTPResponse {
298
- JSON : []match.JSON {
299
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
300
- },
301
- })
269
+ matchDelayedEvents (t , user , 0 )
302
270
303
271
time .Sleep (1 * time .Second )
304
272
res = user .Do (t , "GET" , getPathForState (roomID , eventType , stateKey ))
@@ -328,12 +296,7 @@ func TestDelayedEvents(t *testing.T) {
328
296
delayID := client .GetJSONFieldStr (t , client .ParseJSON (t , res ), "delay_id" )
329
297
330
298
time .Sleep (1 * time .Second )
331
- res = getDelayedEvents (t , user )
332
- must .MatchResponse (t , res , match.HTTPResponse {
333
- JSON : []match.JSON {
334
- match .JSONKeyArrayOfSize ("delayed_events" , 1 ),
335
- },
336
- })
299
+ matchDelayedEvents (t , user , 1 )
337
300
res = user .Do (t , "GET" , getPathForState (roomID , eventType , stateKey ))
338
301
must .MatchResponse (t , res , match.HTTPResponse {
339
302
StatusCode : 404 ,
@@ -347,12 +310,7 @@ func TestDelayedEvents(t *testing.T) {
347
310
"action" : "send" ,
348
311
}),
349
312
)
350
- res = getDelayedEvents (t , user )
351
- must .MatchResponse (t , res , match.HTTPResponse {
352
- JSON : []match.JSON {
353
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
354
- },
355
- })
313
+ matchDelayedEvents (t , user , 0 )
356
314
res = user .Do (t , "GET" , getPathForState (roomID , eventType , stateKey ))
357
315
must .MatchResponse (t , res , match.HTTPResponse {
358
316
JSON : []match.JSON {
@@ -382,12 +340,7 @@ func TestDelayedEvents(t *testing.T) {
382
340
delayID := client .GetJSONFieldStr (t , client .ParseJSON (t , res ), "delay_id" )
383
341
384
342
time .Sleep (1 * time .Second )
385
- res = getDelayedEvents (t , user )
386
- must .MatchResponse (t , res , match.HTTPResponse {
387
- JSON : []match.JSON {
388
- match .JSONKeyArrayOfSize ("delayed_events" , 1 ),
389
- },
390
- })
343
+ matchDelayedEvents (t , user , 1 )
391
344
res = user .Do (t , "GET" , getPathForState (roomID , eventType , stateKey ))
392
345
must .MatchResponse (t , res , match.HTTPResponse {
393
346
StatusCode : 404 ,
@@ -403,24 +356,14 @@ func TestDelayedEvents(t *testing.T) {
403
356
)
404
357
405
358
time .Sleep (1 * time .Second )
406
- res = getDelayedEvents (t , user )
407
- must .MatchResponse (t , res , match.HTTPResponse {
408
- JSON : []match.JSON {
409
- match .JSONKeyArrayOfSize ("delayed_events" , 1 ),
410
- },
411
- })
359
+ matchDelayedEvents (t , user , 1 )
412
360
res = user .Do (t , "GET" , getPathForState (roomID , eventType , stateKey ))
413
361
must .MatchResponse (t , res , match.HTTPResponse {
414
362
StatusCode : 404 ,
415
363
})
416
364
417
365
time .Sleep (1 * time .Second )
418
- res = getDelayedEvents (t , user )
419
- must .MatchResponse (t , res , match.HTTPResponse {
420
- JSON : []match.JSON {
421
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
422
- },
423
- })
366
+ matchDelayedEvents (t , user , 0 )
424
367
res = user .MustDo (t , "GET" , getPathForState (roomID , eventType , stateKey ))
425
368
must .MatchResponse (t , res , match.HTTPResponse {
426
369
JSON : []match.JSON {
@@ -446,12 +389,7 @@ func TestDelayedEvents(t *testing.T) {
446
389
}),
447
390
getDelayQueryParam ("900" ),
448
391
)
449
- res = getDelayedEvents (t , user )
450
- must .MatchResponse (t , res , match.HTTPResponse {
451
- JSON : []match.JSON {
452
- match .JSONKeyArrayOfSize ("delayed_events" , 1 ),
453
- },
454
- })
392
+ matchDelayedEvents (t , user , 1 )
455
393
456
394
setterExpected := "manual"
457
395
user .MustDo (
@@ -462,12 +400,7 @@ func TestDelayedEvents(t *testing.T) {
462
400
setterKey : setterExpected ,
463
401
}),
464
402
)
465
- res = getDelayedEvents (t , user )
466
- must .MatchResponse (t , res , match.HTTPResponse {
467
- JSON : []match.JSON {
468
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
469
- },
470
- })
403
+ matchDelayedEvents (t , user , 0 )
471
404
472
405
time .Sleep (1 * time .Second )
473
406
res = user .MustDo (t , "GET" , getPathForState (roomID , eventType , stateKey ))
@@ -496,12 +429,7 @@ func TestDelayedEvents(t *testing.T) {
496
429
}),
497
430
getDelayQueryParam ("900" ),
498
431
)
499
- res = getDelayedEvents (t , user )
500
- must .MatchResponse (t , res , match.HTTPResponse {
501
- JSON : []match.JSON {
502
- match .JSONKeyArrayOfSize ("delayed_events" , 1 ),
503
- },
504
- })
432
+ matchDelayedEvents (t , user , 1 )
505
433
506
434
setterExpected := "manual"
507
435
user2 .MustDo (
@@ -512,12 +440,7 @@ func TestDelayedEvents(t *testing.T) {
512
440
setterKey : setterExpected ,
513
441
}),
514
442
)
515
- res = getDelayedEvents (t , user )
516
- must .MatchResponse (t , res , match.HTTPResponse {
517
- JSON : []match.JSON {
518
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
519
- },
520
- })
443
+ matchDelayedEvents (t , user , 0 )
521
444
522
445
time .Sleep (1 * time .Second )
523
446
res = user .MustDo (t , "GET" , getPathForState (roomID , eventType , stateKey ))
@@ -532,8 +455,6 @@ func TestDelayedEvents(t *testing.T) {
532
455
// Spec cannot enforce server restart behaviour
533
456
runtime .SkipIf (t , runtime .Dendrite , runtime .Conduit , runtime .Conduwuit )
534
457
535
- var res * http.Response
536
-
537
458
defer cleanupDelayedEvents (t , user )
538
459
539
460
stateKey1 := "1"
@@ -553,32 +474,17 @@ func TestDelayedEvents(t *testing.T) {
553
474
client .WithJSONBody (t , map [string ]interface {}{}),
554
475
getDelayQueryParam ("9900" ),
555
476
)
556
- res = getDelayedEvents (t , user )
557
- must .MatchResponse (t , res , match.HTTPResponse {
558
- JSON : []match.JSON {
559
- match .JSONKeyArrayOfSize ("delayed_events" , 2 ),
560
- },
561
- })
477
+ matchDelayedEvents (t , user , 2 )
562
478
563
479
deployment .StopServer (t , hsName )
564
480
time .Sleep (1 * time .Second )
565
481
deployment .StartServer (t , hsName )
566
482
567
- res = getDelayedEvents (t , user )
568
- must .MatchResponse (t , res , match.HTTPResponse {
569
- JSON : []match.JSON {
570
- match .JSONKeyArrayOfSize ("delayed_events" , 1 ),
571
- },
572
- })
483
+ matchDelayedEvents (t , user , 1 )
573
484
user .MustDo (t , "GET" , getPathForState (roomID , eventType , stateKey1 ))
574
485
575
486
time .Sleep (9 * time .Second )
576
- res = getDelayedEvents (t , user )
577
- must .MatchResponse (t , res , match.HTTPResponse {
578
- JSON : []match.JSON {
579
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
580
- },
581
- })
487
+ matchDelayedEvents (t , user , 0 )
582
488
user .MustDo (t , "GET" , getPathForState (roomID , eventType , stateKey2 ))
583
489
})
584
490
}
@@ -606,6 +512,32 @@ func getDelayedEvents(t *testing.T, user *client.CSAPI) *http.Response {
606
512
return user .MustDo (t , "GET" , getPathForUpdateDelayedEvents ())
607
513
}
608
514
515
+ // Checks if the number of delayed events match the given number. This will
516
+ // retry to handle replication lag.
517
+ func matchDelayedEvents (t * testing.T , user * client.CSAPI , wantNumber int ) {
518
+ t .Helper ()
519
+
520
+ // We need to retry this as replication can sometimes lag.
521
+ user .MustDo (t , "GET" , getPathForUpdateDelayedEvents (),
522
+ client .WithRetryUntil (
523
+ 500 * time .Millisecond ,
524
+ func (res * http.Response ) bool {
525
+ _ , err := should .MatchResponse (res , match.HTTPResponse {
526
+ StatusCode : 200 ,
527
+ JSON : []match.JSON {
528
+ match .JSONKeyArrayOfSize ("delayed_events" , wantNumber ),
529
+ },
530
+ })
531
+ if err != nil {
532
+ t .Log (err )
533
+ return false
534
+ }
535
+ return true
536
+ },
537
+ ),
538
+ )
539
+ }
540
+
609
541
func cleanupDelayedEvents (t * testing.T , user * client.CSAPI ) {
610
542
t .Helper ()
611
543
res := getDelayedEvents (t , user )
@@ -623,9 +555,5 @@ func cleanupDelayedEvents(t *testing.T, user *client.CSAPI) {
623
555
)
624
556
}
625
557
626
- must .MatchResponse (t , getDelayedEvents (t , user ), match.HTTPResponse {
627
- JSON : []match.JSON {
628
- match .JSONKeyArrayOfSize ("delayed_events" , 0 ),
629
- },
630
- })
558
+ matchDelayedEvents (t , user , 0 )
631
559
}
0 commit comments