| 15 | ) |
| 16 | |
| 17 | func TestResumableStep(t *testing.T) { |
| 18 | t.Parallel() |
| 19 | |
| 20 | setup := func(t *testing.T, metadata string) (context.Context, map[string]any, *rivertype.JobRow) { |
| 21 | t.Helper() |
| 22 | |
| 23 | metadataUpdates := make(map[string]any) |
| 24 | ctx := context.WithValue(context.Background(), jobexecutor.ContextKeyMetadataUpdates, metadataUpdates) |
| 25 | |
| 26 | return ctx, metadataUpdates, &rivertype.JobRow{Metadata: []byte(metadata)} |
| 27 | } |
| 28 | |
| 29 | t.Run("DuplicateStepName", func(t *testing.T) { |
| 30 | t.Parallel() |
| 31 | |
| 32 | ctx, _, job := setup(t, `{}`) |
| 33 | |
| 34 | var ran []string |
| 35 | err := (&rivermiddleware.ResumableMiddleware{}).Work(ctx, job, func(ctx context.Context) error { |
| 36 | ResumableStep(ctx, "step1", nil, func(ctx context.Context) error { |
| 37 | ran = append(ran, "first") |
| 38 | return nil |
| 39 | }) |
| 40 | ResumableStep(ctx, "step1", nil, func(ctx context.Context) error { |
| 41 | ran = append(ran, "second") |
| 42 | return nil |
| 43 | }) |
| 44 | |
| 45 | return nil |
| 46 | }) |
| 47 | require.EqualError(t, err, `river: duplicate resumable step name "step1"`) |
| 48 | require.Equal(t, []string{"first"}, ran) |
| 49 | }) |
| 50 | |
| 51 | t.Run("DuplicateStepNameWhenSkippingCompletedSteps", func(t *testing.T) { |
| 52 | t.Parallel() |
| 53 | |
| 54 | ctx, _, job := setup(t, `{"river:resumable_step":"step2"}`) |
| 55 | |
| 56 | var ran []string |
| 57 | err := (&rivermiddleware.ResumableMiddleware{}).Work(ctx, job, func(ctx context.Context) error { |
| 58 | ResumableStep(ctx, "step1", nil, func(ctx context.Context) error { |
| 59 | ran = append(ran, "first") |
| 60 | return nil |
| 61 | }) |
| 62 | ResumableStep(ctx, "step1", nil, func(ctx context.Context) error { |
| 63 | ran = append(ran, "second") |
| 64 | return nil |
| 65 | }) |
| 66 | ResumableStep(ctx, "step2", nil, func(ctx context.Context) error { |
| 67 | ran = append(ran, "third") |
| 68 | return nil |
| 69 | }) |
| 70 | |
| 71 | return nil |
| 72 | }) |
| 73 | require.EqualError(t, err, `river: duplicate resumable step name "step1"`) |
| 74 | require.Empty(t, ran) |