-
Notifications
You must be signed in to change notification settings - Fork 542
/
queue_test.go
346 lines (328 loc) · 7.8 KB
/
queue_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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2017-2020 Lei Ni (nilei81@gmail.com) and other contributors.
//
// 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 dragonboat
import (
"testing"
"github.com/lni/dragonboat/v4/raftio"
"github.com/lni/dragonboat/v4/raftpb"
)
func TestEntryQueueCanBeCreated(t *testing.T) {
q := newEntryQueue(5, 0)
if q.size != 5 || len(q.left) != 5 || len(q.right) != 5 {
t.Errorf("size unexpected")
}
if q.idx != 0 {
t.Errorf("idx %d, want 0", q.idx)
}
}
func TestLazyFreeCanBeDisabled(t *testing.T) {
q := newEntryQueue(5, 0)
q.add(raftpb.Entry{Cmd: make([]byte, 16)})
q.add(raftpb.Entry{Cmd: make([]byte, 16)})
q.add(raftpb.Entry{Cmd: make([]byte, 16)})
q.get(false)
q.get(false)
tq := q.targetQueue()
for i := 0; i < 3; i++ {
if tq[i].Cmd == nil {
t.Errorf("data unexpectedly freed")
}
}
}
func TestLazyFreeCanBeUsed(t *testing.T) {
q := newEntryQueue(5, 1)
q.add(raftpb.Entry{Cmd: make([]byte, 16)})
q.add(raftpb.Entry{Cmd: make([]byte, 16)})
q.add(raftpb.Entry{Cmd: make([]byte, 16)})
q.get(false)
q.get(false)
tq := q.targetQueue()
for i := 0; i < 3; i++ {
if tq[i].Cmd != nil {
t.Errorf("data unexpectedly not freed")
}
}
}
func TestLazyFreeCycleCanBeSet(t *testing.T) {
q := newEntryQueue(5, 6)
q.add(raftpb.Entry{Cmd: make([]byte, 16)})
q.add(raftpb.Entry{Cmd: make([]byte, 16)})
q.add(raftpb.Entry{Cmd: make([]byte, 16)})
q.get(false)
q.get(false)
tq := q.targetQueue()
for i := 0; i < 3; i++ {
if tq[i].Cmd == nil {
t.Errorf("data unexpectedly freed")
}
}
q.get(false)
q.get(false)
tq = q.targetQueue()
for i := 0; i < 3; i++ {
if tq[i].Cmd == nil {
t.Errorf("data unexpectedly freed")
}
}
q.get(false)
q.get(false)
tq = q.targetQueue()
for i := 0; i < 3; i++ {
if tq[i].Cmd != nil {
t.Errorf("data not freed at the expected cycle")
}
}
}
func TestEntryQueueCanBePaused(t *testing.T) {
q := newEntryQueue(5, 0)
if q.paused {
t.Errorf("entry queue is paused by default")
}
for i := 0; i < 5; i++ {
ok, stopped := q.add(raftpb.Entry{})
if !ok || stopped {
t.Errorf("failed to add new entry")
}
if q.stopped {
t.Errorf("stopped too early")
}
}
v := q.get(true)
if len(v) != 5 {
t.Errorf("failed to get all entries")
}
if !q.paused {
t.Errorf("not paused")
}
ok, stopped := q.add(raftpb.Entry{})
if ok {
t.Errorf("entry added to paused queue")
}
if stopped {
t.Errorf("entry queue unexpectedly stopped")
}
}
func TestEntryQueueCanBeClosed(t *testing.T) {
q := newEntryQueue(5, 0)
if q.stopped {
t.Errorf("entry queue is stopped by default")
}
for i := 0; i < 5; i++ {
ok, stopped := q.add(raftpb.Entry{})
if !ok || stopped {
t.Errorf("failed to add new entry")
}
if q.stopped {
t.Errorf("stopped too early")
}
}
ok, _ := q.add(raftpb.Entry{})
if ok {
t.Errorf("not expect to add more")
}
q = newEntryQueue(5, 0)
q.close()
if !q.stopped {
t.Errorf("entry queue is not marked as stopped")
}
if q.idx != 0 {
t.Errorf("idx %d, want 0", q.idx)
}
ok, stopped := q.add(raftpb.Entry{})
if ok {
t.Errorf("not expect to add more")
}
if !stopped {
t.Errorf("stopped flag is not returned")
}
}
func TestEntryQueueAllowEntriesToBeAdded(t *testing.T) {
q := newEntryQueue(5, 0)
for i := uint64(0); i < 5; i++ {
ok, stopped := q.add(raftpb.Entry{Index: i + 1})
if !ok || stopped {
t.Errorf("failed to add new entry")
}
if q.idx != i+1 {
t.Errorf("idx %d, want %d", q.idx, i+1)
}
var r []raftpb.Entry
if q.leftInWrite {
r = q.left
} else {
r = q.right
}
if r[i].Index != i+1 {
t.Errorf("index %d, want %d", r[i].Index, i+1)
}
}
}
func TestEntryQueueAllowAddedEntriesToBeReturned(t *testing.T) {
q := newEntryQueue(5, 0)
for i := 0; i < 3; i++ {
ok, stopped := q.add(raftpb.Entry{Index: uint64(i + 1)})
if !ok || stopped {
t.Errorf("failed to add new entry")
}
}
r := q.get(false)
if len(r) != 3 {
t.Errorf("len %d, want %d", len(r), 3)
}
if q.idx != 0 {
t.Errorf("idx %d, want %d", q.idx, 0)
}
// check whether we can keep adding entries as long as we keep getting
// previously written entries.
expectedIndex := uint64(1)
q = newEntryQueue(5, 0)
for i := 0; i < 1000; i++ {
ok, stopped := q.add(raftpb.Entry{Index: uint64(i + 1)})
if !ok || stopped {
t.Errorf("failed to add new entry")
}
if q.idx == q.size {
r := q.get(false)
if len(r) != 5 {
t.Errorf("len %d, want %d", len(r), 5)
}
for _, e := range r {
if e.Index != expectedIndex {
t.Errorf("index %d, expected %d", e.Index, expectedIndex)
}
expectedIndex++
}
}
}
}
func TestShardCanBeSetAsReady(t *testing.T) {
rc := newReadyShard()
if len(rc.ready) != 0 {
t.Errorf("ready map not empty")
}
rc.setShardReady(1)
rc.setShardReady(2)
rc.setShardReady(2)
if len(rc.ready) != 2 {
t.Errorf("ready map sz %d, want 2", len(rc.ready))
}
_, ok := rc.ready[1]
if !ok {
t.Errorf("shard 1 not set as ready")
}
_, ok = rc.ready[2]
if !ok {
t.Errorf("shard 2 not set as ready")
}
}
func TestReadyShardCanBeReturnedAndCleared(t *testing.T) {
rc := newReadyShard()
if len(rc.ready) != 0 {
t.Errorf("ready map not empty")
}
rc.setShardReady(1)
rc.setShardReady(2)
rc.setShardReady(2)
if len(rc.ready) != 2 {
t.Errorf("ready map sz %d, want 2", len(rc.ready))
}
r := rc.getReadyShards()
if len(r) != 2 {
t.Errorf("ready map sz %d, want 2", len(r))
}
if len(rc.ready) != 0 {
t.Errorf("shard ready map not cleared")
}
r = rc.getReadyShards()
if len(r) != 0 {
t.Errorf("shard ready map not cleared")
}
rc.setShardReady(4)
r = rc.getReadyShards()
if len(r) != 1 {
t.Errorf("shard ready not set")
}
}
func TestLeaderInfoQueueCanBeCreated(t *testing.T) {
q := newLeaderInfoQueue()
if cap(q.workCh) != 1 {
t.Errorf("unexpected queue cap")
}
if len(q.workCh) != 0 {
t.Errorf("unexpected queue length")
}
if len(q.notifications) != 0 {
t.Errorf("unexpected notifications length")
}
ch := q.workReady()
if ch == nil {
t.Errorf("failed to return work ready chan")
}
}
func TestAddToLeaderInfoQueue(t *testing.T) {
q := newLeaderInfoQueue()
q.addLeaderInfo(raftio.LeaderInfo{})
q.addLeaderInfo(raftio.LeaderInfo{})
if len(q.workCh) != 1 {
t.Errorf("unexpected workCh len")
}
if len(q.notifications) != 2 {
t.Errorf("unexpected notifications len")
}
}
func TestGetFromLeaderInfoQueue(t *testing.T) {
q := newLeaderInfoQueue()
_, ok := q.getLeaderInfo()
if ok {
t.Errorf("unexpectedly returned leader info")
}
v1 := raftio.LeaderInfo{ShardID: 101}
v2 := raftio.LeaderInfo{ShardID: 2002}
q.addLeaderInfo(v1)
q.addLeaderInfo(v2)
rv1, ok1 := q.getLeaderInfo()
rv2, ok2 := q.getLeaderInfo()
_, ok3 := q.getLeaderInfo()
if !ok1 || rv1.ShardID != v1.ShardID {
t.Errorf("unexpected result")
}
if !ok2 || rv2.ShardID != v2.ShardID {
t.Errorf("unexpected result")
}
if ok3 {
t.Errorf("unexpectedly return third reader info rec")
}
}
func TestReadIndexQueueCanHandleAddFailure(t *testing.T) {
q := newReadIndexQueue(1)
added, stopped := q.add(&RequestState{})
if !added || stopped {
t.Errorf("unexpected failure")
}
added, stopped = q.add(&RequestState{})
if added {
t.Errorf("unexpectedly added the rs")
}
if stopped {
t.Errorf("unexpectedly reported state as stopped")
}
q.close()
added, stopped = q.add(&RequestState{})
if added {
t.Errorf("unexpectedly added the rs")
}
if !stopped {
t.Errorf("failed to report state as stopped")
}
}