forked from hashicorp/memberlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suspicion_test.go
201 lines (185 loc) · 4.46 KB
/
suspicion_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package memberlist
import (
"testing"
"time"
)
func TestSuspicion_remainingSuspicionTime(t *testing.T) {
cases := []struct {
n int32
k int32
elapsed time.Duration
min time.Duration
max time.Duration
expected time.Duration
}{
{0, 3, 0, 2 * time.Second, 30 * time.Second, 30 * time.Second},
{1, 3, 2 * time.Second, 2 * time.Second, 30 * time.Second, 14 * time.Second},
{2, 3, 3 * time.Second, 2 * time.Second, 30 * time.Second, 4810 * time.Millisecond},
{3, 3, 4 * time.Second, 2 * time.Second, 30 * time.Second, -2 * time.Second},
{4, 3, 5 * time.Second, 2 * time.Second, 30 * time.Second, -3 * time.Second},
{5, 3, 10 * time.Second, 2 * time.Second, 30 * time.Second, -8 * time.Second},
}
for i, c := range cases {
remaining := remainingSuspicionTime(c.n, c.k, c.elapsed, c.min, c.max)
if remaining != c.expected {
t.Errorf("case %d: remaining %9.6f != expected %9.6f", i, remaining.Seconds(), c.expected.Seconds())
}
}
}
func TestSuspicion_Timer(t *testing.T) {
const k = 3
const min = 500 * time.Millisecond
const max = 2 * time.Second
type pair struct {
from string
newInfo bool
}
cases := []struct {
numConfirmations int
from string
confirmations []pair
expected time.Duration
}{
{
0,
"me",
[]pair{},
max,
},
{
1,
"me",
[]pair{
pair{"me", false},
pair{"foo", true},
},
1250 * time.Millisecond,
},
{
1,
"me",
[]pair{
pair{"me", false},
pair{"foo", true},
pair{"foo", false},
pair{"foo", false},
},
1250 * time.Millisecond,
},
{
2,
"me",
[]pair{
pair{"me", false},
pair{"foo", true},
pair{"bar", true},
},
810 * time.Millisecond,
},
{
3,
"me",
[]pair{
pair{"me", false},
pair{"foo", true},
pair{"bar", true},
pair{"baz", true},
},
min,
},
{
3,
"me",
[]pair{
pair{"me", false},
pair{"foo", true},
pair{"bar", true},
pair{"baz", true},
pair{"zoo", false},
},
min,
},
}
for i, c := range cases {
ch := make(chan time.Duration, 1)
start := time.Now()
f := func(numConfirmations int) {
if numConfirmations != c.numConfirmations {
t.Errorf("case %d: bad %d != %d", i, numConfirmations, c.numConfirmations)
}
ch <- time.Now().Sub(start)
}
// Create the timer and add the requested confirmations. Wait
// the fudge amount to help make sure we calculate the timeout
// overall, and don't accumulate extra time.
s := newSuspicion(c.from, k, min, max, f)
fudge := 25 * time.Millisecond
for _, p := range c.confirmations {
time.Sleep(fudge)
if s.Confirm(p.from) != p.newInfo {
t.Fatalf("case %d: newInfo mismatch for %s", i, p.from)
}
}
// Wait until right before the timeout and make sure the
// timer hasn't fired.
already := time.Duration(len(c.confirmations)) * fudge
time.Sleep(c.expected - already - fudge)
select {
case d := <-ch:
t.Fatalf("case %d: should not have fired (%9.6f)", i, d.Seconds())
default:
}
// Wait through the timeout and a little after and make sure it
// fires.
time.Sleep(2 * fudge)
select {
case <-ch:
default:
t.Fatalf("case %d: should have fired", i)
}
// Confirm after to make sure it handles a negative remaining
// time correctly and doesn't fire again.
s.Confirm("late")
time.Sleep(c.expected + 2*fudge)
select {
case d := <-ch:
t.Fatalf("case %d: should not have fired (%9.6f)", i, d.Seconds())
default:
}
}
}
func TestSuspicion_Timer_ZeroK(t *testing.T) {
ch := make(chan struct{}, 1)
f := func(int) {
ch <- struct{}{}
}
// This should select the min time since there are no expected
// confirmations to accelerate the timer.
s := newSuspicion("me", 0, 25*time.Millisecond, 30*time.Second, f)
if s.Confirm("foo") {
t.Fatalf("should not provide new information")
}
select {
case <-ch:
case <-time.After(50 * time.Millisecond):
t.Fatalf("should have fired")
}
}
func TestSuspicion_Timer_Immediate(t *testing.T) {
ch := make(chan struct{}, 1)
f := func(int) {
ch <- struct{}{}
}
// This should underflow the timeout and fire immediately.
s := newSuspicion("me", 1, 100*time.Millisecond, 30*time.Second, f)
time.Sleep(200 * time.Millisecond)
s.Confirm("foo")
// Wait a little while since the function gets called in a goroutine.
select {
case <-ch:
case <-time.After(25 * time.Millisecond):
t.Fatalf("should have fired")
}
}