-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_contract_shared_test.go
More file actions
322 lines (293 loc) · 8.32 KB
/
Copy pathrunner_contract_shared_test.go
File metadata and controls
322 lines (293 loc) · 8.32 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
package flowy
import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
type stubHandoffOutbox struct {
mu sync.Mutex
calls []HandoffIntent
err error
onEnqueue func(intent HandoffIntent) error
}
func (s *stubHandoffOutbox) EnqueueIntent(_ context.Context, intent HandoffIntent) error {
s.mu.Lock()
defer s.mu.Unlock()
s.calls = append(s.calls, intent)
if s.onEnqueue != nil {
if err := s.onEnqueue(intent); err != nil {
return err
}
}
return s.err
}
func (s *stubHandoffOutbox) EnqueueIntentTx(ctx context.Context, _ TransactionHandle, intent HandoffIntent) error {
return s.EnqueueIntent(ctx, intent)
}
func (s *stubHandoffOutbox) lastToken() ResumeToken {
s.mu.Lock()
defer s.mu.Unlock()
if len(s.calls) == 0 {
return ResumeToken{}
}
return s.calls[len(s.calls)-1].ResumeToken
}
func (s *stubHandoffOutbox) lastIntent() HandoffIntent {
s.mu.Lock()
defer s.mu.Unlock()
if len(s.calls) == 0 {
return HandoffIntent{}
}
return s.calls[len(s.calls)-1]
}
type deleteSpyCP[T, E any] struct {
*memoryCP[T, E]
deleteCalls int
}
func (c *deleteSpyCP[T, E]) Delete(ctx context.Context, threadID string) error {
c.deleteCalls++
return c.memoryCP.Delete(ctx, threadID)
}
func retentionFailureGraph[T any, E any](t *testing.T, directive Directive) (*Graph[T, E], *failingMemoryCP[T, E]) {
t.Helper()
cp := &failingMemoryCP[T, E]{
memoryCP: newMemoryCP[T, E](),
failPrune: true,
}
b := NewGraph[T, E](func(_ T, u T) T { return u })
b.AddNode("work", func(_ context.Context, s T) (T, Directive, error) {
return s, directive, nil
})
b.AllowNoOutgoingRoute("work")
b.SetEntryPoint("work")
g, err := b.Compile(WithRetentionLimit(2))
if err != nil {
t.Fatalf("compile: %v", err)
}
return g, cp
}
type infraTestState struct{}
func infraFailureHandoffResolveGraph(
t *testing.T,
) (*Graph[infraTestState, NoEffect], Checkpointer[infraTestState, NoEffect]) {
t.Helper()
cp := newMemoryCP[infraTestState, NoEffect]()
b := NewGraph[infraTestState, NoEffect](func(_ infraTestState, u infraTestState) infraTestState { return u })
b.AddNode("work", func(_ context.Context, s infraTestState) (infraTestState, Directive, error) {
return s, Handoff("bg", ResumeAt("ghost")), nil
})
b.AllowNoOutgoingRoute("work")
b.SetEntryPoint("work")
g, err := b.Compile()
if err != nil {
t.Fatalf("compile: %v", err)
}
return g, cp
}
func infraFailureHandoffSaveGraph(
t *testing.T,
) (*Graph[infraTestState, NoEffect], Checkpointer[infraTestState, NoEffect]) {
t.Helper()
cp := &failingMemoryCP[infraTestState, NoEffect]{failSave: true}
b := NewGraph[infraTestState, NoEffect](func(_ infraTestState, u infraTestState) infraTestState { return u })
b.AddNode("router", func(_ context.Context, s infraTestState) (infraTestState, Directive, error) {
return s, Handoff("bg"), nil
})
b.AddNode("work", func(_ context.Context, s infraTestState) (infraTestState, Directive, error) {
return s, Handoff("bg", ResumeAt("router")), nil
})
b.AllowNoOutgoingRoute("work")
b.AllowNoOutgoingRoute("router")
b.SetEntryPoint("work")
g, err := b.Compile()
if err != nil {
t.Fatalf("compile: %v", err)
}
return g, cp
}
func assertInfraFailureStreamSync[T, E any](
t *testing.T,
g *Graph[T, E],
cp Checkpointer[T, E],
opts []RunOption[T, E],
wantStatus RunStatus,
wantEvent EventType,
wantPointer string,
wantReason string,
) {
t.Helper()
syncRes, syncErr := g.NewRunner(cp).Start(context.Background(), "infra-sync-th", *new(T), opts...)
if syncErr == nil {
t.Fatalf("expected sync infra failure, got res=%+v", syncRes)
}
if syncRes == nil || syncRes.Status != wantStatus {
t.Fatalf("sync status: want %s, got res=%+v", wantStatus, syncRes)
}
if syncRes.Reason != wantReason {
t.Fatalf("sync reason: want %q, got %q", wantReason, syncRes.Reason)
}
if wantPointer != "" && string(syncRes.ExecutionPointer) != wantPointer {
t.Fatalf("sync pointer: want %q, got %q", wantPointer, syncRes.ExecutionPointer)
}
handle, err := g.NewRunner(cp).Stream(context.Background(), "infra-stream-th", *new(T), opts...)
if err != nil {
t.Fatalf("stream: %v", err)
}
events, waitErr := CollectEventsAndWait(context.Background(), handle)
if waitErr == nil {
t.Fatal("expected stream infra failure")
}
switch wantEvent {
case EventFailed:
assertEventFailedReasonMatchesSync(t, events, wantReason)
case EventContextCanceled:
assertTerminalEventReasonMatchesSync(t, events, wantEvent, wantReason)
default:
reason := terminalEventReason(events, wantEvent)
if reason != wantReason {
t.Fatalf("stream event reason: want %q, got %q events=%+v", wantReason, reason, events)
}
}
}
func assertEventFailedReasonMatchesSync[T, E any](t *testing.T, events []RunEvent[T, E], wantReason string) {
t.Helper()
requireEventFailedReason(t, events, wantReason)
}
func leaseLostBlockingGraph(t *testing.T, ready chan struct{}) *Graph[struct{}, NoEffect] {
t.Helper()
b := NewGraph[struct{}, NoEffect](func(_ struct{}, u struct{}) struct{} { return u })
b.AddNode("work", func(ctx context.Context, s struct{}) (struct{}, Directive, error) {
close(ready)
<-ctx.Done()
return s, Completed(), nil
})
b.AllowNoOutgoingRoute("work")
b.SetEntryPoint("work")
g, err := b.Compile()
if err != nil {
t.Fatalf("compile: %v", err)
}
return g
}
func forceLeaseTakeover(t *testing.T, lease *MemoryLeaseManager, threadID string) {
t.Helper()
if relErr := lease.Release(context.Background(), threadID, "worker-a"); relErr != nil {
t.Fatalf("release %q: %v", threadID, relErr)
}
if acqErr := lease.Acquire(context.Background(), threadID, "worker-b", time.Minute); acqErr != nil {
t.Fatalf("acquire b %q: %v", threadID, acqErr)
}
}
func stealLeaseAndWait(t *testing.T, lease *MemoryLeaseManager, threadID string) {
t.Helper()
forceLeaseTakeover(t, lease, threadID)
waitForLeaseTTLExpiry()
}
func blockingHandoffWorkGraph[T any, E any](t *testing.T) (*Graph[T, E], chan struct{}) {
t.Helper()
ready := make(chan struct{})
b := NewGraph[T, E](func(_ T, u T) T { return u })
b.AddNode("work", func(ctx context.Context, s T) (T, Directive, error) {
close(ready)
<-ctx.Done()
return s, Completed(), nil
})
b.AllowNoOutgoingRoute("work")
b.SetEntryPoint("work")
g, err := b.Compile()
if err != nil {
t.Fatalf("compile: %v", err)
}
return g, ready
}
func prodGoFilesForDoDScan(t *testing.T) []string {
t.Helper()
return collectGoFilesForDoDScan(t, false)
}
func testGoFilesForDoDScan(t *testing.T) []string {
t.Helper()
return collectGoFilesForDoDScan(t, true)
}
func collectGoFilesForDoDScan(t *testing.T, testsOnly bool) []string {
t.Helper()
var files []string
err := filepath.WalkDir(".", func(path string, d fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if d.IsDir() {
switch d.Name() {
case ".git", "vendor", "testdata":
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
isTest := strings.HasSuffix(path, "_test.go")
if testsOnly != isTest {
return nil
}
if strings.HasPrefix(path, "examples/") {
return nil
}
files = append(files, path)
return nil
})
if err != nil {
t.Fatalf("walk: %v", err)
}
return files
}
func assertNoCheckpointCollectorNeedles(t *testing.T, files, forbidden []string) {
t.Helper()
for _, file := range files {
if strings.HasSuffix(file, "runner_dod_contracts_test.go") {
continue
}
data, err := os.ReadFile(file)
if err != nil {
t.Fatalf("read %s: %v", file, err)
}
content := string(data)
for _, needle := range forbidden {
if strings.Contains(content, needle) {
t.Fatalf("%s contains forbidden %q", file, needle)
}
}
}
}
type pointerSpyCP[T, E any] struct {
*memoryCP[T, E]
savedPointers []ExecutionPointer
}
func (p *pointerSpyCP[T, E]) Save(
_ context.Context,
expectedRevision uint64,
snapshot Snapshot[T, E],
) (uint64, error) {
p.savedPointers = append(p.savedPointers, snapshot.ExecutionPointer)
return p.memoryCP.Save(context.Background(), expectedRevision, snapshot)
}
type countingFailingMemoryCP[T, E any] struct {
*memoryCP[T, E]
saveCount int
}
func (c *countingFailingMemoryCP[T, E]) Save(
_ context.Context,
expectedRevision uint64,
snapshot Snapshot[T, E],
) (uint64, error) {
c.saveCount++
if c.saveCount > 1 {
return 0, errors.New("save failed")
}
return c.memoryCP.Save(context.Background(), expectedRevision, snapshot)
}