From a7a491458a1dc21772890f41d947d7de0ae19be6 Mon Sep 17 00:00:00 2001 From: Fabian Martinez <46371672+famarting@users.noreply.github.com> Date: Fri, 18 Oct 2024 19:52:02 +0200 Subject: [PATCH] add unit test Signed-off-by: Fabian Martinez <46371672+famarting@users.noreply.github.com> --- task/orchestrator_test.go | 133 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 task/orchestrator_test.go diff --git a/task/orchestrator_test.go b/task/orchestrator_test.go new file mode 100644 index 0000000..d664b40 --- /dev/null +++ b/task/orchestrator_test.go @@ -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) + } + }) + } +}