Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Martinez <[email protected]>
  • Loading branch information
famarting committed Oct 18, 2024
1 parent 715a44d commit a7a4914
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions task/orchestrator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package task

import (
"testing"
"time"
)

func Test_computeNextDelay(t *testing.T) {
time1 := time.Now()
time2 := time.Now().Add(1 * time.Minute)
type args struct {
currentTimeUtc time.Time
policy ActivityRetryPolicy
attempt int
firstAttempt time.Time
err error
}
tests := []struct {
name string
args args
want time.Duration
}{
{
name: "first attempt",
args: args{
currentTimeUtc: time2,
policy: ActivityRetryPolicy{
MaxAttempts: 3,
InitialRetryInterval: 2 * time.Second,
BackoffCoefficient: 2,
MaxRetryInterval: 10 * time.Second,
Handle: func(err error) bool { return true },
RetryTimeout: 2 * time.Minute,
},
attempt: 0,
firstAttempt: time1,
},
want: 2 * time.Second,
},
{
name: "second attempt",
args: args{
currentTimeUtc: time2,
policy: ActivityRetryPolicy{
MaxAttempts: 3,
InitialRetryInterval: 2 * time.Second,
BackoffCoefficient: 2,
MaxRetryInterval: 10 * time.Second,
Handle: func(err error) bool { return true },
RetryTimeout: 2 * time.Minute,
},
attempt: 1,
firstAttempt: time1,
},
want: 4 * time.Second,
},
{
name: "third attempt",
args: args{
currentTimeUtc: time2,
policy: ActivityRetryPolicy{
MaxAttempts: 3,
InitialRetryInterval: 2 * time.Second,
BackoffCoefficient: 2,
MaxRetryInterval: 10 * time.Second,
Handle: func(err error) bool { return true },
RetryTimeout: 2 * time.Minute,
},
attempt: 2,
firstAttempt: time1,
},
want: 8 * time.Second,
},
{
name: "fourth attempt",
args: args{
currentTimeUtc: time2,
policy: ActivityRetryPolicy{
MaxAttempts: 3,
InitialRetryInterval: 2 * time.Second,
BackoffCoefficient: 2,
MaxRetryInterval: 10 * time.Second,
Handle: func(err error) bool { return true },
RetryTimeout: 2 * time.Minute,
},
attempt: 3,
firstAttempt: time1,
},
want: 10 * time.Second,
},
{
name: "expired",
args: args{
currentTimeUtc: time2,
policy: ActivityRetryPolicy{
MaxAttempts: 3,
InitialRetryInterval: 2 * time.Second,
BackoffCoefficient: 2,
MaxRetryInterval: 10 * time.Second,
Handle: func(err error) bool { return true },
RetryTimeout: 30 * time.Second,
},
attempt: 3,
firstAttempt: time1,
},
want: 0,
},
{
name: "fourth attempt backoff 1",
args: args{
currentTimeUtc: time2,
policy: ActivityRetryPolicy{
MaxAttempts: 3,
InitialRetryInterval: 2 * time.Second,
BackoffCoefficient: 1,
MaxRetryInterval: 10 * time.Second,
Handle: func(err error) bool { return true },
RetryTimeout: 2 * time.Minute,
},
attempt: 3,
firstAttempt: time1,
},
want: 2 * time.Second,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := computeNextDelay(tt.args.currentTimeUtc, tt.args.policy, tt.args.attempt, tt.args.firstAttempt, tt.args.err); got != tt.want {
t.Errorf("computeNextDelay() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit a7a4914

Please sign in to comment.