khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame^] | 2 | * Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 3 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame^] | 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 |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 7 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame^] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 9 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame^] | 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. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 15 | */ |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 16 | package grpc_test |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "os" |
| 21 | "testing" |
| 22 | "time" |
| 23 | |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 24 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 25 | "github.com/stretchr/testify/assert" |
| 26 | ) |
| 27 | |
| 28 | func TestBackoffNoWait(t *testing.T) { |
| 29 | initTime := 1 * time.Millisecond |
| 30 | maxTime := 500 * time.Millisecond |
| 31 | maxElapsedTime := 0 * time.Millisecond |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 32 | backoff := vgrpc.NewBackoff(initTime, maxTime, maxElapsedTime) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 33 | ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 34 | defer cancel() |
| 35 | err := backoff.Backoff(ctx) |
| 36 | assert.Nil(t, err) |
| 37 | } |
| 38 | |
| 39 | func TestBackoffElapsedTime(t *testing.T) { |
| 40 | initTime := 10 * time.Millisecond |
| 41 | maxTime := 50 * time.Millisecond |
| 42 | maxElapsedTime := 500 * time.Millisecond |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 43 | b := vgrpc.NewBackoff(initTime, maxTime, maxElapsedTime) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 44 | start := time.Now() |
| 45 | loop: |
| 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 | |
| 55 | func TestBackoffSuccess(t *testing.T) { |
| 56 | initTime := 100 * time.Millisecond |
| 57 | maxTime := 500 * time.Millisecond |
| 58 | maxElapsedTime := 0 * time.Millisecond |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 59 | backoff := vgrpc.NewBackoff(initTime, maxTime, maxElapsedTime) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 60 | 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 | |
| 75 | func TestBackoffContextTimeout(t *testing.T) { |
| 76 | initTime := 1000 * time.Millisecond |
| 77 | maxTime := 1100 * time.Millisecond |
| 78 | maxElapsedTime := 0 * time.Millisecond |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 79 | backoff := vgrpc.NewBackoff(initTime, maxTime, maxElapsedTime) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 80 | 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 | |
| 87 | func TestSetFromEnvVariable(t *testing.T) { |
| 88 | // 1. Test with unsupported type |
| 89 | var valInt int |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 90 | err := vgrpc.SetFromEnvVariable("MY-KEY", valInt) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 91 | assert.NotNil(t, err) |
| 92 | |
| 93 | //2. Test with supported type but no env variable present |
| 94 | var valDuration time.Duration |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 95 | err = vgrpc.SetFromEnvVariable("MY-KEY", &valDuration) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 96 | 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) |
khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 101 | err = vgrpc.SetFromEnvVariable("MY-KEY", &valDuration) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 102 | assert.Nil(t, err) |
| 103 | assert.Equal(t, 10*time.Second, valDuration) |
| 104 | } |