forked from siderolabs/grpc-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec_test.go
73 lines (56 loc) · 1.92 KB
/
codec_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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) {
d := []byte{0xDE, 0xAD, 0xBE, 0xEF}
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()
buffer := mem.Copy(val, mem.DefaultBufferPool())
defer func() { buffer.Free() }()
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")
out.Free()
buffer.Free()
buffer = mem.Copy([]byte{0x55}, mem.DefaultBufferPool())
// 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")
})
}
}