-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathexecution_semaphore_test.go
More file actions
289 lines (243 loc) · 8.74 KB
/
Copy pathexecution_semaphore_test.go
File metadata and controls
289 lines (243 loc) · 8.74 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
package host
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/smartcontractkit/chainlink-common/pkg/settings/limits"
sdkpb "github.com/smartcontractkit/chainlink-protos/cre/go/sdk"
wfpb "github.com/smartcontractkit/chainlink-protos/workflows/go/v2"
)
// slowCapStub delays CallCapability by a configurable duration and counts in-flight calls.
type slowCapStub struct {
delay time.Duration
inflight atomic.Int32
peakLoad atomic.Int32
callCount atomic.Int32
}
func (s *slowCapStub) CallCapability(_ context.Context, _ *sdkpb.CapabilityRequest) (*sdkpb.CapabilityResponse, error) {
s.callCount.Add(1)
cur := s.inflight.Add(1)
for {
peak := s.peakLoad.Load()
if cur <= peak || s.peakLoad.CompareAndSwap(peak, cur) {
break
}
}
time.Sleep(s.delay)
s.inflight.Add(-1)
payload, _ := anypb.New(&emptypb.Empty{})
return &sdkpb.CapabilityResponse{
Response: &sdkpb.CapabilityResponse_Payload{Payload: payload},
}, nil
}
func (s *slowCapStub) GetSecrets(context.Context, *sdkpb.GetSecretsRequest) ([]*sdkpb.SecretResponse, error) {
return nil, nil
}
func (s *slowCapStub) GetWorkflowExecutionID() string { return "test-exec" }
func (s *slowCapStub) GetNodeTime() time.Time { return time.Now() }
func (s *slowCapStub) GetDONTime() (time.Time, error) { return time.Now(), nil }
func (s *slowCapStub) EmitUserLog(string) error { return nil }
func (s *slowCapStub) EmitUserMetric(context.Context, *wfpb.WorkflowUserMetric) error {
return nil
}
var _ ExecutionHelper = (*slowCapStub)(nil)
func newTestExec(maxPending int, stub ExecutionHelper) *execution[*sdkpb.ExecutionResult] {
return &execution[*sdkpb.ExecutionResult]{
ctx: context.Background(),
capabilityResponses: make(map[int32]*asyncResponse[sdkpb.CapabilityRequest, sdkpb.CapabilityResponse]),
secretsResponses: make(map[int32]<-chan *secretsResponse),
pendingCallsLimiter: limits.GlobalResourcePoolLimiter[int](maxPending),
executor: stub,
}
}
// TestSemaphore_BackpressureBlocksCallN proves that call N+1 blocks when
// N == MaxPendingCalls and nothing has been awaited yet.
func TestSemaphore_BackpressureBlocksCallN(t *testing.T) {
t.Parallel()
const max = 5
// Use a delay longer than the check window so goroutines hold their slots.
stub := &slowCapStub{delay: 5 * time.Second}
exec := newTestExec(max, stub)
ctx := t.Context()
// Fill semaphore.
for i := int32(0); i < max; i++ {
require.NoError(t, exec.callCapAsync(ctx, &sdkpb.CapabilityRequest{CallbackId: i}))
}
// Next call should block.
blocked := make(chan struct{})
go func() {
_ = exec.callCapAsync(ctx, &sdkpb.CapabilityRequest{CallbackId: max})
close(blocked)
}()
select {
case <-blocked:
t.Fatal("call max+1 did not block; semaphore backpressure broken")
case <-time.After(200 * time.Millisecond):
// expected — still blocked
}
// Await the first call to free a slot.
resp, err := exec.awaitCapabilities(ctx, &sdkpb.AwaitCapabilitiesRequest{Ids: []int32{0}})
require.NoError(t, err)
require.Len(t, resp.Responses, 1)
// Now the blocked call should proceed.
select {
case <-blocked:
// success
case <-time.After(2 * time.Second):
t.Fatal("call max+1 did not unblock after await freed a slot")
}
}
// TestSemaphore_HighThroughputBounded issues many calls in batches,
// awaiting each batch before the next. Peak in-flight goroutines must never
// exceed MaxPendingCalls.
func TestSemaphore_HighThroughputBounded(t *testing.T) {
t.Parallel()
const max = 10
const batches = 50
const callsPerBatch = max
stub := &slowCapStub{delay: 1 * time.Millisecond}
exec := newTestExec(max, stub)
ctx := t.Context()
var callId int32
for b := 0; b < batches; b++ {
ids := make([]int32, callsPerBatch)
for i := 0; i < callsPerBatch; i++ {
ids[i] = callId
require.NoError(t, exec.callCapAsync(ctx, &sdkpb.CapabilityRequest{CallbackId: callId}))
callId++
}
resp, err := exec.awaitCapabilities(ctx, &sdkpb.AwaitCapabilitiesRequest{Ids: ids})
require.NoError(t, err)
require.Len(t, resp.Responses, callsPerBatch)
}
assert.LessOrEqual(t, int(stub.peakLoad.Load()), max,
"peak in-flight goroutines exceeded MaxPendingCalls")
assert.Equal(t, int32(batches*callsPerBatch), stub.callCount.Load())
}
// TestSemaphore_ContextCancelUnblocksCall proves that a blocked callCapAsync
// returns ctx.Err() when the context is cancelled.
func TestSemaphore_ContextCancelUnblocksCall(t *testing.T) {
t.Parallel()
const max = 2
stub := &slowCapStub{delay: 5 * time.Second} // very slow, won't finish
exec := newTestExec(max, stub)
ctx, cancel := context.WithCancel(t.Context())
// Fill semaphore.
for i := int32(0); i < max; i++ {
require.NoError(t, exec.callCapAsync(ctx, &sdkpb.CapabilityRequest{CallbackId: i}))
}
// Next call will block on semaphore.
var callErr error
done := make(chan struct{})
go func() {
callErr = exec.callCapAsync(ctx, &sdkpb.CapabilityRequest{CallbackId: max})
close(done)
}()
// Cancel context.
cancel()
select {
case <-done:
require.ErrorIs(t, callErr, context.Canceled)
case <-time.After(2 * time.Second):
t.Fatal("callCapAsync did not unblock after context cancel")
}
}
// TestSemaphore_SlotsRecycledCorrectly ensures that after many await cycles,
// the semaphore is back to its full capacity and new calls can proceed.
func TestSemaphore_SlotsRecycledCorrectly(t *testing.T) {
t.Parallel()
const max = 5
const rounds = 100
stub := &slowCapStub{delay: 0}
exec := newTestExec(max, stub)
ctx := t.Context()
for r := 0; r < rounds; r++ {
ids := make([]int32, max)
for i := int32(0); i < max; i++ {
id := int32(r*max) + i
ids[i] = id
require.NoError(t, exec.callCapAsync(ctx, &sdkpb.CapabilityRequest{CallbackId: id}))
}
_, err := exec.awaitCapabilities(ctx, &sdkpb.AwaitCapabilitiesRequest{Ids: ids})
require.NoError(t, err)
}
// After all rounds, all slots should be available again.
// Goroutines release slots via defer after the channel send, so allow a
// brief window for the last batch of defers to execute.
assert.Eventually(t, func() bool {
avail, err := exec.pendingCallsLimiter.Available(ctx)
return err == nil && avail == max
}, time.Second, 5*time.Millisecond,
"limiter still has occupied slots after all awaits completed")
}
// TestSemaphore_MapCleanedOnAwait verifies the capabilityResponses map
// doesn't leak entries.
func TestSemaphore_MapCleanedOnAwait(t *testing.T) {
t.Parallel()
const max = 10
const total = 200
stub := &slowCapStub{delay: 0}
exec := newTestExec(max, stub)
ctx := t.Context()
for i := int32(0); i < total; i += max {
ids := make([]int32, max)
for j := int32(0); j < max; j++ {
id := i + j
ids[j] = id
require.NoError(t, exec.callCapAsync(ctx, &sdkpb.CapabilityRequest{CallbackId: id}))
}
_, err := exec.awaitCapabilities(ctx, &sdkpb.AwaitCapabilitiesRequest{Ids: ids})
require.NoError(t, err)
}
exec.lock.RLock()
mapLen := len(exec.capabilityResponses)
exec.lock.RUnlock()
assert.Equal(t, 0, mapLen, "capabilityResponses map leaked %d entries", mapLen)
}
// TestSemaphore_ConcurrentCallAndAwait exercises concurrent callers issuing
// callCapAsync from multiple goroutines while others await, simulating the
// real engine dispatching multiple workflow executions.
func TestSemaphore_ConcurrentCallAndAwait(t *testing.T) {
t.Parallel()
const max = 10
const workers = 20
const callsPerWorker = 50
stub := &slowCapStub{delay: 10 * time.Microsecond}
// Each worker gets its own execution (like real CRE — one per WASM invocation).
// We want to prove that WITHIN a single execution, concurrent isn't needed because
// WASM is single-threaded. But let's stress the shared semaphore anyway.
exec := newTestExec(max, stub)
ctx := t.Context()
var wg sync.WaitGroup
// Simulate sequential call-then-await pattern from a single WASM thread
// (the real case). We run it in parallel workers to stress-test the lock.
for w := 0; w < workers; w++ {
wg.Go(func() {
for i := 0; i < callsPerWorker; i++ {
id := int32(w*callsPerWorker + i)
err := exec.callCapAsync(ctx, &sdkpb.CapabilityRequest{CallbackId: id})
if err != nil {
return
}
_, err = exec.awaitCapabilities(ctx, &sdkpb.AwaitCapabilitiesRequest{Ids: []int32{id}})
if err != nil {
return
}
}
})
}
wg.Wait()
assert.LessOrEqual(t, int(stub.peakLoad.Load()), max)
assert.Equal(t, int32(workers*callsPerWorker), stub.callCount.Load())
assert.Eventually(t, func() bool {
avail, err := exec.pendingCallsLimiter.Available(context.Background())
return err == nil && avail == max
}, time.Second, 5*time.Millisecond,
"limiter still has occupied slots after all awaits completed")
}