-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevents_test.go
More file actions
132 lines (119 loc) · 3.79 KB
/
Copy pathevents_test.go
File metadata and controls
132 lines (119 loc) · 3.79 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
package waxtap
import (
"fmt"
"sync"
"testing"
"time"
"github.com/colespringer/waxtap/v3/internal/httpx"
)
func TestEmitThrottleDedup(t *testing.T) {
var mu sync.Mutex
var throttled, retried int
em := newEmitter(func(ev Event) {
if ev.Stage != StageWarning || ev.Warning == nil {
return
}
mu.Lock()
switch ev.Warning.Code {
case WarnThrottled:
throttled++
case WarnRateLimitedRetried:
retried++
}
mu.Unlock()
}, "dummyVideo0")
// Parallel reports of the same response produce one warning.
var wg sync.WaitGroup
for range 12 {
wg.Go(func() {
emitThrottle(em, httpx.ThrottleEvent{
Host: "googlevideo.com", StatusCode: 429, Penalty: time.Second, Phase: httpx.ThrottleDetected,
})
})
}
wg.Wait()
// The retry phase produces a separate warning.
emitThrottle(em, httpx.ThrottleEvent{Host: "googlevideo.com", StatusCode: 429, Phase: httpx.ThrottleRetryStarted})
if throttled != 1 {
t.Errorf("WarnThrottled emitted %d times, want 1 (deduped)", throttled)
}
if retried != 1 {
t.Errorf("WarnRateLimitedRetried emitted %d times, want 1", retried)
}
if len(em.warnings) != 2 {
t.Errorf("recorded warnings = %d, want 2", len(em.warnings))
}
}
func TestEmitterConcurrentProgressAndThrottle(t *testing.T) {
// Run with -race to verify concurrent progress and warning delivery.
em := newEmitter(func(Event) {}, "dummyVideo0")
var wg sync.WaitGroup
for i := range 50 {
wg.Go(func() { em.progress(int64(i), 100) })
wg.Go(func() {
emitThrottle(em, httpx.ThrottleEvent{Host: "h", StatusCode: 429, Phase: httpx.ThrottleDetected})
})
}
wg.Wait()
if len(em.warnings) != 1 {
t.Errorf("recorded warnings = %d, want 1 (deduped under concurrency)", len(em.warnings))
}
}
func TestEmitterCallbackPanicRecovered(t *testing.T) {
em := newEmitter(func(Event) { panic("boom") }, "dummyVideo0")
em.warn(WarnProceedUncut, "x")
emitThrottle(em, httpx.ThrottleEvent{Host: "h", StatusCode: 429, Phase: httpx.ThrottleDetected})
if len(em.warnings) != 2 {
t.Errorf("recorded warnings = %d, want 2", len(em.warnings))
}
}
// TestEmitterWarnNthNumbersPerJob covers F8: repeated warnings of the same code
// number themselves, job-wide, so a run that rotated twice does not read as one
// duplicated entry. The ordinal must survive a whole-chain retry, whose second
// attempt carries its own per-attempt counters.
func TestEmitterWarnNthNumbersPerJob(t *testing.T) {
em := newEmitter(nil, "dummyVideo0")
rotate := func() {
em.warnNth(WarnSessionRotated, func(n int) string { return fmt.Sprintf("rotated (rotation %d)", n) })
}
rotate()
em.warn(WarnFallbackProfile, "unrelated") // an interleaved code must not shift the count
rotate()
em.warnNth(WarnURLReResolved, func(n int) string { return fmt.Sprintf("re-resolved (refresh %d)", n) })
got := em.collected()
want := []string{"rotated (rotation 1)", "unrelated", "rotated (rotation 2)", "re-resolved (refresh 1)"}
if len(got) != len(want) {
t.Fatalf("warnings = %v, want %d entries", got, len(want))
}
for i, w := range got {
if w.Detail != want[i] {
t.Errorf("warning %d detail = %q, want %q", i, w.Detail, want[i])
}
}
}
// TestEmitterWarnNthConcurrent pins the ordinals as unique under concurrency:
// the count and the append are one critical section, so no two callers draw the
// same number.
func TestEmitterWarnNthConcurrent(t *testing.T) {
em := newEmitter(nil, "dummyVideo0")
const n = 50
var wg sync.WaitGroup
wg.Add(n)
for range n {
go func() {
defer wg.Done()
em.warnNth(WarnSessionRotated, func(i int) string { return fmt.Sprintf("%d", i) })
}()
}
wg.Wait()
seen := map[string]bool{}
for _, w := range em.collected() {
if seen[w.Detail] {
t.Fatalf("ordinal %q was drawn twice", w.Detail)
}
seen[w.Detail] = true
}
if len(seen) != n {
t.Errorf("got %d distinct ordinals, want %d", len(seen), n)
}
}