-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrent_test.go
More file actions
286 lines (254 loc) · 7.65 KB
/
Copy pathconcurrent_test.go
File metadata and controls
286 lines (254 loc) · 7.65 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
package chat_test
import (
"context"
"log/slog"
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/coder/chat"
)
// lockCountingState wraps fakeState to prove the concurrent strategy never
// touches the Thread Lock.
type lockCountingState struct {
*fakeState
countMu sync.Mutex
acquires int
}
func (s *lockCountingState) AcquireLock(ctx context.Context, key string, ttl time.Duration) (chat.LockLease, bool, error) {
s.countMu.Lock()
s.acquires++
s.countMu.Unlock()
return s.fakeState.AcquireLock(ctx, key, ttl)
}
func (s *lockCountingState) acquireCalls() int {
s.countMu.Lock()
defer s.countMu.Unlock()
return s.acquires
}
func newConcurrentRuntime(t *testing.T, state chat.State, adapter chat.Adapter, logs *syncBuffer, mutate ...func(*chat.RuntimeOptions)) *chat.Chat {
t.Helper()
options := chat.RuntimeOptions{
DedupeTTL: time.Hour,
ThreadLockTTL: time.Hour,
Concurrency: chat.ConcurrencyConcurrent,
MaxConcurrent: 4,
Dispatch: chat.DispatchDeferred,
MaxDetached: 1024,
DetachTimeout: 5 * time.Second,
}
for _, m := range mutate {
m(&options)
}
bot, err := chat.New(context.Background(),
chat.WithState(state),
chat.WithAdapter(adapter),
chat.WithLogger(slog.New(slog.NewTextHandler(logs, &slog.HandlerOptions{Level: slog.LevelDebug}))),
chat.WithRuntimeOptions(options),
)
if err != nil {
t.Fatalf("new concurrent runtime: %v", err)
}
return bot
}
func TestConcurrentRunsSameThreadHandlersInParallelWithoutLock(t *testing.T) {
t.Parallel()
state := &lockCountingState{fakeState: newFakeState()}
adapter := newFakeAdapter("fake")
var logs syncBuffer
bot := newConcurrentRuntime(t, state, adapter, &logs)
// The first handler only finishes once the second has started: proof that
// two handlers for the SAME Thread run concurrently (no serialization).
secondStarted := make(chan struct{})
firstDone := make(chan struct{})
bot.OnNewMention(func(ctx context.Context, ev *chat.MessageEvent) error {
switch ev.Event.ID {
case "first":
select {
case <-secondStarted:
close(firstDone)
case <-ctx.Done():
}
case "second":
close(secondStarted)
}
return nil
})
for _, id := range []string{"first", "second"} {
if status := postEvent(t, bot, "fake", mentionEvent(id, "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("%s status = %d", id, status)
}
}
select {
case <-firstDone:
case <-time.After(2 * time.Second):
t.Fatal("handlers for the same thread did not run concurrently")
}
if calls := state.acquireCalls(); calls != 0 {
t.Fatalf("concurrent strategy acquired the thread lock %d times, want 0", calls)
}
if strings.Contains(logs.String(), "chat lock conflict dropped") {
t.Fatalf("concurrent strategy surfaced a lock conflict; logs:\n%s", logs.String())
}
}
func TestConcurrentBoundedByMaxConcurrent(t *testing.T) {
t.Parallel()
state := newFakeState()
adapter := newFakeAdapter("fake")
var logs syncBuffer
bot := newConcurrentRuntime(t, state, adapter, &logs, func(o *chat.RuntimeOptions) {
o.MaxConcurrent = 1
})
firstStarted := make(chan struct{})
releaseFirst := make(chan struct{})
var mu sync.Mutex
var handled []string
bot.OnNewMention(func(ctx context.Context, ev *chat.MessageEvent) error {
mu.Lock()
first := len(handled) == 0
handled = append(handled, ev.Event.ID)
mu.Unlock()
if first {
close(firstStarted)
<-releaseFirst
}
return nil
})
if status := postEvent(t, bot, "fake", mentionEvent("first", "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("first status = %d", status)
}
select {
case <-firstStarted:
case <-time.After(2 * time.Second):
t.Fatal("first handler did not start")
}
// The second event is acked promptly but must wait for the single slot.
if status := postEvent(t, bot, "fake", mentionEvent("second", "fake:v1:thread-2")); status != http.StatusOK {
t.Fatalf("second status = %d", status)
}
time.Sleep(80 * time.Millisecond)
mu.Lock()
if len(handled) != 1 {
got := append([]string(nil), handled...)
mu.Unlock()
t.Fatalf("MaxConcurrent=1 did not bound execution, handled = %v", got)
}
mu.Unlock()
close(releaseFirst)
deadline := time.After(2 * time.Second)
for {
mu.Lock()
n := len(handled)
mu.Unlock()
if n >= 2 {
break
}
select {
case <-deadline:
t.Fatal("second handler did not run after the slot freed")
case <-time.After(5 * time.Millisecond):
}
}
}
func TestConcurrentSlotWaiterDrainedByShutdown(t *testing.T) {
t.Parallel()
state := newFakeState()
adapter := newFakeAdapter("fake")
var logs syncBuffer
bot := newConcurrentRuntime(t, state, adapter, &logs, func(o *chat.RuntimeOptions) {
o.MaxConcurrent = 1
o.DetachTimeout = time.Hour
})
firstStarted := make(chan struct{})
bot.OnNewMention(func(ctx context.Context, ev *chat.MessageEvent) error {
if ev.Event.ID == "first" {
close(firstStarted)
// Occupy the only slot until the detached context is cancelled.
<-ctx.Done()
return ctx.Err()
}
return nil
})
if status := postEvent(t, bot, "fake", mentionEvent("first", "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("first status = %d", status)
}
select {
case <-firstStarted:
case <-time.After(2 * time.Second):
t.Fatal("first handler did not start")
}
if status := postEvent(t, bot, "fake", mentionEvent("waiter", "fake:v1:thread-2")); status != http.StatusOK {
t.Fatalf("waiter status = %d", status)
}
time.Sleep(40 * time.Millisecond)
done := make(chan error, 1)
go func() { done <- bot.Shutdown(context.Background()) }()
select {
case err := <-done:
if err != nil {
t.Fatalf("shutdown error: %v", err)
}
case <-time.After(2 * time.Second):
t.Fatal("shutdown did not drain the slot waiter")
}
if !strings.Contains(logs.String(), "chat concurrent slot wait abandoned") {
t.Fatalf("abandoned slot wait not surfaced via observation; logs:\n%s", logs.String())
}
}
func TestConcurrentSyncDispatchRunsWithoutLock(t *testing.T) {
t.Parallel()
state := &lockCountingState{fakeState: newFakeState()}
adapter := newFakeAdapter("fake")
var logs syncBuffer
bot := newConcurrentRuntime(t, state, adapter, &logs, func(o *chat.RuntimeOptions) {
o.Dispatch = chat.DispatchSync
o.DetachTimeout = 0
})
handled := make(chan string, 1)
bot.OnNewMention(func(ctx context.Context, ev *chat.MessageEvent) error {
handled <- ev.Event.ID
return nil
})
if status := postEvent(t, bot, "fake", mentionEvent("sync", "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("status = %d", status)
}
select {
case id := <-handled:
if id != "sync" {
t.Fatalf("handled %q, want sync", id)
}
default:
t.Fatal("sync dispatch returned before the handler ran")
}
if calls := state.acquireCalls(); calls != 0 {
t.Fatalf("concurrent strategy acquired the thread lock %d times, want 0", calls)
}
}
func TestConcurrentConstructionValidation(t *testing.T) {
t.Parallel()
newRuntime := func(mutate func(*chat.RuntimeOptions)) error {
options := chat.RuntimeOptions{
DedupeTTL: time.Hour,
ThreadLockTTL: time.Hour,
Concurrency: chat.ConcurrencyConcurrent,
MaxConcurrent: 2,
}
mutate(&options)
_, err := chat.New(context.Background(),
chat.WithState(newFakeState()),
chat.WithAdapter(newFakeAdapter("fake")),
chat.WithRuntimeOptions(options),
)
return err
}
if err := newRuntime(func(o *chat.RuntimeOptions) {}); err != nil {
t.Fatalf("valid concurrent options rejected: %v", err)
}
if err := newRuntime(func(o *chat.RuntimeOptions) { o.MaxConcurrent = 0 }); err == nil {
t.Fatal("expected concurrent without a max concurrent bound to fail")
}
if err := newRuntime(func(o *chat.RuntimeOptions) { o.MaxConcurrent = -1 }); err == nil {
t.Fatal("expected a negative max concurrent bound to fail")
}
}