Repository navigation
Expand file tree
/
Copy pathexpense_test.go
More file actions
354 lines (308 loc) · 9.57 KB
/
Copy pathexpense_test.go
File metadata and controls
354 lines (308 loc) · 9.57 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Based on https://github.com/temporalio/samples-go/blob/main/expense/
//
// This example shows a simple payment workflow with an async approval input,
// going from CreatingExpense to PaymentCompleted.
//
// Part of the workflow is implemented imperatively, just like in Temporal.
//
// Sample output (LogLevel == LogChanges):
//
// === RUN TestExpense
// expense_test.go:145: [state] +CreatingExpense
// expense_test.go:145: [state:auto] +WaitingForApproval +PaymentInProgress
// expense_test.go:164: waiting: CreatingExpense to WaitingForApproval
// expense_test.go:180: waiting: WaitingForApproval to ApprovalGranted
// expense_test.go:250: expense API called
// expense_test.go:233: approval request received: 123
// expense_test.go:263: payment API called
// expense_test.go:145: [state] +ExpenseCreated -CreatingExpense
// expense_test.go:145: [state] +PaymentCompleted -PaymentInProgress
// expense_test.go:237: granting fake approval
// expense_test.go:239: sent fake approval
// expense_test.go:192: received approval ID: fake
// expense_test.go:180: waiting: WaitingForApproval to ApprovalGranted
// expense_test.go:243: granting real approval
// expense_test.go:192: received approval ID: 123
// expense_test.go:245: sent real approval
// expense_test.go:145: [state] +ApprovalGranted -WaitingForApproval
// expense_test.go:180: waiting: WaitingForApproval to ApprovalGranted
// expense_test.go:213: waiting: PaymentInProgress to PaymentCompleted
// expense_test.go:280:
// (ApprovalGranted:1 PaymentCompleted:1 ExpenseCreated:1)
// expense_test.go:281:
// (ApprovalGranted:1 ExpenseCreated:1 PaymentCompleted:1)[CreatingExpense:2 Exception:0 PaymentInProgress:2 WaitingForApproval:2]
// expense_test.go:282:
// ApprovalGranted:
// State: true 1
// Remove: WaitingForApproval
//
// CreatingExpense:
// State: false 2
// Remove: ExpenseCreated
//
// Exception:
// State: false 0
// Multi: true
//
// ExpenseCreated:
// State: true 1
// Remove: CreatingExpense
//
// PaymentCompleted:
// State: true 1
// Remove: PaymentInProgress
//
// PaymentInProgress:
// State: false 2
// Auto: true
// Remove: PaymentCompleted
//
// WaitingForApproval:
// State: false 2
// Auto: true
// Remove: ApprovalGranted
//
//
// --- PASS: TestExpense (0.02s)
// PASS
package main
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/joho/godotenv"
ss "github.com/pancsta/asyncmachine-go/examples/temporal_expense/states"
amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
)
var (
expenseBackendURL string
paymentBackendURL string
)
type Logger func(msg string, args ...any)
// MachineHandlers is a struct of handlers & their data for the Expense machine.
// None of the handlers can block.
type MachineHandlers struct {
// default handler for the build in Exception state
am.ExceptionHandler
expenseID string
}
func init() {
// load .env
_ = godotenv.Load()
// am-dbg is required for debugging, go run it
// go run github.com/pancsta/asyncmachine-go/tools/cmd/am-dbg@latest
// amhelp.EnableDebugging(false)
// amhelp.SetEnvLogLevel(am.LogOps)
}
// CreatingExpenseState is a _final_ entry handler for the CreatingExpense
// state.
func (h *MachineHandlers) CreatingExpenseState(e *am.Event) {
// args definition
h.expenseID = e.Args["expenseID"].(string)
// tick-based ctx
stateCtx := e.Machine().NewStateCtx(ss.CreatingExpense)
// unblock
go func() {
if stateCtx.Err() != nil {
return // expired
}
resp, err := http.Get(expenseBackendURL + "?id=" + h.expenseID)
if err != nil {
e.Machine().AddErr(err, nil)
return
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
e.Machine().AddErr(err, nil)
return
}
if string(body) != "SUCCEED" {
e.Machine().AddErr(fmt.Errorf("%s", body), nil)
return
}
// make sure this should still be happening
if stateCtx.Err() != nil {
return // expired
}
e.Machine().Add1(ss.ExpenseCreated, nil)
}()
}
// ApprovalGrantedEnter is a _negotiation_ entry handler for the ApprovalGranted
// state. It can cancel the transition by returning false.
func (h *MachineHandlers) ApprovalGrantedEnter(e *am.Event) bool {
// args definition
approvedID := e.Args["approvedID"].(string)
// return TRUE for when negotiating for ApprovalGranted
// only if approved and expense is the same ID
return approvedID == h.expenseID
}
// PaymentInProgressState is a _final_ entry handler for the PaymentInProgress
// state.
func (h *MachineHandlers) PaymentInProgressState(e *am.Event) {
// tick-based ctx
stateCtx := e.Machine().NewStateCtx(ss.PaymentInProgress)
// unblock
go func() {
if stateCtx.Err() != nil {
return // expired
}
url := paymentBackendURL + "?id=" + h.expenseID
resp, err := http.Get(url)
if err != nil {
e.Machine().AddErr(err, nil)
return
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
e.Machine().AddErr(err, nil)
return
}
if string(body) != "SUCCEED" {
e.Machine().AddErr(fmt.Errorf("%s", body), nil)
return
}
e.Machine().Add1(ss.PaymentCompleted, nil)
}()
}
// ExpenseFlow is an example of how to use the Expense machine.
//
// NOTE: this whole flow could be implemented declaratively as states,
// but mimics the `SampleExpenseWorkflow` function from the Temporal example
// for clarity.
//
// approvalCh: channel producing approved expense IDs
func ExpenseFlow(
ctx context.Context, log Logger, approvalCh chan string, expenseID string,
) (*am.Machine, error) {
// init
mach := am.New(ctx, ss.States, &am.Opts{
DontLogId: true,
})
amhelp.MachDebugEnv(mach)
// different log granularity and a custom output
mach.SemLogger().SetLevel(am.LogChanges)
// machine.SemLogger().SetLevel(am.LogOps)
// machine.SemLogger().SetLevel(am.LogDecisions)
// machine.SemLogger().SetLevel(am.LogEverything)
mach.SemLogger().SetLogger(func(l am.LogLevel, msg string, args ...any) {
log(msg, args...)
})
// bind handlers
_, err := mach.HandlersBind(&MachineHandlers{})
if err != nil {
return mach, err
}
// reusable error channel
errCh := mach.WhenErr(nil)
// start the flow
mach.Add1(ss.CreatingExpense, am.A{"expenseID": expenseID})
// CreatingExpense is an automatic state, it will add itself at this point.
// string(machine) == [Enabled, CreatingExpense]
// CreatingExpense to WaitingForApproval
log("waiting: CreatingExpense to WaitingForApproval")
select {
case <-time.After(10 * time.Second):
return mach, errors.New("timeout")
case <-errCh:
return mach, mach.Err()
case <-mach.When1(ss.WaitingForApproval, nil):
// WaitingForApproval is an automatic state
}
_ = mach.String() // (ExpenseCreated:1 WaitingForApproval:1)
// WaitingForApproval to ApprovalGranted
// Passes all new approval IDs to the machine, multiple times.
granted := false
for !granted {
log("waiting: WaitingForApproval to ApprovalGranted")
// wait with a timeout
select {
// new approvals
case approvedID, ok := <-approvalCh:
if !ok {
return mach, errors.New("approval channel closed")
}
// TRY to approve the existing expense
log("received approval ID: %s", approvedID)
mach.Add1(ss.ApprovalGranted, am.A{"approvedID": approvedID})
// approval timeout
case <-time.After(10 * time.Minute):
return mach, errors.New("timeout")
// error or machine disposed
case <-errCh:
return mach, mach.Err()
// approval granted
case <-mach.When1(ss.ApprovalGranted, nil):
granted = true
}
}
// PaymentInProgress is an automatic state, it will add itself at this point.
_ = mach.String() // (ExpenseCreated:1 ApprovalGranted:1 PaymentInProgress:1)
// PaymentInProgress to PaymentCompleted
log("waiting: PaymentInProgress to PaymentCompleted")
select {
case <-errCh:
return mach, mach.Err()
case <-mach.When1(ss.PaymentCompleted, nil):
}
_ = mach.String() // (ExpenseCreated:1 ApprovalGranted:1 PaymentCompleted:1)
return mach, nil
}
func TestExpense(t *testing.T) {
// init
expenseCh := make(chan string)
approvalCh := make(chan string)
// mock an external approval flow
go func() {
expenseId := <-expenseCh
t.Log("approval request received: " + expenseId)
time.Sleep(10 * time.Millisecond)
// approve a random ID
t.Log("granting fake approval")
approvalCh <- "fake"
t.Log("sent fake approval")
time.Sleep(10 * time.Millisecond)
// approve our ID
t.Log("granting real approval")
approvalCh <- expenseId
t.Log("sent real approval")
}()
// mock the expense API
expenseAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Log("expense API called")
// new expense ID
fmt.Fprint(w, "SUCCEED")
// start the external approval flow
expenseCh <- "123"
}))
defer expenseAPI.Close()
expenseBackendURL = expenseAPI.URL
// mock the payment API
paymentAPI := httptest.NewServer(http.HandlerFunc(func(w http.
ResponseWriter, r *http.Request,
) {
t.Log("payment API called")
// payment successful
_, _ = fmt.Fprint(w, "SUCCEED")
}))
defer paymentAPI.Close()
paymentBackendURL = paymentAPI.URL
// start the flow and wait for the result
mach, err := ExpenseFlow(context.Background(), t.Logf, approvalCh, "123")
if err != nil {
t.Fatal(err)
}
if !mach.Is1(ss.PaymentCompleted) {
t.Fatal("not PaymentCompleted")
}
t.Log(mach.String())
t.Log(mach.StringAll())
t.Log("\n" + mach.Inspect(nil))
}