Skip to content

Commit 376000a

Browse files
committed
Tweak test
1 parent a4ea6b5 commit 376000a

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

backend/redis/expire_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/cschleiden/go-workflows/backend"
99
"github.com/cschleiden/go-workflows/client"
10+
"github.com/cschleiden/go-workflows/internal/core"
1011
"github.com/cschleiden/go-workflows/worker"
1112
"github.com/cschleiden/go-workflows/workflow"
1213
"github.com/google/uuid"
@@ -35,6 +36,8 @@ func Test_AutoExpiration(t *testing.T) {
3536
return nil
3637
}
3738

39+
w.RegisterWorkflow(wf)
40+
3841
wfi, err := c.CreateWorkflowInstance(ctx, client.WorkflowInstanceOptions{
3942
InstanceID: uuid.NewString(),
4043
}, wf)
@@ -51,3 +54,64 @@ func Test_AutoExpiration(t *testing.T) {
5154
cancel()
5255
require.NoError(t, w.WaitForCompletion())
5356
}
57+
58+
func Test_AutoExpiration_SubWorkflow(t *testing.T) {
59+
if testing.Short() {
60+
t.Skip()
61+
}
62+
63+
autoExpirationTime := time.Second * 1
64+
65+
redisClient := getClient()
66+
setup := getCreateBackend(redisClient, WithAutoExpiration(autoExpirationTime))
67+
b := setup()
68+
69+
c := client.New(b)
70+
w := worker.New(b, &worker.DefaultWorkerOptions)
71+
72+
ctx, cancel := context.WithCancel(context.Background())
73+
74+
require.NoError(t, w.Start(ctx))
75+
76+
swf := func(ctx workflow.Context) (int, error) {
77+
return 42, nil
78+
}
79+
80+
swfInstanceID := uuid.NewString()
81+
82+
wf := func(ctx workflow.Context) (int, error) {
83+
r, err := workflow.CreateSubWorkflowInstance[int](ctx, workflow.SubWorkflowOptions{
84+
InstanceID: swfInstanceID,
85+
}, swf).Get(ctx)
86+
87+
workflow.ScheduleTimer(ctx, time.Second*2).Get(ctx)
88+
89+
return r, err
90+
}
91+
92+
w.RegisterWorkflow(wf)
93+
w.RegisterWorkflow(swf)
94+
95+
wfi, err := c.CreateWorkflowInstance(ctx, client.WorkflowInstanceOptions{
96+
InstanceID: uuid.NewString(),
97+
}, wf)
98+
require.NoError(t, err)
99+
100+
r, err := client.GetWorkflowResult[int](ctx, c, wfi, time.Second*10)
101+
require.NoError(t, err)
102+
require.Equal(t, 42, r)
103+
104+
// Subworkflow should be expired by now
105+
_, err = b.GetWorkflowInstanceState(ctx, core.NewWorkflowInstance(swfInstanceID))
106+
require.ErrorIs(t, err, backend.ErrInstanceNotFound)
107+
108+
// Wait for redis to expire the keys
109+
time.Sleep(autoExpirationTime)
110+
111+
// Main workflow should now be expired
112+
_, err = b.GetWorkflowInstanceState(ctx, wfi)
113+
require.ErrorIs(t, err, backend.ErrInstanceNotFound)
114+
115+
cancel()
116+
require.NoError(t, w.WaitForCompletion())
117+
}

0 commit comments

Comments
 (0)