File tree Expand file tree Collapse file tree 2 files changed +30
-15
lines changed Expand file tree Collapse file tree 2 files changed +30
-15
lines changed Original file line number Diff line number Diff line change @@ -176,6 +176,9 @@ func retrieveAndUpdateCpuStat() {
176176 return
177177 }
178178
179+ // fix getProcessCpuStat return value (in percentage) breaks compatibility.
180+ cpuPercent = cpuPercent / 100.0
181+
179182 cpuRatioGauge .Set (cpuPercent )
180183
181184 currentCpuUsage .Store (cpuPercent )
Original file line number Diff line number Diff line change @@ -47,21 +47,8 @@ func TestCurrentCpuUsage(t *testing.T) {
4747 assert .True (t , util .Float64Equals (v , cpuUsage ))
4848}
4949
50- func Test_getProcessCpuStat (t * testing.T ) {
51- wg := & sync.WaitGroup {}
52- wg .Add (1 )
53- go func () {
54- i := 0
55- wg .Done ()
56- for i < 10000000000 {
57- i ++
58- if i == 1000000000 {
59- i = 0
60- }
61- }
62- }()
63- wg .Wait ()
64-
50+ func TestGetProcessCpuStat (t * testing.T ) {
51+ upraiseCpuRate ()
6552 got , err := getProcessCpuStat ()
6653 if err != nil {
6754 t .Error (err )
@@ -83,3 +70,28 @@ func Test_getProcessCpuStat(t *testing.T) {
8370 assert .True (t , int (got ) > 0 )
8471 time .Sleep (time .Millisecond * 200 )
8572}
73+
74+ func TestRetrieveAndUpdateCpuStatReturnValueRange (t * testing.T ) {
75+ // Initial cpu retrieval.
76+ retrieveAndUpdateCpuStat ()
77+ upraiseCpuRate ()
78+ time .Sleep (time .Millisecond * 200 )
79+ retrieveAndUpdateCpuStat ()
80+ assert .True (t , true , CurrentCpuUsage () < 1.0 )
81+ }
82+
83+ func upraiseCpuRate () {
84+ wg := & sync.WaitGroup {}
85+ wg .Add (1 )
86+ go func () {
87+ i := 0
88+ wg .Done ()
89+ for i < 10000000000 {
90+ i ++
91+ if i == 1000000000 {
92+ i = 0
93+ }
94+ }
95+ }()
96+ wg .Wait ()
97+ }
You can’t perform that action at this time.
0 commit comments