Browse code

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 <github@gone.nl>

Sebastiaan van Stijn authored on 2025/02/08 20:12:40
Showing 3 changed files
... ...
@@ -17,7 +17,7 @@ import (
17 17
 )
18 18
 
19 19
 const (
20
-	defaultTimeOut = 30
20
+	defaultTimeOut = 30 * time.Second
21 21
 
22 22
 	// dummyHost is a hostname used for local communication.
23 23
 	//
... ...
@@ -107,7 +107,7 @@ type RequestOpts struct {
107 107
 	Timeout time.Duration
108 108
 
109 109
 	// testTimeOut is used during tests to limit the max timeout in [abort]
110
-	testTimeOut int
110
+	testTimeOut time.Duration
111 111
 }
112 112
 
113 113
 // WithRequestTimeout sets a timeout duration for plugin requests
... ...
@@ -239,7 +239,7 @@ func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool,
239 239
 }
240 240
 
241 241
 func backoff(retries int) time.Duration {
242
-	b, maxTimeout := 1, defaultTimeOut
242
+	b, maxTimeout := 1*time.Second, defaultTimeOut
243 243
 	for b < maxTimeout && retries > 0 {
244 244
 		b *= 2
245 245
 		retries--
... ...
@@ -247,18 +247,18 @@ func backoff(retries int) time.Duration {
247 247
 	if b > maxTimeout {
248 248
 		b = maxTimeout
249 249
 	}
250
-	return time.Duration(b) * time.Second
250
+	return b
251 251
 }
252 252
 
253 253
 // testNonExistingPlugin is a special plugin-name, which overrides defaultTimeOut in tests.
254 254
 const testNonExistingPlugin = "this-plugin-does-not-exist"
255 255
 
256
-func abort(start time.Time, timeOff time.Duration, overrideTimeout int) bool {
256
+func abort(start time.Time, timeOff time.Duration, overrideTimeout time.Duration) bool {
257 257
 	to := defaultTimeOut
258 258
 	if overrideTimeout > 0 {
259 259
 		to = overrideTimeout
260 260
 	}
261
-	return timeOff+time.Since(start) >= time.Duration(to)*time.Second
261
+	return timeOff+time.Since(start) >= to
262 262
 }
263 263
 
264 264
 func httpScheme(u *url.URL) string {
... ...
@@ -96,20 +96,18 @@ func TestBackoff(t *testing.T) {
96 96
 		retries    int
97 97
 		expTimeOff time.Duration
98 98
 	}{
99
-		{expTimeOff: time.Duration(1)},
100
-		{retries: 1, expTimeOff: time.Duration(2)},
101
-		{retries: 2, expTimeOff: time.Duration(4)},
102
-		{retries: 4, expTimeOff: time.Duration(16)},
103
-		{retries: 6, expTimeOff: time.Duration(30)},
104
-		{retries: 10, expTimeOff: time.Duration(30)},
99
+		{retries: 0, expTimeOff: 1 * time.Second},
100
+		{retries: 1, expTimeOff: 2 * time.Second},
101
+		{retries: 2, expTimeOff: 4 * time.Second},
102
+		{retries: 4, expTimeOff: 16 * time.Second},
103
+		{retries: 6, expTimeOff: 30 * time.Second},
104
+		{retries: 10, expTimeOff: 30 * time.Second},
105 105
 	}
106 106
 
107 107
 	for _, tc := range cases {
108 108
 		t.Run(fmt.Sprintf("retries: %v", tc.retries), func(t *testing.T) {
109
-			s := tc.expTimeOff * time.Second
110
-			if d := backoff(tc.retries); d != s {
111
-				t.Fatalf("Retry %v, expected %v, was %v\n", tc.retries, s, d)
112
-			}
109
+			d := backoff(tc.retries)
110
+			assert.Check(t, is.Equal(d, tc.expTimeOff))
113 111
 		})
114 112
 	}
115 113
 }
... ...
@@ -120,18 +118,17 @@ func TestAbortRetry(t *testing.T) {
120 120
 		timeOff  time.Duration
121 121
 		expAbort bool
122 122
 	}{
123
-		{timeOff: time.Duration(1)},
124
-		{timeOff: time.Duration(2)},
125
-		{timeOff: time.Duration(10)},
126
-		{timeOff: time.Duration(30), expAbort: true},
127
-		{timeOff: time.Duration(40), expAbort: true},
123
+		{timeOff: 1 * time.Second},
124
+		{timeOff: 2 * time.Second},
125
+		{timeOff: 10 * time.Second},
126
+		{timeOff: 30 * time.Second, expAbort: true},
127
+		{timeOff: 40 * time.Second, expAbort: true},
128 128
 	}
129 129
 
130 130
 	for _, tc := range cases {
131 131
 		t.Run(fmt.Sprintf("duration: %v", tc.timeOff), func(t *testing.T) {
132
-			s := tc.timeOff * time.Second
133
-			if a := abort(time.Now(), s, 0); a != tc.expAbort {
134
-				t.Fatalf("Duration %v, expected %v, was %v\n", tc.timeOff, s, a)
132
+			if a := abort(time.Now(), tc.timeOff, 0); a != tc.expAbort {
133
+				t.Fatalf("Duration %v, expected %v, was %v\n", tc.timeOff, tc.timeOff, a)
135 134
 			}
136 135
 		})
137 136
 	}
... ...
@@ -174,7 +171,7 @@ func TestNewClientWithTimeout(t *testing.T) {
174 174
 	c, _ := NewClientWithTimeout(addr, &tlsconfig.Options{InsecureSkipVerify: true}, timeout)
175 175
 	var output Manifest
176 176
 
177
-	err := c.CallWithOptions("Test.Echo", m, &output, func(opts *RequestOpts) { opts.testTimeOut = 1 })
177
+	err := c.CallWithOptions("Test.Echo", m, &output, func(opts *RequestOpts) { opts.testTimeOut = 1 * time.Second })
178 178
 	var tErr interface {
179 179
 		Timeout() bool
180 180
 	}
... ...
@@ -204,10 +204,10 @@ func (p *Plugin) implements(kind string) bool {
204 204
 func loadWithRetry(name string, retry bool) (*Plugin, error) {
205 205
 	registry := NewLocalRegistry()
206 206
 	start := time.Now()
207
-	var testTimeOut int
207
+	var testTimeOut time.Duration
208 208
 	if name == testNonExistingPlugin {
209 209
 		// override the timeout in tests
210
-		testTimeOut = 2
210
+		testTimeOut = 2 * time.Second
211 211
 	}
212 212
 	var retries int
213 213
 	for {