Skip to content

Commit a00bea2

Browse files
authored
Handle slicer Toxic with zero SizeVariation (#359)
This change skips randomized size variation for the slicer toxic if the SizeVariation parameter is 0, which is also the default setting. Additionally, it removes `rand.Intn(1)` was being added to mid to adjust for integer division rounding. However, rand.Intn(n) returns an integer in the range [0, n), so rand.Intn(1) always returns 0. It was removed.
1 parent a6cfd96 commit a00bea2

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Added verbose error on proxy upstream dialing (#355, @f-dg)
77
* Improve server startup message (#358, @areveny)
88
* Introduce yaml linter. (#362, @miry)
9+
* Handle slicer toxic with zero `SizeVariation` and fix slicing randomization (#359, @areveny)
910

1011
# [2.3.0] - 2021-12-23
1112

toxics/slicer.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ func (t *SlicerToxic) chunk(start int, end int) []int {
3737
return []int{start, end}
3838
}
3939

40-
// +1 in the size variation to offset favoring of smaller
41-
// numbers by integer division
40+
mid := start + (end-start)/2
4241
//#nosec
43-
mid := start + (end-start)/2 + (rand.Intn(t.SizeVariation*2) - t.SizeVariation) + rand.Intn(1)
42+
if t.SizeVariation > 0 {
43+
mid += rand.Intn(t.SizeVariation*2) - t.SizeVariation
44+
}
4445
left := t.chunk(start, mid)
4546
right := t.chunk(mid, end)
4647

toxics/slicer_test.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ func TestSlicerToxic(t *testing.T) {
3838

3939
buf := make([]byte, 0, len(data))
4040
reads := 0
41-
L:
42-
for {
41+
42+
for timeout := false; !timeout; {
4343
select {
4444
case c := <-output:
4545
reads++
4646
buf = append(buf, c.Data...)
4747
case <-time.After(10 * time.Millisecond):
48-
break L
48+
timeout = true
4949
}
5050
}
5151

@@ -56,3 +56,51 @@ L:
5656
t.Errorf("Server did not read correct buffer from client!")
5757
}
5858
}
59+
60+
func TestSlicerToxicZeroSizeVariation(t *testing.T) {
61+
data := []byte(strings.Repeat("hello world ", 2)) // 24 bytes
62+
// SizeVariation: 0 by default
63+
slicer := &toxics.SlicerToxic{AverageSize: 1, Delay: 10}
64+
65+
input := make(chan *stream.StreamChunk)
66+
output := make(chan *stream.StreamChunk)
67+
stub := toxics.NewToxicStub(input, output)
68+
69+
done := make(chan bool)
70+
go func() {
71+
slicer.Pipe(stub)
72+
done <- true
73+
}()
74+
defer func() {
75+
close(input)
76+
for {
77+
select {
78+
case <-done:
79+
return
80+
case <-output:
81+
}
82+
}
83+
}()
84+
85+
input <- &stream.StreamChunk{Data: data}
86+
87+
buf := make([]byte, 0, len(data))
88+
reads := 0
89+
90+
for timeout := false; !timeout; {
91+
select {
92+
case c := <-output:
93+
reads++
94+
buf = append(buf, c.Data...)
95+
case <-time.After(10 * time.Millisecond):
96+
timeout = true
97+
}
98+
}
99+
100+
if reads != 24 {
101+
t.Errorf("Expected to read 24 times, but read %d times.", reads)
102+
}
103+
if !bytes.Equal(buf, data) {
104+
t.Errorf("Server did not read correct buffer from client!")
105+
}
106+
}

0 commit comments

Comments
 (0)