-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtesting_test.go
218 lines (179 loc) · 7.19 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package workflow_test
import (
"context"
"encoding/json"
"io"
"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.Workflow", func(t *testing.T) {
require.PanicsWithValue(t,
"*workflow.Workflow required for testing utility functions",
func() {
api := &apiImpl[string, status]{}
workflow.Require(t, api, "", StatusEnd, "")
}, "Not providing a workflow using a TestingRecordStore implemented record store 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)
w := b.Build(
memstreamer.New(),
nil,
memrolescheduler.New(),
)
workflow.TriggerCallbackOn(t, w, "", "", 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")
})
t.Run("AwaitTimeoutInsert must be provided with a *workflow.Workflow", func(t *testing.T) {
require.PanicsWithValue(t,
"*workflow.Workflow required for testing utility functions",
func() {
api := &apiImpl[string, status]{}
workflow.AwaitTimeoutInsert(t, api, "", "", 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")
})
t.Run("Require must be provided with a *workflow.Workflow", func(t *testing.T) {
require.PanicsWithValue(t,
"*workflow.Workflow required for testing utility functions",
func() {
api := &apiImpl[string, status]{}
workflow.Require(t, api, "", 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
})
}
var _ workflow.API[string, status] = (*apiImpl[string, status])(nil)
type apiImpl[Type any, Status workflow.StatusType] struct{}
func (a apiImpl[Type, Status]) Name() string {
return "test"
}
func (a apiImpl[Type, Status]) Trigger(ctx context.Context, foreignID string, startingStatus Status, opts ...workflow.TriggerOption[Type, Status]) (runID string, err error) {
return "", nil
}
func (a apiImpl[Type, Status]) Schedule(foreignID string, startingStatus Status, spec string, opts ...workflow.ScheduleOption[Type, Status]) error {
return nil
}
func (a apiImpl[Type, Status]) Await(ctx context.Context, foreignID, runID string, status Status, opts ...workflow.AwaitOption) (*workflow.Run[Type, Status], error) {
return &workflow.Run[Type, Status]{}, nil
}
func (a apiImpl[Type, Status]) Callback(ctx context.Context, foreignID string, status Status, payload io.Reader) error {
return nil
}
func (a apiImpl[Type, Status]) Run(ctx context.Context) {}
func (a apiImpl[Type, Status]) Stop() {}