Skip to content

Commit

Permalink
Mock queue backoff duration (#30553)
Browse files Browse the repository at this point in the history
During testing, the backoff duration shouldn't be longer than other
durations
  • Loading branch information
wxiaoguang authored Apr 18, 2024
1 parent dd8e6ae commit bcbeb24
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion modules/queue/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"
)

const (
var (
backoffBegin = 50 * time.Millisecond
backoffUpper = 2 * time.Second
)
Expand All @@ -18,6 +18,14 @@ type (
backoffFuncErr func() (retry bool, err error)
)

func mockBackoffDuration(d time.Duration) func() {
oldBegin, oldUpper := backoffBegin, backoffUpper
backoffBegin, backoffUpper = d, d
return func() {
backoffBegin, backoffUpper = oldBegin, oldUpper
}
}

func backoffRetErr[T any](ctx context.Context, begin, upper time.Duration, end <-chan time.Time, fn backoffFuncRetErr[T]) (ret T, err error) {
d := begin
for {
Expand Down
1 change: 1 addition & 0 deletions modules/queue/workerqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func TestWorkerPoolQueueShutdown(t *testing.T) {

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

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

0 comments on commit bcbeb24

Please sign in to comment.