blob: d8d2404758c851d639b84a87a2e2b4a45a43e690 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002* Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Scott Baker2c1c4822019-10-16 11:02:41 -07003
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
Scott Baker2c1c4822019-10-16 11:02:41 -07007
Joey Armstrong7f8436c2023-07-09 20:23:27 -04008* http://www.apache.org/licenses/LICENSE-2.0
Scott Baker2c1c4822019-10-16 11:02:41 -07009
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.
Scott Baker2c1c4822019-10-16 11:02:41 -070015 */
16package kvstore
17
18import (
Girish Gowdra248971a2021-06-01 15:14:15 -070019 "context"
Scott Baker2c1c4822019-10-16 11:02:41 -070020 "github.com/stretchr/testify/assert"
21 "testing"
Girish Gowdra248971a2021-06-01 15:14:15 -070022 "time"
Scott Baker2c1c4822019-10-16 11:02:41 -070023)
24
Scott Baker2c1c4822019-10-16 11:02:41 -070025func TestToStringWithString(t *testing.T) {
26 actualResult, _ := ToString("myString")
27 var expectedResult = "myString"
28
29 assert.Equal(t, expectedResult, actualResult)
30}
31
32func TestToStringWithEmpty(t *testing.T) {
33 actualResult, _ := ToString("")
34 var expectedResult = ""
35
36 assert.Equal(t, expectedResult, actualResult)
37}
38
39func 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
47func TestToStringWithEmptyByte(t *testing.T) {
48 mByte := []byte("")
49 actualResult, _ := ToString(mByte)
50 var expectedResult = ""
51
52 assert.Equal(t, expectedResult, actualResult)
53}
54
55func 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 Gowdra248971a2021-06-01 15:14:15 -070063
64func 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
71func 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
85func 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}