forked from Shopify/toxiproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_test.go
325 lines (289 loc) · 8.05 KB
/
link_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
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
package toxiproxy
import (
"context"
"encoding/binary"
"flag"
"io"
"os"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/Shopify/toxiproxy/v2/stream"
"github.com/Shopify/toxiproxy/v2/testhelper"
"github.com/Shopify/toxiproxy/v2/toxics"
)
func TestToxicsAreLoaded(t *testing.T) {
if toxics.Count() < 1 {
t.Fatal("No toxics loaded!")
}
}
func TestStubInitializaation(t *testing.T) {
collection := NewToxicCollection(nil)
link := NewToxicLink(nil, collection, stream.Downstream, zerolog.Nop())
if len(link.stubs) != 1 {
t.Fatalf("Link created with wrong number of stubs: %d != 1", len(link.stubs))
}
if cap(link.stubs) != toxics.Count()+1 {
t.Fatalf("Link created with wrong capacity: %d != %d", cap(link.stubs), toxics.Count()+1)
}
if cap(link.stubs[0].Input) != 0 {
t.Fatalf("Noop buffer was not initialized as 0: %d", cap(link.stubs[0].Input))
}
if cap(link.stubs[0].Output) != 0 {
t.Fatalf("Link output buffer was not initialized as 0: %d", cap(link.stubs[0].Output))
}
}
func TestStubInitializaationWithToxics(t *testing.T) {
collection := NewToxicCollection(nil)
collection.chainAddToxic(&toxics.ToxicWrapper{
Toxic: new(toxics.LatencyToxic),
Type: "latency",
Direction: stream.Downstream,
BufferSize: 1024,
Toxicity: 1,
})
collection.chainAddToxic(&toxics.ToxicWrapper{
Toxic: new(toxics.BandwidthToxic),
Type: "bandwidth",
Direction: stream.Downstream,
Toxicity: 1,
})
link := NewToxicLink(nil, collection, stream.Downstream, zerolog.Nop())
if len(link.stubs) != 3 {
t.Fatalf("Link created with wrong number of stubs: %d != 3", len(link.stubs))
}
if cap(link.stubs) != toxics.Count()+1 {
t.Fatalf("Link created with wrong capacity: %d != %d", cap(link.stubs), toxics.Count()+1)
}
if cap(link.stubs[len(link.stubs)-1].Output) != 0 {
t.Fatalf("Link output buffer was not initialized as 0: %d", cap(link.stubs[0].Output))
}
for i, toxic := range collection.chain[stream.Downstream] {
if cap(link.stubs[i].Input) != toxic.BufferSize {
t.Fatalf(
"%s buffer was not initialized as %d: %d",
toxic.Type,
toxic.BufferSize,
cap(link.stubs[i].Input),
)
}
}
}
func TestAddRemoveStubs(t *testing.T) {
ctx := context.Background()
collection := NewToxicCollection(nil)
link := NewToxicLink(nil, collection, stream.Downstream, zerolog.Nop())
go link.stubs[0].Run(collection.chain[stream.Downstream][0])
collection.links["test"] = link
// Add stubs
collection.chainAddToxic(&toxics.ToxicWrapper{
Toxic: new(toxics.LatencyToxic),
Type: "latency",
Direction: stream.Downstream,
BufferSize: 1024,
Toxicity: 1,
})
toxic := &toxics.ToxicWrapper{
Toxic: new(toxics.BandwidthToxic),
Type: "bandwidth",
Direction: stream.Downstream,
BufferSize: 2048,
Toxicity: 1,
}
collection.chainAddToxic(toxic)
if cap(link.stubs[len(link.stubs)-1].Output) != 0 {
t.Fatalf("Link output buffer was not initialized as 0: %d", cap(link.stubs[0].Output))
}
for i, toxic := range collection.chain[stream.Downstream] {
if cap(link.stubs[i].Input) != toxic.BufferSize {
t.Fatalf(
"%s buffer was not initialized as %d: %d",
toxic.Type,
toxic.BufferSize,
cap(link.stubs[i].Input),
)
}
}
// Remove stubs
collection.chainRemoveToxic(ctx, toxic)
if cap(link.stubs[len(link.stubs)-1].Output) != 0 {
t.Fatalf("Link output buffer was not initialized as 0: %d", cap(link.stubs[0].Output))
}
for i, toxic := range collection.chain[stream.Downstream] {
if cap(link.stubs[i].Input) != toxic.BufferSize {
t.Fatalf(
"%s buffer was not initialized as %d: %d",
toxic.Type,
toxic.BufferSize,
cap(link.stubs[i].Input),
)
}
}
}
func TestNoDataDropped(t *testing.T) {
ctx := context.Background()
collection := NewToxicCollection(nil)
link := NewToxicLink(nil, collection, stream.Downstream, zerolog.Nop())
go link.stubs[0].Run(collection.chain[stream.Downstream][0])
collection.links["test"] = link
toxic := &toxics.ToxicWrapper{
Toxic: &toxics.LatencyToxic{
Latency: 1000,
},
Type: "latency",
Direction: stream.Downstream,
BufferSize: 1024,
Toxicity: 1,
}
done := make(chan struct{})
defer close(done)
go func() {
for i := 0; i < 64*1024; i++ {
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, uint16(i))
link.input.Write(buf)
}
link.input.Close()
}()
go func(ctx context.Context) {
for {
select {
case <-done:
return
default:
collection.chainAddToxic(toxic)
collection.chainRemoveToxic(ctx, toxic)
}
}
}(ctx)
buf := make([]byte, 2)
for i := 0; i < 64*1024; i++ {
n, err := link.output.Read(buf)
if n != 2 || err != nil {
t.Fatalf("Read failed: %d %v", n, err)
} else {
val := binary.BigEndian.Uint16(buf)
if val != uint16(i) {
t.Fatalf("Read incorrect bytes: %v != %d", val, i)
}
}
}
n, err := link.output.Read(buf)
if n != 0 || err != io.EOF {
t.Fatalf("Expected EOF: %d %v", n, err)
}
}
func TestToxicity(t *testing.T) {
collection := NewToxicCollection(nil)
link := NewToxicLink(nil, collection, stream.Downstream, zerolog.Nop())
go link.stubs[0].Run(collection.chain[stream.Downstream][0])
collection.links["test"] = link
toxic := &toxics.ToxicWrapper{
Toxic: new(toxics.TimeoutToxic),
Name: "timeout1",
Type: "timeout",
Direction: stream.Downstream,
Toxicity: 0,
}
collection.chainAddToxic(toxic)
// Toxic should be a Noop because of toxicity
n, err := link.input.Write([]byte{42})
if n != 1 || err != nil {
t.Fatalf("Write failed: %d %v", n, err)
}
buf := make([]byte, 2)
n, err = link.output.Read(buf)
if n != 1 || err != nil {
t.Fatalf("Read failed: %d %v", n, err)
} else if buf[0] != 42 {
t.Fatalf("Read wrong byte: %x", buf[0])
}
toxic.Toxicity = 1
toxic.Toxic.(*toxics.TimeoutToxic).Timeout = 100
collection.chainUpdateToxic(toxic)
err = testhelper.TimeoutAfter(150*time.Millisecond, func() {
n, err = link.input.Write([]byte{42})
if n != 1 || err != nil {
t.Fatalf("Write failed: %d %v", n, err)
}
n, err = link.output.Read(buf)
if n != 0 || err != io.EOF {
t.Fatalf("Read did not get EOF: %d %v", n, err)
}
})
if err != nil {
t.Fatal(err)
}
}
func TestStateCreated(t *testing.T) {
collection := NewToxicCollection(nil)
log := zerolog.Nop()
if flag.Lookup("test.v").DefValue == "true" {
log = zerolog.New(os.Stdout).With().Caller().Timestamp().Logger()
}
link := NewToxicLink(nil, collection, stream.Downstream, log)
go link.stubs[0].Run(collection.chain[stream.Downstream][0])
collection.links["test"] = link
collection.chainAddToxic(&toxics.ToxicWrapper{
Toxic: new(toxics.LimitDataToxic),
Type: "limit_data",
Direction: stream.Downstream,
Toxicity: 1,
})
if link.stubs[len(link.stubs)-1].State == nil {
t.Fatalf("New toxic did not have state object created.")
}
}
func TestRemoveToxicWithBrokenConnection(t *testing.T) {
ctx := context.Background()
log := zerolog.Nop()
if flag.Lookup("test.v").DefValue == "true" {
log = zerolog.New(os.Stdout).With().Caller().Timestamp().Logger()
}
ctx = log.WithContext(ctx)
collection := NewToxicCollection(nil)
link := NewToxicLink(nil, collection, stream.Downstream, log)
go link.stubs[0].Run(collection.chain[stream.Downstream][0])
collection.links["test"] = link
toxics := [2]*toxics.ToxicWrapper{
{
Toxic: &toxics.BandwidthToxic{
Rate: 0,
},
Type: "bandwidth",
Direction: stream.Downstream,
Toxicity: 1,
},
{
Toxic: &toxics.BandwidthToxic{
Rate: 0,
},
Type: "bandwidth",
Direction: stream.Upstream,
Toxicity: 1,
},
}
collection.chainAddToxic(toxics[0])
collection.chainAddToxic(toxics[1])
done := make(chan struct{})
defer close(done)
var data uint16 = 42
go func(log zerolog.Logger) {
for {
select {
case <-done:
link.input.Close()
return
case <-time.After(10 * time.Second):
log.Print("Finish load")
return
default:
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, data)
link.input.Write(buf)
}
}
}(log)
collection.chainRemoveToxic(ctx, toxics[0])
collection.chainRemoveToxic(ctx, toxics[1])
}