-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_test.go
More file actions
217 lines (178 loc) · 5.19 KB
/
metrics_test.go
File metadata and controls
217 lines (178 loc) · 5.19 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
package workers
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// mockMetrics records all metric calls for assertions.
type mockMetrics struct {
mu sync.Mutex
started []string
stopped []string
panicked []string
failed []string
restarted []string
durations map[string]time.Duration
activeCount int
}
func newMockMetrics() *mockMetrics {
return &mockMetrics{durations: make(map[string]time.Duration)}
}
func (m *mockMetrics) WorkerStarted(name string) {
m.mu.Lock()
defer m.mu.Unlock()
m.started = append(m.started, name)
}
func (m *mockMetrics) WorkerStopped(name string) {
m.mu.Lock()
defer m.mu.Unlock()
m.stopped = append(m.stopped, name)
}
func (m *mockMetrics) WorkerPanicked(name string) {
m.mu.Lock()
defer m.mu.Unlock()
m.panicked = append(m.panicked, name)
}
func (m *mockMetrics) WorkerFailed(name string, _ error) {
m.mu.Lock()
defer m.mu.Unlock()
m.failed = append(m.failed, name)
}
func (m *mockMetrics) WorkerRestarted(name string, _ int) {
m.mu.Lock()
defer m.mu.Unlock()
m.restarted = append(m.restarted, name)
}
func (m *mockMetrics) ObserveRunDuration(name string, d time.Duration) {
m.mu.Lock()
defer m.mu.Unlock()
m.durations[name] = d
}
func (m *mockMetrics) SetActiveWorkers(count int) {
m.mu.Lock()
defer m.mu.Unlock()
m.activeCount = count
}
func TestMetrics_StartedStopped(t *testing.T) {
m := newMockMetrics()
w := NewWorker("test-worker", func(ctx WorkerContext) error {
<-ctx.Done()
return ctx.Err()
})
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
Run(ctx, []*Worker{w}, WithMetrics(m))
m.mu.Lock()
defer m.mu.Unlock()
assert.Contains(t, m.started, "test-worker")
assert.Contains(t, m.stopped, "test-worker")
assert.NotEmpty(t, m.durations["test-worker"])
}
func TestMetrics_Failed(t *testing.T) {
m := newMockMetrics()
w := NewWorker("failer", func(ctx WorkerContext) error {
return errors.New("boom")
})
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
Run(ctx, []*Worker{w}, WithMetrics(m))
m.mu.Lock()
defer m.mu.Unlock()
assert.Contains(t, m.failed, "failer")
}
func TestMetrics_Restarted(t *testing.T) {
m := newMockMetrics()
var attempts atomic.Int32
w := NewWorker("restarter", func(ctx WorkerContext) error {
if attempts.Add(1) <= 2 {
return errors.New("fail")
}
<-ctx.Done()
return ctx.Err()
}).WithRestart(true)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
Run(ctx, []*Worker{w}, WithMetrics(m))
m.mu.Lock()
defer m.mu.Unlock()
assert.NotEmpty(t, m.restarted, "should record restarts")
}
func TestMetrics_Inherited(t *testing.T) {
m := newMockMetrics()
manager := NewWorker("manager", func(ctx WorkerContext) error {
ctx.Add(NewWorker("child", func(ctx WorkerContext) error {
<-ctx.Done()
return ctx.Err()
}))
<-ctx.Done()
return ctx.Err()
})
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
Run(ctx, []*Worker{manager}, WithMetrics(m))
m.mu.Lock()
defer m.mu.Unlock()
assert.Contains(t, m.started, "manager")
assert.Contains(t, m.started, "child", "child should inherit parent metrics")
}
func TestMetrics_ChildOverride(t *testing.T) {
parentM := newMockMetrics()
childM := newMockMetrics()
manager := NewWorker("manager", func(ctx WorkerContext) error {
ctx.Add(NewWorker("child", func(ctx WorkerContext) error {
<-ctx.Done()
return ctx.Err()
}).WithMetrics(childM))
<-ctx.Done()
return ctx.Err()
})
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
Run(ctx, []*Worker{manager}, WithMetrics(parentM))
parentM.mu.Lock()
assert.Contains(t, parentM.started, "manager")
assert.NotContains(t, parentM.started, "child", "child should NOT use parent metrics")
parentM.mu.Unlock()
childM.mu.Lock()
assert.Contains(t, childM.started, "child", "child should use its own metrics")
childM.mu.Unlock()
}
func TestMetrics_NoopDefault(t *testing.T) {
// Run without WithMetrics — should not panic.
w := NewWorker("noop-test", func(ctx WorkerContext) error {
<-ctx.Done()
return ctx.Err()
})
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
Run(ctx, []*Worker{w}) // no WithMetrics — uses NoopMetrics
}
func TestNewPrometheusMetrics_CachesSameNamespace(t *testing.T) {
m1 := NewPrometheusMetrics("test_cache")
m2 := NewPrometheusMetrics("test_cache")
assert.Same(t, m1, m2, "same namespace should return same instance")
}
func TestNewPrometheusMetrics_DifferentNamespace(t *testing.T) {
m1 := NewPrometheusMetrics("ns_a")
m2 := NewPrometheusMetrics("ns_b")
assert.NotSame(t, m1, m2, "different namespaces should return different instances")
}
func TestNewPrometheusMetrics_ConcurrentSafe(t *testing.T) {
var wg sync.WaitGroup
results := make([]Metrics, 100)
for i := range results {
wg.Add(1)
go func(i int) {
defer wg.Done()
results[i] = NewPrometheusMetrics("concurrent_test")
}(i)
}
wg.Wait()
for _, m := range results {
assert.Same(t, results[0], m, "all concurrent calls should return same instance")
}
}