Skip to content

Commit 6871a90

Browse files
committed
Beginning of e2e test
1 parent 30abdcc commit 6871a90

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

backend/sqlite/sqlite_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ func Test_SqliteBackend(t *testing.T) {
1313
return NewInMemoryBackend(backend.WithStickyTimeout(0))
1414
}, nil)
1515
}
16+
17+
func Test_EndToEndSqliteBackend(t *testing.T) {
18+
test.EndToEndBackendTest(t, func() backend.Backend {
19+
// Disable sticky workflow behavior for the test execution
20+
return NewInMemoryBackend(backend.WithStickyTimeout(0))
21+
}, nil)
22+
}

backend/test/e2e.go

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

0 commit comments

Comments
 (0)