1/* 2 * 3 * Copyright 2018 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19package health 20 21import ( 22 "context" 23 "errors" 24 "reflect" 25 "testing" 26 "time" 27 28 "google.golang.org/grpc/connectivity" 29) 30 31func TestClientHealthCheckBackoff(t *testing.T) { 32 const maxRetries = 5 33 34 var want []time.Duration 35 for i := 0; i < maxRetries; i++ { 36 want = append(want, time.Duration(i+1)*time.Second) 37 } 38 39 var got []time.Duration 40 newStream := func(string) (interface{}, error) { 41 if len(got) < maxRetries { 42 return nil, errors.New("backoff") 43 } 44 return nil, nil 45 } 46 47 oldBackoffFunc := backoffFunc 48 backoffFunc = func(ctx context.Context, retries int) bool { 49 got = append(got, time.Duration(retries+1)*time.Second) 50 return true 51 } 52 defer func() { backoffFunc = oldBackoffFunc }() 53 54 clientHealthCheck(context.Background(), newStream, func(connectivity.State) {}, "test") 55 56 if !reflect.DeepEqual(got, want) { 57 t.Fatalf("Backoff durations for %v retries are %v. (expected: %v)", maxRetries, got, want) 58 } 59} 60