From c8acd2d7a1def1b7e239ea33262e763b46e74c06 Mon Sep 17 00:00:00 2001 From: Shivansh Vij Date: Tue, 26 Mar 2024 17:27:05 -0700 Subject: [PATCH] Updating `polyglot` version to v1.2.1 and fixing associated test cases --- async.go | 4 ++-- async_test.go | 32 +++++++++++++++++------------ client.go | 2 +- go.mod | 8 ++++---- go.sum | 14 +++++++------ pkg/packet/packet_test.go | 17 ++++++++-------- pkg/packet/pool_test.go | 17 ++++++++-------- server.go | 4 ++-- server_test.go | 42 ++++++++++++++++++++++++++++----------- sync.go | 13 ++++++------ sync_test.go | 24 +++++++++++++--------- 11 files changed, 103 insertions(+), 74 deletions(-) diff --git a/async.go b/async.go index c0f052a..6805091 100644 --- a/async.go +++ b/async.go @@ -301,7 +301,7 @@ func (c *Async) Close() error { // write packet is the internal write packet function that does not check for reserved operations. func (c *Async) writePacket(p *packet.Packet, closeOnErr bool) error { - if int(p.Metadata.ContentLength) != len(*p.Content) { + if int(p.Metadata.ContentLength) != p.Content.Len() { return InvalidContentLength } @@ -343,7 +343,7 @@ func (c *Async) writePacket(p *packet.Packet, closeOnErr bool) error { return err } if p.Metadata.ContentLength != 0 { - _, err = c.writer.Write((*p.Content)[:p.Metadata.ContentLength]) + _, err = c.writer.Write(p.Content.Bytes()[:p.Metadata.ContentLength]) if err != nil { c.Unlock() if c.closed.Load() { diff --git a/async_test.go b/async_test.go index 12923a6..ab3fb25 100644 --- a/async_test.go +++ b/async_test.go @@ -58,7 +58,7 @@ func TestNewAsync(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) data := make([]byte, packetSize) _, _ = rand.Read(data) @@ -77,8 +77,10 @@ func TestNewAsync(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(packetSize), p.Metadata.ContentLength) - assert.Equal(t, len(data), len(*p.Content)) - assert.Equal(t, polyglot.Buffer(data), *p.Content) + assert.Equal(t, len(data), p.Content.Len()) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) packet.Put(p) @@ -124,8 +126,10 @@ func TestAsyncLargeWrite(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(packetSize), p.Metadata.ContentLength) - assert.Equal(t, len(randomData[i]), len(*p.Content)) - assert.Equal(t, polyglot.Buffer(randomData[i]), *p.Content) + assert.Equal(t, len(randomData[i]), p.Content.Len()) + expected := polyglot.NewBufferFromBytes(randomData[i]) + expected.MoveOffset(len(randomData[i])) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) packet.Put(p) } @@ -172,8 +176,10 @@ func TestAsyncRawConn(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(packetSize), p.Metadata.ContentLength) - assert.Equal(t, packetSize, len(*p.Content)) - assert.Equal(t, polyglot.Buffer(randomData), *p.Content) + assert.Equal(t, packetSize, p.Content.Len()) + expected := polyglot.NewBufferFromBytes(randomData) + expected.MoveOffset(len(randomData)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) } rawReaderConn := readerConn.Raw() @@ -226,7 +232,7 @@ func TestAsyncReadClose(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) err = readerConn.conn.Close() assert.NoError(t, err) @@ -277,7 +283,7 @@ func TestAsyncReadAvailableClose(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) p, err = readerConn.ReadPacket() require.NoError(t, err) @@ -285,7 +291,7 @@ func TestAsyncReadAvailableClose(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) _, err = readerConn.ReadPacket() require.Error(t, err) @@ -324,7 +330,7 @@ func TestAsyncWriteClose(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) err = writerConn.WritePacket(p) assert.NoError(t, err) @@ -377,7 +383,7 @@ func TestAsyncTimeout(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) time.Sleep(DefaultDeadline * 2) @@ -408,7 +414,7 @@ func TestAsyncTimeout(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) _, err = readerConn.ReadPacket() require.ErrorIs(t, err, ConnectionClosed) diff --git a/client.go b/client.go index 8ddff54..f2ea027 100644 --- a/client.go +++ b/client.go @@ -192,7 +192,7 @@ func (c *Client) handleConn() { packetCtx = c.PacketContext(packetCtx, p) } outgoing, action = handlerFunc(packetCtx, p) - if outgoing != nil && outgoing.Metadata.ContentLength == uint32(len(*outgoing.Content)) { + if outgoing != nil && outgoing.Metadata.ContentLength == uint32(outgoing.Content.Len()) { err = c.conn.WritePacket(outgoing) if outgoing != p { packet.Put(outgoing) diff --git a/go.mod b/go.mod index b978bdd..8fdb910 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,10 @@ go 1.20 require ( github.com/loopholelabs/common v0.4.9 - github.com/loopholelabs/polyglot v1.1.4 + github.com/loopholelabs/polyglot v1.2.1 github.com/loopholelabs/testing v0.2.3 - github.com/rs/zerolog v1.31.0 - github.com/stretchr/testify v1.8.4 + github.com/rs/zerolog v1.32.0 + github.com/stretchr/testify v1.9.0 go.uber.org/atomic v1.11.0 go.uber.org/goleak v1.3.0 ) @@ -18,6 +18,6 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/sys v0.18.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f51ac76..267ef95 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/loopholelabs/common v0.4.9 h1:9MPUYlZZ/qx3Kt8LXgXxcSXthrM91od8026c4DlGpAU= github.com/loopholelabs/common v0.4.9/go.mod h1:Wop5srN1wYT+mdQ9gZ+kn2I9qKAyVd0FB48pThwIa9M= -github.com/loopholelabs/polyglot v1.1.4 h1:+yCGD3MQnmmX5ln+9mCbSCUgH7GQH0RxKJn/54hAaQU= -github.com/loopholelabs/polyglot v1.1.4/go.mod h1:EA88BEkIluKHAWxhyOV88xXz68YkRdo9IzZ+1dj+7Ao= +github.com/loopholelabs/polyglot v1.2.1 h1:s/QIHKadxMGBspmg6yJsGjw40SIcQ9fUaB25ocfU7Sg= +github.com/loopholelabs/polyglot v1.2.1/go.mod h1:w9F5kdv3EzE2r7scpmj29hoySRr7NNLjpeUL4ALMusA= github.com/loopholelabs/testing v0.2.3 h1:4nVuK5ctaE6ua5Z0dYk2l7xTFmcpCYLUeGjRBp8keOA= github.com/loopholelabs/testing v0.2.3/go.mod h1:gqtGY91soYD1fQoKQt/6kP14OYpS7gcbcIgq5mc9m8Q= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -22,10 +22,10 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= -github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= -github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= +github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= @@ -35,6 +35,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/pkg/packet/packet_test.go b/pkg/packet/packet_test.go index ff1df0f..9c40106 100644 --- a/pkg/packet/packet_test.go +++ b/pkg/packet/packet_test.go @@ -18,7 +18,6 @@ package packet import ( "crypto/rand" - "github.com/loopholelabs/polyglot" "github.com/stretchr/testify/assert" "testing" ) @@ -33,7 +32,7 @@ func TestNew(t *testing.T) { assert.Equal(t, uint16(0), p.Metadata.Id) assert.Equal(t, uint16(0), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, polyglot.Buffer{}, *p.Content) + assert.Equal(t, Get().Content, p.Content) Put(p) } @@ -48,12 +47,12 @@ func TestWrite(t *testing.T) { assert.NoError(t, err) p.Content.Write(b) - assert.Equal(t, polyglot.Buffer(b), *p.Content) + assert.Equal(t, b, p.Content.Bytes()) p.Reset() - assert.NotEqual(t, b, *p.Content) - assert.Equal(t, 0, len(*p.Content)) - assert.Equal(t, 512, cap(*p.Content)) + assert.NotEqual(t, b, p.Content.Bytes()) + assert.Equal(t, 0, p.Content.Len()) + assert.Equal(t, 512, p.Content.Cap()) b = make([]byte, 1024) _, err = rand.Read(b) @@ -61,8 +60,8 @@ func TestWrite(t *testing.T) { p.Content.Write(b) - assert.Equal(t, polyglot.Buffer(b), *p.Content) - assert.Equal(t, 1024, len(*p.Content)) - assert.GreaterOrEqual(t, cap(*p.Content), 1024) + assert.Equal(t, b, p.Content.Bytes()) + assert.Equal(t, 1024, p.Content.Len()) + assert.GreaterOrEqual(t, p.Content.Cap(), 1024) } diff --git a/pkg/packet/pool_test.go b/pkg/packet/pool_test.go index fad62e0..261eaeb 100644 --- a/pkg/packet/pool_test.go +++ b/pkg/packet/pool_test.go @@ -17,8 +17,7 @@ package packet import ( - "github.com/loopholelabs/polyglot" - "math/rand" + "crypto/rand" "testing" "github.com/stretchr/testify/assert" @@ -36,7 +35,7 @@ func TestRecycle(t *testing.T) { pool.Put(p) p = pool.Get() - testData := make([]byte, cap(*p.Content)*2) + testData := make([]byte, p.Content.Cap()*2) _, err := rand.Read(testData) assert.NoError(t, err) for { @@ -44,11 +43,11 @@ func TestRecycle(t *testing.T) { assert.Equal(t, uint16(0), p.Metadata.Id) assert.Equal(t, uint16(0), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, polyglot.Buffer{}, *p.Content) + assert.Equal(t, *pool.Get().Content, *p.Content) p.Content.Write(testData) - assert.Equal(t, len(testData), len(*p.Content)) - assert.GreaterOrEqual(t, cap(*p.Content), len(testData)) + assert.Equal(t, len(testData), p.Content.Len()) + assert.GreaterOrEqual(t, p.Content.Cap(), len(testData)) pool.Put(p) p = pool.Get() @@ -58,11 +57,11 @@ func TestRecycle(t *testing.T) { assert.Equal(t, uint16(0), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - if cap(*p.Content) < len(testData) { + if p.Content.Cap() < len(testData) { continue } - assert.Equal(t, 0, len(*p.Content)) - assert.GreaterOrEqual(t, cap(*p.Content), len(testData)) + assert.Equal(t, 0, p.Content.Len()) + assert.GreaterOrEqual(t, p.Content.Cap(), len(testData)) break } diff --git a/server.go b/server.go index d3cc969..6ce2b3a 100644 --- a/server.go +++ b/server.go @@ -262,7 +262,7 @@ func (s *Server) createHandler(conn *Async, closed *atomic.Bool, wg *sync.WaitGr packetCtx = s.PacketContext(packetCtx, p) } outgoing, action := handlerFunc(packetCtx, p) - if outgoing != nil && outgoing.Metadata.ContentLength == uint32(len(*outgoing.Content)) { + if outgoing != nil && outgoing.Metadata.ContentLength == uint32(outgoing.Content.Len()) { s.preWrite() err := conn.WritePacket(outgoing) if outgoing != p { @@ -317,7 +317,7 @@ func (s *Server) handleSinglePacket(frisbeeConn *Async, connCtx context.Context) packetCtx = s.PacketContext(packetCtx, p) } outgoing, action = handlerFunc(packetCtx, p) - if outgoing != nil && outgoing.Metadata.ContentLength == uint32(len(*outgoing.Content)) { + if outgoing != nil && outgoing.Metadata.ContentLength == uint32(outgoing.Content.Len()) { s.preWrite() err = frisbeeConn.WritePacket(outgoing) if outgoing != p { diff --git a/server_test.go b/server_test.go index f50f330..6fdb1ce 100644 --- a/server_test.go +++ b/server_test.go @@ -97,7 +97,9 @@ func TestServerRawSingle(t *testing.T) { p.Content.Write(data) p.Metadata.ContentLength = packetSize p.Metadata.Operation = metadata.PacketPing - assert.Equal(t, polyglot.Buffer(data), *p.Content) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) for q := 0; q < testSize; q++ { p.Metadata.Id = uint16(q) @@ -106,7 +108,7 @@ func TestServerRawSingle(t *testing.T) { } p.Reset() - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) p.Metadata.Operation = metadata.PacketProbe err = c.WritePacket(p) @@ -191,7 +193,9 @@ func TestServerStaleCloseSingle(t *testing.T) { p.Content.Write(data) p.Metadata.ContentLength = packetSize p.Metadata.Operation = metadata.PacketPing - assert.Equal(t, polyglot.Buffer(data), *p.Content) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) for q := 0; q < testSize; q++ { p.Metadata.Id = uint16(q) @@ -280,7 +284,9 @@ func TestServerMultipleConnectionsSingle(t *testing.T) { p.Content.Write(data) p.Metadata.ContentLength = packetSize p.Metadata.Operation = metadata.PacketPing - assert.Equal(t, polyglot.Buffer(data), *p.Content) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) for q := 0; q < testSize; q++ { p.Metadata.Id = uint16(q) err := clients[idx].WritePacket(p) @@ -366,7 +372,9 @@ func TestServerRawUnlimited(t *testing.T) { p.Content.Write(data) p.Metadata.ContentLength = packetSize p.Metadata.Operation = metadata.PacketPing - assert.Equal(t, polyglot.Buffer(data), *p.Content) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) for q := 0; q < testSize; q++ { p.Metadata.Id = uint16(q) @@ -375,7 +383,7 @@ func TestServerRawUnlimited(t *testing.T) { } p.Reset() - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) p.Metadata.Operation = metadata.PacketProbe err = c.WritePacket(p) @@ -462,7 +470,9 @@ func TestServerStaleCloseUnlimited(t *testing.T) { p.Content.Write(data) p.Metadata.ContentLength = packetSize p.Metadata.Operation = metadata.PacketPing - assert.Equal(t, polyglot.Buffer(data), *p.Content) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) for q := 0; q < testSize; q++ { p.Metadata.Id = uint16(q) @@ -558,7 +568,9 @@ func TestServerMultipleConnectionsUnlimited(t *testing.T) { p.Metadata.ContentLength = packetSize p.Metadata.Operation = metadata.PacketPing p.Metadata.Id = uint16(idx) - assert.Equal(t, polyglot.Buffer(data), *p.Content) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) for q := 0; q < testSize; q++ { err := clients[idx].WritePacket(p) assert.NoError(t, err) @@ -643,7 +655,9 @@ func TestServerRawLimited(t *testing.T) { p.Content.Write(data) p.Metadata.ContentLength = packetSize p.Metadata.Operation = metadata.PacketPing - assert.Equal(t, polyglot.Buffer(data), *p.Content) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) for q := 0; q < testSize; q++ { p.Metadata.Id = uint16(q) @@ -652,7 +666,7 @@ func TestServerRawLimited(t *testing.T) { } p.Reset() - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) p.Metadata.Operation = metadata.PacketProbe err = c.WritePacket(p) @@ -739,7 +753,9 @@ func TestServerStaleCloseLimited(t *testing.T) { p.Content.Write(data) p.Metadata.ContentLength = packetSize p.Metadata.Operation = metadata.PacketPing - assert.Equal(t, polyglot.Buffer(data), *p.Content) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) for q := 0; q < testSize; q++ { p.Metadata.Id = uint16(q) @@ -836,7 +852,9 @@ func TestServerMultipleConnectionsLimited(t *testing.T) { p.Metadata.ContentLength = packetSize p.Metadata.Operation = metadata.PacketPing p.Metadata.Id = uint16(idx) - assert.Equal(t, polyglot.Buffer(data), *p.Content) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) for q := 0; q < testSize; q++ { err := clients[idx].WritePacket(p) assert.NoError(t, err) diff --git a/sync.go b/sync.go index 9e58809..d426fd4 100644 --- a/sync.go +++ b/sync.go @@ -141,7 +141,7 @@ func (c *Sync) RemoteAddr() net.Addr { // // If packet.Metadata.ContentLength == 0, then the content array must be nil. Otherwise, it is required that packet.Metadata.ContentLength == len(content). func (c *Sync) WritePacket(p *packet.Packet) error { - if int(p.Metadata.ContentLength) != len(*p.Content) { + if int(p.Metadata.ContentLength) != p.Content.Len() { return InvalidContentLength } @@ -168,7 +168,7 @@ func (c *Sync) WritePacket(p *packet.Packet) error { return c.closeWithError(err) } if p.Metadata.ContentLength != 0 { - _, err = c.conn.Write((*p.Content)[:p.Metadata.ContentLength]) + _, err = c.conn.Write(p.Content.Bytes()[:p.Metadata.ContentLength]) if err != nil { c.Unlock() if c.closed.Load() { @@ -208,11 +208,10 @@ func (c *Sync) ReadPacket() (*packet.Packet, error) { p.Metadata.ContentLength = binary.BigEndian.Uint32(encodedPacket[metadata.ContentLengthOffset : metadata.ContentLengthOffset+metadata.ContentLengthSize]) if p.Metadata.ContentLength > 0 { - for cap(*p.Content) < int(p.Metadata.ContentLength) { - *p.Content = append((*p.Content)[:cap(*p.Content)], 0) - } - *p.Content = (*p.Content)[:p.Metadata.ContentLength] - _, err = io.ReadAtLeast(c.conn, *p.Content, int(p.Metadata.ContentLength)) + contentLength := int(p.Metadata.ContentLength) + p.Content.Grow(contentLength) + p.Content.MoveOffset(contentLength) + _, err = io.ReadAtLeast(c.conn, p.Content.Bytes(), contentLength) if err != nil { if c.closed.Load() { c.Logger().Debug().Err(ConnectionClosed).Msg("error while reading from underlying net.Conn") diff --git a/sync_test.go b/sync_test.go index 05ddcf2..4bbdadd 100644 --- a/sync_test.go +++ b/sync_test.go @@ -55,7 +55,7 @@ func TestNewSync(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) end <- struct{}{} packet.Put(p) }() @@ -79,8 +79,10 @@ func TestNewSync(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(packetSize), p.Metadata.ContentLength) - assert.Equal(t, packetSize, len(*p.Content)) - assert.Equal(t, polyglot.Buffer(data), *p.Content) + assert.Equal(t, packetSize, p.Content.Len()) + expected := polyglot.NewBufferFromBytes(data) + expected.MoveOffset(len(data)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) end <- struct{}{} packet.Put(p) }() @@ -130,8 +132,10 @@ func TestSyncLargeWrite(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(packetSize), p.Metadata.ContentLength) - assert.Equal(t, packetSize, len(*p.Content)) - assert.Equal(t, polyglot.Buffer(randomData[i]), *p.Content) + assert.Equal(t, packetSize, p.Content.Len()) + expected := polyglot.NewBufferFromBytes(randomData[i]) + expected.MoveOffset(len(randomData[i])) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) packet.Put(p) } end <- struct{}{} @@ -191,8 +195,10 @@ func TestSyncRawConn(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(packetSize), p.Metadata.ContentLength) - assert.Equal(t, packetSize, len(*p.Content)) - assert.Equal(t, polyglot.Buffer(randomData), *p.Content) + assert.Equal(t, packetSize, p.Content.Len()) + expected := polyglot.NewBufferFromBytes(randomData) + expected.MoveOffset(len(randomData)) + assert.Equal(t, expected.Bytes(), p.Content.Bytes()) packet.Put(p) } end <- struct{}{} @@ -255,7 +261,7 @@ func TestSyncReadClose(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) end <- struct{}{} packet.Put(p) }() @@ -305,7 +311,7 @@ func TestSyncWriteClose(t *testing.T) { assert.Equal(t, uint16(64), p.Metadata.Id) assert.Equal(t, uint16(32), p.Metadata.Operation) assert.Equal(t, uint32(0), p.Metadata.ContentLength) - assert.Equal(t, 0, len(*p.Content)) + assert.Equal(t, 0, p.Content.Len()) packet.Put(p) end <- struct{}{} }()