forked from couchbase/cbgt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdest_test.go
233 lines (203 loc) · 5.86 KB
/
dest_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2014-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbgt
import (
"bytes"
"encoding/json"
"fmt"
"io"
"testing"
)
type TestDest struct{}
func (s *TestDest) Close(remove bool) error {
return nil
}
func (s *TestDest) DataUpdate(partition string,
key []byte, seq uint64, val []byte,
cas uint64,
extrasType DestExtrasType, extras []byte) error {
return nil
}
func (s *TestDest) DataDelete(partition string,
key []byte, seq uint64,
cas uint64,
extrasType DestExtrasType, extras []byte) error {
return nil
}
func (s *TestDest) SnapshotStart(partition string,
snapStart, snapEnd uint64) error {
return nil
}
func (s *TestDest) OpaqueSet(partition string,
value []byte) error {
return nil
}
func (s *TestDest) OpaqueGet(partition string) (
value []byte, lastSeq uint64, err error) {
return nil, 0, nil
}
func (s *TestDest) Rollback(partition string,
rollbackSeq uint64) error {
return nil
}
func (s *TestDest) ConsistencyWait(partition, partitionUUID string,
consistencyLevel string,
consistencySeq uint64,
cancelCh <-chan bool) error {
return nil
}
func (t *TestDest) Count(pindex *PIndex,
cancelCh <-chan bool) (uint64, error) {
return 0, nil
}
func (t *TestDest) Query(pindex *PIndex, req []byte, res io.Writer,
cancelCh <-chan bool) error {
return nil
}
func (t *TestDest) Stats(w io.Writer) error {
return nil
}
func TestBasicPartitionFunc(t *testing.T) {
dest := &TestDest{}
dest2 := &TestDest{}
s, err := BasicPartitionFunc("", nil, map[string]Dest{"": dest})
if err != nil || s != dest {
t.Errorf("expected BasicPartitionFunc to work")
}
s, err = BasicPartitionFunc("foo", nil, map[string]Dest{"": dest})
if err != nil || s != dest {
t.Errorf("expected BasicPartitionFunc to hit the catch-all dest")
}
s, err = BasicPartitionFunc("", nil, map[string]Dest{"foo": dest})
if err == nil || s == dest {
t.Errorf("expected BasicPartitionFunc to not work")
}
s, err = BasicPartitionFunc("foo", nil, map[string]Dest{"foo": dest})
if err != nil || s != dest {
t.Errorf("expected BasicPartitionFunc to work on partition hit")
}
s, err = BasicPartitionFunc("foo", nil, map[string]Dest{"foo": dest, "": dest2})
if err != nil || s != dest {
t.Errorf("expected BasicPartitionFunc to work on partition hit")
}
}
type ErrorOnlyDestProvider struct{}
func (dp *ErrorOnlyDestProvider) Dest(partition string) (Dest, error) {
return nil, fmt.Errorf("always error for testing")
}
func (dp *ErrorOnlyDestProvider) Count(pindex *PIndex,
cancelCh <-chan bool) (uint64, error) {
return 0, fmt.Errorf("always error for testing")
}
func (dp *ErrorOnlyDestProvider) Query(pindex *PIndex, req []byte, res io.Writer,
cancelCh <-chan bool) error {
return fmt.Errorf("always error for testing")
}
func (dp *ErrorOnlyDestProvider) Stats(io.Writer) error {
return fmt.Errorf("always error for testing")
}
func (dp *ErrorOnlyDestProvider) Close(remove bool) error {
return fmt.Errorf("always error for testing")
}
func TestErrorOnlyDestProviderWithDestForwarder(t *testing.T) {
df := &DestForwarder{&ErrorOnlyDestProvider{}}
if df.DataUpdate("", nil, 0, nil, 0, DEST_EXTRAS_TYPE_NIL, nil) == nil {
t.Errorf("expected err")
}
if df.DataDelete("", nil, 0, 0, DEST_EXTRAS_TYPE_NIL, nil) == nil {
t.Errorf("expected err")
}
if df.SnapshotStart("", 0, 0) == nil {
t.Errorf("expected err")
}
if df.OpaqueSet("", nil) == nil {
t.Errorf("expected err")
}
value, lastSeq, err := df.OpaqueGet("")
if err == nil || value != nil || lastSeq != 0 {
t.Errorf("expected err")
}
if df.Rollback("", 0) == nil {
t.Errorf("expected err")
}
if df.ConsistencyWait("", "", "", 0, nil) == nil {
t.Errorf("expected err")
}
if _, err := df.Count(nil, nil); err == nil {
t.Errorf("expected err")
}
if df.Query(nil, nil, nil, nil) == nil {
t.Errorf("expected err")
}
if df.Stats(nil) == nil {
t.Errorf("expected err")
}
if df.Close(false) == nil {
t.Errorf("expected err")
}
}
type FanInDestProvider struct {
Target Dest
}
func (dp *FanInDestProvider) Dest(partition string) (Dest, error) {
return dp.Target, nil
}
func (dp *FanInDestProvider) Count(pindex *PIndex,
cancelCh <-chan bool) (uint64, error) {
return 0, fmt.Errorf("always error for testing")
}
func (dp *FanInDestProvider) Query(pindex *PIndex, req []byte, res io.Writer,
cancelCh <-chan bool) error {
return fmt.Errorf("always error for testing")
}
func (dp *FanInDestProvider) Stats(io.Writer) error {
return fmt.Errorf("always error for testing")
}
func (dp *FanInDestProvider) Close(remove bool) error {
return fmt.Errorf("always error for testing")
}
func TestChainedDestForwarder(t *testing.T) {
df := &DestForwarder{&FanInDestProvider{
&DestForwarder{&ErrorOnlyDestProvider{}},
}}
if df.DataUpdate("", nil, 0, nil, 0, DEST_EXTRAS_TYPE_NIL, nil) == nil {
t.Errorf("expected err")
}
if df.DataDelete("", nil, 0, 0, DEST_EXTRAS_TYPE_NIL, nil) == nil {
t.Errorf("expected err")
}
if df.SnapshotStart("", 0, 0) == nil {
t.Errorf("expected err")
}
if df.OpaqueSet("", nil) == nil {
t.Errorf("expected err")
}
value, lastSeq, err := df.OpaqueGet("")
if err == nil || value != nil || lastSeq != 0 {
t.Errorf("expected err")
}
if df.Rollback("", 0) == nil {
t.Errorf("expected err")
}
if df.ConsistencyWait("", "", "", 0, nil) == nil {
t.Errorf("expected err")
}
}
func TestDestStatsWriteJSON(t *testing.T) {
ds := NewDestStats()
var buf bytes.Buffer
ds.WriteJSON(&buf)
m := map[string]interface{}{}
err := json.Unmarshal(buf.Bytes(), &m)
if err != nil {
t.Errorf("expected clean json, err: %v", err)
}
if m == nil || len(m) <= 0 {
t.Errorf("expected some m")
}
}