forked from cloudfoundry/gosigar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concrete_sigar_test.go
99 lines (76 loc) · 2.46 KB
/
concrete_sigar_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
99
package sigar_test
import (
"runtime"
"time"
. "github.com/scalingdata/ginkgo"
. "github.com/scalingdata/gomega"
sigar "github.com/scalingdata/gosigar"
)
var _ = Describe("ConcreteSigar", func() {
var concreteSigar *sigar.ConcreteSigar
BeforeEach(func() {
concreteSigar = &sigar.ConcreteSigar{}
})
Describe("CollectCpuStats", func() {
It("immediately makes first CPU usage available even though it's not very accurate", func() {
samplesCh, stop := concreteSigar.CollectCpuStats(500 * time.Millisecond)
firstValue := <-samplesCh
if runtime.GOOS == "darwin" {
Expect(firstValue.User).To(Equal(uint64(0)))
} else {
Expect(firstValue.User).To(BeNumerically(">", 0))
}
stop <- struct{}{}
})
It("makes CPU usage delta values available", func() {
samplesCh, stop := concreteSigar.CollectCpuStats(500 * time.Millisecond)
firstValue := <-samplesCh
secondValue := <-samplesCh
if runtime.GOOS == "darwin" {
Expect(firstValue.User).To(Equal(uint64(0)))
Expect(secondValue.User).To(Equal(uint64(0)))
} else {
Expect(secondValue.User).To(BeNumerically("<", firstValue.User))
}
stop <- struct{}{}
})
It("does not block", func() {
_, stop := concreteSigar.CollectCpuStats(10 * time.Millisecond)
// Sleep long enough for samplesCh to fill at least 2 values
time.Sleep(20 * time.Millisecond)
stop <- struct{}{}
// If CollectCpuStats blocks it will never get here
Expect(true).To(BeTrue())
})
})
It("GetLoadAverage", func() {
avg, err := concreteSigar.GetLoadAverage()
Expect(avg.One).ToNot(BeNil())
Expect(avg.Five).ToNot(BeNil())
Expect(avg.Fifteen).ToNot(BeNil())
if runtime.GOOS == "windows" {
Expect(err).To(Equal(sigar.ErrNotImplemented))
} else {
Expect(err).ToNot(HaveOccurred())
}
})
It("GetMem", func() {
mem, err := concreteSigar.GetMem()
Expect(err).ToNot(HaveOccurred())
Expect(mem.Total).To(BeNumerically(">", 0))
Expect(mem.Used + mem.Free).To(BeNumerically("<=", mem.Total))
})
It("GetSwap", func() {
swap, err := concreteSigar.GetSwap()
Expect(err).ToNot(HaveOccurred())
Expect(swap.Used + swap.Free).To(BeNumerically("<=", swap.Total))
})
It("GetSwap", func() {
fsusage, err := concreteSigar.GetFileSystemUsage("/")
Expect(err).ToNot(HaveOccurred())
Expect(fsusage.Total).ToNot(BeNil())
fsusage, err = concreteSigar.GetFileSystemUsage("T O T A L L Y B O G U S")
Expect(err).To(HaveOccurred())
Expect(fsusage.Total).To(Equal(uint64(0)))
})
})