-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifo_test.go
237 lines (205 loc) · 6.72 KB
/
fifo_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
package main
import (
"bytes"
"context"
"io"
"sync"
"testing"
"time"
"github.com/fsnotify/fsnotify"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFIFO_integration(t *testing.T) {
addr, err := parseNetAddress("10.10.1.1")
require.NoError(t, err)
refreshCounter := map[string]int{}
provider := &fakeElasticIPProvider{refreshCounter: refreshCounter}
cfg := notifyConfig{
ManagedAddresses: []netAddress{addr},
RefreshInterval: 100 * time.Millisecond,
RefreshTimeout: time.Second,
}
handler, pipe, eventChan := SetupFIFOTest(t, defaultNotificatonHandler(provider, cfg))
ctx, done := context.WithCancel(context.Background())
defer done()
go func() {
assert.NoError(t, handler.HandleFifo(ctx), "Handler should not fail")
}()
WriteToPipe(t, pipe, eventChan, "INSTANCE \"foo\" MASTER 100\n")
require.Eventuallyf(t, func() bool {
c, ok := refreshCounter[addr.String()]
return ok && c > 0
}, time.Second, 50*time.Millisecond, "Not updating IP as master")
WriteToPipe(t, pipe, eventChan, "INSTANCE \"foo\" BACKUP 100\n")
oldc := 0
require.Eventually(t, func() bool {
c, ok := refreshCounter[addr.String()]
res := ok && c == oldc
oldc = c
return res
}, 5*time.Second, 500*time.Millisecond, "Not stopping to update IP")
}
func TestFIFO_interleaving(t *testing.T) {
nh := newFakeNotificationHandler()
handler, pipe, eventChan := SetupFIFOTest(t, nh.GetHandler(t))
ctx, done := context.WithCancel(context.Background())
defer done()
go func() {
assert.NoError(t, handler.HandleFifo(ctx), "Handler should not fail")
}()
WriteToPipe(t, pipe, eventChan, "INSTANCE \"bar\" MASTER 100\n")
WriteToPipe(t, pipe, eventChan, "INSTANCE \"foo\" MASTER 100\n")
WriteToPipe(t, pipe, eventChan, "\n")
WriteToPipe(t, pipe, eventChan, "INSTANCE \"bar\" BACKUP 100\n")
nh.isEventuallyMaster(t, "foo")
nh.isEventuallyNotMaster(t, "bar")
WriteToPipe(t, pipe, eventChan, "INSTANCE \"foo\" FAULT 100\nINSTANCE \"bar\" FAULT 100\nINSTANCE \"bar\" MASTER 100\n")
nh.isEventuallyNotMaster(t, "foo")
nh.isEventuallyMaster(t, "bar")
WriteToPipe(t, pipe, eventChan, "GROUP \"bar\" BACKUP 100\n")
WriteToPipe(t, pipe, eventChan, "G s\"bar\" BACKUP 100\n")
nh.isEventuallyNotMaster(t, "foo")
nh.isEventuallyMaster(t, "bar")
WriteToPipe(t, pipe, eventChan, "INSTANCE \"bar\" BACKUP 100\n")
nh.isEventuallyNotMaster(t, "bar")
WriteToPipe(t, pipe, eventChan, "INSTANCE \"foo\" MASTER 100\n")
nh.isEventuallyMaster(t, "foo")
}
func TestFIFO_beforeStart(t *testing.T) {
nh := newFakeNotificationHandler()
handler, pipe, eventChan := SetupFIFOTest(t, nh.GetHandler(t))
ctx, done := context.WithCancel(context.Background())
defer done()
_, err := pipe.Write([]byte("INSTANCE \"foo\" MASTER 100\n"))
require.NoError(t, err)
_, err = pipe.Write([]byte("INSTANCE \"foo\" FAULT 100\n"))
require.NoError(t, err)
_, err = pipe.Write([]byte("INSTANCE \"foo\" MASTER 100\n"))
require.NoError(t, err)
_, err = pipe.Write([]byte("INSTANCE \"bar\" FAULT 100\n"))
require.NoError(t, err)
go func() {
assert.NoError(t, handler.HandleFifo(ctx), "Handler should not fail")
}()
time.Sleep(100 * time.Millisecond)
nh.isEventuallyMaster(t, "foo")
WriteToPipe(t, pipe, eventChan, "INSTANCE \"foo\" FAULT 100\n")
nh.isEventuallyNotMaster(t, "foo")
}
func TestFIFO_withEOF(t *testing.T) {
nh := newFakeNotificationHandler()
handler, pipe, eventChan := SetupFIFOTest(t, nh.GetHandler(t))
ctx, done := context.WithCancel(context.Background())
defer done()
go func() {
assert.NoError(t, handler.HandleFifo(ctx), "Handler should not fail")
}()
pipe.setSendEOF(true)
WriteToPipe(t, pipe, eventChan, "INSTANCE \"bar\" MASTER 100\n")
nh.isEventuallyMaster(t, "bar")
WriteToPipe(t, pipe, eventChan, "INSTANCE \"foo\" FAULT 100\nINSTANCE \"bar\" FAULT 100\nINSTANCE \"bar\" MASTER 100\n")
nh.isEventuallyMaster(t, "bar")
WriteToPipe(t, pipe, eventChan, "GROUP \"bar\" BACKUP 100\n")
WriteToPipe(t, pipe, eventChan, "G s\"bar\" BACKUP 100\n")
nh.isEventuallyMaster(t, "bar")
WriteToPipe(t, pipe, eventChan, "INSTANCE \"bar\" BACKUP 100\n")
nh.isEventuallyNotMaster(t, "bar")
}
type testBuffer struct {
p bytes.Buffer
mu sync.Mutex
sendEOF bool
}
func (r *testBuffer) Read(p []byte) (int, error) {
r.mu.Lock()
defer r.mu.Unlock()
n, err := r.p.Read(p)
for err == io.EOF {
if n == 0 {
r.mu.Unlock()
// We give the writer time to actually write to the buffer
time.Sleep(time.Millisecond)
r.mu.Lock()
n, err = r.p.Read(p)
} else {
return n, nil
}
}
return n, err
}
func (r *testBuffer) Write(p []byte) (int, error) {
r.mu.Lock()
defer r.mu.Unlock()
return r.p.Write(p)
}
func (r *testBuffer) setSendEOF(sendEOF bool) {
r.mu.Lock()
defer r.mu.Unlock()
r.sendEOF = sendEOF
}
func SetupFIFOTest(t *testing.T, fn notificationHandlerFunc) (FifoHandler, *testBuffer, chan fsnotify.Event) {
pipe := testBuffer{
mu: sync.Mutex{},
sendEOF: false,
}
eventChan := make(chan fsnotify.Event, 30)
handler := FifoHandler{
pipe: &pipe,
events: eventChan,
running: map[string]context.CancelFunc{},
handleNotification: fn,
}
return handler, &pipe, eventChan
}
func WriteToPipe(t *testing.T, pipe io.Writer, events chan fsnotify.Event, content string) {
_, err := pipe.Write([]byte(content))
require.NoError(t, err, "Failed to write to pipe, test is probably wrong")
events <- fsnotify.Event{
Op: fsnotify.Write,
}
}
type fakeHandlerState struct {
ctx context.Context
master bool
}
type fakeNotificationHandler struct {
mu sync.Mutex
running map[string]fakeHandlerState
}
func newFakeNotificationHandler() *fakeNotificationHandler {
return &fakeNotificationHandler{
mu: sync.Mutex{},
running: map[string]fakeHandlerState{},
}
}
func (h *fakeNotificationHandler) GetHandler(t *testing.T) notificationHandlerFunc {
return func(ctx context.Context, notification Notification) {
h.mu.Lock()
defer h.mu.Unlock()
oldCtx, ok := h.running[notification.Instance]
if ok {
assert.Error(t, oldCtx.ctx.Err(), "old handler not stopped")
}
h.running[notification.Instance] = fakeHandlerState{
ctx: ctx,
master: notification.Status == NotificationMaster,
}
}
}
func (h *fakeNotificationHandler) isEventuallyMaster(t *testing.T, instance string) {
require.Eventuallyf(t, func() bool {
h.mu.Lock()
defer h.mu.Unlock()
s, ok := h.running[instance]
return ok && s.master
}, time.Second, 50*time.Millisecond, "%s should be in master state", instance)
}
func (h *fakeNotificationHandler) isEventuallyNotMaster(t *testing.T, instance string) {
require.Eventuallyf(t, func() bool {
h.mu.Lock()
defer h.mu.Unlock()
s, ok := h.running[instance]
return !ok || !s.master
}, time.Second, 50*time.Millisecond, "%s should be in master state", instance)
}