blob: 55ffdf88be38b9c44fd8a394e19f390bb019fa76 [file] [log] [blame]
khenaidoo26721882021-08-11 17:42:52 -04001/*
2 * Copyright 2021-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package grpc
17
18import (
19 "context"
20 "os"
21 "testing"
22 "time"
23
24 "github.com/stretchr/testify/assert"
25)
26
27func TestBackoffNoWait(t *testing.T) {
28 initTime := 1 * time.Millisecond
29 maxTime := 500 * time.Millisecond
30 maxElapsedTime := 0 * time.Millisecond
31 backoff := NewBackoff(initTime, maxTime, maxElapsedTime)
32 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
33 defer cancel()
34 err := backoff.Backoff(ctx)
35 assert.Nil(t, err)
36}
37
38func TestBackoffElapsedTime(t *testing.T) {
39 initTime := 10 * time.Millisecond
40 maxTime := 50 * time.Millisecond
41 maxElapsedTime := 500 * time.Millisecond
42 b := NewBackoff(initTime, maxTime, maxElapsedTime)
43 start := time.Now()
44loop:
45 for {
46 err := b.Backoff(context.Background())
47 if err != nil {
48 break loop
49 }
50 }
51 assert.GreaterOrEqual(t, time.Since(start).Milliseconds(), maxTime.Milliseconds())
52}
53
54func TestBackoffSuccess(t *testing.T) {
55 initTime := 100 * time.Millisecond
56 maxTime := 500 * time.Millisecond
57 maxElapsedTime := 0 * time.Millisecond
58 backoff := NewBackoff(initTime, maxTime, maxElapsedTime)
59 ctx, cancel := context.WithTimeout(context.Background(), 2000*time.Millisecond)
60 defer cancel()
61 previous := time.Duration(0)
62 for i := 1; i < 5; i++ {
63 start := time.Now()
64 err := backoff.Backoff(ctx)
65 assert.Nil(t, err)
66 current := time.Since(start)
67 if current < maxTime {
68 assert.GreaterOrEqual(t, current.Milliseconds(), previous.Milliseconds())
69 }
70 previous = current
71 }
72}
73
74func TestBackoffContextTimeout(t *testing.T) {
75 initTime := 1000 * time.Millisecond
76 maxTime := 1100 * time.Millisecond
77 maxElapsedTime := 0 * time.Millisecond
78 backoff := NewBackoff(initTime, maxTime, maxElapsedTime)
79 ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
80 defer cancel()
81 err := backoff.Backoff(ctx)
82 assert.NotNil(t, err)
83 assert.Equal(t, context.DeadlineExceeded, err)
84}
85
86func TestSetFromEnvVariable(t *testing.T) {
87 // 1. Test with unsupported type
88 var valInt int
89 err := SetFromEnvVariable("MY-KEY", valInt)
90 assert.NotNil(t, err)
91
92 //2. Test with supported type but no env variable present
93 var valDuration time.Duration
94 err = SetFromEnvVariable("MY-KEY", &valDuration)
95 assert.Nil(t, err)
96
97 //3. Test with supported type and env variable present
98 err = os.Setenv("MY-KEY", "10s")
99 assert.Nil(t, err)
100 err = SetFromEnvVariable("MY-KEY", &valDuration)
101 assert.Nil(t, err)
102 assert.Equal(t, 10*time.Second, valDuration)
103}