...
1
18
19 package health
20
21 import (
22 "context"
23 "errors"
24 "reflect"
25 "testing"
26 "time"
27
28 "google.golang.org/grpc/connectivity"
29 )
30
31 const defaultTestTimeout = 10 * time.Second
32
33 func (s) TestClientHealthCheckBackoff(t *testing.T) {
34 const maxRetries = 5
35
36 var want []time.Duration
37 for i := 0; i < maxRetries; i++ {
38 want = append(want, time.Duration(i+1)*time.Second)
39 }
40
41 var got []time.Duration
42 newStream := func(string) (any, error) {
43 if len(got) < maxRetries {
44 return nil, errors.New("backoff")
45 }
46 return nil, nil
47 }
48
49 oldBackoffFunc := backoffFunc
50 backoffFunc = func(ctx context.Context, retries int) bool {
51 got = append(got, time.Duration(retries+1)*time.Second)
52 return true
53 }
54 defer func() { backoffFunc = oldBackoffFunc }()
55
56 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
57 defer cancel()
58 clientHealthCheck(ctx, newStream, func(connectivity.State, error) {}, "test")
59
60 if !reflect.DeepEqual(got, want) {
61 t.Fatalf("Backoff durations for %v retries are %v. (expected: %v)", maxRetries, got, want)
62 }
63 }
64
View as plain text