Skip to content

Commit eab5c92

Browse files
authored
rpc: refactor read limit test (#32494)
closes #32240 #32232 The main cause for the time out is the slow json encoding of large data. In #32240 they tried to resolve the issue by reducing the size of the test. However as Felix pointed out, the test is still kind of confusing. I've refactored the test so it is more understandable and have reduced the amount of data needed to be json encoded. I think it is still important to ensure that the default read limit is not active, so I have retained one large (~32 MB) test case, but it's at least smaller than the existing ~64 MB test case.
1 parent 95ab643 commit eab5c92

File tree

1 file changed

+36
-43
lines changed

1 file changed

+36
-43
lines changed

rpc/websocket_test.go

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -121,56 +121,49 @@ func TestWebsocketLargeRead(t *testing.T) {
121121
srv = newTestServer()
122122
httpsrv = httptest.NewServer(srv.WebsocketHandler([]string{"*"}))
123123
wsURL = "ws:" + strings.TrimPrefix(httpsrv.URL, "http:")
124+
buffer = 64
124125
)
125126
defer srv.Stop()
126127
defer httpsrv.Close()
127128

128-
testLimit := func(limit *int64) {
129-
opts := []ClientOption{}
130-
expLimit := int64(wsDefaultReadLimit)
131-
if limit != nil && *limit >= 0 {
132-
opts = append(opts, WithWebsocketMessageSizeLimit(*limit))
133-
if *limit > 0 {
134-
expLimit = *limit // 0 means infinite
129+
for _, tt := range []struct {
130+
size int
131+
limit int
132+
err bool
133+
}{
134+
{200, 200, false}, // Small, successful request and limit
135+
{2048, 1024, true}, // Normal, failed request
136+
{wsDefaultReadLimit + buffer, 0, false}, // Large, successful request, infinite limit
137+
} {
138+
func() {
139+
if tt.limit != 0 {
140+
// Some buffer is added to the limit to account for JSON encoding. It's
141+
// skipped when the limit is zero since the intention is for the limit
142+
// to be infinite.
143+
tt.limit += buffer
135144
}
136-
}
137-
client, err := DialOptions(context.Background(), wsURL, opts...)
138-
if err != nil {
139-
t.Fatalf("can't dial: %v", err)
140-
}
141-
defer client.Close()
142-
// Remove some bytes for json encoding overhead.
143-
underLimit := int(expLimit - 128)
144-
overLimit := expLimit + 1
145-
if expLimit == wsDefaultReadLimit {
146-
// No point trying the full 32MB in tests. Just sanity-check that
147-
// it's not obviously limited.
148-
underLimit = 1024
149-
overLimit = -1
150-
}
151-
var res string
152-
// Check under limit
153-
if err = client.Call(&res, "test_repeat", "A", underLimit); err != nil {
154-
t.Fatalf("unexpected error with limit %d: %v", expLimit, err)
155-
}
156-
if len(res) != underLimit || strings.Count(res, "A") != underLimit {
157-
t.Fatal("incorrect data")
158-
}
159-
// Check over limit
160-
if overLimit > 0 {
161-
err = client.Call(&res, "test_repeat", "A", expLimit+1)
162-
if err == nil || err != websocket.ErrReadLimit {
163-
t.Fatalf("wrong error with limit %d: %v expecting %v", expLimit, err, websocket.ErrReadLimit)
145+
opts := []ClientOption{WithWebsocketMessageSizeLimit(int64(tt.limit))}
146+
client, err := DialOptions(context.Background(), wsURL, opts...)
147+
if err != nil {
148+
t.Fatalf("failed to dial test server: %v", err)
164149
}
165-
}
166-
}
167-
ptr := func(v int64) *int64 { return &v }
150+
defer client.Close()
168151

169-
testLimit(ptr(-1)) // Should be ignored (use default)
170-
testLimit(ptr(0)) // Should be ignored (use default)
171-
testLimit(nil) // Should be ignored (use default)
172-
testLimit(ptr(200))
173-
testLimit(ptr(wsDefaultReadLimit * 2))
152+
var res string
153+
err = client.Call(&res, "test_repeat", "A", tt.size)
154+
if tt.err && err == nil {
155+
t.Fatalf("expected error, got none")
156+
}
157+
if !tt.err {
158+
if err != nil {
159+
t.Fatalf("unexpected error with limit %d: %v", tt.limit, err)
160+
}
161+
if strings.Count(res, "A") != tt.size {
162+
t.Fatal("incorrect data")
163+
}
164+
}
165+
}()
166+
}
174167
}
175168

176169
func TestWebsocketPeerInfo(t *testing.T) {

0 commit comments

Comments
 (0)