|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/cschleiden/go-workflows/backend" |
| 10 | + "github.com/cschleiden/go-workflows/client" |
| 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 | +func EndToEndBackendTest(t *testing.T, setup func() backend.Backend, teardown func(b backend.Backend)) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + f func(t *testing.T, ctx context.Context, c client.Client, w worker.Worker) |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "SimpleWorkflow", |
| 24 | + f: func(t *testing.T, ctx context.Context, c client.Client, w worker.Worker) { |
| 25 | + wf := func(ctx workflow.Context, msg string) (string, error) { |
| 26 | + return msg + " world", nil |
| 27 | + } |
| 28 | + register(t, ctx, w, []interface{}{wf}, nil) |
| 29 | + |
| 30 | + output, err := runWorkflowWithResult[string](t, ctx, c, wf, "hello") |
| 31 | + |
| 32 | + require.Equal(t, "hello world", output) |
| 33 | + require.NoError(t, err) |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "UnregisteredWorkflow", |
| 38 | + f: func(t *testing.T, ctx context.Context, c client.Client, w worker.Worker) { |
| 39 | + wf := func(ctx workflow.Context, msg string) (string, error) { |
| 40 | + return msg + " world", nil |
| 41 | + } |
| 42 | + register(t, ctx, w, nil, nil) |
| 43 | + |
| 44 | + output, err := runWorkflowWithResult[string](t, ctx, c, wf, "hello") |
| 45 | + |
| 46 | + require.Zero(t, output) |
| 47 | + require.ErrorContains(t, err, "workflow 1 not found") |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "UnregisteredActivity", |
| 52 | + f: func(t *testing.T, ctx context.Context, c client.Client, w worker.Worker) { |
| 53 | + a := func(context.Context) error { return nil } |
| 54 | + wf := func(ctx workflow.Context) (int, error) { |
| 55 | + return workflow.ExecuteActivity[int](ctx, workflow.DefaultActivityOptions, a).Get(ctx) |
| 56 | + } |
| 57 | + register(t, ctx, w, []interface{}{wf}, nil) |
| 58 | + |
| 59 | + output, err := runWorkflowWithResult[int](t, ctx, c, wf) |
| 60 | + |
| 61 | + require.Zero(t, output) |
| 62 | + require.ErrorContains(t, err, "activity not found") |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + b := setup() |
| 70 | + ctx := context.Background() |
| 71 | + ctx, cancel := context.WithCancel(ctx) |
| 72 | + |
| 73 | + c := client.New(b) |
| 74 | + w := worker.New(b, &worker.DefaultWorkerOptions) |
| 75 | + |
| 76 | + tt.f(t, ctx, c, w) |
| 77 | + |
| 78 | + cancel() |
| 79 | + if err := w.WaitForCompletion(); err != nil { |
| 80 | + fmt.Println("Worker did not stop in time") |
| 81 | + t.FailNow() |
| 82 | + } |
| 83 | + |
| 84 | + if teardown != nil { |
| 85 | + teardown(b) |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func register(t *testing.T, ctx context.Context, w worker.Worker, workflows []interface{}, activities []interface{}) { |
| 92 | + for _, wf := range workflows { |
| 93 | + require.NoError(t, w.RegisterWorkflow(wf)) |
| 94 | + } |
| 95 | + |
| 96 | + for _, a := range activities { |
| 97 | + require.NoError(t, w.RegisterActivity(a)) |
| 98 | + } |
| 99 | + |
| 100 | + err := w.Start(ctx) |
| 101 | + require.NoError(t, err) |
| 102 | +} |
| 103 | + |
| 104 | +func runWorkflow(t *testing.T, ctx context.Context, c client.Client, wf interface{}, inputs ...interface{}) *workflow.Instance { |
| 105 | + instance, err := c.CreateWorkflowInstance(ctx, client.WorkflowInstanceOptions{ |
| 106 | + InstanceID: uuid.NewString(), |
| 107 | + }, wf, inputs...) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + return instance |
| 111 | +} |
| 112 | + |
| 113 | +func runWorkflowWithResult[T any](t *testing.T, ctx context.Context, c client.Client, wf interface{}, inputs ...interface{}) (T, error) { |
| 114 | + instance := runWorkflow(t, ctx, c, wf, inputs...) |
| 115 | + return client.GetWorkflowResult[T](ctx, c, instance, time.Second*10) |
| 116 | +} |
0 commit comments