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
|
package toxiproxy
import (
"encoding/binary"
"io"
"testing"
"github.com/Shopify/toxiproxy/stream"
"github.com/Shopify/toxiproxy/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)
if len(link.stubs) != 1 {
t.Fatalf("Link created with wrong number of stubs: %d != 1", len(link.stubs))
} else if cap(link.stubs) != toxics.Count()+1 {
t.Fatalf("Link created with wrong capacity: %d != %d", cap(link.stubs), toxics.Count()+1)
} else if cap(link.stubs[0].Input) != 0 {
t.Fatalf("Noop buffer was not initialized as 0: %d", cap(link.stubs[0].Input))
} else 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)
if len(link.stubs) != 3 {
t.Fatalf("Link created with wrong number of stubs: %d != 3", len(link.stubs))
} else if cap(link.stubs) != toxics.Count()+1 {
t.Fatalf("Link created with wrong capacity: %d != %d", cap(link.stubs), toxics.Count()+1)
} else 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) {
collection := NewToxicCollection(nil)
link := NewToxicLink(nil, collection, stream.Downstream)
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(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) {
collection := NewToxicCollection(nil)
link := NewToxicLink(nil, collection, stream.Downstream)
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() {
for {
select {
case <-done:
return
default:
collection.chainAddToxic(toxic)
collection.chainRemoveToxic(toxic)
}
}
}()
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)
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)
// Toxic should timeout after 100ms
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)
}
}
|