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
|
package toxics_test
import (
"bytes"
"strings"
"testing"
"time"
"github.com/Shopify/toxiproxy/stream"
"github.com/Shopify/toxiproxy/toxics"
)
func TestSlicerToxic(t *testing.T) {
data := []byte(strings.Repeat("hello world ", 40000)) // 480 kb
slicer := &toxics.SlicerToxic{AverageSize: 1024, SizeVariation: 512, Delay: 10}
input := make(chan *stream.StreamChunk)
output := make(chan *stream.StreamChunk)
stub := toxics.NewToxicStub(input, output)
done := make(chan bool)
go func() {
slicer.Pipe(stub)
done <- true
}()
defer func() {
close(input)
for {
select {
case <-done:
return
case <-output:
}
}
}()
input <- &stream.StreamChunk{Data: data}
buf := make([]byte, 0, len(data))
reads := 0
L:
for {
select {
case c := <-output:
reads++
buf = append(buf, c.Data...)
case <-time.After(10 * time.Millisecond):
break L
}
}
if reads < 480/2 || reads > 480/2+480 {
t.Errorf("Expected to read about 480 times, but read %d times.", reads)
}
if bytes.Compare(buf, data) != 0 {
t.Errorf("Server did not read correct buffer from client!")
}
}
|