Skip to content

Commit

Permalink
Clean up leftover delayed events after each test
Browse files Browse the repository at this point in the history
in case a test fails & leaves delayed events behind, which would
interfere with a later test.
  • Loading branch information
AndrewFerr committed Sep 18, 2024
1 parent b0bc680 commit 6f86880
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/msc4140/delayed_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func TestDelayedEvents(t *testing.T) {

_, token := user.MustSync(t, client.SyncReq{})

defer cleanupDelayedEvents(t, user)

txnIdBase := "txn-delayed-msg-timeout-%d"

countKey := "count"
Expand Down Expand Up @@ -137,6 +139,8 @@ func TestDelayedEvents(t *testing.T) {
t.Run("delayed state events are sent on timeout", func(t *testing.T) {
var res *http.Response

defer cleanupDelayedEvents(t, user)

stateKey := "to_send_on_timeout"

setterKey := "setter"
Expand Down Expand Up @@ -309,6 +313,8 @@ func TestDelayedEvents(t *testing.T) {
t.Run("delayed state events can be sent on request", func(t *testing.T) {
var res *http.Response

defer cleanupDelayedEvents(t, user)

stateKey := "to_send_on_request"

setterKey := "setter"
Expand Down Expand Up @@ -363,6 +369,8 @@ func TestDelayedEvents(t *testing.T) {

stateKey := "to_send_on_restarted_timeout"

defer cleanupDelayedEvents(t, user)

setterKey := "setter"
setterExpected := "on_timeout"
res = user.MustDo(
Expand Down Expand Up @@ -429,6 +437,8 @@ func TestDelayedEvents(t *testing.T) {

stateKey := "to_be_cancelled_by_same_user"

defer cleanupDelayedEvents(t, user)

setterKey := "setter"
user.MustDo(
t,
Expand Down Expand Up @@ -476,6 +486,9 @@ func TestDelayedEvents(t *testing.T) {

stateKey := "to_be_cancelled_by_other_user"

defer cleanupDelayedEvents(t, user)
defer cleanupDelayedEvents(t, user2)

setterKey := "setter"
user.MustDo(
t,
Expand Down Expand Up @@ -524,6 +537,8 @@ func TestDelayedEvents(t *testing.T) {

var res *http.Response

defer cleanupDelayedEvents(t, user)

stateKey1 := "1"
stateKey2 := "2"

Expand Down Expand Up @@ -593,3 +608,27 @@ func getDelayedEvents(t *testing.T, user *client.CSAPI) *http.Response {
t.Helper()
return user.MustDo(t, "GET", getPathForUpdateDelayedEvents())
}

func cleanupDelayedEvents(t *testing.T, user *client.CSAPI) {
t.Helper()
res := getDelayedEvents(t, user)
defer res.Body.Close()
body := must.ParseJSON(t, res.Body)
for _, delayedEvent := range body.Get("delayed_events").Array() {
delayID := delayedEvent.Get("delay_id").String()
user.MustDo(
t,
"POST",
append(getPathForUpdateDelayedEvents(), delayID),
client.WithJSONBody(t, map[string]interface{}{
"action": "cancel",
}),
)
}

must.MatchResponse(t, getDelayedEvents(t, user), match.HTTPResponse{
JSON: []match.JSON{
match.JSONKeyArrayOfSize("delayed_events", 0),
},
})
}

0 comments on commit 6f86880

Please sign in to comment.