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

Retry /publicRooms a few times to allow for propagation delay before a new room appears in the room directory #415

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
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
48 changes: 28 additions & 20 deletions tests/csapi/apidoc_room_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package csapi_tests
import (
"net/url"
"testing"
"time"

"github.com/tidwall/gjson"

"github.com/matrix-org/complement/internal/b"
"github.com/matrix-org/complement/internal/client"
"github.com/matrix-org/complement/internal/match"
"github.com/matrix-org/complement/internal/must"

"net/http"
)

func TestRoomState(t *testing.T) {
Expand Down Expand Up @@ -104,26 +107,31 @@ func TestRoomState(t *testing.T) {
"preset": "public_chat",
})

res := authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "publicRooms"})
foundRoom := false

must.MatchResponse(t, res, match.HTTPResponse{
JSON: []match.JSON{
match.JSONKeyPresent("chunk"),
match.JSONArrayEach("chunk", func(val gjson.Result) error {
gotRoomID := val.Get("room_id").Str
if gotRoomID == roomID {
foundRoom = true
return nil
}
return nil
}),
},
})

if !foundRoom {
t.Errorf("failed to find room with id: %s", roomID)
}
authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "publicRooms"},
client.WithRetryUntil(time.Second, func(res *http.Response) bool {
foundRoom := false

must.MatchResponse(t, res, match.HTTPResponse{
JSON: []match.JSON{
match.JSONKeyPresent("chunk"),
match.JSONArrayEach("chunk", func(val gjson.Result) error {
gotRoomID := val.Get("room_id").Str
if gotRoomID == roomID {
foundRoom = true
return nil
}
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a slight misuse of matchers, given you always return nil it will always succeed even if the room isn't found, and then you side-effect foundRoom to determine retry-ness. It's not something that the matchers were designed for, but in practice it seems to read pretty well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, though I will say 'don't blame me, it was there before!' :-P

I suspect this sort of pattern will crop up again, so if you get any better ideas then it may be worth putting them in

}),
},
})

if !foundRoom {
t.Logf("failed to find room with id: %s", roomID)
}

return foundRoom
}),
)
})
// sytest: GET /directory/room/:room_alias yields room ID
t.Run("GET /directory/room/:room_alias yields room ID", func(t *testing.T) {
Expand Down
64 changes: 32 additions & 32 deletions tests/knocking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"testing"
"time"

"net/http"

"github.com/matrix-org/gomatrixserverlib"
"github.com/tidwall/gjson"

Expand Down Expand Up @@ -433,39 +435,37 @@ func publishAndCheckRoomJoinRule(t *testing.T, c *client.CSAPI, roomID, expected
)

// Check that we can see the room in the directory
res := c.MustDo(
t,
"GET",
[]string{"_matrix", "client", "v3", "publicRooms"},
nil,
)

roomFound := false
must.MatchResponse(t, res, match.HTTPResponse{
JSON: []match.JSON{
// For each public room directory chunk (representing a single room entry)
match.JSONArrayEach("chunk", func(r gjson.Result) error {
// If this is our room
if r.Get("room_id").Str == roomID {
roomFound = true

// Check that the join_rule key exists and is as we expect
if roomJoinRule := r.Get("join_rule").Str; roomJoinRule != expectedJoinRule {
return fmt.Errorf(
"'join_rule' key for room in public room chunk is '%s', expected '%s'",
roomJoinRule, expectedJoinRule,
)
}
}
return nil
}),
},
})
c.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "publicRooms"},
client.WithRetryUntil(time.Second, func(res *http.Response) bool {
roomFound := false
must.MatchResponse(t, res, match.HTTPResponse{
JSON: []match.JSON{
// For each public room directory chunk (representing a single room entry)
match.JSONArrayEach("chunk", func(r gjson.Result) error {
// If this is our room
if r.Get("room_id").Str == roomID {
roomFound = true

// Check that the join_rule key exists and is as we expect
if roomJoinRule := r.Get("join_rule").Str; roomJoinRule != expectedJoinRule {
return fmt.Errorf(
"'join_rule' key for room in public room chunk is '%s', expected '%s'",
roomJoinRule, expectedJoinRule,
)
}
}
return nil
}),
},
})

// Check that we did in fact see the room
if !roomFound {
t.Fatalf("Room was not present in public room directory response")
}
// Check that we did in fact see the room
if !roomFound {
t.Logf("Room was not present in public room directory response")
}
return roomFound
}),
)
}

// TestCannotSendNonKnockViaSendKnock checks that we cannot submit anything via /send_knock except a knock
Expand Down