-
Notifications
You must be signed in to change notification settings - Fork 7
/
testing_test.go
162 lines (135 loc) · 5.05 KB
/
testing_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package workflow_test
import (
"context"
"encoding/json"
"strings"
"testing"
"time"
"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 TestTriggerCallbackOn_validation(t *testing.T) {
t.Run("TriggerCallbackOn must be used in tests", func(t *testing.T) {
require.PanicsWithValue(t,
"TriggerCallbackOn can only be used for testing",
func() {
workflow.TriggerCallbackOn[string, status](nil, nil, "", "", StatusStart, "")
}, "Not providing a testing.T or testing.B should panic")
})
t.Run("TriggerCallbackOn must be provided with a workflow that is using a record store that implements TestingRecordStore", func(t *testing.T) {
require.PanicsWithValue(t,
"TestingRecordStore implementation for record store dependency required",
func() {
b := workflow.NewBuilder[string, status]("test")
b.AddStep(StatusStart, func(ctx context.Context, r *workflow.Run[string, status]) (status, error) {
return r.Cancel(ctx)
}, StatusEnd)
wf := b.Build(
memstreamer.New(),
nil,
memrolescheduler.New(),
)
workflow.Require(t, wf, "", StatusEnd, "")
}, "Not providing a workflow using a TestingRecordStore implemented record store should panic")
})
}
func TestAwaitTimeoutInsert_validation(t *testing.T) {
t.Run("AwaitTimeoutInsert must be used in tests", func(t *testing.T) {
require.PanicsWithValue(t,
"AwaitTimeoutInsert can only be used for testing",
func() {
workflow.AwaitTimeoutInsert[string, status](nil, nil, "", "", StatusStart)
}, "Not providing a testing.T or testing.B should panic")
})
t.Run("AwaitTimeoutInsert must be provided with a workflow that is using a record store that implements TestingRecordStore", func(t *testing.T) {
require.PanicsWithValue(t,
"TestingRecordStore implementation for record store dependency required",
func() {
b := workflow.NewBuilder[string, status]("test")
b.AddStep(StatusStart, func(ctx context.Context, r *workflow.Run[string, status]) (status, error) {
return r.Cancel(ctx)
}, StatusEnd)
wf := b.Build(
memstreamer.New(),
nil,
memrolescheduler.New(),
)
workflow.Require(t, wf, "", StatusEnd, "")
}, "Not providing a workflow using a TestingRecordStore implemented record store should panic")
})
}
func TestRequire(t *testing.T) {
b := workflow.NewBuilder[testCustomMarshaler, status]("test")
b.AddStep(StatusStart, func(ctx context.Context, r *workflow.Run[testCustomMarshaler, status]) (status, error) {
*r.Object = "Lower"
return StatusEnd, nil
}, StatusEnd)
wf := b.Build(
memstreamer.New(),
memrecordstore.New(),
memrolescheduler.New(),
)
ctx := context.Background()
wf.Run(ctx)
t.Cleanup(wf.Stop)
fid := "10298309123"
_, err := wf.Trigger(ctx, fid, StatusStart)
require.Nil(t, err)
workflow.Require(t, wf, fid, StatusEnd, "Lower")
}
func TestRequire_validation(t *testing.T) {
t.Run("Require must be provided with testing and will panic without", func(t *testing.T) {
require.PanicsWithValue(t,
"Require can only be used for testing",
func() {
workflow.Require[string, status](nil, nil, "", StatusEnd, "")
}, "Not providing a testing.T or testing.B should panic")
})
t.Run("Require must be provided with a workflow that is using a record store that implements TestingRecordStore", func(t *testing.T) {
require.PanicsWithValue(t,
"TestingRecordStore implementation for record store dependency required",
func() {
b := workflow.NewBuilder[string, status]("test")
b.AddStep(StatusStart, func(ctx context.Context, r *workflow.Run[string, status]) (status, error) {
return r.Cancel(ctx)
}, StatusEnd)
wf := b.Build(
memstreamer.New(),
nil,
memrolescheduler.New(),
)
workflow.Require(t, wf, "", StatusEnd, "")
}, "Not providing a workflow using a TestingRecordStore implemented record store should panic")
})
}
// testCustomMarshaler is for testing and implements custom and weird behaviour via the
// MarshalJSON method and this is to test that the Require function can successfully compare
// the actual and expected by running them through the same encoding / decoding process.
type testCustomMarshaler string
func (t testCustomMarshaler) MarshalJSON() ([]byte, error) {
return json.Marshal(strings.ToLower(string(t)))
}
func TestWaitFor(t *testing.T) {
b := workflow.NewBuilder[string, status]("test")
b.AddStep(StatusStart, func(ctx context.Context, r *workflow.Run[string, status]) (status, error) {
return StatusEnd, nil
}, StatusEnd)
wf := b.Build(
memstreamer.New(),
memrecordstore.New(),
memrolescheduler.New(),
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)
wf.Run(ctx)
t.Cleanup(wf.Stop)
fid := "10298309123"
_, err := wf.Trigger(ctx, fid, StatusStart)
require.Nil(t, err)
workflow.WaitFor(t, wf, fid, func(r *workflow.Run[string, status]) (bool, error) {
return r.RunState == workflow.RunStateCompleted, nil
})
}