Skip to content

Commit

Permalink
Merge pull request #123 from Allenxuxu/fix-protocol
Browse files Browse the repository at this point in the history
Fix default protocol
  • Loading branch information
Allenxuxu committed Apr 3, 2022
2 parents 9262fa1 + 2f131ab commit 3ac80c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ func (d *DefaultProtocol) UnPacket(c *Connection, buffer *ringbuffer.RingBuffer)

copy(userBuffer, s)
copy(userBuffer[len(s):], e)
buffer.RetrieveAll()

return nil, userBuffer
return nil, userBuffer[:size]
} else {
buffer.RetrieveAll()

Expand Down
36 changes: 36 additions & 0 deletions protocol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gev

import (
"testing"

"github.com/Allenxuxu/gev/eventloop"
"github.com/Allenxuxu/ringbuffer"
"github.com/stretchr/testify/assert"
)

func TestDefaultProtocol_UnPacket(t *testing.T) {
p := DefaultProtocol{}

buffer := ringbuffer.New(4)
n, err := buffer.Write([]byte("1234"))
assert.Equal(t, 4, n)
assert.Nil(t, err)

buffer.Peek(2)
buffer.Retrieve(2)
n, err = buffer.Write([]byte("ab"))
assert.Equal(t, 2, n)
assert.Nil(t, err)

_, data := p.UnPacket(newTmpConnection(), buffer)
assert.Equal(t, []byte("34ab"), data)

assert.Equal(t, 0, buffer.Length())
}

func newTmpConnection() *Connection {
lp, _ := eventloop.New()
return &Connection{
loop: lp,
}
}

0 comments on commit 3ac80c5

Please sign in to comment.