Skip to content

Commit 8744466

Browse files
authored
test: refactor test suite to (hopefully) fix flakiness (#65)
A major test suite refactor that attempts to address two possible sources of flakiness: - use of a throwaway bufio.Reader when completing client handshake, which could potentially consume websocket frame bytes if written by the server too quickly. fix here is to wrap the conn such that buffered bytes are still available after the handshake is completed. See newConnWithBufferedReader. - rewriting all client/server tests to set up dedicated, separate client and server conns instead of setting up a single conn and "simulating" client/server reads/writes to that shared conn. Unfortunately, this has not addressed all flakiness, but based on CI runs it does seem to have improved things?
1 parent ca6c1e4 commit 8744466

File tree

3 files changed

+682
-639
lines changed

3 files changed

+682
-639
lines changed

proto.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,14 @@ type Opcode uint8
1818
// See the RFC for the set of defined opcodes:
1919
// https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
2020
const (
21-
OpcodeContinuation Opcode = 0b0000_0000 // 0x0
22-
OpcodeText Opcode = 0b0000_0001 // 0x1
23-
OpcodeBinary Opcode = 0b0000_0010 // 0x2
24-
OpcodeClose Opcode = 0b0000_1000 // 0x8
25-
OpcodePing Opcode = 0b0000_1001 // 0x9
26-
OpcodePong Opcode = 0b0000_1010 // 0xA
21+
OpcodeContinuation Opcode = 0x0
22+
OpcodeText Opcode = 0x1
23+
OpcodeBinary Opcode = 0x2
24+
OpcodeClose Opcode = 0x8
25+
OpcodePing Opcode = 0x9
26+
OpcodePong Opcode = 0xA
2727
)
2828

29-
func (c Opcode) String() string {
30-
switch c {
31-
case OpcodeContinuation:
32-
return "Cont"
33-
case OpcodeText:
34-
return "Text"
35-
case OpcodeBinary:
36-
return "Binary"
37-
case OpcodeClose:
38-
return "Close"
39-
case OpcodePing:
40-
return "Ping"
41-
case OpcodePong:
42-
return "Pong"
43-
default:
44-
panic(fmt.Sprintf("unknown opcode: %#v", c))
45-
}
46-
}
47-
4829
// StatusCode is a websocket close status code.
4930
type StatusCode uint16
5031

websocket.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,18 @@ func (ws *Websocket) ReadMessage(ctx context.Context) (*Message, error) {
201201
switch opcode {
202202
case OpcodeBinary, OpcodeText:
203203
if msg != nil {
204-
return nil, ws.startCloseOnReadError(ErrContinuationExpected)
204+
return nil, ws.closeImmediately(ErrContinuationExpected)
205205
}
206206
msg = &Message{
207207
Binary: opcode == OpcodeBinary,
208208
Payload: frame.Payload,
209209
}
210210
case OpcodeContinuation:
211211
if msg == nil {
212-
return nil, ws.startCloseOnReadError(ErrContinuationUnexpected)
212+
return nil, ws.closeImmediately(ErrContinuationUnexpected)
213213
}
214214
if len(msg.Payload)+len(frame.Payload) > ws.maxMessageSize {
215-
return nil, ws.startCloseOnReadError(ErrMessageTooLarge)
215+
return nil, ws.closeImmediately(ErrMessageTooLarge)
216216
}
217217
msg.Payload = append(msg.Payload, frame.Payload...)
218218
case OpcodeClose:
@@ -234,7 +234,7 @@ func (ws *Websocket) ReadMessage(ctx context.Context) (*Message, error) {
234234

235235
if frame.Fin() {
236236
if !msg.Binary && !utf8.Valid(msg.Payload) {
237-
return nil, ws.startCloseOnReadError(ErrInvalidFramePayload)
237+
return nil, ws.closeImmediately(ErrInvalidFramePayload)
238238
}
239239
ws.hooks.OnReadMessage(ws.clientKey, msg)
240240
return msg, nil
@@ -342,11 +342,6 @@ func (ws *Websocket) startCloseOnError(cause error) error {
342342
return ws.doCloseHandshake(closeFrame, cause)
343343
}
344344

345-
func (ws *Websocket) startCloseOnReadError(err error) error {
346-
ws.hooks.OnReadError(ws.clientKey, err)
347-
return ws.startCloseOnError(err)
348-
}
349-
350345
func (ws *Websocket) startCloseOnWriteError(err error) error {
351346
ws.hooks.OnWriteError(ws.clientKey, err)
352347
return ws.startCloseOnError(err)

0 commit comments

Comments
 (0)