|
1 | 1 | package proxy_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "strings" |
4 | 6 | "testing"
|
5 | 7 |
|
6 | 8 | "github.com/stretchr/testify/require"
|
7 | 9 | "google.golang.org/grpc/mem"
|
8 | 10 |
|
9 | 11 | "github.com/siderolabs/grpc-proxy/proxy"
|
| 12 | + talos_testproto "github.com/siderolabs/grpc-proxy/testservice" |
10 | 13 | )
|
11 | 14 |
|
12 | 15 | func TestCodec_ReadYourWrites(t *testing.T) {
|
13 |
| - framePtr := proxy.NewFrame(nil) |
14 |
| - data := []byte{0xDE, 0xAD, 0xBE, 0xEF} |
15 |
| - codec := proxy.Codec() |
| 16 | + d := []byte{0xDE, 0xAD, 0xBE, 0xEF} |
16 | 17 |
|
17 |
| - buffer := mem.Copy(data, mem.DefaultBufferPool()) |
18 |
| - defer buffer.Free() |
| 18 | + for key, val := range map[string][]byte{ |
| 19 | + "short message": d, |
| 20 | + "long message": bytes.Repeat(d, 3072), |
| 21 | + } { |
| 22 | + t.Run(key, func(t *testing.T) { |
| 23 | + framePtr := proxy.NewFrame(nil) |
| 24 | + codec := proxy.Codec() |
19 | 25 |
|
20 |
| - require.NoError(t, codec.Unmarshal(mem.BufferSlice{buffer}, framePtr), "unmarshalling must go ok") |
21 |
| - out, err := codec.Marshal(framePtr) |
22 |
| - require.NoError(t, err, "no marshal error") |
23 |
| - require.Equal(t, data, out.Materialize(), "output and data must be the same") |
| 26 | + buffer := mem.Copy(val, mem.DefaultBufferPool()) |
| 27 | + defer func() { buffer.Free() }() |
24 | 28 |
|
25 |
| - out.Free() |
26 |
| - buffer.Free() |
27 |
| - buffer = mem.Copy([]byte{0x55}, mem.DefaultBufferPool()) |
| 29 | + require.NoError(t, codec.Unmarshal(mem.BufferSlice{buffer}, framePtr), "unmarshalling must go ok") |
| 30 | + out, err := codec.Marshal(framePtr) |
| 31 | + require.NoError(t, err, "no marshal error") |
| 32 | + require.Equal(t, val, out.Materialize(), "output and data must be the same") |
28 | 33 |
|
29 |
| - // reuse |
30 |
| - require.NoError(t, codec.Unmarshal(mem.BufferSlice{buffer}, framePtr), "unmarshalling must go ok") |
31 |
| - out, err = codec.Marshal(framePtr) |
32 |
| - require.NoError(t, err, "no marshal error") |
33 |
| - require.Equal(t, []byte{0x55}, out.Materialize(), "output and data must be the same") |
| 34 | + out.Free() |
| 35 | + buffer.Free() |
| 36 | + buffer = mem.Copy([]byte{0x55}, mem.DefaultBufferPool()) |
34 | 37 |
|
35 |
| - out.Free() |
| 38 | + // reuse |
| 39 | + require.NoError(t, codec.Unmarshal(mem.BufferSlice{buffer}, framePtr), "unmarshalling must go ok") |
| 40 | + out, err = codec.Marshal(framePtr) |
| 41 | + require.NoError(t, err, "no marshal error") |
| 42 | + require.Equal(t, []byte{0x55}, out.Materialize(), "output and data must be the same") |
| 43 | + |
| 44 | + out.Free() |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestCodecUsualMessage(t *testing.T) { |
| 50 | + const msg = "short message" |
| 51 | + |
| 52 | + for key, val := range map[string]string{ |
| 53 | + "short message": "edbca", |
| 54 | + "long message": strings.Repeat(msg, 3072), |
| 55 | + } { |
| 56 | + t.Run(key, func(t *testing.T) { |
| 57 | + msg := &talos_testproto.PingRequest{Value: val} |
| 58 | + |
| 59 | + codec := proxy.Codec() |
| 60 | + |
| 61 | + out, err := codec.Marshal(msg) |
| 62 | + require.NoError(t, err, "no marshal error") |
| 63 | + |
| 64 | + defer out.Free() |
| 65 | + |
| 66 | + var dst talos_testproto.PingRequest |
| 67 | + |
| 68 | + require.NoError(t, codec.Unmarshal(out, &dst), "unmarshalling must go ok") |
| 69 | + require.NotZero(t, dst.Value, "output must not be zero") |
| 70 | + require.Equal(t, msg.Value, dst.Value, "output and data must be the same") |
| 71 | + }) |
| 72 | + } |
36 | 73 | }
|
0 commit comments