Skip to content

Commit

Permalink
reader: ignore empty messages (#81)
Browse files Browse the repository at this point in the history
Co-authored-by: Kaleb Elwert <[email protected]>
  • Loading branch information
hhirtz and belak authored Jan 19, 2021
1 parent db51cb8 commit 81824d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
30 changes: 19 additions & 11 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,24 @@ func NewReader(r io.Reader) *Reader {
}

// ReadMessage returns the next message from the stream or an error.
func (r *Reader) ReadMessage() (*Message, error) {
line, err := r.reader.ReadString('\n')
if err != nil {
return nil, err
// It ignores empty messages.
func (r *Reader) ReadMessage() (msg *Message, err error) {
// It's valid for a message to be empty. Clients should ignore these,
// so we do to be good citizens.
err = ErrZeroLengthMessage
for err == ErrZeroLengthMessage {
var line string
line, err = r.reader.ReadString('\n')
if err != nil {
return nil, err
}

if r.DebugCallback != nil {
r.DebugCallback(line)
}

// Parse the message from our line
msg, err = ParseMessage(line)
}

if r.DebugCallback != nil {
r.DebugCallback(line)
}

// Parse the message from our line
return ParseMessage(line)
return msg, err
}
6 changes: 6 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ func TestConn(t *testing.T) {
_, err := c.ReadMessage()
assert.Equal(t, ErrMissingDataAfterPrefix, err)

// Ensure empty messages are ignored
m = MustParseMessage("001 test_nick")
rwc.server.WriteString("\r\n" + m.String() + "\r\n")
m2 = testReadMessage(t, c)
assert.EqualValues(t, m, m2, "Message returned by client did not match input")

// This is an odd one... if there wasn't any output, it'll hit
// EOF, so we expect an error here so we can test an error
// condition.
Expand Down

0 comments on commit 81824d7

Please sign in to comment.