Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create pkg/utils/incremental_duration unit tests #857

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 64 additions & 9 deletions pkg/utils/incremental_duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,78 @@ import (
"time"
)

func TestIncrementalDuration(t *testing.T) {
const newIncrementalDurationMessage string = "NewIncrementalDuration() = %v, want %v"

func TestNewIncrementalDuration(t *testing.T) {
type args struct {
duration time.Duration
maxDuration time.Duration
multiplier float64
}

tests := []struct {
name string
args args
want time.Duration
}{
{
name: "NewIncrementalDuration1",
args: args{
duration: 1 * time.Second,
maxDuration: 10 * time.Second,
multiplier: 2.0,
},
want: 1 * time.Second,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewIncrementalDuration(tt.args.duration, tt.args.maxDuration, tt.args.multiplier); got.duration != tt.want {
t.Errorf(newIncrementalDurationMessage, got, tt.want)
}
})
}
}

func TestIncrementalDurationReset(t *testing.T) {
delay := NewIncrementalDuration(1*time.Second, 10*time.Second, 2.0)
if delay.duration != 1*time.Second {
t.Fail()
want1 := 1 * time.Second
if delay.duration != want1 {
t.Errorf(newIncrementalDurationMessage, delay.duration, want1)
}
<-delay.NextTimeout()
if delay.duration != 2*time.Second {
t.Fail()

want2 := 2 * time.Second
if delay.duration != want2 {
t.Errorf(newIncrementalDurationMessage, delay.duration, want2)
}
delay.Reset()
if delay.duration != 1*time.Second {
t.Fail()
if delay.duration != want1 {
t.Errorf("Reset() = %v, want %v", delay.duration, want1)
}
}

func TestIncrementalDurationincreaseDuration(t *testing.T) {
delay := NewIncrementalDuration(1*time.Second, 10*time.Second, 2.0)
for i := 0; i <= 10; i++ {
delay.increaseDuration()
}
if delay.duration != 10*time.Second {
t.Fail()
want10 := 10 * time.Second
if delay.duration != want10 {
t.Errorf("increaseDuration() = %v, want %v", delay.duration, want10)
}
}

func TestIncrementalDurationNextTimeout(t *testing.T) {
delay := NewIncrementalDuration(1*time.Second, 10*time.Second, 2.0)
want1 := 1 * time.Second
if delay.duration != want1 {
t.Errorf(newIncrementalDurationMessage, delay.duration, want1)
}
<-delay.NextTimeout()

want2 := 2 * time.Second
if delay.duration != want2 {
t.Errorf("NextTimeout() = %v, want %v", delay.duration, want2)
}
}
Loading