File: backoff_test.go

package info (click to toggle)
golang-github-grpc-ecosystem-go-grpc-middleware 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,564 kB
  • sloc: makefile: 100; sh: 9
file content (39 lines) | stat: -rw-r--r-- 1,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
package retry

import (
	"context"
	"testing"
	"time"
)

func TestBackoffExponentialWithJitter(t *testing.T) {
	scalar := 100 * time.Millisecond
	jitterFrac := 0.10
	backoffFunc := BackoffExponentialWithJitter(scalar, jitterFrac)
	// use 64 so we are past number of attempts where exponentBase2 would overflow
	for i := 0; i < 64; i++ {
		waitFor := backoffFunc(nil, uint(i))
		if waitFor < 0 {
			t.Errorf("BackoffExponentialWithJitter(%d) = %d; want >= 0", i, waitFor)
		}
	}
}

func TestBackoffExponentialWithJitterBounded(t *testing.T) {
	scalar := 100 * time.Millisecond
	jitterFrac := 0.10
	maxBound := 10 * time.Second
	backoff := BackoffExponentialWithJitterBounded(scalar, jitterFrac, maxBound)
	// use 64 so we are past number of attempts where exponentBase2 would overflow
	for i := 0; i < 64; i++ {
		waitFor := backoff(context.Background(), uint(i))
		if waitFor > maxBound {
			t.Fatalf("expected dur to be less than %v, got %v for %d", maxBound, waitFor, i)
		}
		if waitFor < 0 {
			t.Fatalf("expected dur to be greater than 0, got %v for %d", waitFor, i)
		}
	}
}