forked from samuel/go-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs_test.go
39 lines (36 loc) · 1.14 KB
/
structs_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
package zk
import (
"reflect"
"testing"
)
func TestEncodeDecodePacket(t *testing.T) {
encodeDecodeTest(t, &requestHeader{-2, 5})
encodeDecodeTest(t, &connectResponse{1, 2, 3, nil})
encodeDecodeTest(t, &connectResponse{1, 2, 3, []byte{4, 5, 6}})
encodeDecodeTest(t, &getAclResponse{responseHeader{1, 2, 3}, []acl{acl{12, id{"s", 23}}}, stat{}})
encodeDecodeTest(t, &getChildrenResponse{responseHeader{1, 2, 3}, []string{"foo", "bar"}})
encodeDecodeTest(t, &pathWatchRequest{requestHeader{1, 2}, "path", true})
encodeDecodeTest(t, &pathWatchRequest{requestHeader{1, 2}, "path", false})
}
func encodeDecodeTest(t *testing.T, r interface{}) {
buf := make([]byte, 1024)
n, err := encodePacket(buf, r)
if err != nil {
t.Errorf("encodePacket returned non-nil error %+v\n", err)
return
}
r2 := reflect.New(reflect.ValueOf(r).Elem().Type()).Interface()
n2, err := decodePacket(buf, r2)
if err != nil {
t.Errorf("decodePacket returned non-nil error %+v\n", err)
return
}
if n != n2 {
t.Errorf("sizes don't match: %d != %d", n, n2)
return
}
if !reflect.DeepEqual(r, r2) {
t.Errorf("results don't match: %+v != %+v", r, r2)
return
}
}