Skip to content

Commit 08c0b55

Browse files
committed
Try and reduce flakey TestDelayedEvents tests
1 parent 7485d36 commit 08c0b55

File tree

1 file changed

+50
-122
lines changed

1 file changed

+50
-122
lines changed

Diff for: tests/msc4140/delayed_event_test.go

+50-122
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/matrix-org/complement/match"
1414
"github.com/matrix-org/complement/must"
1515
"github.com/matrix-org/complement/runtime"
16+
"github.com/matrix-org/complement/should"
1617
"github.com/tidwall/gjson"
1718
)
1819

@@ -40,12 +41,7 @@ func TestDelayedEvents(t *testing.T) {
4041
user2.MustJoinRoom(t, roomID, nil)
4142

4243
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)
4945
})
5046

5147
t.Run("delayed message events are sent on timeout", func(t *testing.T) {
@@ -87,30 +83,15 @@ func TestDelayedEvents(t *testing.T) {
8783
})
8884
}
8985

90-
res = getDelayedEvents(t, user)
9186
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)
9788

9889
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)
10591
})
10692

10793
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)
11495
queryParams := url.Values{}
11596
queryParams.Set("dir", "f")
11697
queryParams.Set("from", token)
@@ -151,10 +132,12 @@ func TestDelayedEvents(t *testing.T) {
151132
}),
152133
getDelayQueryParam("900"),
153134
)
135+
136+
matchDelayedEvents(t, user, 1)
137+
154138
res = getDelayedEvents(t, user)
155139
must.MatchResponse(t, res, match.HTTPResponse{
156140
JSON: []match.JSON{
157-
match.JSONKeyArrayOfSize("delayed_events", 1),
158141
match.JSONArrayEach("delayed_events", func(val gjson.Result) error {
159142
content := val.Get("content").Map()
160143
if l := len(content); l != 1 {
@@ -173,12 +156,7 @@ func TestDelayedEvents(t *testing.T) {
173156
})
174157

175158
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)
182160
res = user.MustDo(t, "GET", getPathForState(roomID, eventType, stateKey))
183161
must.MatchResponse(t, res, match.HTTPResponse{
184162
JSON: []match.JSON{
@@ -274,12 +252,7 @@ func TestDelayedEvents(t *testing.T) {
274252
delayID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "delay_id")
275253

276254
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)
283256
res = user.Do(t, "GET", getPathForState(roomID, eventType, stateKey))
284257
must.MatchResponse(t, res, match.HTTPResponse{
285258
StatusCode: 404,
@@ -293,12 +266,7 @@ func TestDelayedEvents(t *testing.T) {
293266
"action": "cancel",
294267
}),
295268
)
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)
302270

303271
time.Sleep(1 * time.Second)
304272
res = user.Do(t, "GET", getPathForState(roomID, eventType, stateKey))
@@ -328,12 +296,7 @@ func TestDelayedEvents(t *testing.T) {
328296
delayID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "delay_id")
329297

330298
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)
337300
res = user.Do(t, "GET", getPathForState(roomID, eventType, stateKey))
338301
must.MatchResponse(t, res, match.HTTPResponse{
339302
StatusCode: 404,
@@ -347,12 +310,7 @@ func TestDelayedEvents(t *testing.T) {
347310
"action": "send",
348311
}),
349312
)
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)
356314
res = user.Do(t, "GET", getPathForState(roomID, eventType, stateKey))
357315
must.MatchResponse(t, res, match.HTTPResponse{
358316
JSON: []match.JSON{
@@ -382,12 +340,7 @@ func TestDelayedEvents(t *testing.T) {
382340
delayID := client.GetJSONFieldStr(t, client.ParseJSON(t, res), "delay_id")
383341

384342
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)
391344
res = user.Do(t, "GET", getPathForState(roomID, eventType, stateKey))
392345
must.MatchResponse(t, res, match.HTTPResponse{
393346
StatusCode: 404,
@@ -403,24 +356,14 @@ func TestDelayedEvents(t *testing.T) {
403356
)
404357

405358
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)
412360
res = user.Do(t, "GET", getPathForState(roomID, eventType, stateKey))
413361
must.MatchResponse(t, res, match.HTTPResponse{
414362
StatusCode: 404,
415363
})
416364

417365
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)
424367
res = user.MustDo(t, "GET", getPathForState(roomID, eventType, stateKey))
425368
must.MatchResponse(t, res, match.HTTPResponse{
426369
JSON: []match.JSON{
@@ -446,12 +389,7 @@ func TestDelayedEvents(t *testing.T) {
446389
}),
447390
getDelayQueryParam("900"),
448391
)
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)
455393

456394
setterExpected := "manual"
457395
user.MustDo(
@@ -462,12 +400,7 @@ func TestDelayedEvents(t *testing.T) {
462400
setterKey: setterExpected,
463401
}),
464402
)
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)
471404

472405
time.Sleep(1 * time.Second)
473406
res = user.MustDo(t, "GET", getPathForState(roomID, eventType, stateKey))
@@ -496,12 +429,7 @@ func TestDelayedEvents(t *testing.T) {
496429
}),
497430
getDelayQueryParam("900"),
498431
)
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)
505433

506434
setterExpected := "manual"
507435
user2.MustDo(
@@ -512,12 +440,7 @@ func TestDelayedEvents(t *testing.T) {
512440
setterKey: setterExpected,
513441
}),
514442
)
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)
521444

522445
time.Sleep(1 * time.Second)
523446
res = user.MustDo(t, "GET", getPathForState(roomID, eventType, stateKey))
@@ -532,8 +455,6 @@ func TestDelayedEvents(t *testing.T) {
532455
// Spec cannot enforce server restart behaviour
533456
runtime.SkipIf(t, runtime.Dendrite, runtime.Conduit, runtime.Conduwuit)
534457

535-
var res *http.Response
536-
537458
defer cleanupDelayedEvents(t, user)
538459

539460
stateKey1 := "1"
@@ -553,32 +474,17 @@ func TestDelayedEvents(t *testing.T) {
553474
client.WithJSONBody(t, map[string]interface{}{}),
554475
getDelayQueryParam("9900"),
555476
)
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)
562478

563479
deployment.StopServer(t, hsName)
564480
time.Sleep(1 * time.Second)
565481
deployment.StartServer(t, hsName)
566482

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)
573484
user.MustDo(t, "GET", getPathForState(roomID, eventType, stateKey1))
574485

575486
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)
582488
user.MustDo(t, "GET", getPathForState(roomID, eventType, stateKey2))
583489
})
584490
}
@@ -606,6 +512,32 @@ func getDelayedEvents(t *testing.T, user *client.CSAPI) *http.Response {
606512
return user.MustDo(t, "GET", getPathForUpdateDelayedEvents())
607513
}
608514

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+
609541
func cleanupDelayedEvents(t *testing.T, user *client.CSAPI) {
610542
t.Helper()
611543
res := getDelayedEvents(t, user)
@@ -623,9 +555,5 @@ func cleanupDelayedEvents(t *testing.T, user *client.CSAPI) {
623555
)
624556
}
625557

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)
631559
}

0 commit comments

Comments
 (0)