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
|
package toxics_test
import (
"bytes"
"io"
"net"
"strings"
"testing"
"time"
"github.com/Shopify/toxiproxy/toxics"
)
func TestBandwidthToxic(t *testing.T) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal("Failed to create TCP server", err)
}
defer ln.Close()
proxy := NewTestProxy("test", ln.Addr().String())
proxy.Start()
defer proxy.Stop()
serverConnRecv := make(chan net.Conn)
go func() {
conn, err := ln.Accept()
if err != nil {
t.Error("Unable to accept TCP connection", err)
}
serverConnRecv <- conn
}()
conn, err := net.Dial("tcp", proxy.Listen)
if err != nil {
t.Error("Unable to dial TCP server", err)
}
serverConn := <-serverConnRecv
rate := 1000 // 1MB/s
proxy.Toxics.AddToxicJson(ToxicToJson(t, "", "bandwidth", "upstream", &toxics.BandwidthToxic{Rate: int64(rate)}))
buf := []byte(strings.Repeat("hello world ", 40000)) // 480KB
go func() {
n, err := conn.Write(buf)
conn.Close()
if n != len(buf) || err != nil {
t.Errorf("Failed to write buffer: (%d == %d) %v", n, len(buf), err)
}
}()
buf2 := make([]byte, len(buf))
start := time.Now()
_, err = io.ReadAtLeast(serverConn, buf2, len(buf2))
if err != nil {
t.Errorf("Proxy read failed: %v", err)
} else if bytes.Compare(buf, buf2) != 0 {
t.Errorf("Server did not read correct buffer from client!")
}
AssertDeltaTime(t,
"Bandwidth",
time.Since(start),
time.Duration(len(buf))*time.Second/time.Duration(rate*1000),
10*time.Millisecond,
)
}
func BenchmarkBandwidthToxic100MB(b *testing.B) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
b.Fatal("Failed to create TCP server", err)
}
defer ln.Close()
proxy := NewTestProxy("test", ln.Addr().String())
proxy.Start()
defer proxy.Stop()
buf := []byte(strings.Repeat("hello world ", 1000))
go func() {
conn, err := ln.Accept()
if err != nil {
b.Error("Unable to accept TCP connection", err)
}
buf2 := make([]byte, len(buf))
for err == nil {
_, err = conn.Read(buf2)
}
}()
conn, err := net.Dial("tcp", proxy.Listen)
if err != nil {
b.Error("Unable to dial TCP server", err)
}
proxy.Toxics.AddToxicJson(ToxicToJson(nil, "", "bandwidth", "upstream", &toxics.BandwidthToxic{Rate: 100 * 1000}))
b.SetBytes(int64(len(buf)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
n, err := conn.Write(buf)
if err != nil || n != len(buf) {
b.Errorf("%v, %d == %d", err, n, len(buf))
break
}
}
err = conn.Close()
if err != nil {
b.Error("Failed to close TCP connection", err)
}
}
|