-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhook_test.go
101 lines (78 loc) · 2.31 KB
/
hook_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package workflow_test
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/luno/workflow"
"github.com/luno/workflow/adapters/memrecordstore"
"github.com/luno/workflow/adapters/memrolescheduler"
"github.com/luno/workflow/adapters/memstreamer"
)
func TestWorkflow_OnPauseHook(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
wf := setupHookTest(t, func(b *workflow.Builder[MyType, status]) {
b.AddStep(StatusStart, func(ctx context.Context, r *workflow.Run[MyType, status]) (status, error) {
return r.Pause(ctx)
}, StatusMiddle)
b.OnPause(func(ctx context.Context, record *workflow.TypedRecord[MyType, status]) error {
wg.Done()
return nil
})
})
foreignID := "andrew"
_, err := wf.Trigger(context.Background(), foreignID, StatusStart)
require.Nil(t, err)
wg.Wait()
}
func TestWorkflow_OnCancelHook(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
wf := setupHookTest(t, func(b *workflow.Builder[MyType, status]) {
b.AddStep(StatusStart, func(ctx context.Context, r *workflow.Run[MyType, status]) (status, error) {
return r.Cancel(ctx)
}, StatusMiddle)
b.OnCancel(func(ctx context.Context, record *workflow.TypedRecord[MyType, status]) error {
wg.Done()
return nil
})
})
foreignID := "andrew"
_, err := wf.Trigger(context.Background(), foreignID, StatusStart)
require.Nil(t, err)
wg.Wait()
}
func TestWorkflow_OnCompleteHook(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
wf := setupHookTest(t, func(b *workflow.Builder[MyType, status]) {
b.AddStep(StatusStart, func(ctx context.Context, r *workflow.Run[MyType, status]) (status, error) {
return StatusEnd, nil
}, StatusEnd)
b.OnComplete(func(ctx context.Context, record *workflow.TypedRecord[MyType, status]) error {
wg.Done()
return nil
})
})
foreignID := "andrew"
_, err := wf.Trigger(context.Background(), foreignID, StatusStart)
require.Nil(t, err)
wg.Wait()
}
func setupHookTest(t *testing.T, custom func(b *workflow.Builder[MyType, status])) *workflow.Workflow[MyType, status] {
b := workflow.NewBuilder[MyType, status]("hooks")
custom(b)
wf := b.Build(
memstreamer.New(),
memrecordstore.New(),
memrolescheduler.New(),
)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
wf.Run(ctx)
t.Cleanup(wf.Stop)
return wf
}