diff --git a/tests/msc4140/delayed_event_test.go b/tests/msc4140/delayed_event_test.go index cf81bf83..7c100c4e 100644 --- a/tests/msc4140/delayed_event_test.go +++ b/tests/msc4140/delayed_event_test.go @@ -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" @@ -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" @@ -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" @@ -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( @@ -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, @@ -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, @@ -524,6 +537,8 @@ func TestDelayedEvents(t *testing.T) { var res *http.Response + defer cleanupDelayedEvents(t, user) + stateKey1 := "1" stateKey2 := "2" @@ -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), + }, + }) +}