blob: 7f0e48cc9c2dcbc23e46487c6f2d9fcba070c23f [file] [log] [blame]
khenaidoo26721882021-08-11 17:42:52 -04001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002* Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoo26721882021-08-11 17:42:52 -04003
Joey Armstrong7f8436c2023-07-09 20:23:27 -04004* 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
khenaidoo26721882021-08-11 17:42:52 -04007
Joey Armstrong7f8436c2023-07-09 20:23:27 -04008* http://www.apache.org/licenses/LICENSE-2.0
khenaidoo26721882021-08-11 17:42:52 -04009
Joey Armstrong7f8436c2023-07-09 20:23:27 -040010* 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.
khenaidoo26721882021-08-11 17:42:52 -040015 */
khenaidoo0927c722021-12-15 16:49:32 -050016package grpc_test
khenaidoo26721882021-08-11 17:42:52 -040017
18import (
19 "context"
20 "os"
21 "testing"
22 "time"
23
khenaidoo0927c722021-12-15 16:49:32 -050024 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
khenaidoo26721882021-08-11 17:42:52 -040025 "github.com/stretchr/testify/assert"
26)
27
28func TestBackoffNoWait(t *testing.T) {
29 initTime := 1 * time.Millisecond
30 maxTime := 500 * time.Millisecond
31 maxElapsedTime := 0 * time.Millisecond
khenaidoo0927c722021-12-15 16:49:32 -050032 backoff := vgrpc.NewBackoff(initTime, maxTime, maxElapsedTime)
khenaidoo26721882021-08-11 17:42:52 -040033 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
34 defer cancel()
35 err := backoff.Backoff(ctx)
36 assert.Nil(t, err)
37}
38
39func TestBackoffElapsedTime(t *testing.T) {
40 initTime := 10 * time.Millisecond
41 maxTime := 50 * time.Millisecond
42 maxElapsedTime := 500 * time.Millisecond
khenaidoo0927c722021-12-15 16:49:32 -050043 b := vgrpc.NewBackoff(initTime, maxTime, maxElapsedTime)
khenaidoo26721882021-08-11 17:42:52 -040044 start := time.Now()
45loop:
46 for {
47 err := b.Backoff(context.Background())
48 if err != nil {
49 break loop
50 }
51 }
52 assert.GreaterOrEqual(t, time.Since(start).Milliseconds(), maxTime.Milliseconds())
53}
54
55func TestBackoffSuccess(t *testing.T) {
56 initTime := 100 * time.Millisecond
57 maxTime := 500 * time.Millisecond
58 maxElapsedTime := 0 * time.Millisecond
khenaidoo0927c722021-12-15 16:49:32 -050059 backoff := vgrpc.NewBackoff(initTime, maxTime, maxElapsedTime)
khenaidoo26721882021-08-11 17:42:52 -040060 ctx, cancel := context.WithTimeout(context.Background(), 2000*time.Millisecond)
61 defer cancel()
62 previous := time.Duration(0)
63 for i := 1; i < 5; i++ {
64 start := time.Now()
65 err := backoff.Backoff(ctx)
66 assert.Nil(t, err)
67 current := time.Since(start)
68 if current < maxTime {
69 assert.GreaterOrEqual(t, current.Milliseconds(), previous.Milliseconds())
70 }
71 previous = current
72 }
73}
74
75func TestBackoffContextTimeout(t *testing.T) {
76 initTime := 1000 * time.Millisecond
77 maxTime := 1100 * time.Millisecond
78 maxElapsedTime := 0 * time.Millisecond
khenaidoo0927c722021-12-15 16:49:32 -050079 backoff := vgrpc.NewBackoff(initTime, maxTime, maxElapsedTime)
khenaidoo26721882021-08-11 17:42:52 -040080 ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
81 defer cancel()
82 err := backoff.Backoff(ctx)
83 assert.NotNil(t, err)
84 assert.Equal(t, context.DeadlineExceeded, err)
85}
86
87func TestSetFromEnvVariable(t *testing.T) {
88 // 1. Test with unsupported type
89 var valInt int
khenaidoo0927c722021-12-15 16:49:32 -050090 err := vgrpc.SetFromEnvVariable("MY-KEY", valInt)
khenaidoo26721882021-08-11 17:42:52 -040091 assert.NotNil(t, err)
92
93 //2. Test with supported type but no env variable present
94 var valDuration time.Duration
khenaidoo0927c722021-12-15 16:49:32 -050095 err = vgrpc.SetFromEnvVariable("MY-KEY", &valDuration)
khenaidoo26721882021-08-11 17:42:52 -040096 assert.Nil(t, err)
97
98 //3. Test with supported type and env variable present
99 err = os.Setenv("MY-KEY", "10s")
100 assert.Nil(t, err)
khenaidoo0927c722021-12-15 16:49:32 -0500101 err = vgrpc.SetFromEnvVariable("MY-KEY", &valDuration)
khenaidoo26721882021-08-11 17:42:52 -0400102 assert.Nil(t, err)
103 assert.Equal(t, 10*time.Second, valDuration)
104}