|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "log/slog" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/benbjohnson/clock" |
| 10 | + "github.com/cschleiden/go-workflows/backend/converter" |
| 11 | + "github.com/cschleiden/go-workflows/core" |
| 12 | + "github.com/cschleiden/go-workflows/internal/contextvalue" |
| 13 | + "github.com/cschleiden/go-workflows/internal/sync" |
| 14 | + "github.com/cschleiden/go-workflows/internal/workflowerrors" |
| 15 | + "github.com/cschleiden/go-workflows/internal/workflowstate" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "go.opentelemetry.io/otel/trace/noop" |
| 18 | +) |
| 19 | + |
| 20 | +func TestWithRetries(t *testing.T) { |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + setupCtx func(Context) (Context, func()) // returns context and cleanup function |
| 24 | + retryOptions RetryOptions |
| 25 | + fn func(t *testing.T, attemptCount *int) func(Context, int) Future[string] |
| 26 | + expectedErr error |
| 27 | + expectedResult string |
| 28 | + expectedCalls int |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "context canceled before execution", |
| 32 | + setupCtx: func(ctx Context) (Context, func()) { |
| 33 | + ctx, cancel := WithCancel(ctx) |
| 34 | + cancel() // Cancel immediately |
| 35 | + return ctx, func() {} |
| 36 | + }, |
| 37 | + retryOptions: DefaultRetryOptions, |
| 38 | + fn: func(t *testing.T, attemptCount *int) func(Context, int) Future[string] { |
| 39 | + return func(ctx Context, attempt int) Future[string] { |
| 40 | + require.FailNow(t, "function should not be called when context is already canceled") |
| 41 | + return sync.NewFuture[string]() |
| 42 | + } |
| 43 | + }, |
| 44 | + expectedErr: Canceled, |
| 45 | + expectedCalls: 0, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "no retry needed - success", |
| 49 | + setupCtx: func(ctx Context) (Context, func()) { |
| 50 | + return ctx, func() {} |
| 51 | + }, |
| 52 | + retryOptions: DefaultRetryOptions, |
| 53 | + fn: func(t *testing.T, attemptCount *int) func(Context, int) Future[string] { |
| 54 | + return func(ctx Context, attempt int) Future[string] { |
| 55 | + f := sync.NewFuture[string]() |
| 56 | + *attemptCount++ |
| 57 | + f.Set("success", nil) |
| 58 | + return f |
| 59 | + } |
| 60 | + }, |
| 61 | + expectedResult: "success", |
| 62 | + expectedCalls: 1, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "max attempts one - no retries", |
| 66 | + setupCtx: func(ctx Context) (Context, func()) { |
| 67 | + return ctx, func() {} |
| 68 | + }, |
| 69 | + retryOptions: RetryOptions{ |
| 70 | + MaxAttempts: 1, |
| 71 | + FirstRetryInterval: 1 * time.Second, |
| 72 | + BackoffCoefficient: 1, |
| 73 | + }, |
| 74 | + fn: func(t *testing.T, attemptCount *int) func(Context, int) Future[string] { |
| 75 | + return func(ctx Context, attempt int) Future[string] { |
| 76 | + f := sync.NewFuture[string]() |
| 77 | + *attemptCount++ |
| 78 | + f.Set("", errors.New("error")) |
| 79 | + return f |
| 80 | + } |
| 81 | + }, |
| 82 | + expectedErr: errors.New("error"), // Only compare error message in assertion |
| 83 | + expectedCalls: 1, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, tt := range tests { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + state := workflowstate.NewWorkflowState( |
| 90 | + core.NewWorkflowInstance("a", ""), slog.Default(), noop.NewTracerProvider().Tracer("test"), clock.New()) |
| 91 | + |
| 92 | + ctx := sync.Background() |
| 93 | + ctx = contextvalue.WithConverter(ctx, converter.DefaultConverter) |
| 94 | + ctx = workflowstate.WithWorkflowState(ctx, state) |
| 95 | + |
| 96 | + ctx, cleanup := tt.setupCtx(ctx) |
| 97 | + defer cleanup() |
| 98 | + |
| 99 | + attemptCount := 0 |
| 100 | + c := sync.NewCoroutine(ctx, func(ctx Context) error { |
| 101 | + f := WithRetries(ctx, tt.retryOptions, tt.fn(t, &attemptCount)) |
| 102 | + |
| 103 | + result, err := f.Get(ctx) |
| 104 | + |
| 105 | + if tt.expectedErr != nil { |
| 106 | + if tt.expectedErr == Canceled { |
| 107 | + require.Equal(t, Canceled, err) |
| 108 | + } else { |
| 109 | + require.EqualError(t, err, tt.expectedErr.Error()) |
| 110 | + } |
| 111 | + } else { |
| 112 | + require.NoError(t, err) |
| 113 | + require.Equal(t, tt.expectedResult, result) |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | + }) |
| 118 | + |
| 119 | + c.Execute() |
| 120 | + require.True(t, c.Finished()) |
| 121 | + require.Equal(t, tt.expectedCalls, attemptCount) |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// Test_WithRetries_PermanentError is kept separate as it has different async behavior |
| 127 | +func Test_WithRetries_PermanentError(t *testing.T) { |
| 128 | + state := workflowstate.NewWorkflowState( |
| 129 | + core.NewWorkflowInstance("a", ""), slog.Default(), noop.NewTracerProvider().Tracer("test"), clock.New()) |
| 130 | + |
| 131 | + ctx := sync.Background() |
| 132 | + ctx = contextvalue.WithConverter(ctx, converter.DefaultConverter) |
| 133 | + ctx = workflowstate.WithWorkflowState(ctx, state) |
| 134 | + |
| 135 | + attemptCount := 0 |
| 136 | + c := sync.NewCoroutine(ctx, func(ctx Context) error { |
| 137 | + _ = WithRetries(ctx, DefaultRetryOptions, func(ctx Context, attempt int) Future[int] { |
| 138 | + f := sync.NewFuture[int]() |
| 139 | + attemptCount++ |
| 140 | + |
| 141 | + if attempt > 0 { |
| 142 | + // Return permanent error on retry |
| 143 | + f.Set(attempt, workflowerrors.NewPermanentError(errors.New("permanent error"))) |
| 144 | + } else { |
| 145 | + // Return regular error on first attempt |
| 146 | + f.Set(attempt, errors.New("generic error")) |
| 147 | + } |
| 148 | + |
| 149 | + return f |
| 150 | + }) |
| 151 | + |
| 152 | + // The future won't be ready immediately since WithRetries spawns a goroutine |
| 153 | + return nil |
| 154 | + }) |
| 155 | + |
| 156 | + c.Execute() |
| 157 | + |
| 158 | + // Workflow finishes immediately as it just calls WithRetries, which starts an async goroutine |
| 159 | + require.True(t, c.Finished()) |
| 160 | + require.Equal(t, 1, attemptCount) // Only first attempt happens synchronously |
| 161 | +} |
0 commit comments