|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/cschleiden/go-workflows/backend" |
| 9 | + "github.com/cschleiden/go-workflows/client" |
| 10 | + "github.com/cschleiden/go-workflows/core" |
| 11 | + "github.com/cschleiden/go-workflows/worker" |
| 12 | + "github.com/cschleiden/go-workflows/workflow" |
| 13 | + "github.com/google/uuid" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +var e2eContinueAsNewTests = []backendTest{ |
| 18 | + { |
| 19 | + name: "ContinueAsNew/RestartsWorkflowInstance", |
| 20 | + f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) { |
| 21 | + wf := func(ctx workflow.Context, run int) (int, error) { |
| 22 | + run = run + 1 |
| 23 | + if run < 3 { |
| 24 | + return run, workflow.ContinueAsNew(ctx, run) |
| 25 | + } |
| 26 | + |
| 27 | + return run, nil |
| 28 | + } |
| 29 | + register(t, ctx, w, []interface{}{wf}, nil) |
| 30 | + |
| 31 | + instance := runWorkflow(t, ctx, c, wf, 0) |
| 32 | + |
| 33 | + r, err := client.GetWorkflowResult[int](ctx, c, instance, time.Second*10) |
| 34 | + require.NoError(t, err) |
| 35 | + require.Equal(t, 1, r) |
| 36 | + |
| 37 | + state, err := b.GetWorkflowInstanceState(ctx, instance) |
| 38 | + require.NoError(t, err) |
| 39 | + require.Equal(t, core.WorkflowInstanceStateContinuedAsNew, state) |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "ContinueAsNew/Subworkflow", |
| 44 | + f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) { |
| 45 | + swf := func(ctx workflow.Context, run int) (int, error) { |
| 46 | + l := workflow.Logger(ctx) |
| 47 | + |
| 48 | + run = run + 1 |
| 49 | + if run < 3 { |
| 50 | + l.Debug("continue as new", "run", run) |
| 51 | + return run, workflow.ContinueAsNew(ctx, run) |
| 52 | + } |
| 53 | + |
| 54 | + return run, nil |
| 55 | + } |
| 56 | + |
| 57 | + wf := func(ctx workflow.Context, run int) (int, error) { |
| 58 | + return workflow.CreateSubWorkflowInstance[int](ctx, workflow.DefaultSubWorkflowOptions, swf, run).Get(ctx) |
| 59 | + } |
| 60 | + register(t, ctx, w, []interface{}{wf, swf}, nil) |
| 61 | + |
| 62 | + instance := runWorkflow(t, ctx, c, wf, 0) |
| 63 | + |
| 64 | + r, err := client.GetWorkflowResult[int](ctx, c, instance, time.Second*20) |
| 65 | + require.NoError(t, err) |
| 66 | + require.Equal(t, 3, r) |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "ContinueAsNew/OldInstancesAreRemoved", |
| 71 | + options: []backend.BackendOption{backend.WithRemoveContinuedAsNewInstances()}, |
| 72 | + f: func(t *testing.T, ctx context.Context, c *client.Client, w *worker.Worker, b TestBackend) { |
| 73 | + var swfInstances []*workflow.Instance |
| 74 | + |
| 75 | + swf := func(ctx workflow.Context, iteration int) (int, error) { |
| 76 | + if iteration > 3 { |
| 77 | + return 42, nil |
| 78 | + } |
| 79 | + |
| 80 | + // Keep track of continuedasnew instances |
| 81 | + swfInstances = append(swfInstances, workflow.WorkflowInstance(ctx)) |
| 82 | + |
| 83 | + return 0, workflow.ContinueAsNew(ctx, iteration+1) |
| 84 | + } |
| 85 | + |
| 86 | + swfInstanceID := uuid.NewString() |
| 87 | + |
| 88 | + wf := func(ctx workflow.Context) (int, error) { |
| 89 | + l := workflow.Logger(ctx) |
| 90 | + l.Debug("Starting sub workflow", "instanceID", swfInstanceID) |
| 91 | + |
| 92 | + r, err := workflow.CreateSubWorkflowInstance[int](ctx, workflow.SubWorkflowOptions{ |
| 93 | + InstanceID: swfInstanceID, |
| 94 | + }, swf, 0).Get(ctx) |
| 95 | + |
| 96 | + workflow.ScheduleTimer(ctx, time.Second*2).Get(ctx) |
| 97 | + |
| 98 | + return r, err |
| 99 | + } |
| 100 | + |
| 101 | + register(t, ctx, w, []interface{}{wf, swf}, nil) |
| 102 | + |
| 103 | + wfi := runWorkflow(t, ctx, c, wf) |
| 104 | + |
| 105 | + // // Wait for redis to expire the keys |
| 106 | + // time.Sleep(autoExpirationTime * 2) |
| 107 | + |
| 108 | + // Main workflow should still be there |
| 109 | + r, err := client.GetWorkflowResult[int](ctx, c, wfi, time.Second*10) |
| 110 | + require.NoError(t, err) |
| 111 | + require.Equal(t, 42, r) |
| 112 | + |
| 113 | + // All continued-as-new sub-workflow instances should be expired |
| 114 | + for _, swfInstance := range swfInstances { |
| 115 | + _, err = b.GetWorkflowInstanceState(ctx, swfInstance) |
| 116 | + require.ErrorIs(t, err, backend.ErrInstanceNotFound) |
| 117 | + } |
| 118 | + }, |
| 119 | + }, |
| 120 | +} |
0 commit comments