Repository navigation
Expand file tree
/
Copy pathburst_test.go
More file actions
219 lines (200 loc) · 6.31 KB
/
Copy pathburst_test.go
File metadata and controls
219 lines (200 loc) · 6.31 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
package chat_test
import (
"context"
"log/slog"
"strings"
"sync"
"testing"
"time"
"github.com/coder/chat"
)
// newBurstRuntime builds a burst runtime with a short collection window and a
// fast lock poll cadence, optional observer, and optional option mutations.
func newBurstRuntime(t *testing.T, state chat.State, adapter chat.Adapter, logs *syncBuffer, observer chat.Observer, mutate ...func(*chat.RuntimeOptions)) *chat.Chat {
t.Helper()
options := chat.RuntimeOptions{
DedupeTTL: time.Hour,
Concurrency: chat.ConcurrencyBurst,
BurstWindow: 60 * time.Millisecond,
Dispatch: chat.DispatchDeferred,
MaxDetached: 1024,
// ThreadLockTTL/20 is the lock poll cadence, so a short TTL keeps
// batch lock acquisition prompt in tests.
ThreadLockTTL: 200 * time.Millisecond,
DetachTimeout: 5 * time.Second,
}
for _, m := range mutate {
m(&options)
}
opts := []chat.Option{
chat.WithState(state),
chat.WithAdapter(adapter),
chat.WithLogger(slog.New(slog.NewTextHandler(logs, &slog.HandlerOptions{Level: slog.LevelDebug}))),
chat.WithRuntimeOptions(options),
}
if observer != nil {
opts = append(opts, chat.WithObserver(observer))
}
bot, err := chat.New(context.Background(), opts...)
if err != nil {
t.Fatalf("new burst runtime: %v", err)
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := bot.Shutdown(ctx); err != nil {
t.Errorf("shutdown burst runtime: %v", err)
}
})
return bot
}
// burstRecorder tracks handled event IDs in handler completion order.
type burstRecorder struct {
mu sync.Mutex
handled []string
}
func (r *burstRecorder) handle(_ context.Context, ev *chat.MessageEvent) error {
r.mu.Lock()
defer r.mu.Unlock()
r.handled = append(r.handled, ev.Event.ID)
return nil
}
func (r *burstRecorder) snapshot() []string {
r.mu.Lock()
defer r.mu.Unlock()
return append([]string(nil), r.handled...)
}
func TestBurstDispatchesCollectedBatchInJoinOrder(t *testing.T) {
t.Parallel()
state := newFakeState()
adapter := newFakeAdapter("fake")
var logs syncBuffer
bot := newBurstRuntime(t, state, adapter, &logs, nil, func(o *chat.RuntimeOptions) {
o.BurstWindow = 250 * time.Millisecond
})
recorder := &burstRecorder{}
bot.OnNewMention(recorder.handle)
// Three events arrive well inside one collection window: they dispatch as
// one batch, in join order, under a single Thread Lock hold.
for _, id := range []string{"event-1", "event-2", "event-3"} {
if status := postEvent(t, bot, "fake", mentionEvent(id, "fake:v1:thread-1")); status != 200 {
t.Fatalf("dispatch %s: status %d", id, status)
}
}
eventually(t, 5*time.Second, func() bool {
return len(recorder.snapshot()) == 3
}, "batch members were not all handled")
if got := recorder.snapshot(); !equalStrings(got, []string{"event-1", "event-2", "event-3"}) {
t.Fatalf("batch ran out of join order: %v", got)
}
if !strings.Contains(logs.String(), "chat burst batch dispatch") || !strings.Contains(logs.String(), "size=3") {
t.Fatalf("expected one size-3 batch dispatch; logs:\n%s", logs.String())
}
}
func TestBurstWindowOpenedDuringDispatchRunsAfterCurrentBatch(t *testing.T) {
t.Parallel()
state := newFakeState()
adapter := newFakeAdapter("fake")
var logs syncBuffer
bot := newBurstRuntime(t, state, adapter, &logs, nil)
started := make(chan string, 8)
gate := make(chan struct{})
var mu sync.Mutex
var handled []string
bot.OnNewMention(func(ctx context.Context, ev *chat.MessageEvent) error {
started <- ev.Event.ID
if ev.Event.ID == "event-1" {
<-gate
}
mu.Lock()
handled = append(handled, ev.Event.ID)
mu.Unlock()
return nil
})
if status := postEvent(t, bot, "fake", mentionEvent("event-1", "fake:v1:thread-1")); status != 200 {
t.Fatalf("dispatch event-1: status %d", status)
}
select {
case <-started:
case <-time.After(5 * time.Second):
t.Fatal("first batch never started")
}
// The first batch is mid-dispatch (its member blocked): new arrivals open
// a successor window that dispatches strictly after the current batch.
if status := postEvent(t, bot, "fake", mentionEvent("event-2", "fake:v1:thread-1")); status != 200 {
t.Fatalf("dispatch event-2: status %d", status)
}
if status := postEvent(t, bot, "fake", mentionEvent("event-3", "fake:v1:thread-1")); status != 200 {
t.Fatalf("dispatch event-3: status %d", status)
}
close(gate)
eventually(t, 5*time.Second, func() bool {
mu.Lock()
defer mu.Unlock()
return len(handled) == 3
}, "successor window members were not handled")
mu.Lock()
got := append([]string(nil), handled...)
mu.Unlock()
if got[0] != "event-1" {
t.Fatalf("successor batch overtook the dispatching batch: %v", got)
}
if got[1] == "event-1" || got[2] == "event-1" {
t.Fatalf("first batch member ran twice: %v", got)
}
}
func TestBurstConstructionValidation(t *testing.T) {
t.Parallel()
base := func() chat.RuntimeOptions {
return chat.RuntimeOptions{
DedupeTTL: time.Hour,
ThreadLockTTL: time.Minute,
Concurrency: chat.ConcurrencyBurst,
BurstWindow: 50 * time.Millisecond,
Dispatch: chat.DispatchDeferred,
MaxDetached: 1024,
DetachTimeout: time.Second,
}
}
cases := []struct {
name string
mutate func(*chat.RuntimeOptions)
wantErr string
}{
{
name: "zero burst window",
mutate: func(o *chat.RuntimeOptions) { o.BurstWindow = 0 },
wantErr: "burst window must be positive",
},
{
name: "negative burst window",
mutate: func(o *chat.RuntimeOptions) { o.BurstWindow = -time.Second },
wantErr: "burst window must be positive",
},
{
name: "negative max burst batch",
mutate: func(o *chat.RuntimeOptions) { o.MaxBurstBatch = -1 },
wantErr: "max burst batch must not be negative",
},
{
name: "synchronous dispatch",
mutate: func(o *chat.RuntimeOptions) { o.Dispatch = chat.DispatchSync },
wantErr: "burst strategy requires deferred dispatch",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
options := base()
tc.mutate(&options)
_, err := chat.New(context.Background(),
chat.WithState(newFakeState()),
chat.WithAdapter(newFakeAdapter("fake")),
chat.WithRuntimeOptions(options),
)
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
t.Fatalf("expected %q construction error, got %v", tc.wantErr, err)
}
})
}
}