Skip to content

Commit

Permalink
Merge pull request #146 from grafana/fix-delay-argument-missing-unit
Browse files Browse the repository at this point in the history
Fix delay argument in fault injection
  • Loading branch information
pablochacin authored Apr 27, 2023
2 parents 7d19c2d + aabe2b7 commit 884bfb2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
7 changes: 4 additions & 3 deletions pkg/agent/protocol/grpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ func (h *handler) streamHandler(srv interface{}, serverStream grpc.ServerStream)

// add delay
if h.disruption.AverageDelay > 0 {
delay := int(h.disruption.AverageDelay)
delay := int64(h.disruption.AverageDelay)
if h.disruption.DelayVariation > 0 {
delay = delay + int(h.disruption.DelayVariation) - 2*rand.Intn(int(h.disruption.DelayVariation))
variation := int64(h.disruption.DelayVariation)
delay = delay + variation - 2*rand.Int63n(variation)
}
time.Sleep(time.Duration(delay) * time.Millisecond)
time.Sleep(time.Duration(delay))
}
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/agent/protocol/http/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ func (h *httpHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

if !excluded && h.disruption.AverageDelay > 0 {
delay := int(h.disruption.AverageDelay)
delay := int64(h.disruption.AverageDelay)
if h.disruption.DelayVariation > 0 {
delay = delay + int(h.disruption.DelayVariation) - 2*rand.Intn(int(h.disruption.DelayVariation))
variation := int64(h.disruption.DelayVariation)
delay = delay + variation - 2*rand.Int63n(variation)
}
time.Sleep(time.Duration(delay) * time.Millisecond)
time.Sleep(time.Duration(delay))
}

// return response to the client
Expand Down
8 changes: 4 additions & 4 deletions pkg/disruptors/cmd_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func buildGrpcFaultCmd(fault GrpcFault, duration time.Duration, options GrpcDisr
cmd = append(
cmd,
"-a",
fmt.Sprint(fault.AverageDelay.Milliseconds()),
utils.DurationMillSeconds(fault.AverageDelay),
"-v",
fmt.Sprint(fault.DelayVariation.Milliseconds()),
utils.DurationMillSeconds(fault.DelayVariation),
)
}

Expand Down Expand Up @@ -69,9 +69,9 @@ func buildHTTPFaultCmd(fault HTTPFault, duration time.Duration, options HTTPDisr
cmd = append(
cmd,
"-a",
fmt.Sprint(fault.AverageDelay.Milliseconds()),
utils.DurationMillSeconds(fault.AverageDelay),
"-v",
fmt.Sprint(fault.DelayVariation.Milliseconds()),
utils.DurationMillSeconds(fault.DelayVariation),
)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/disruptors/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func Test_PodHTTPFaultInjection(t *testing.T) {
},
},
targets: []string{"my-app-pod"},
expectedCmd: "xk6-disruptor-agent http -d 60s -a 100 -v 0",
expectedCmd: "xk6-disruptor-agent http -d 60s -a 100ms -v 0ms",
expectError: false,
cmdError: nil,
fault: HTTPFault{
Expand Down Expand Up @@ -243,7 +243,7 @@ func Test_PodGrpcPFaultInjection(t *testing.T) {
{
title: "Test error with status message",
selector: PodSelector{
Namespace: " duration: 60,testns",
Namespace: "testns",
Select: PodAttributes{
Labels: map[string]string{
"app": "myapp",
Expand Down Expand Up @@ -278,7 +278,7 @@ func Test_PodGrpcPFaultInjection(t *testing.T) {
},
opts: GrpcDisruptionOptions{},
duration: 60 * time.Second,
expectedCmd: "xk6-disruptor-agent grpc -d 60s -a 100 -v 0",
expectedCmd: "xk6-disruptor-agent grpc -d 60s -a 100ms -v 0ms",
expectError: false,
cmdError: nil,
},
Expand Down
5 changes: 5 additions & 0 deletions pkg/utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ import (
func DurationSeconds(d time.Duration) string {
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", d.Seconds()), "0"), ".") + "s"
}

// DurationMillSeconds returns the duration is milliseconds (e.g. "15ms")
func DurationMillSeconds(d time.Duration) string {
return fmt.Sprintf("%dms", d.Milliseconds())
}

0 comments on commit 884bfb2

Please sign in to comment.