forked from 0zAND1z/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
330 lines (313 loc) · 7.36 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
// Copyright 2017-2019 Lei Ni ([email protected])
//
// 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.
// +build !dragonboat_slowtest,!dragonboat_errorinjectiontest
package dragonboat
import (
"testing"
"github.com/lni/dragonboat/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()
q.get()
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()
q.get()
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()
q.get()
tq := q.targetQueue()
for i := 0; i < 3; i++ {
if tq[i].Cmd == nil {
t.Errorf("data unexpectedly freed")
}
}
q.get()
q.get()
tq = q.targetQueue()
for i := 0; i < 3; i++ {
if tq[i].Cmd == nil {
t.Errorf("data unexpectedly freed")
}
}
q.get()
q.get()
tq = q.targetQueue()
for i := 0; i < 3; i++ {
if tq[i].Cmd != nil {
t.Errorf("data not freed at the expected cycle")
}
}
}
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: uint64(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 != uint64(i+1) {
t.Errorf("index %d, want %d", r[i].Index, uint64(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()
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()
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 TestClusterCanBeSetAsReady(t *testing.T) {
rc := newReadyCluster()
if len(rc.ready) != 0 {
t.Errorf("ready map not empty")
}
rc.setClusterReady(1)
rc.setClusterReady(2)
rc.setClusterReady(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("cluster 1 not set as ready")
}
_, ok = rc.ready[2]
if !ok {
t.Errorf("cluster 2 not set as ready")
}
}
func TestReadyClusterCanBeReturnedAndCleared(t *testing.T) {
rc := newReadyCluster()
if len(rc.ready) != 0 {
t.Errorf("ready map not empty")
}
rc.setClusterReady(1)
rc.setClusterReady(2)
rc.setClusterReady(2)
if len(rc.ready) != 2 {
t.Errorf("ready map sz %d, want 2", len(rc.ready))
}
r := rc.getReadyClusters()
if len(r) != 2 {
t.Errorf("ready map sz %d, want 2", len(r))
}
if len(rc.ready) != 0 {
t.Errorf("cluster ready map not cleared")
}
r = rc.getReadyClusters()
if len(r) != 0 {
t.Errorf("cluster ready map not cleared")
}
rc.setClusterReady(4)
r = rc.getReadyClusters()
if len(r) != 1 {
t.Errorf("cluster ready not set")
}
}
func TestWorkReadyCanBeCreated(t *testing.T) {
wr := newWorkReady(4)
if len(wr.readyMapList) != 4 || len(wr.readyChList) != 4 {
t.Errorf("unexpected ready list len")
}
if wr.count != 4 {
t.Errorf("unexpected count value")
}
}
func TestPartitionerWorksAsExpected(t *testing.T) {
wr := newWorkReady(4)
p := wr.getPartitioner()
vals := make(map[uint64]struct{})
for i := uint64(0); i < uint64(128); i++ {
idx := p.GetPartitionID(i)
vals[idx] = struct{}{}
}
if len(vals) != 4 {
t.Errorf("unexpected partitioner outcome")
}
}
func TestWorkCanBeSetAsReady(t *testing.T) {
wr := newWorkReady(4)
select {
case <-wr.waitCh(1):
t.Errorf("ready signaled")
case <-wr.waitCh(2):
t.Errorf("ready signaled")
case <-wr.waitCh(3):
t.Errorf("ready signaled")
case <-wr.waitCh(4):
t.Errorf("ready signaled")
default:
}
wr.clusterReady(0)
select {
case <-wr.waitCh(1):
case <-wr.waitCh(2):
t.Errorf("ready signaled")
case <-wr.waitCh(3):
t.Errorf("ready signaled")
case <-wr.waitCh(4):
t.Errorf("ready signaled")
default:
t.Errorf("ready not signaled")
}
wr.clusterReady(9)
select {
case <-wr.waitCh(1):
t.Errorf("ready signaled")
case <-wr.waitCh(2):
case <-wr.waitCh(3):
t.Errorf("ready signaled")
case <-wr.waitCh(4):
t.Errorf("ready signaled")
default:
t.Errorf("ready not signaled")
}
}
func TestReturnedReadyMapContainsReadyClusterID(t *testing.T) {
wr := newWorkReady(4)
wr.clusterReady(0)
wr.clusterReady(4)
wr.clusterReady(129)
ready := wr.getReadyMap(1)
if len(ready) != 2 {
t.Errorf("unexpected ready map size, sz: %d", len(ready))
}
_, ok := ready[0]
_, ok2 := ready[4]
if !ok || !ok2 {
t.Errorf("missing cluster id")
}
ready = wr.getReadyMap(2)
if len(ready) != 1 {
t.Errorf("unexpected ready map size")
}
_, ok = ready[129]
if !ok {
t.Errorf("missing cluster id")
}
ready = wr.getReadyMap(3)
if len(ready) != 0 {
t.Errorf("unexpected ready map size")
}
}