This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology_test.go
184 lines (160 loc) · 4.04 KB
/
topology_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
package rdsmysql
import (
"testing"
"time"
)
func TestTopologyLeft(t *testing.T) {
var left []string
var now time.Time
s := newTopology(topologyOpts{
MaxTimeLeaving: time.Second,
OnLeave: func(id string) {
left = append(left, id)
},
Now: func() time.Time {
return now
},
})
now = date(1, 1)
s.SetAvailableFromReplicaHostStatus("a", []string{"a", "b", "c"})
assertEq(t,
[]string{"a", "b", "c"},
s.GetAvailable())
now = date(1, 2)
s.SetAvailableFromReplicaHostStatus("a", []string{"a", "b"})
assertEq(t,
[]string{"a", "b"},
s.GetAvailable())
now = date(1, 3)
s.ExecuteOnLeaveIfNeeded()
assertEq(t, 0, len(left), "none left")
now = date(1, 5)
s.ExecuteOnLeaveIfNeeded()
assertEq(t, []string{"c"}, left, "left")
}
func TestTopologyLeavingAndBack(t *testing.T) {
var left []string
var now time.Time
s := newTopology(topologyOpts{
MaxTimeLeaving: time.Second,
OnLeave: func(id string) {
left = append(left, id)
},
Now: func() time.Time {
return now
},
})
now = date(1, 1)
s.SetAvailableFromReplicaHostStatus("a", []string{"a", "b", "c"})
assertEq(t,
[]string{"a", "b", "c"},
s.GetAvailable())
now = date(1, 2)
s.SetAvailableFromReplicaHostStatus("a", []string{"a", "b"})
assertEq(t,
[]string{"a", "b"},
s.GetAvailable())
now = date(1, 3)
s.ExecuteOnLeaveIfNeeded()
assertEq(t, 0, len(left), "none left")
now = date(1, 4)
s.SetAvailableFromReplicaHostStatus("a", []string{"a", "b", "c"})
assertEq(t,
[]string{"a", "b", "c"},
s.GetAvailable())
now = date(1, 10)
s.ExecuteOnLeaveIfNeeded()
assertEq(t, 0, len(left), "none left")
}
func TestTopologyMarkFailedAndBack1(t *testing.T) {
var left []string
var now time.Time
s := newTopology(topologyOpts{
MaxTimeLeaving: time.Second,
FailDuration: 10 * time.Second,
OnLeave: func(id string) {
left = append(left, id)
},
Now: func() time.Time {
return now
},
})
now = date(1, 1)
s.SetAvailableFromReplicaHostStatus("a", []string{"a", "b", "c"})
assertEq(t,
[]string{"a", "b", "c"},
s.GetAvailable())
now = date(1, 2)
s.MarkFailed("c")
now = date(1, 4)
s.ExecuteOnLeaveIfNeeded()
assertEq(t, []string{"c"}, left, "left")
assertEq(t,
[]string{"a", "b"},
s.GetAvailable())
now = date(1, 12)
assertEq(t,
[]string{"a", "b", "c"},
s.GetAvailable())
}
func TestTopologyMarkFailedAndBack2(t *testing.T) {
var left []string
var now time.Time
s := newTopology(topologyOpts{
MaxTimeLeaving: time.Second,
FailDuration: 10 * time.Second,
OnLeave: func(id string) {
left = append(left, id)
},
Now: func() time.Time {
return now
},
})
now = date(1, 1)
s.SetAvailableFromReplicaHostStatus("a", []string{"a"})
s.MarkFailed("a")
now = date(1, 2)
s.SetAvailableFromReplicaHostStatus("a", []string{"a", "b"})
assertEq(t,
[]string{"b"},
s.GetAvailable())
}
// When query fails cluster marks the node as failed. Because multiple queries can happen at the same time, this can cause MarkFailed to be called multiple times. Make sure that last timestamp is used for failed and leaving.
func TestTopologyMarkFailedMultiple(t *testing.T) {
var left []string
var now time.Time
s := newTopology(topologyOpts{
MaxTimeLeaving: 10 * time.Second,
FailDuration: 20 * time.Second,
OnLeave: func(id string) {
left = append(left, id)
},
Now: func() time.Time {
return now
},
})
now = date(1, 0)
s.SetAvailableFromReplicaHostStatus("a", []string{"a", "b"})
now = date(1, 1)
s.MarkFailed("b")
now = date(1, 5)
s.MarkFailed("b")
now = date(1, 12)
s.ExecuteOnLeaveIfNeeded()
assertEq(t, 0, len(left),
"OnLeave should not be called for node yet, since we use last mark failed time for this")
assertEq(t,
[]string{"a"},
s.GetAvailable(), "node b should not be marked as available yet")
now = date(1, 16)
s.ExecuteOnLeaveIfNeeded()
assertEq(t, []string{"b"}, left,
"OnLeave should have been called")
assertEq(t,
[]string{"a"},
s.GetAvailable(), "node b should not be marked as available yet")
now = date(1, 26)
assertEq(t,
[]string{"a", "b"},
s.GetAvailable(), "node b should not be back")
}