Skip to content

Commit 2d03e0c

Browse files
committed
Fixed rcrowley#252 - fixed test to work with the actual registry metric...
1 parent 80818d2 commit 2d03e0c

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

debug_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,22 @@ func testDebugGCStatsBlocking(ch chan int) {
5050
func TestDebugGCStatsDoubleRegister(t *testing.T) {
5151
r := NewRegistry()
5252
RegisterDebugGCStats(r)
53-
zero := debugMetrics.GCStats.NumGC.Value() // Get a "zero" since GC may have run before these tests.
53+
var storedGauge = (r.Get("debug.GCStats.LastGC")).(Gauge)
54+
5455
runtime.GC()
5556
CaptureDebugGCStatsOnce(r)
56-
if numGC := debugMetrics.GCStats.NumGC.Value(); 1 != numGC-zero {
57-
t.Errorf("NumGC got %d, expected 1", numGC)
57+
58+
firstGC := storedGauge.Value()
59+
if 0 == firstGC {
60+
t.Errorf("firstGC got %d, expected > 0", firstGC)
5861
}
5962

63+
time.Sleep(time.Millisecond)
64+
6065
RegisterDebugGCStats(r)
61-
if numGC := debugMetrics.GCStats.NumGC.Value(); 1 != numGC-zero {
62-
t.Errorf("NumGC got %d, expected 1", numGC-zero)
66+
runtime.GC()
67+
CaptureDebugGCStatsOnce(r)
68+
if lastGC := storedGauge.Value(); firstGC == lastGC {
69+
t.Errorf("lastGC got %d, expected a higher timestamp value", lastGC)
6370
}
6471
}

runtime_test.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ import (
66
"time"
77
)
88

9+
func TestRuntimeMemStatsDoubleRegister(t *testing.T) {
10+
r := NewRegistry()
11+
RegisterRuntimeMemStats(r)
12+
storedGauge := r.Get("runtime.MemStats.LastGC").(Gauge)
13+
14+
runtime.GC()
15+
CaptureRuntimeMemStatsOnce(r)
16+
17+
firstGC := storedGauge.Value()
18+
if 0 == firstGC {
19+
t.Errorf("firstGC got %d, expected timestamp > 0", firstGC)
20+
}
21+
22+
time.Sleep(time.Millisecond)
23+
24+
RegisterRuntimeMemStats(r)
25+
runtime.GC()
26+
CaptureRuntimeMemStatsOnce(r)
27+
if lastGC := storedGauge.Value(); firstGC == lastGC {
28+
t.Errorf("lastGC got %d, expected a higher timestamp value", lastGC)
29+
}
30+
}
31+
932
func BenchmarkRuntimeMemStats(b *testing.B) {
1033
r := NewRegistry()
1134
RegisterRuntimeMemStats(r)
@@ -86,20 +109,3 @@ func testRuntimeMemStatsBlocking(ch chan int) {
86109
}
87110
}
88111
}
89-
90-
func TestRuntimeMemStatsDoubleRegister(t *testing.T) {
91-
r := NewRegistry()
92-
RegisterRuntimeMemStats(r)
93-
zero := runtimeMetrics.MemStats.NumGC.Value() // Get a "zero" since GC may have run before these tests.
94-
runtime.GC()
95-
CaptureRuntimeMemStatsOnce(r)
96-
97-
if count := runtimeMetrics.MemStats.NumGC.Value(); 1 != count-zero {
98-
t.Errorf("NumGC got %d, expected 1", count-zero)
99-
}
100-
101-
RegisterRuntimeMemStats(r)
102-
if count := runtimeMetrics.MemStats.NumGC.Value(); 1 != count-zero {
103-
t.Errorf("NumGC got %d, expected 1", count-zero)
104-
}
105-
}

0 commit comments

Comments
 (0)