-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathengine_nested_test.go
More file actions
156 lines (126 loc) · 3.97 KB
/
Copy pathengine_nested_test.go
File metadata and controls
156 lines (126 loc) · 3.97 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
package floxy
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type maybeErrorCtxKey struct{}
func TestEngineNested(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
store, txManager, cleanup := setupTestStore(t)
t.Cleanup(cleanup)
ctx := context.Background()
engine := NewEngine(nil,
WithEngineStore(store),
WithEngineTxManager(txManager),
WithEngineCancelInterval(time.Minute),
)
defer func() { _ = engine.Shutdown(time.Second) }()
target := &FloxyStressTarget{
engine: engine,
}
target.registerHandlers()
workflowDef := nestedWorkflow(t)
err := engine.RegisterWorkflow(ctx, workflowDef)
require.NoError(t, err)
t.Run("without error", func(t *testing.T) {
// Create random order data
order := map[string]any{
"order_id": fmt.Sprintf("ORD-%d", time.Now().UnixNano()),
"user_id": fmt.Sprintf("user-%d", rand.Intn(1000)),
"amount": float64(rand.Intn(500) + 50),
"items": rand.Intn(10) + 1,
}
input, _ := json.Marshal(order)
instanceID, err := engine.Start(ctx, "nested-workflow-v1", input)
require.NoError(t, err)
// Process workflow until completion or failure
for i := 0; i < 20; i++ {
empty, err := engine.ExecuteNext(ctx, "worker1")
require.NoError(t, err)
if empty {
break
}
time.Sleep(10 * time.Millisecond)
}
// Check final status
status, err := engine.GetStatus(ctx, instanceID)
require.NoError(t, err)
// Check steps
steps, err := engine.GetSteps(ctx, instanceID)
require.NoError(t, err)
stepNames := make([]string, len(steps))
stepStatuses := make(map[string]StepStatus)
for i, step := range steps {
stepNames[i] = step.StepName
stepStatuses[step.StepName] = step.Status
}
t.Logf("Final status: %s", status)
t.Logf("Executed steps: %v", stepNames)
t.Logf("Step statuses: %v", stepStatuses)
assert.Equal(t, StatusCompleted, status)
assert.Equal(t,
[]string{"validate", "process-payment", "payment-checkpoint", "nested-parallel",
"reserve-inventory-1", "reserve-inventory-2", "ship-1", "ship-2", "join", "notify"},
stepNames,
)
for _, step := range steps {
assert.Equal(t, StepStatusCompleted, step.Status, "Step %s should be completed", step.StepName)
}
})
t.Run("with error in shipping", func(t *testing.T) {
// Create random order data
order := map[string]any{
"order_id": fmt.Sprintf("ORD-%d", time.Now().UnixNano()),
"user_id": fmt.Sprintf("user-%d", rand.Intn(1000)),
"amount": float64(rand.Intn(500) + 50),
"items": rand.Intn(10) + 1,
}
input, _ := json.Marshal(order)
instanceID, err := engine.Start(ctx, "nested-workflow-v1", input)
require.NoError(t, err)
ctx = context.WithValue(ctx, maybeErrorCtxKey{}, "shipping")
// Process workflow until completion or failure
for i := 0; i < 100; i++ {
empty, err := engine.ExecuteNext(ctx, "worker1")
require.NoError(t, err)
if empty {
break
}
time.Sleep(10 * time.Millisecond)
}
// Check final status
status, err := engine.GetStatus(ctx, instanceID)
require.NoError(t, err)
// Check steps
steps, err := engine.GetSteps(ctx, instanceID)
require.NoError(t, err)
stepNames := make([]string, len(steps))
stepStatuses := make(map[string]StepStatus)
for i, step := range steps {
stepNames[i] = step.StepName
stepStatuses[step.StepName] = step.Status
}
t.Logf("Final status: %s", status)
t.Logf("Executed steps: %v", stepNames)
t.Logf("Step statuses: %v", stepStatuses)
assert.Equal(t, StatusFailed, status)
for _, step := range steps {
switch step.StepName {
case "validate", "process-payment", "payment-checkpoint":
assert.Equal(t, StepStatusCompleted, step.Status)
case "fork", "join", "nested-parallel", "reserve-inventory-1", "reserve-inventory-2", "ship-1", "ship-2":
assert.Equal(t, StepStatusRolledBack, step.Status)
default:
assert.Fail(t, "Unexpected step %s", step.StepName)
}
}
})
}