Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to MSC4140 tests #737

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 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 @@ -85,24 +87,11 @@ func TestDelayedEvents(t *testing.T) {
})
}

checkContent := func(val gjson.Result) error {
content := val.Get("content").Map()
if l := len(content); l != 1 {
return fmt.Errorf("wrong number of content fields: expected 1, got %d", l)
}
countExpected++
if countActual := content[countKey].Uint(); countActual != countExpected {
return fmt.Errorf("wrong count in delayed event content: expected %v, got %v", countExpected, countActual)
}
return nil
}

res = getDelayedEvents(t, user)
countExpected = 0
must.MatchResponse(t, res, match.HTTPResponse{
JSON: []match.JSON{
match.JSONKeyArrayOfSize("delayed_events", numEvents),
match.JSONArrayEach("delayed_events", checkContent),
},
})

Expand All @@ -129,14 +118,26 @@ func TestDelayedEvents(t *testing.T) {
countExpected = 0
must.MatchResponse(t, res, match.HTTPResponse{
JSON: []match.JSON{
match.JSONArrayEach("chunk", checkContent),
match.JSONArrayEach("chunk", func(val gjson.Result) error {
content := val.Get("content").Map()
if l := len(content); l != 1 {
return fmt.Errorf("wrong number of content fields: expected 1, got %d", l)
}
countExpected++
if countActual := content[countKey].Uint(); countActual != countExpected {
return fmt.Errorf("wrong count in delayed event content: expected %v, got %v", countExpected, countActual)
}
return nil
}),
},
})
})

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 +310,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 +366,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 +434,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 +483,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 +534,8 @@ func TestDelayedEvents(t *testing.T) {

var res *http.Response

defer cleanupDelayedEvents(t, user)

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

Expand Down Expand Up @@ -593,3 +605,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),
},
})
}
Loading