-
Notifications
You must be signed in to change notification settings - Fork 2
/
rpc_test.go
66 lines (61 loc) · 1.67 KB
/
rpc_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
package metrics
import (
"reflect"
"testing"
"github.com/goccy/go-json"
"github.com/vmihailenco/msgpack/v5"
)
type unmarshal func([]byte, any) error
func Test_Metric_Unmarshal(t *testing.T) {
tests := []struct {
name string
unmarshaler unmarshal
payload []byte
want Metric
}{
{"json",
json.Unmarshal,
[]byte(`{"name":"test","value":123,"labels": ["test1", "test2"]}`),
Metric{
Name: "test",
Value: 123,
Labels: []string{"test1", "test2"},
},
},
{
"msgpack",
msgpack.Unmarshal,
// {"name":"test","value":123,"labels": ["test1", "test2"]}
[]byte{0x83, 0xa4, 0x6e, 0x61, 0x6d, 0x65, 0xa4, 0x74, 0x65, 0x73, 0x74, 0xa5, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x7b, 0xa6, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x73, 0x92, 0xa5, 0x74, 0x65, 0x73, 0x74, 0x31, 0xa5, 0x74, 0x65, 0x73, 0x74, 0x32},
Metric{
Name: "test",
Value: 123,
Labels: []string{"test1", "test2"},
},
},
{
"msgpack pascal case",
msgpack.Unmarshal,
// {"Name":"test","Value":123,"Labels": ["test1", "test2"]}
[]byte{0x83, 0xa4, 0x4e, 0x61, 0x6d, 0x65, 0xa4, 0x74, 0x65, 0x73, 0x74, 0xa5, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x7b, 0xa6, 0x4c, 0x61, 0x62, 0x65, 0x6c, 0x73, 0x92, 0xa5, 0x74, 0x65, 0x73, 0x74, 0x31, 0xa5, 0x74, 0x65, 0x73, 0x74, 0x32},
Metric{
Name: "test",
Value: 123,
Labels: []string{"test1", "test2"},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Metric{}
err := test.unmarshaler(test.payload, &got)
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(test.want, got) {
t.Errorf("Want: %v, Got: %v", test.want, got)
}
})
}
}