Skip to content

Commit bcbeb24

Browse files
authored
Mock queue backoff duration (#30553)
During testing, the backoff duration shouldn't be longer than other durations
1 parent dd8e6ae commit bcbeb24

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

modules/queue/backoff.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"time"
99
)
1010

11-
const (
11+
var (
1212
backoffBegin = 50 * time.Millisecond
1313
backoffUpper = 2 * time.Second
1414
)
@@ -18,6 +18,14 @@ type (
1818
backoffFuncErr func() (retry bool, err error)
1919
)
2020

21+
func mockBackoffDuration(d time.Duration) func() {
22+
oldBegin, oldUpper := backoffBegin, backoffUpper
23+
backoffBegin, backoffUpper = d, d
24+
return func() {
25+
backoffBegin, backoffUpper = oldBegin, oldUpper
26+
}
27+
}
28+
2129
func backoffRetErr[T any](ctx context.Context, begin, upper time.Duration, end <-chan time.Time, fn backoffFuncRetErr[T]) (ret T, err error) {
2230
d := begin
2331
for {

modules/queue/workerqueue_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ func TestWorkerPoolQueueShutdown(t *testing.T) {
250250

251251
func TestWorkerPoolQueueWorkerIdleReset(t *testing.T) {
252252
defer test.MockVariableValue(&workerIdleDuration, 10*time.Millisecond)()
253+
defer mockBackoffDuration(10 * time.Millisecond)()
253254

254255
handler := func(items ...int) (unhandled []int) {
255256
time.Sleep(50 * time.Millisecond)

0 commit comments

Comments
 (0)