-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🤦 I forgot to actually copy data from the frame on `len(payload) > bufferPoolingThreshold`. Also add test to ensure that this works correctly. Signed-off-by: Dmitriy Matrenichev <[email protected]>
- Loading branch information
Showing
4 changed files
with
59 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,73 @@ | ||
package proxy_test | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/mem" | ||
|
||
"github.com/siderolabs/grpc-proxy/proxy" | ||
talos_testproto "github.com/siderolabs/grpc-proxy/testservice" | ||
) | ||
|
||
func TestCodec_ReadYourWrites(t *testing.T) { | ||
framePtr := proxy.NewFrame(nil) | ||
data := []byte{0xDE, 0xAD, 0xBE, 0xEF} | ||
codec := proxy.Codec() | ||
d := []byte{0xDE, 0xAD, 0xBE, 0xEF} | ||
|
||
buffer := mem.Copy(data, mem.DefaultBufferPool()) | ||
defer buffer.Free() | ||
for key, val := range map[string][]byte{ | ||
"short message": d, | ||
"long message": bytes.Repeat(d, 3072), | ||
} { | ||
t.Run(key, func(t *testing.T) { | ||
framePtr := proxy.NewFrame(nil) | ||
codec := proxy.Codec() | ||
|
||
require.NoError(t, codec.Unmarshal(mem.BufferSlice{buffer}, framePtr), "unmarshalling must go ok") | ||
out, err := codec.Marshal(framePtr) | ||
require.NoError(t, err, "no marshal error") | ||
require.Equal(t, data, out.Materialize(), "output and data must be the same") | ||
buffer := mem.Copy(val, mem.DefaultBufferPool()) | ||
defer func() { buffer.Free() }() | ||
|
||
out.Free() | ||
buffer.Free() | ||
buffer = mem.Copy([]byte{0x55}, mem.DefaultBufferPool()) | ||
require.NoError(t, codec.Unmarshal(mem.BufferSlice{buffer}, framePtr), "unmarshalling must go ok") | ||
out, err := codec.Marshal(framePtr) | ||
require.NoError(t, err, "no marshal error") | ||
require.Equal(t, val, out.Materialize(), "output and data must be the same") | ||
|
||
// reuse | ||
require.NoError(t, codec.Unmarshal(mem.BufferSlice{buffer}, framePtr), "unmarshalling must go ok") | ||
out, err = codec.Marshal(framePtr) | ||
require.NoError(t, err, "no marshal error") | ||
require.Equal(t, []byte{0x55}, out.Materialize(), "output and data must be the same") | ||
out.Free() | ||
buffer.Free() | ||
buffer = mem.Copy([]byte{0x55}, mem.DefaultBufferPool()) | ||
|
||
out.Free() | ||
// reuse | ||
require.NoError(t, codec.Unmarshal(mem.BufferSlice{buffer}, framePtr), "unmarshalling must go ok") | ||
out, err = codec.Marshal(framePtr) | ||
require.NoError(t, err, "no marshal error") | ||
require.Equal(t, []byte{0x55}, out.Materialize(), "output and data must be the same") | ||
|
||
out.Free() | ||
}) | ||
} | ||
} | ||
|
||
func TestCodecUsualMessage(t *testing.T) { | ||
const msg = "short message" | ||
|
||
for key, val := range map[string]string{ | ||
"short message": "edbca", | ||
"long message": strings.Repeat(msg, 3072), | ||
} { | ||
t.Run(key, func(t *testing.T) { | ||
msg := &talos_testproto.PingRequest{Value: val} | ||
|
||
codec := proxy.Codec() | ||
|
||
out, err := codec.Marshal(msg) | ||
require.NoError(t, err, "no marshal error") | ||
|
||
defer out.Free() | ||
|
||
var dst talos_testproto.PingRequest | ||
|
||
require.NoError(t, codec.Unmarshal(out, &dst), "unmarshalling must go ok") | ||
require.NotZero(t, dst.Value, "output must not be zero") | ||
require.Equal(t, msg.Value, dst.Value, "output and data must be the same") | ||
}) | ||
} | ||
} |