-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathwait_buffer_test.go
More file actions
271 lines (215 loc) · 6.09 KB
/
wait_buffer_test.go
File metadata and controls
271 lines (215 loc) · 6.09 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
package testutil_test
import (
"context"
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/testutil"
)
func TestWaitBuffer_WaitFor_Blocks(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
wb := testutil.NewWaitBuffer()
done := make(chan struct{})
go func() {
defer close(done)
_ = wb.WaitFor(ctx, "hello")
}()
// Write the signal after the goroutine is blocking.
_, err := wb.Write([]byte("hello"))
require.NoError(t, err)
select {
case <-done:
case <-ctx.Done():
t.Fatal("WaitFor did not unblock after signal was written")
}
}
func TestWaitBuffer_WaitFor_AlreadyPresent(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
wb := testutil.NewWaitBuffer()
_, err := wb.Write([]byte("already here"))
require.NoError(t, err)
// Signal is already in the buffer; WaitFor returns immediately.
require.NoError(t, wb.WaitFor(ctx, "already"))
}
func TestWaitBuffer_WaitFor_ContextExpired(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
cancel() // Already expired.
wb := testutil.NewWaitBuffer()
err := wb.WaitFor(ctx, "never")
require.ErrorIs(t, err, context.Canceled)
}
func TestWaitBuffer_WaitFor_MultipleWrites(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
wb := testutil.NewWaitBuffer()
// Write partial content that doesn't satisfy the condition.
_, err := wb.Write([]byte("hell"))
require.NoError(t, err)
done := make(chan struct{})
go func() {
defer close(done)
_ = wb.WaitFor(ctx, "hello")
}()
// Complete the signal with a second write.
_, err = wb.Write([]byte("o"))
require.NoError(t, err)
select {
case <-done:
case <-ctx.Done():
t.Fatal("WaitFor did not unblock after multiple writes completed the signal")
}
}
func TestWaitBuffer_WaitForCond(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
wb := testutil.NewWaitBuffer()
done := make(chan struct{})
go func() {
defer close(done)
// Wait until the buffer has at least 10 bytes.
_ = wb.WaitForCond(ctx, func(s string) bool {
return len(s) >= 10
})
}()
_, err := wb.Write([]byte("12345"))
require.NoError(t, err)
_, err = wb.Write([]byte("67890"))
require.NoError(t, err)
select {
case <-done:
case <-ctx.Done():
t.Fatal("WaitForCond did not unblock when condition was met")
}
}
func TestWaitBuffer_ConcurrentWrites(t *testing.T) {
t.Parallel()
wb := testutil.NewWaitBuffer()
var wg sync.WaitGroup
const writers = 10
const iterations = 100
wg.Add(writers)
for i := range writers {
go func() {
defer wg.Done()
for j := range iterations {
_, _ = wb.Write([]byte(fmt.Sprintf("w%d-%d ", i, j)))
}
}()
}
wg.Wait()
// Every write should have landed; verify no data was lost by
// checking the length is at least as large as expected.
assert.GreaterOrEqual(t, len(wb.Bytes()), writers*iterations)
}
func TestWaitBuffer_WaitFor_BackgroundGoroutine(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
cancel() // Expire immediately.
wb := testutil.NewWaitBuffer()
// WaitFor from a background goroutine should return the
// context error rather than calling t.Fatal.
done := make(chan error, 1)
go func() {
done <- wb.WaitFor(ctx, "never")
}()
err := <-done
require.ErrorIs(t, err, context.Canceled)
}
func TestWaitBuffer_SequentialWaits(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
wb := testutil.NewWaitBuffer()
_, err := wb.Write([]byte("first "))
require.NoError(t, err)
require.NoError(t, wb.WaitFor(ctx, "first"))
_, err = wb.Write([]byte("second"))
require.NoError(t, err)
require.NoError(t, wb.WaitFor(ctx, "second"))
}
func TestWaitBuffer_WaitForNth_Blocks(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
wb := testutil.NewWaitBuffer()
_, err := wb.Write([]byte("Foo "))
require.NoError(t, err)
// First occurrence is already present, but we want two.
done := make(chan struct{})
go func() {
defer close(done)
_ = wb.WaitForNth(ctx, "Foo", 2)
}()
_, err = wb.Write([]byte("Bar Foo"))
require.NoError(t, err)
select {
case <-done:
case <-ctx.Done():
t.Fatal("WaitForNth did not unblock after second occurrence")
}
}
func TestWaitBuffer_WaitForNth_AlreadySatisfied(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
wb := testutil.NewWaitBuffer()
_, err := wb.Write([]byte("Foo Foo Foo"))
require.NoError(t, err)
// All three occurrences already present.
require.NoError(t, wb.WaitForNth(ctx, "Foo", 3))
}
func TestWaitBuffer_RequireWaitFor_Timeout(t *testing.T) {
t.Parallel()
// Use a mock testing.TB to capture the fatal call without
// killing the real test.
mock := &tbMock{}
ctx, cancel := context.WithCancel(context.Background())
cancel()
wb := testutil.NewWaitBuffer()
_, err := wb.Write([]byte("some output"))
require.NoError(t, err)
wb.RequireWaitFor(ctx, mock, "missing-signal")
assert.True(t, mock.failed(), "expected RequireWaitFor to fail the mock test")
}
// tbMock is a minimal testing.TB that records Fatalf calls.
type tbMock struct {
testing.TB // Embed to satisfy the interface.
mu sync.Mutex
fatalCalls int
}
func (*tbMock) Helper() {}
func (m *tbMock) Fatalf(string, ...any) {
m.mu.Lock()
defer m.mu.Unlock()
m.fatalCalls++
}
func (m *tbMock) failed() bool {
m.mu.Lock()
defer m.mu.Unlock()
return m.fatalCalls > 0
}
func TestWaitBuffer_Bytes_ReturnsCopy(t *testing.T) {
t.Parallel()
wb := testutil.NewWaitBuffer()
_, err := wb.Write([]byte("original"))
require.NoError(t, err)
b := wb.Bytes()
// Mutate the returned slice.
for i := range b {
b[i] = 'X'
}
// The internal buffer must be unchanged.
require.Equal(t, "original", wb.String())
}
func TestWaitBuffer_PlainBuffer(t *testing.T) {
t.Parallel()
wb := testutil.NewWaitBuffer()
_, err := wb.Write([]byte("hello "))
require.NoError(t, err)
_, err = wb.Write([]byte("world"))
require.NoError(t, err)
require.Equal(t, "hello world", wb.String())
require.Equal(t, []byte("hello world"), wb.Bytes())
}