From 07e2fc83ae9c525eb6bb61fa0f6472e59e237ec4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 8 Feb 2025 12:12:40 +0100 Subject: [PATCH] pkg/plugins: fix "Multiplication of durations" (durationcheck) Change some variables to a time.Duration to reduce conversions between integers and durations, which also makes the code slightly more transparent. pkg/plugins/client_test.go:109:9: Multiplication of durations: `tc.expTimeOff * time.Second` (durationcheck) s := tc.expTimeOff * time.Second ^ pkg/plugins/client_test.go:132:9: Multiplication of durations: `tc.timeOff * time.Second` (durationcheck) s := tc.timeOff * time.Second ^ Signed-off-by: Sebastiaan van Stijn --- pkg/plugins/client.go | 12 ++++++------ pkg/plugins/client_test.go | 35 ++++++++++++++++------------------- pkg/plugins/plugins.go | 4 ++-- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/pkg/plugins/client.go b/pkg/plugins/client.go index f7756f2097..8ac39972c0 100644 --- a/pkg/plugins/client.go +++ b/pkg/plugins/client.go @@ -17,7 +17,7 @@ import ( ) const ( - defaultTimeOut = 30 + defaultTimeOut = 30 * time.Second // dummyHost is a hostname used for local communication. // @@ -107,7 +107,7 @@ type RequestOpts struct { Timeout time.Duration // testTimeOut is used during tests to limit the max timeout in [abort] - testTimeOut int + testTimeOut time.Duration } // WithRequestTimeout sets a timeout duration for plugin requests @@ -239,7 +239,7 @@ func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool, } func backoff(retries int) time.Duration { - b, maxTimeout := 1, defaultTimeOut + b, maxTimeout := 1*time.Second, defaultTimeOut for b < maxTimeout && retries > 0 { b *= 2 retries-- @@ -247,18 +247,18 @@ func backoff(retries int) time.Duration { if b > maxTimeout { b = maxTimeout } - return time.Duration(b) * time.Second + return b } // testNonExistingPlugin is a special plugin-name, which overrides defaultTimeOut in tests. const testNonExistingPlugin = "this-plugin-does-not-exist" -func abort(start time.Time, timeOff time.Duration, overrideTimeout int) bool { +func abort(start time.Time, timeOff time.Duration, overrideTimeout time.Duration) bool { to := defaultTimeOut if overrideTimeout > 0 { to = overrideTimeout } - return timeOff+time.Since(start) >= time.Duration(to)*time.Second + return timeOff+time.Since(start) >= to } func httpScheme(u *url.URL) string { diff --git a/pkg/plugins/client_test.go b/pkg/plugins/client_test.go index 33722c33ee..3e6a4c18c9 100644 --- a/pkg/plugins/client_test.go +++ b/pkg/plugins/client_test.go @@ -96,20 +96,18 @@ func TestBackoff(t *testing.T) { retries int expTimeOff time.Duration }{ - {expTimeOff: time.Duration(1)}, - {retries: 1, expTimeOff: time.Duration(2)}, - {retries: 2, expTimeOff: time.Duration(4)}, - {retries: 4, expTimeOff: time.Duration(16)}, - {retries: 6, expTimeOff: time.Duration(30)}, - {retries: 10, expTimeOff: time.Duration(30)}, + {retries: 0, expTimeOff: 1 * time.Second}, + {retries: 1, expTimeOff: 2 * time.Second}, + {retries: 2, expTimeOff: 4 * time.Second}, + {retries: 4, expTimeOff: 16 * time.Second}, + {retries: 6, expTimeOff: 30 * time.Second}, + {retries: 10, expTimeOff: 30 * time.Second}, } for _, tc := range cases { t.Run(fmt.Sprintf("retries: %v", tc.retries), func(t *testing.T) { - s := tc.expTimeOff * time.Second - if d := backoff(tc.retries); d != s { - t.Fatalf("Retry %v, expected %v, was %v\n", tc.retries, s, d) - } + d := backoff(tc.retries) + assert.Check(t, is.Equal(d, tc.expTimeOff)) }) } } @@ -120,18 +118,17 @@ func TestAbortRetry(t *testing.T) { timeOff time.Duration expAbort bool }{ - {timeOff: time.Duration(1)}, - {timeOff: time.Duration(2)}, - {timeOff: time.Duration(10)}, - {timeOff: time.Duration(30), expAbort: true}, - {timeOff: time.Duration(40), expAbort: true}, + {timeOff: 1 * time.Second}, + {timeOff: 2 * time.Second}, + {timeOff: 10 * time.Second}, + {timeOff: 30 * time.Second, expAbort: true}, + {timeOff: 40 * time.Second, expAbort: true}, } for _, tc := range cases { t.Run(fmt.Sprintf("duration: %v", tc.timeOff), func(t *testing.T) { - s := tc.timeOff * time.Second - if a := abort(time.Now(), s, 0); a != tc.expAbort { - t.Fatalf("Duration %v, expected %v, was %v\n", tc.timeOff, s, a) + if a := abort(time.Now(), tc.timeOff, 0); a != tc.expAbort { + t.Fatalf("Duration %v, expected %v, was %v\n", tc.timeOff, tc.timeOff, a) } }) } @@ -174,7 +171,7 @@ func TestNewClientWithTimeout(t *testing.T) { c, _ := NewClientWithTimeout(addr, &tlsconfig.Options{InsecureSkipVerify: true}, timeout) var output Manifest - err := c.CallWithOptions("Test.Echo", m, &output, func(opts *RequestOpts) { opts.testTimeOut = 1 }) + err := c.CallWithOptions("Test.Echo", m, &output, func(opts *RequestOpts) { opts.testTimeOut = 1 * time.Second }) var tErr interface { Timeout() bool } diff --git a/pkg/plugins/plugins.go b/pkg/plugins/plugins.go index 380b7fa99a..552f21585f 100644 --- a/pkg/plugins/plugins.go +++ b/pkg/plugins/plugins.go @@ -204,10 +204,10 @@ func (p *Plugin) implements(kind string) bool { func loadWithRetry(name string, retry bool) (*Plugin, error) { registry := NewLocalRegistry() start := time.Now() - var testTimeOut int + var testTimeOut time.Duration if name == testNonExistingPlugin { // override the timeout in tests - testTimeOut = 2 + testTimeOut = 2 * time.Second } var retries int for {