forked from couchbase/cbgt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed_test.go
234 lines (207 loc) · 6.3 KB
/
feed_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
234
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an "AS
// IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language
// governing permissions and limitations under the License.
package cbgt
import (
"bytes"
"fmt"
"io"
"testing"
)
type ErrorOnlyFeed struct {
name string
}
func (t *ErrorOnlyFeed) Name() string {
return t.name
}
func (t *ErrorOnlyFeed) IndexName() string {
return t.name
}
func (t *ErrorOnlyFeed) Start() error {
return fmt.Errorf("ErrorOnlyFeed Start() invoked")
}
func (t *ErrorOnlyFeed) Close() error {
return fmt.Errorf("ErrorOnlyFeed Close() invoked")
}
func (t *ErrorOnlyFeed) Dests() map[string]Dest {
return nil
}
func (t *ErrorOnlyFeed) Stats(w io.Writer) error {
return fmt.Errorf("ErrorOnlyFeed Stats() invoked")
}
func TestDataSourcePartitions(t *testing.T) {
a, err := dataSourcePartitions("a fake source type",
"sourceName", "sourceUUID", "sourceParams", "serverURL", nil)
if err == nil || a != nil {
t.Errorf("expected fake data source type to error")
}
a, err = dataSourcePartitions("nil",
"sourceName", "sourceUUID", "sourceParams", "serverURL", nil)
if err != nil || a != nil {
t.Errorf("expected nil source type to work, but have no partitions")
}
a, err = dataSourcePartitions("primary",
"sourceName", "sourceUUID", "sourceParams", "serverURL", nil)
if err == nil || a != nil {
t.Errorf("expected dest source type to error on non-json server params")
}
a, err = dataSourcePartitions("primary",
"sourceName", "sourceUUID", "", "serverURL", nil)
if err != nil || a == nil {
t.Errorf("expected dest source type to ok on empty server params")
}
a, err = dataSourcePartitions("primary",
"sourceName", "sourceUUID", "{}", "serverURL", nil)
if err != nil || a == nil {
t.Errorf("expected dest source type to ok on empty JSON server params")
}
}
func TestNilFeedStart(t *testing.T) {
f := NewNILFeed("aaa", "bbb", nil)
if f.Name() != "aaa" {
t.Errorf("expected aaa name")
}
if f.IndexName() != "bbb" {
t.Errorf("expected bbb index name")
}
if f.Start() != nil {
t.Errorf("expected NILFeed.Start() to work")
}
if f.Dests() != nil {
t.Errorf("expected nil dests")
}
w := bytes.NewBuffer(nil)
if f.Stats(w) != nil {
t.Errorf("expected no err on nil feed stats")
}
if w.String() != "{}" {
t.Errorf("expected json stats")
}
if f.Close() != nil {
t.Errorf("expected nil dests")
}
}
func TestPrimaryFeed(t *testing.T) {
df := NewPrimaryFeed("aaa", "bbb",
BasicPartitionFunc, map[string]Dest{})
if df.Name() != "aaa" {
t.Errorf("expected aaa name")
}
if df.IndexName() != "bbb" {
t.Errorf("expected bbb index name")
}
if df.Start() != nil {
t.Errorf("expected PrimaryFeed start to work")
}
buf := make([]byte, 0, 100)
err := df.Stats(bytes.NewBuffer(buf))
if err != nil {
t.Errorf("expected PrimaryFeed stats to work")
}
key := []byte("k")
seq := uint64(123)
val := []byte("v")
if df.DataUpdate("unknown-partition", key, seq, val,
0, DEST_EXTRAS_TYPE_NIL, nil) == nil {
t.Errorf("expected err on bad partition")
}
if df.DataDelete("unknown-partition", key, seq,
0, DEST_EXTRAS_TYPE_NIL, nil) == nil {
t.Errorf("expected err on bad partition")
}
if df.SnapshotStart("unknown-partition", seq, seq) == nil {
t.Errorf("expected err on bad partition")
}
if df.OpaqueSet("unknown-partition", val) == nil {
t.Errorf("expected err on bad partition")
}
_, _, err = df.OpaqueGet("unknown-partition")
if err == nil {
t.Errorf("expected err on bad partition")
}
if df.Rollback("unknown-partition", seq) == nil {
t.Errorf("expected err on bad partition")
}
if df.ConsistencyWait("unknown-partition", "unknown-partition-UUID",
"level", seq, nil) == nil {
t.Errorf("expected err on bad partition")
}
df2 := NewPrimaryFeed("", "", BasicPartitionFunc, map[string]Dest{
"some-partition": &TestDest{},
})
if df2.ConsistencyWait("some-partition", "some-partition-UUID",
"level", seq, nil) != nil {
t.Errorf("expected no err on some partition to TestDest")
}
_, err = df.Count(nil, nil)
if err == nil {
t.Errorf("expected err on counting a primary feed")
}
if df.Query(nil, nil, nil, nil) == nil {
t.Errorf("expected err on querying a primary feed")
}
}
func TestDataSourcePrepParams(t *testing.T) {
a, err := dataSourcePrepParams("a fake source type",
"sourceName", "sourceUUID", "sourceParams", "serverURL", nil)
if err == nil || a != "" {
t.Errorf("expected fake data source type to error")
}
a, err = dataSourcePrepParams("primary",
"sourceName", "sourceUUID", "", "serverURL", nil)
if err != nil || a != "" {
t.Errorf("expected empty data source params to ok")
}
a, err = dataSourcePrepParams("primary",
"sourceName", "sourceUUID", "{}", "serverURL", nil)
if err != nil || a != "{}" {
t.Errorf("expected {} data source params to ok")
}
saw_testFeedPartitions := 0
saw_testFeedPartitionSeqs := 0
testFeedPartitions := func(sourceType,
sourceName, sourceUUID, sourceParams,
serverIn string, options map[string]string,
) (
partitions []string, err error,
) {
saw_testFeedPartitions++
return nil, nil
}
testFeedPartitionSeqs := func(sourceType, sourceName, sourceUUID,
sourceParams, serverIn string, options map[string]string,
) (
map[string]UUIDSeq, error,
) {
saw_testFeedPartitionSeqs++
return nil, nil
}
RegisterFeedType("testFeed", &FeedType{
Partitions: testFeedPartitions,
PartitionSeqs: testFeedPartitionSeqs,
})
sourceParams := `{"foo":"hoo","markPartitionSeqs":"currentPartitionSeqs"}`
a, err = dataSourcePrepParams("testFeed",
"sourceName", "sourceUUID", sourceParams, "serverURL", nil)
if err != nil {
t.Errorf("expected no err")
}
if a == sourceParams {
t.Errorf("expected transformed data source params")
}
if saw_testFeedPartitions != 1 {
t.Errorf("expected 1 saw_testFeedPartitions call, got: %d",
saw_testFeedPartitions)
}
if saw_testFeedPartitionSeqs != 1 {
t.Errorf("expected 1 saw_testFeedPartitionSeqs call, got: %d",
saw_testFeedPartitionSeqs)
}
}