forked from lni/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback_test.go
168 lines (159 loc) · 4.23 KB
/
feedback_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
// 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
// +build !dragonboat_errorinjectiontest
package dragonboat
import (
"testing"
"github.com/lni/dragonboat/raftio"
)
func TestSnapshotStatusAddStatus(t *testing.T) {
p := newSnapshotFeedback(nil)
p.addStatus(100, 1, true, 100)
p.addStatus(100, 2, true, 100)
if len(p.pendings) != 2 {
t.Errorf("got %d, want 2", len(p.pendings))
}
p.addStatus(100, 1, true, 200)
if len(p.pendings) != 2 {
t.Errorf("got %d, want 2", len(p.pendings))
}
v, ok := p.pendings[raftio.GetNodeInfo(100, 1)]
if !ok {
t.Fatalf("failed to get rec")
}
if v.releaseTick != 200+feedbackPushDelayTick {
t.Errorf("unexpected release tick")
}
}
func TestSnapshotStatusGetReady(t *testing.T) {
p := newSnapshotFeedback(nil)
p.addStatus(100, 1, true, 100)
p.addStatus(100, 2, true, 200)
if len(p.pendings) != 2 {
t.Errorf("got %d, want 2", len(p.pendings))
}
r := p.getReady(150 + feedbackPushDelayTick)
if len(r) != 1 {
t.Errorf("got %d, want 1", len(r))
}
p = newSnapshotFeedback(nil)
p.addStatus(100, 1, true, 100)
p.addStatus(100, 2, true, 200)
r = p.getReady(200 + feedbackPushDelayTick)
if len(r) != 1 {
t.Errorf("got %d, want 1", len(r))
}
p = newSnapshotFeedback(nil)
p.addStatus(100, 1, true, 100)
p.addStatus(100, 2, true, 200)
r = p.getReady(201 + feedbackPushDelayTick)
if len(r) != 2 {
t.Errorf("got %d, want 2", len(r))
}
}
func TestSnapshotStatusAddRetry(t *testing.T) {
p := newSnapshotFeedback(nil)
p.addStatus(100, 1, true, 100)
s1 := snapshotStatus{
clusterID: 100,
nodeID: 1,
releaseTick: 500,
failed: true,
}
s2 := snapshotStatus{
clusterID: 100,
nodeID: 2,
releaseTick: 600,
failed: false,
}
p.addRetry([]snapshotStatus{s1, s2}, 1000)
if len(p.pendings) != 2 {
t.Errorf("got %d, want 2", len(p.pendings))
}
v, ok := p.pendings[raftio.GetNodeInfo(100, 1)]
if !ok {
t.Fatalf("rec not found")
}
if v.releaseTick != 1000+feedbackRetryDelayTick {
t.Errorf("unexpected release tick")
}
v, ok = p.pendings[raftio.GetNodeInfo(100, 2)]
if !ok {
t.Fatalf("rec not found")
}
if v.releaseTick != 1000+feedbackRetryDelayTick {
t.Errorf("unexpected release tick")
}
}
func TestSnapshotStatusAddConfirmation(t *testing.T) {
p := newSnapshotFeedback(nil)
p.addStatus(100, 1, true, 100)
p.confirm(100, 1, 120)
p.confirm(100, 2, 130)
if len(p.pendings) != 2 {
t.Errorf("got %d, want 2", len(p.pendings))
}
v, ok := p.pendings[raftio.GetNodeInfo(100, 1)]
if !ok {
t.Fatalf("rec not found")
}
if v.releaseTick != 120+feedbackConfirmedDelayTick {
t.Errorf("unexpected release tick")
}
v, ok = p.pendings[raftio.GetNodeInfo(100, 2)]
if !ok {
t.Fatalf("rec not found")
}
if v.releaseTick != 130+feedbackConfirmedDelayTick {
t.Errorf("unexpected release tick")
}
}
func TestSnapshotStatusPushReady(t *testing.T) {
pushed := 0
pf := func(clusterID uint64, nodeID uint64, failed bool) bool {
pushed++
return true
}
p := newSnapshotFeedback(pf)
p.addStatus(100, 1, true, 100)
p.pushReady(101 + feedbackPushDelayTick)
if pushed != 1 {
t.Errorf("rec not pushed")
}
if len(p.pendings) != 0 {
t.Errorf("unexpected rec in pendings")
}
pushed = 0
pfr := func(clusterID uint64, nodeID uint64, failed bool) bool {
pushed++
return false
}
p = newSnapshotFeedback(pfr)
p.addStatus(100, 1, true, 100)
p.pushReady(101 + feedbackPushDelayTick)
if pushed != 1 {
t.Errorf("rec not pushed")
}
if len(p.pendings) != 1 {
t.Errorf("rec not added back")
}
v, ok := p.pendings[raftio.GetNodeInfo(100, 1)]
if !ok {
t.Fatalf("rec not found")
}
if v.releaseTick != 101+feedbackPushDelayTick+feedbackRetryDelayTick {
t.Errorf("unexpected release tick")
}
}