Skip to content

Commit

Permalink
refactor(acceptor): check if the header sizer is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcorado committed Dec 10, 2020
1 parent 7cb7a91 commit eb8d352
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions acceptor/tcp_acceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (t *tcpPlayerConn) GetNextMessage() (b []byte, err error) {
if err != nil {
return nil, err
}
// if the header has no data, we can consider it as a closed connection
if len(header) == 0 {
return nil, constants.ErrConnectionClosed
}
msgSize, _, err := codec.ParseHeader(header)
if err != nil {
return nil, err
Expand Down
23 changes: 23 additions & 0 deletions acceptor/tcp_acceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,30 @@ func TestGetNextMessageEOF(t *testing.T) {

_, err = playerConn.GetNextMessage()
assert.EqualError(t, err, constants.ErrReceivedMsgSmallerThanExpected.Error())
}

func TestGetNextMessageEmptyEOF(t *testing.T) {
a := NewTCPAcceptor("0.0.0.0:0")
go a.ListenAndServe()
defer a.Stop()
c := a.GetConnChan()
// should be able to connect within 100 milliseconds
var conn net.Conn
var err error
helpers.ShouldEventuallyReturn(t, func() error {
conn, err = net.Dial("tcp", a.GetAddr())
return err
}, nil, 10*time.Millisecond, 100*time.Millisecond)

playerConn := helpers.ShouldEventuallyReceive(t, c, 100*time.Millisecond).(PlayerConn)

go func() {
time.Sleep(100 * time.Millisecond)
conn.Close()
}()

_, err = playerConn.GetNextMessage()
assert.EqualError(t, err, constants.ErrConnectionClosed.Error())
}

func TestGetNextMessageInParts(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ var (
ErrRateLimitExceeded = errors.New("rate limit exceeded")
ErrReceivedMsgSmallerThanExpected = errors.New("received less data than expected, EOF?")
ErrReceivedMsgBiggerThanExpected = errors.New("received more data than expected")
ErrConnectionClosed = errors.New("client connection closed")
)

0 comments on commit eb8d352

Please sign in to comment.