Repository navigation
Expand file tree
/
Copy pathpause_test.go
More file actions
421 lines (360 loc) · 11.1 KB
/
Copy pathpause_test.go
File metadata and controls
421 lines (360 loc) · 11.1 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package domainfront
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPauseGate(t *testing.T) {
t.Run("resumed by default", func(t *testing.T) {
g := newPauseGate()
assert.False(t, g.isPaused())
assert.True(t, g.wait(context.Background()))
})
t.Run("wait blocks until resume", func(t *testing.T) {
g := newPauseGate()
g.pause()
require.True(t, g.isPaused())
released := make(chan bool, 1)
go func() { released <- g.wait(context.Background()) }()
select {
case <-released:
t.Fatal("wait returned while paused")
case <-time.After(50 * time.Millisecond):
}
g.resume()
select {
case ok := <-released:
assert.True(t, ok)
case <-time.After(time.Second):
t.Fatal("wait did not return after resume")
}
assert.False(t, g.isPaused())
})
t.Run("wait returns false when ctx ends", func(t *testing.T) {
g := newPauseGate()
g.pause()
ctx, cancel := context.WithCancel(context.Background())
cancel()
assert.False(t, g.wait(ctx))
})
t.Run("repeated calls are idempotent", func(t *testing.T) {
g := newPauseGate()
g.pause()
g.pause()
assert.True(t, g.isPaused())
g.resume()
assert.False(t, g.isPaused())
g.resume()
assert.False(t, g.isPaused())
assert.True(t, g.wait(context.Background()))
})
}
// blockingDialer holds every dial open until release is closed, so a test can
// pause a client with a crawl already in flight.
type blockingDialer struct {
dials atomic.Int32
entered chan struct{}
release chan struct{}
}
func newBlockingDialer() *blockingDialer {
return &blockingDialer{
entered: make(chan struct{}, 1),
release: make(chan struct{}),
}
}
func (d *blockingDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
d.dials.Add(1)
select {
case d.entered <- struct{}{}:
default:
}
select {
case <-d.release:
case <-ctx.Done():
}
return nil, errors.New("dial refused")
}
func manyFrontsConfig(n int) *Config {
masquerades := make([]*Masquerade, n)
for i := range masquerades {
masquerades[i] = &Masquerade{
Domain: "cdn.example.com",
IpAddress: fmt.Sprintf("192.0.2.%d:443", i+1),
}
}
return &Config{
Providers: map[string]*Provider{
"test": {
HostAliases: map[string]string{"origin.example.com": "cdn.example.com"},
TestURL: "https://cdn.example.com/ping",
Masquerades: masquerades,
},
},
}
}
func TestClient_PauseStopsCrawling(t *testing.T) {
dialer := newBlockingDialer()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, err := New(ctx, manyFrontsConfig(20),
WithDialer(dialer),
WithCrawlerConcurrency(1),
withCrawlInterval(100*time.Millisecond),
)
require.NoError(t, err)
defer client.Close()
// Pause with the first dial in flight, so the crawl is mid-pass rather than
// between passes: the candidates behind it must not be dialed.
select {
case <-dialer.entered:
case <-time.After(5 * time.Second):
t.Fatal("crawler never dialed")
}
client.Pause()
close(dialer.release)
require.Never(t, func() bool {
return dialer.dials.Load() > 1
}, 500*time.Millisecond, 20*time.Millisecond, "paused crawler kept dialing")
client.Resume()
require.Eventually(t, func() bool {
return dialer.dials.Load() > 1
}, 5*time.Second, 20*time.Millisecond, "crawler did not resume")
}
type countingCache struct {
saves atomic.Int32
}
func (c *countingCache) Load() ([]*CachedFront, error) { return nil, nil }
func (c *countingCache) Save([]*CachedFront) error {
c.saves.Add(1)
return nil
}
func TestClient_PauseStopsCacheSaver(t *testing.T) {
cache := &countingCache{}
dialer := newBlockingDialer()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, err := New(ctx, manyFrontsConfig(1),
WithDialer(dialer),
WithCache(cache),
WithCacheSaveInterval(10*time.Millisecond),
)
require.NoError(t, err)
defer client.Close()
client.notifyCacheDirty()
require.Eventually(t, func() bool {
return cache.saves.Load() > 0
}, 2*time.Second, 10*time.Millisecond, "cache saver never ran")
client.Pause()
// The saver may be past its gate on the tick already in flight, so let the
// count settle before sampling the value it must then hold at.
paused := waitForStableCount(t, cache.saves.Load)
client.notifyCacheDirty()
require.Never(t, func() bool {
return cache.saves.Load() > paused
}, 300*time.Millisecond, 10*time.Millisecond, "paused cache saver kept saving")
client.Resume()
client.notifyCacheDirty()
require.Eventually(t, func() bool {
return cache.saves.Load() > paused
}, 2*time.Second, 10*time.Millisecond, "cache saver did not resume")
}
func TestClient_CloseWhilePaused(t *testing.T) {
dialer := newBlockingDialer()
defer close(dialer.release)
client, err := New(context.Background(), manyFrontsConfig(5),
WithDialer(dialer),
WithCacheSaveInterval(10*time.Millisecond),
// Starts the config updater; refused immediately so no real fetch runs.
WithConfigURL("http://127.0.0.1:1/config.yaml.gz"),
)
require.NoError(t, err)
client.Pause()
done := make(chan struct{})
go func() {
client.Close()
close(done)
}()
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatal("Close() timed out while paused")
}
}
func TestClient_PausedClientStillRoundTrips(t *testing.T) {
caCert, caKey := newTestCA(t)
leafCert := newTestLeafCert(t, caCert, caKey, "cdn.example.com")
addr, cleanup := mockCDN(t, leafCert)
defer cleanup()
_, port, _ := net.SplitHostPort(addr)
config := &Config{
TrustedCAs: []*CA{{CommonName: "Test CA", Cert: string(pemEncodeCert(caCert))}},
Providers: map[string]*Provider{
"testprovider": {
HostAliases: map[string]string{"origin.example.com": "cdn.example.com"},
TestURL: fmt.Sprintf("https://cdn.example.com:%s/ping", port),
Masquerades: []*Masquerade{
{Domain: "cdn.example.com", IpAddress: "127.0.0.1:" + port},
},
},
},
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, err := New(ctx, config,
WithClientHelloID(utlsHelloGolang()),
WithMaxRetries(3),
WithCrawlerConcurrency(2),
)
require.NoError(t, err)
defer client.Close()
require.Eventually(t, func() bool {
return client.pool.readyCount() > 0
}, 10*time.Second, 100*time.Millisecond, "should find working fronts")
client.Pause()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://origin.example.com/hello", nil)
resp, err := client.RoundTripper().RoundTrip(req)
require.NoError(t, err)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
assert.Contains(t, string(body), "hello from CDN")
}
// waitForStableCount polls until count holds steady across consecutive samples,
// so a test can read a paused loop's final tally without assuming how long the
// cycle already in flight takes to land.
func waitForStableCount(t *testing.T, count func() int32) int32 {
t.Helper()
last := count()
steady := 0
for range 200 {
time.Sleep(10 * time.Millisecond)
got := count()
if got != last {
last, steady = got, 0
continue
}
if steady++; steady == 3 {
return got
}
}
t.Fatal("count never stabilized")
return 0
}
// seedReadyFront puts one known-good front into the ready queue and returns it,
// standing in for a front an earlier crawl had vetted.
func seedReadyFront(t *testing.T, client *Client) *front {
t.Helper()
candidates := client.pool.candidates()
require.NotEmpty(t, candidates)
f := candidates[0]
f.markSucceeded()
client.pool.addReady(f)
return f
}
// failingCountDialer fails every dial and counts them, so a test can wait for a
// crawl pass to quiesce before touching the pool it vets.
type failingCountDialer struct{ dials atomic.Int32 }
func (d *failingCountDialer) DialContext(context.Context, string, string) (net.Conn, error) {
d.dials.Add(1)
return nil, errors.New("dial refused")
}
func TestClient_PausedDialFailureKeepsFrontInRotation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dialer := &failingCountDialer{}
client, err := New(ctx, manyFrontsConfig(1),
WithDialer(dialer),
WithMaxRetries(3),
)
require.NoError(t, err)
defer client.Close()
client.Pause()
// Let the crawler's in-flight first pass settle before seeding, so its
// markFailed can't race the front we seed as succeeding.
waitForStableCount(t, dialer.dials.Load)
f := seedReadyFront(t, client)
reqCtx, reqCancel := context.WithTimeout(ctx, time.Second)
defer reqCancel()
req, _ := http.NewRequestWithContext(reqCtx, http.MethodGet, "https://origin.example.com/", nil)
_, err = client.RoundTripper().RoundTrip(req)
require.Error(t, err, "every dial fails, so the round trip cannot succeed")
assert.True(t, f.isSucceeding(), "a dial failure while paused must not drop the front")
assert.Equal(t, 1, client.pool.readyCount(), "the front should be back in the ready queue")
}
func TestClient_DialFailureDropsFrontWhenRunning(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dialer := &failingCountDialer{}
client, err := New(ctx, manyFrontsConfig(1),
WithDialer(dialer),
WithMaxRetries(3),
withCrawlInterval(time.Hour),
)
require.NoError(t, err)
defer client.Close()
// Let the crawler's first pass settle before seeding, so its markFailed
// can't be what drops the front instead of returnAfterDialFailure.
waitForStableCount(t, dialer.dials.Load)
f := seedReadyFront(t, client)
// Once the front is dropped, the second retry's Take blocks on an empty
// queue, so a short deadline ends the round trip without waiting it out.
reqCtx, reqCancel := context.WithTimeout(ctx, 200*time.Millisecond)
defer reqCancel()
req, _ := http.NewRequestWithContext(reqCtx, http.MethodGet, "https://origin.example.com/", nil)
_, err = client.RoundTripper().RoundTrip(req)
require.Error(t, err)
assert.False(t, f.isSucceeding(), "an unpaused dial failure should still drop the front")
}
// observedWaitContext lets a test order resume/pause before a waiter wakes.
// Done is evaluated after wait snapshots the gate channel.
type observedWaitContext struct {
context.Context
once sync.Once
entered chan struct{}
proceed chan struct{}
}
func (c *observedWaitContext) Done() <-chan struct{} {
c.once.Do(func() {
close(c.entered)
<-c.proceed
})
return c.Context.Done()
}
func TestPauseGate_ResumeThenPauseBeforeWaiterWakes(t *testing.T) {
g := newPauseGate()
g.pause()
base, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := &observedWaitContext{Context: base, entered: make(chan struct{}), proceed: make(chan struct{})}
released := make(chan bool, 1)
go func() { released <- g.wait(ctx) }()
<-ctx.entered
g.resume()
g.pause()
close(ctx.proceed)
select {
case <-released:
t.Fatal("wait returned through a previous resume while paused again")
case <-time.After(50 * time.Millisecond):
}
g.resume()
select {
case ok := <-released:
require.True(t, ok)
case <-time.After(time.Second):
t.Fatal("wait did not return after the final resume")
}
}
func TestPauseGate_CanceledWhileRunning(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
require.False(t, newPauseGate().wait(ctx))
}