-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Fabian Martinez <[email protected]>
- Loading branch information
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |