From 9462058cd2cf88a94cf970edf99dafb91a3b3aaa Mon Sep 17 00:00:00 2001 From: Thom Carlin Date: Wed, 20 Sep 2023 07:26:33 -0400 Subject: [PATCH 1/2] Create pkg/utils/incremental_duration unit tests --- pkg/utils/incremental_duration_test.go | 71 ++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/pkg/utils/incremental_duration_test.go b/pkg/utils/incremental_duration_test.go index 9599c7fb0..37d4e5f7b 100644 --- a/pkg/utils/incremental_duration_test.go +++ b/pkg/utils/incremental_duration_test.go @@ -5,23 +5,76 @@ import ( "time" ) -func TestIncrementalDuration(t *testing.T) { +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("NewIncrementalDuration() = %v, want %v", 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("NewIncrementalDuration() = %v, want %v", delay.duration, want1) } <-delay.NextTimeout() - if delay.duration != 2*time.Second { - t.Fail() + + want2 := 2 * time.Second + if delay.duration != want2 { + t.Errorf("NewIncrementalDuration() = %v, want %v", 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("NewIncrementalDuration() = %v, want %v", delay.duration, want1) + } + <-delay.NextTimeout() + + want2 := 2 * time.Second + if delay.duration != want2 { + t.Errorf("NextTimeout() = %v, want %v", delay.duration, want2) } } From 22d714dfb12537aa6725d289b0178028a4527578 Mon Sep 17 00:00:00 2001 From: Thom Carlin Date: Wed, 20 Sep 2023 07:40:29 -0400 Subject: [PATCH 2/2] Fix SonarLint code smell --- pkg/utils/incremental_duration_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/utils/incremental_duration_test.go b/pkg/utils/incremental_duration_test.go index 37d4e5f7b..9b7fa9d08 100644 --- a/pkg/utils/incremental_duration_test.go +++ b/pkg/utils/incremental_duration_test.go @@ -5,6 +5,8 @@ import ( "time" ) +const newIncrementalDurationMessage string = "NewIncrementalDuration() = %v, want %v" + func TestNewIncrementalDuration(t *testing.T) { type args struct { duration time.Duration @@ -30,7 +32,7 @@ func TestNewIncrementalDuration(t *testing.T) { 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("NewIncrementalDuration() = %v, want %v", got, tt.want) + t.Errorf(newIncrementalDurationMessage, got, tt.want) } }) } @@ -40,13 +42,13 @@ func TestIncrementalDurationReset(t *testing.T) { delay := NewIncrementalDuration(1*time.Second, 10*time.Second, 2.0) want1 := 1 * time.Second if delay.duration != want1 { - t.Errorf("NewIncrementalDuration() = %v, want %v", delay.duration, want1) + t.Errorf(newIncrementalDurationMessage, delay.duration, want1) } <-delay.NextTimeout() want2 := 2 * time.Second if delay.duration != want2 { - t.Errorf("NewIncrementalDuration() = %v, want %v", delay.duration, want2) + t.Errorf(newIncrementalDurationMessage, delay.duration, want2) } delay.Reset() if delay.duration != want1 { @@ -69,7 +71,7 @@ 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("NewIncrementalDuration() = %v, want %v", delay.duration, want1) + t.Errorf(newIncrementalDurationMessage, delay.duration, want1) } <-delay.NextTimeout()