forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransport_test.go
More file actions
377 lines (322 loc) · 9.43 KB
/
transport_test.go
File metadata and controls
377 lines (322 loc) · 9.43 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package transport
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.etcd.io/raft/v3"
"go.etcd.io/raft/v3/raftpb"
)
const testSnapSize = 1 << 20 // 1 MB
// Build a snapshot message where each byte in the data is of the value (index % sizeof(byte))
func newSnapshotMessage(from uint64, to uint64) raftpb.Message {
data := make([]byte, testSnapSize)
for i := 0; i < testSnapSize; i++ {
data[i] = byte(i % (1 << 8))
}
return raftpb.Message{
Type: raftpb.MsgSnap,
From: from,
To: to,
Snapshot: &raftpb.Snapshot{
Data: data,
// Include the snapshot size in the Index field for testing.
Metadata: raftpb.SnapshotMetadata{
Index: uint64(len(data)),
},
},
}
}
// Verify that the snapshot data where each byte is of the value (index % sizeof(byte)).
func verifySnapshot(raftMsg *raftpb.Message) bool {
for i, b := range raftMsg.Snapshot.Data {
if int(b) != i%(1<<8) {
return false
}
}
return len(raftMsg.Snapshot.Data) == int(raftMsg.Snapshot.Metadata.Index)
}
func sendMessages(ctx context.Context, c *mockCluster, from uint64, to []uint64, msgType raftpb.MessageType) error {
var firstErr error
for _, id := range to {
var err error
if msgType == raftpb.MsgSnap {
err = c.Get(from).tr.Send(newSnapshotMessage(from, id))
} else {
err = c.Get(from).tr.Send(raftpb.Message{
Type: msgType,
From: from,
To: id,
})
}
if firstErr == nil {
firstErr = err
}
}
return firstErr
}
func testSend(ctx context.Context, c *mockCluster, from uint64, to []uint64, msgType raftpb.MessageType) func(*testing.T) {
return func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
defer cancel()
require.NoError(t, sendMessages(ctx, c, from, to, msgType))
for _, id := range to {
select {
case msg := <-c.Get(id).processedMessages:
assert.Equal(t, msg.To, id)
assert.Equal(t, msg.From, from)
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
if msgType == raftpb.MsgSnap {
var snaps []snapshotReport
for i := 0; i < len(to); i++ {
select {
case snap := <-c.Get(from).processedSnapshots:
snaps = append(snaps, snap)
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
loop:
for _, id := range to {
for _, s := range snaps {
if s.id == id {
assert.Equal(t, s.status, raft.SnapshotFinish)
continue loop
}
}
t.Fatalf("snapshot id %d is not reported", id)
}
}
}
}
// TestSplitSnapshotDataDoesNotMutateInput is a regression test for #3231.
// Before the fix, splitSnapshotData did a shallow copy of the raft message
// and re-sliced the shared Snapshot.Data on each iteration, shrinking the
// original slice's capacity and eventually panicking with
// "slice bounds out of range".
func TestSplitSnapshotDataDoesNotMutateInput(t *testing.T) {
ctx := context.Background()
// Build a MsgSnap whose Snapshot.Data clearly exceeds GRPCMaxMsgSize so
// that the split loop runs multiple iterations (where the bug manifests).
const dataSize = 3 * GRPCMaxMsgSize
data := make([]byte, dataSize)
for i := range data {
data[i] = byte(i % (1 << 8))
}
m := raftpb.Message{
Type: raftpb.MsgSnap,
From: 1,
To: 2,
Snapshot: &raftpb.Snapshot{
Data: data,
Metadata: raftpb.SnapshotMetadata{
Index: uint64(len(data)),
},
},
}
origData := m.Snapshot.Data
origLen, origCap := len(origData), cap(origData)
msgs := splitSnapshotData(ctx, &m)
require.Greater(t, len(msgs), 1, "data larger than GRPCMaxMsgSize must split into multiple chunks")
// Chunks must reassemble to the original data.
var assembled []byte
for _, msg := range msgs {
assembled = append(assembled, msg.Message.Snapshot.Data...)
}
assert.Equal(t, data, assembled)
// The input message's Snapshot.Data must be untouched (regression guard).
assert.Equal(t, origLen, len(m.Snapshot.Data))
assert.Equal(t, origCap, cap(m.Snapshot.Data))
assert.Equal(t, data, m.Snapshot.Data)
}
func TestSend(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
c := newCluster()
defer func() {
cancel()
c.Stop()
}()
require.NoError(t, c.Add(1))
require.NoError(t, c.Add(2))
require.NoError(t, c.Add(3))
t.Run("Send Message", testSend(ctx, c, 1, []uint64{2, 3}, raftpb.MsgHup))
t.Run("Send_Snapshot_Message", testSend(ctx, c, 1, []uint64{2, 3}, raftpb.MsgSnap))
// Return error on streaming.
for _, raft := range c.rafts {
raft.forceErrorStream = true
}
// Messages should still be delivered.
t.Run("Send Message", testSend(ctx, c, 1, []uint64{2, 3}, raftpb.MsgHup))
}
func TestSendRemoved(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
c := newCluster()
defer func() {
cancel()
c.Stop()
}()
require.NoError(t, c.Add(1))
require.NoError(t, c.Add(2))
require.NoError(t, c.Add(3))
require.NoError(t, c.Get(1).RemovePeer(2))
err := sendMessages(ctx, c, 1, []uint64{2, 3}, raftpb.MsgHup)
require.Error(t, err)
require.Contains(t, err.Error(), "to removed member")
}
func TestSendSnapshotFailure(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
c := newCluster()
defer func() {
cancel()
c.Stop()
}()
require.NoError(t, c.Add(1))
require.NoError(t, c.Add(2))
// stop peer server to emulate error
c.Get(2).s.Stop()
msgCtx, msgCancel := context.WithTimeout(ctx, 4*time.Second)
defer msgCancel()
require.NoError(t, sendMessages(msgCtx, c, 1, []uint64{2}, raftpb.MsgSnap))
select {
case snap := <-c.Get(1).processedSnapshots:
assert.Equal(t, snap.id, uint64(2))
assert.Equal(t, snap.status, raft.SnapshotFailure)
case <-msgCtx.Done():
t.Fatal(ctx.Err())
}
select {
case id := <-c.Get(1).reportedUnreachables:
assert.Equal(t, id, uint64(2))
case <-msgCtx.Done():
t.Fatal(ctx.Err())
}
}
func TestSendUnknown(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
c := newCluster()
defer func() {
cancel()
c.Stop()
}()
require.NoError(t, c.Add(1))
require.NoError(t, c.Add(2))
require.NoError(t, c.Add(3))
// remove peer from 1 transport to make it "unknown" to it
oldPeer := c.Get(1).tr.peers[2]
delete(c.Get(1).tr.peers, 2)
oldPeer.cancel()
<-oldPeer.done
// give peers time to mark each other as active
time.Sleep(1 * time.Second)
msgCtx, msgCancel := context.WithTimeout(ctx, 4*time.Second)
defer msgCancel()
require.NoError(t, sendMessages(msgCtx, c, 1, []uint64{2}, raftpb.MsgHup))
select {
case msg := <-c.Get(2).processedMessages:
assert.Equal(t, msg.To, uint64(2))
assert.Equal(t, msg.From, uint64(1))
case <-msgCtx.Done():
t.Fatal(msgCtx.Err())
}
}
func TestUpdatePeerAddr(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
c := newCluster()
defer func() {
cancel()
c.Stop()
}()
require.NoError(t, c.Add(1))
require.NoError(t, c.Add(2))
require.NoError(t, c.Add(3))
t.Run("Send Message Before Address Update", testSend(ctx, c, 1, []uint64{2, 3}, raftpb.MsgHup))
nr, err := newMockRaft()
require.NoError(t, err)
c.Get(3).Stop()
c.rafts[3] = nr
require.NoError(t, c.Get(1).tr.UpdatePeer(3, nr.Addr()))
require.NoError(t, c.Get(1).tr.UpdatePeer(3, nr.Addr()))
t.Run("Send Message After Address Update", testSend(ctx, c, 1, []uint64{2, 3}, raftpb.MsgHup))
}
func TestUpdatePeerAddrDelayed(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
c := newCluster()
defer func() {
cancel()
c.Stop()
}()
require.NoError(t, c.Add(1))
require.NoError(t, c.Add(2))
require.NoError(t, c.Add(3))
t.Run("Send Message Before Address Update", testSend(ctx, c, 1, []uint64{2, 3}, raftpb.MsgHup))
nr, err := newMockRaft()
require.NoError(t, err)
c.Get(3).Stop()
c.rafts[3] = nr
require.NoError(t, c.Get(1).tr.UpdatePeerAddr(3, nr.Addr()))
// initiate failure to replace connection, and wait for it
sendMessages(ctx, c, 1, []uint64{3}, raftpb.MsgHup)
updateCtx, updateCancel := context.WithTimeout(ctx, 4*time.Second)
defer updateCancel()
select {
case update := <-c.Get(1).updatedNodes:
require.Equal(t, update.id, uint64(3))
require.Equal(t, update.addr, nr.Addr())
case <-updateCtx.Done():
t.Fatal(updateCtx.Err())
}
t.Run("Send Message After Address Update", testSend(ctx, c, 1, []uint64{2, 3}, raftpb.MsgHup))
}
func TestSendUnreachable(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
c := newCluster()
defer func() {
cancel()
c.Stop()
}()
require.NoError(t, c.Add(1))
require.NoError(t, c.Add(2))
// set channel to nil to emulate full queue
// we need to reset some fields after cancel
p2 := c.Get(1).tr.peers[2]
p2.cancel()
<-p2.done
p2.msgc = nil
p2.done = make(chan struct{})
p2.ctx = ctx
go p2.run(ctx)
msgCtx, msgCancel := context.WithTimeout(ctx, 4*time.Second)
defer msgCancel()
err := sendMessages(msgCtx, c, 1, []uint64{2}, raftpb.MsgSnap)
require.Error(t, err)
require.Contains(t, err.Error(), "peer is unreachable")
select {
case id := <-c.Get(1).reportedUnreachables:
assert.Equal(t, id, uint64(2))
case <-msgCtx.Done():
t.Fatal(ctx.Err())
}
}
func TestSendNodeRemoved(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
c := newCluster()
defer func() {
cancel()
c.Stop()
}()
require.NoError(t, c.Add(1))
require.NoError(t, c.Add(2))
require.NoError(t, c.Get(1).RemovePeer(2))
msgCtx, msgCancel := context.WithTimeout(ctx, 4*time.Second)
defer msgCancel()
require.NoError(t, sendMessages(msgCtx, c, 2, []uint64{1}, raftpb.MsgSnap))
select {
case <-c.Get(2).nodeRemovedSignal:
case <-msgCtx.Done():
t.Fatal(msgCtx.Err())
}
}