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

Change incremental_duration to use black box testing #1117

Merged
Show file tree
Hide file tree
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
22 changes: 11 additions & 11 deletions pkg/utils/incremental_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
42 changes: 22 additions & 20 deletions pkg/utils/incremental_duration_test.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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,
},
Expand All @@ -31,52 +33,52 @@ 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)
}
})
}
}

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)
}
}