-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc_read_linux_test.go
98 lines (93 loc) · 2.3 KB
/
proc_read_linux_test.go
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
package procstats
import (
"os"
"strconv"
"testing"
"time"
)
func TestReadCPUUsage(t *testing.T) {
t.Parallel()
dur := time.Minute
var table = []struct {
Name string
Stat []byte
Err bool
Want CPUTime
}{
{
Name: "zeroes",
Stat: []byte("x x x x x x x x x x x x x 0 0 0 0 x"),
Err: false,
Want: CPUTime{},
},
{
Name: "err parse",
Stat: []byte("x x x x x x x x x x x x 0 0 0 0"),
Err: true,
Want: CPUTime{},
},
{
Name: "err fmt utime",
Stat: []byte("x x x x x x x x x x x x x x 0 0 0 x"),
Err: true,
Want: CPUTime{},
},
{
Name: "err fmt stime",
Stat: []byte("x x x x x x x x x x x x x 0 0 0 x x"),
Err: true,
Want: CPUTime{},
},
{
Name: "parse",
Stat: []byte("x x x x x x x x x x x x x "),
Err: false,
Want: CPUTime{2 * dur, 2 * dur},
},
}
// Need to create an additional test case that's specific to the system
// because we call out to sysconf to do the parsing.
thz := time.Second / time.Duration(sysClockTick())
x := &table[len(table)-1]
x.Stat = strconv.AppendInt(x.Stat, int64(dur/thz), 10)
x.Stat = append(x.Stat, ' ')
x.Stat = strconv.AppendInt(x.Stat, int64(dur/thz), 10)
x.Stat = append(x.Stat, ' ')
x.Stat = strconv.AppendInt(x.Stat, int64(dur/thz), 10)
x.Stat = append(x.Stat, ' ')
x.Stat = strconv.AppendInt(x.Stat, int64(dur/thz), 10)
x.Stat = append(x.Stat, []byte(" x")...)
for _, c := range table {
t.Run(c.Name, func(t *testing.T) {
t.Logf("%q", string(c.Stat))
ct, err := linuxParseCPUTime(c.Stat)
t.Logf("%+v", ct)
if c.Err {
if err == nil {
t.Fatalf("want: %v, got: %v", c.Err, err)
}
t.Logf("got error: %v", err)
return
}
if err != nil {
t.Fatalf("want: %v, got: %v", c.Err, err)
}
if want, got := c.Want, ct; want != got {
t.Fatalf("want: %v, got: %v", want, got)
}
})
}
// NOTE(hank) This test actually round-trips everything via looking at the parent process.
// Since this test is run in a separate binary, hopefully `go test` did enough work to
// spawn us for us to notice. If this test goes flaky, feel free to remove.
t.Run("parent", func(t *testing.T) {
ct, err := readProcessCPUTime(os.Getppid())
t.Logf("%+v", ct)
if err != nil {
t.Fatal(err)
}
if ct.eq(&CPUTime{}) {
t.Errorf("want: <non-zero>, got: %+v", ct)
}
})
}