feat: implement 'on error retry' policy (#428)

This commit is contained in:
Gareth
2024-08-26 19:21:18 -07:00
committed by GitHub
parent 8c1cf791bb
commit 038bc87070
16 changed files with 654 additions and 111 deletions

View File

@@ -2,6 +2,7 @@ package orchestrator
import (
"context"
"errors"
"sync"
"testing"
"time"
@@ -135,6 +136,58 @@ func TestTaskRescheduling(t *testing.T) {
}
}
func TestTaskRetry(t *testing.T) {
t.Parallel()
// Arrange
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
orch, err := NewOrchestrator("", config.NewDefaultConfig(), nil, nil)
if err != nil {
t.Fatalf("failed to create orchestrator: %v", err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
orch.Run(ctx)
}()
// Act
count := 0
ranTimes := 0
orch.ScheduleTask(newTestTask(
func() error {
ranTimes += 1
if ranTimes == 10 {
cancel()
}
return &tasks.TaskRetryError{
Err: errors.New("retry please"),
Backoff: func(attempt int) time.Duration { return 0 },
}
},
func(t time.Time) *time.Time {
count += 1
return &t
},
), tasks.TaskPriorityDefault)
wg.Wait()
if count != 1 {
t.Errorf("expected 1 Next calls because this test covers retries, got %d", count)
}
if ranTimes != 10 {
t.Errorf("expected 10 Run calls, got %d", ranTimes)
}
}
func TestGracefulShutdown(t *testing.T) {
t.Parallel()