Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame^] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 7 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame^] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 15 | */ |
| 16 | package kvstore |
| 17 | |
| 18 | import ( |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 19 | "context" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 20 | "github.com/stretchr/testify/assert" |
| 21 | "testing" |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 22 | "time" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 25 | func TestToStringWithString(t *testing.T) { |
| 26 | actualResult, _ := ToString("myString") |
| 27 | var expectedResult = "myString" |
| 28 | |
| 29 | assert.Equal(t, expectedResult, actualResult) |
| 30 | } |
| 31 | |
| 32 | func TestToStringWithEmpty(t *testing.T) { |
| 33 | actualResult, _ := ToString("") |
| 34 | var expectedResult = "" |
| 35 | |
| 36 | assert.Equal(t, expectedResult, actualResult) |
| 37 | } |
| 38 | |
| 39 | func TestToStringWithByte(t *testing.T) { |
| 40 | mByte := []byte("Hello") |
| 41 | actualResult, _ := ToString(mByte) |
| 42 | var expectedResult = "Hello" |
| 43 | |
| 44 | assert.Equal(t, expectedResult, actualResult) |
| 45 | } |
| 46 | |
| 47 | func TestToStringWithEmptyByte(t *testing.T) { |
| 48 | mByte := []byte("") |
| 49 | actualResult, _ := ToString(mByte) |
| 50 | var expectedResult = "" |
| 51 | |
| 52 | assert.Equal(t, expectedResult, actualResult) |
| 53 | } |
| 54 | |
| 55 | func TestToStringForErrorCase(t *testing.T) { |
| 56 | mInt := 200 |
| 57 | actualResult, error := ToString(mInt) |
| 58 | var expectedResult = "" |
| 59 | |
| 60 | assert.Equal(t, expectedResult, actualResult) |
| 61 | assert.NotEqual(t, error, nil) |
| 62 | } |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 63 | |
| 64 | func TestBackoffNoWait(t *testing.T) { |
| 65 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond) |
| 66 | defer cancel() |
| 67 | err := backoff(ctx, 0) |
| 68 | assert.Nil(t, err) |
| 69 | } |
| 70 | |
| 71 | func TestBackoffSuccess(t *testing.T) { |
| 72 | ctx, cancel := context.WithTimeout(context.Background(), 1000*time.Millisecond) |
| 73 | defer cancel() |
| 74 | previous := time.Duration(0) |
| 75 | for i := 1; i < 5; i++ { |
| 76 | start := time.Now() |
| 77 | err := backoff(ctx, i) |
| 78 | assert.Nil(t, err) |
| 79 | current := time.Since(start) |
| 80 | assert.Greater(t, current.Milliseconds(), previous.Milliseconds()) |
| 81 | previous = current |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestBackoffContextTimeout(t *testing.T) { |
| 86 | ctx, cancel := context.WithTimeout(context.Background(), 1000*time.Millisecond) |
| 87 | defer cancel() |
| 88 | err := backoff(ctx, 10) |
| 89 | assert.NotNil(t, err) |
| 90 | assert.Equal(t, context.DeadlineExceeded, err) |
| 91 | } |