forked from ha/doozerd
/
server_test.go
114 lines (99 loc) · 2.09 KB
/
server_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package server
import (
"bytes"
"code.google.com/p/goprotobuf/proto"
"github.com/bmizerany/assert"
"github.com/ha/doozerd/store"
"io"
"testing"
)
var (
fooPath = "/foo"
)
type bchan chan []byte
func (b bchan) Write(buf []byte) (int, error) {
b <- buf
return len(buf), nil
}
func (b bchan) Read(buf []byte) (int, error) {
return 0, io.EOF // not implemented
}
func mustUnmarshal(b []byte) (r *response) {
r = new(response)
err := proto.Unmarshal(b, r)
if err != nil {
panic(err)
}
return
}
func assertResponseErrCode(t *testing.T, exp response_Err, c *conn) {
b := c.c.(*bytes.Buffer).Bytes()
assert.T(t, len(b) > 4, b)
assert.Equal(t, &exp, mustUnmarshal(b[4:]).ErrCode)
}
func TestDelNilFields(t *testing.T) {
c := &conn{
c: &bytes.Buffer{},
canWrite: true,
waccess: true,
}
tx := &txn{
c: c,
req: request{Tag: proto.Int32(1)},
}
tx.del()
assertResponseErrCode(t, response_MISSING_ARG, c)
}
func TestSetNilFields(t *testing.T) {
c := &conn{
c: &bytes.Buffer{},
canWrite: true,
waccess: true,
}
tx := &txn{
c: c,
req: request{Tag: proto.Int32(1)},
}
tx.set()
assertResponseErrCode(t, response_MISSING_ARG, c)
}
func TestServerNoAccess(t *testing.T) {
b := make(bchan, 2)
c := &conn{
c: b,
canWrite: true,
st: store.New(),
}
tx := &txn{
c: c,
req: request{Tag: proto.Int32(1)},
}
for i, op := range ops {
if i != int32(request_ACCESS) {
op(tx)
var exp response_Err = response_OTHER
assert.Equal(t, 4, len(<-b), request_Verb_name[i])
assert.Equal(t, &exp, mustUnmarshal(<-b).ErrCode, request_Verb_name[i])
}
}
}
func TestServerRo(t *testing.T) {
b := make(bchan, 2)
c := &conn{
c: b,
canWrite: true,
st: store.New(),
}
tx := &txn{
c: c,
req: request{Tag: proto.Int32(1)},
}
wops := []int32{int32(request_DEL), int32(request_NOP), int32(request_SET)}
for _, i := range wops {
op := ops[i]
op(tx)
var exp response_Err = response_OTHER
assert.Equal(t, 4, len(<-b), request_Verb_name[i])
assert.Equal(t, &exp, mustUnmarshal(<-b).ErrCode, request_Verb_name[i])
}
}