Repository navigation
Expand file tree
/
Copy pathcontext_test.go
More file actions
215 lines (181 loc) · 6.81 KB
/
Copy pathcontext_test.go
File metadata and controls
215 lines (181 loc) · 6.81 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
package systats
import (
"context"
"errors"
"sync"
"testing"
"time"
)
// cpuFixtureStats points the CPU code at the fixture tree so these tests
// run anywhere, not just on Linux. Reading the same stat fixture for both
// samples yields a zero delta, which is fine - what's under test here is
// the timing and cancellation behavior, not the arithmetic.
func cpuFixtureStats() *SyStats {
return &SyStats{
StatFilePath: "./test_files/stat.txt",
CPUinfoFilePath: "./test_files/cpuinfo.txt",
LoadAvgPath: "./test_files/loadavg.txt",
}
}
func TestSleepCtxCompletes(t *testing.T) {
if err := sleepCtx(context.Background(), 10*time.Millisecond); err != nil {
t.Errorf("sleepCtx() on an uncancelled context = %v, want nil", err)
}
}
func TestSleepCtxReturnsImmediatelyWhenCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
start := time.Now()
err := sleepCtx(ctx, 5*time.Second)
elapsed := time.Since(start)
if !errors.Is(err, context.Canceled) {
t.Errorf("sleepCtx() = %v, want context.Canceled", err)
}
// The point of the helper: a cancelled caller does not wait out the
// full duration. A plain time.Sleep would have taken 5s here.
if elapsed > time.Second {
t.Errorf("sleepCtx() took %v on a cancelled context, want well under the 5s duration", elapsed)
}
}
func TestSleepCtxHonorsDeadline(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
if err := sleepCtx(ctx, 5*time.Second); !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("sleepCtx() = %v, want context.DeadlineExceeded", err)
}
}
func TestCPUSampleWindowDefaults(t *testing.T) {
// A hand-constructed SyStats must not end up sampling over no time at
// all - a zero window would make every CPU percentage meaningless.
var zero SyStats
if got := zero.cpuSampleWindow(); got != defaultCPUSampleWindow {
t.Errorf("zero-value SyStats.cpuSampleWindow() = %v, want %v", got, defaultCPUSampleWindow)
}
fromNew := New()
if got := fromNew.cpuSampleWindow(); got != defaultCPUSampleWindow {
t.Errorf("New().cpuSampleWindow() = %v, want %v", got, defaultCPUSampleWindow)
}
negative := SyStats{CPUSampleWindow: -1 * time.Second}
if got := negative.cpuSampleWindow(); got != defaultCPUSampleWindow {
t.Errorf("negative CPUSampleWindow = %v, want the default %v", got, defaultCPUSampleWindow)
}
custom := SyStats{CPUSampleWindow: 50 * time.Millisecond}
if got := custom.cpuSampleWindow(); got != 50*time.Millisecond {
t.Errorf("custom CPUSampleWindow = %v, want 50ms", got)
}
}
func TestGetCPUWithContextAbortsOnCancel(t *testing.T) {
syStats := cpuFixtureStats()
ctx, cancel := context.WithCancel(context.Background())
cancel()
start := time.Now()
_, err := syStats.GetCPUWithContext(ctx)
elapsed := time.Since(start)
if !errors.Is(err, context.Canceled) {
t.Fatalf("GetCPUWithContext() = %v, want context.Canceled", err)
}
if elapsed >= defaultCPUSampleWindow {
t.Errorf("GetCPUWithContext() took %v on a cancelled context, want less than the %v sample window", elapsed, defaultCPUSampleWindow)
}
}
func TestGetCPUHonorsSampleWindow(t *testing.T) {
syStats := cpuFixtureStats()
syStats.CPUSampleWindow = 20 * time.Millisecond
start := time.Now()
if _, err := syStats.GetCPU(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
elapsed := time.Since(start)
if elapsed < 20*time.Millisecond {
t.Errorf("GetCPU() returned in %v, faster than the %v window it was told to sample over", elapsed, syStats.CPUSampleWindow)
}
// The whole point of the knob: materially faster than the default.
if elapsed >= defaultCPUSampleWindow {
t.Errorf("GetCPU() took %v with a 20ms window, want well under the %v default", elapsed, defaultCPUSampleWindow)
}
}
func TestGetProcessWithContextAbortsOnCancel(t *testing.T) {
syStats := procFixtureStats()
// Average mode has no sampling window, so force the instant path that
// actually sleeps.
syStats.ProcessCPUMode = CPUUsageInstant
ctx, cancel := context.WithCancel(context.Background())
cancel()
start := time.Now()
_, err := syStats.GetProcessWithContext(ctx, 1234)
elapsed := time.Since(start)
if !errors.Is(err, context.Canceled) {
t.Fatalf("GetProcessWithContext() = %v, want context.Canceled", err)
}
if elapsed >= defaultCPUSampleWindow {
t.Errorf("GetProcessWithContext() took %v on a cancelled context, want less than the %v sample window", elapsed, defaultCPUSampleWindow)
}
}
func TestGetTopProcessesWithContextAbortsOnCancel(t *testing.T) {
syStats := procFixtureStats()
syStats.ProcessCPUMode = CPUUsageInstant
ctx, cancel := context.WithCancel(context.Background())
cancel()
start := time.Now()
_, err := syStats.GetTopProcessesWithContext(ctx, 5, SortByCPU)
elapsed := time.Since(start)
if !errors.Is(err, context.Canceled) {
t.Fatalf("GetTopProcessesWithContext() = %v, want context.Canceled", err)
}
if elapsed >= defaultCPUSampleWindow {
t.Errorf("GetTopProcessesWithContext() took %v on a cancelled context, want less than the %v sample window", elapsed, defaultCPUSampleWindow)
}
}
// The average-mode collector has no sleep to interrupt, so its
// cancellation check lives in the per-pid loop instead.
func TestGetTopProcessesAverageModeAbortsOnCancel(t *testing.T) {
syStats := procFixtureStats() // already CPUUsageAverage
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := syStats.GetTopProcessesWithContext(ctx, 5, SortByCPU); !errors.Is(err, context.Canceled) {
t.Errorf("GetTopProcessesWithContext() = %v, want context.Canceled", err)
}
}
// The no-context methods must keep working exactly as before - that is
// what makes this change additive.
func TestLegacyMethodsStillWork(t *testing.T) {
syStats := procFixtureStats()
if _, err := syStats.GetTopProcesses(5, SortByCPU); err != nil {
t.Errorf("GetTopProcesses() = %v, want nil", err)
}
if _, err := syStats.GetProcess(1234); err != nil {
t.Errorf("GetProcess() = %v, want nil", err)
}
if _, err := cpuFixtureStats().GetCPU(); err != nil {
t.Errorf("GetCPU() = %v, want nil", err)
}
}
// TestConcurrentUse backs the thread-safety claim in the package docs with
// the race detector. Run as part of `go test -race`.
func TestConcurrentUse(t *testing.T) {
syStats := procFixtureStats()
cpu := cpuFixtureStats()
cpu.CPUSampleWindow = time.Millisecond
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 5; j++ {
if _, err := syStats.GetTopProcesses(3, SortByCPU); err != nil {
t.Errorf("GetTopProcesses: %v", err)
}
if _, err := syStats.GetProcess(1234); err != nil {
t.Errorf("GetProcess: %v", err)
}
if _, err := syStats.GetMemory(Megabyte); err != nil {
t.Errorf("GetMemory: %v", err)
}
if _, err := cpu.GetCPU(); err != nil {
t.Errorf("GetCPU: %v", err)
}
}
}()
}
wg.Wait()
}