Skip to content

Commit

Permalink
Retry /publicRooms a few times to allow for propagation delay before …
Browse files Browse the repository at this point in the history
…a new room appears in the room directory (#415)
  • Loading branch information
reivilibre authored Jul 28, 2022
1 parent 14c156d commit 73de567
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 52 deletions.
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
}),
},
})

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

0 comments on commit 73de567

Please sign in to comment.