From 34f7ac90b713df5416f0ca5e01d4f1c4d43dda01 Mon Sep 17 00:00:00 2001 From: Thom Carlin Date: Fri, 9 Aug 2024 11:20:42 -0400 Subject: [PATCH] Change incremental_duration to use black box testing (#1117) --- pkg/utils/incremental_duration.go | 22 +++++++------- pkg/utils/incremental_duration_test.go | 42 ++++++++++++++------------ 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/pkg/utils/incremental_duration.go b/pkg/utils/incremental_duration.go index 41aa1773d..4327b1259 100644 --- a/pkg/utils/incremental_duration.go +++ b/pkg/utils/incremental_duration.go @@ -7,35 +7,35 @@ import ( // IncrementalDuration handles a time.Duration with max limits. type IncrementalDuration struct { - duration time.Duration - initialDuration time.Duration - maxDuration time.Duration + Duration time.Duration + InitialDuration time.Duration + MaxDuration time.Duration multiplier float64 } // NewIncrementalDuration returns an IncrementalDuration object with initialized values. func NewIncrementalDuration(duration, maxDuration time.Duration, multiplier float64) *IncrementalDuration { return &IncrementalDuration{ - duration: duration, - initialDuration: duration, - maxDuration: maxDuration, + Duration: duration, + InitialDuration: duration, + MaxDuration: maxDuration, multiplier: multiplier, } } // Reset sets current duration to initial duration. func (id *IncrementalDuration) Reset() { - id.duration = id.initialDuration + id.Duration = id.InitialDuration } -func (id *IncrementalDuration) increaseDuration() { - id.duration = time.Duration(math.Min(id.multiplier*float64(id.duration), float64(id.maxDuration))) +func (id *IncrementalDuration) IncreaseDuration() { + id.Duration = time.Duration(math.Min(id.multiplier*float64(id.Duration), float64(id.MaxDuration))) } // NextTimeout returns a timeout channel based on current duration. func (id *IncrementalDuration) NextTimeout() <-chan time.Time { - ch := time.After(id.duration) - id.increaseDuration() + ch := time.After(id.Duration) + id.IncreaseDuration() return ch } diff --git a/pkg/utils/incremental_duration_test.go b/pkg/utils/incremental_duration_test.go index 9b7fa9d08..1728f74df 100644 --- a/pkg/utils/incremental_duration_test.go +++ b/pkg/utils/incremental_duration_test.go @@ -1,15 +1,17 @@ -package utils +package utils_test import ( "testing" "time" + + "github.com/ansible/receptor/pkg/utils" ) const newIncrementalDurationMessage string = "NewIncrementalDuration() = %v, want %v" func TestNewIncrementalDuration(t *testing.T) { type args struct { - duration time.Duration + Duration time.Duration maxDuration time.Duration multiplier float64 } @@ -22,7 +24,7 @@ func TestNewIncrementalDuration(t *testing.T) { { name: "NewIncrementalDuration1", args: args{ - duration: 1 * time.Second, + Duration: 1 * time.Second, maxDuration: 10 * time.Second, multiplier: 2.0, }, @@ -31,7 +33,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 { + if got := utils.NewIncrementalDuration(tt.args.Duration, tt.args.maxDuration, tt.args.multiplier); got.Duration != tt.want { t.Errorf(newIncrementalDurationMessage, got, tt.want) } }) @@ -39,44 +41,44 @@ func TestNewIncrementalDuration(t *testing.T) { } func TestIncrementalDurationReset(t *testing.T) { - delay := NewIncrementalDuration(1*time.Second, 10*time.Second, 2.0) + delay := utils.NewIncrementalDuration(1*time.Second, 10*time.Second, 2.0) want1 := 1 * time.Second - if delay.duration != want1 { - t.Errorf(newIncrementalDurationMessage, delay.duration, want1) + if delay.Duration != want1 { + t.Errorf(newIncrementalDurationMessage, delay.Duration, want1) } <-delay.NextTimeout() want2 := 2 * time.Second - if delay.duration != want2 { - t.Errorf(newIncrementalDurationMessage, delay.duration, want2) + if delay.Duration != want2 { + t.Errorf(newIncrementalDurationMessage, delay.Duration, want2) } delay.Reset() - if delay.duration != want1 { - t.Errorf("Reset() = %v, want %v", delay.duration, want1) + 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) + delay := utils.NewIncrementalDuration(1*time.Second, 10*time.Second, 2.0) for i := 0; i <= 10; i++ { - delay.increaseDuration() + delay.IncreaseDuration() } want10 := 10 * time.Second - if delay.duration != want10 { - t.Errorf("increaseDuration() = %v, want %v", delay.duration, want10) + 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) + delay := utils.NewIncrementalDuration(1*time.Second, 10*time.Second, 2.0) want1 := 1 * time.Second - if delay.duration != want1 { - t.Errorf(newIncrementalDurationMessage, delay.duration, want1) + 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) + if delay.Duration != want2 { + t.Errorf("NextTimeout() = %v, want %v", delay.Duration, want2) } }