forked from couchbaselabs/cbgb
/
observe_test.go
79 lines (67 loc) · 1.53 KB
/
observe_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
74
75
76
77
78
79
package main
import (
"bytes"
"testing"
)
func TestObserveParse(t *testing.T) {
// From the spec
sample := []byte{
0x00, 0x04, 0x00, 0x05,
'h', 'e', 'l', 'l', 'o',
0x00, 0x05, 0x00, 0x05,
'w', 'o', 'r', 'l', 'd',
}
exp := []obsKey{
{4, []byte("hello")},
{5, []byte("world")},
}
parsed, err := parseObserveKeys(sample)
if err != nil {
t.Fatalf("Error parsing: %v", err)
}
if len(parsed) != len(exp) {
t.Fatalf("Expected to find %v items, found %v",
len(exp), len(parsed))
}
for i := range exp {
if parsed[i].vbid != exp[i].vbid {
t.Errorf("Incorrect vbucket at %v: %v",
i, parsed[i].vbid)
}
if !bytes.Equal(parsed[i].key, exp[i].key) {
t.Errorf("Incorrect key at %v: %s", i, parsed[i].key)
}
}
}
func TestObserveParseErrors(t *testing.T) {
failures := [][]byte{
{0, 0, 3}, // short
{0, 0, 0, 1}, // no key
{0, 0, 0, 1, 'x', 0}, // good, then short
}
for _, test := range failures {
_, err := parseObserveKeys(test)
if err == nil {
t.Errorf("Expected failure on %v", test)
}
}
}
func TestObserveEncode(t *testing.T) {
got := encodeObserveBody([]obsStatus{
{obsKey{4, []byte("hello")}, obsPersisted, 0xa},
{obsKey{5, []byte("world")}, obsNotPersisted, 0xdeadbeefdeadcafe},
})
exp := []byte{
// Hello
0, 4, 0, 5, 'h', 'e', 'l', 'l', 'o',
1,
0, 0, 0, 0, 0, 0, 0, 0xa,
// World
0, 5, 0, 5, 'w', 'o', 'r', 'l', 'd',
0,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xca, 0xfe,
}
if !bytes.Equal(got, exp) {
t.Fatalf("Encoding failed:\n%x\n%x", got, exp)
}
}