Skip to content

Commit

Permalink
Fixed rcrowley#252 - fixed test to work with the actual registry metr…
Browse files Browse the repository at this point in the history
…ic...
  • Loading branch information
eranharel committed Dec 12, 2018
1 parent 80818d2 commit 2d03e0c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
17 changes: 12 additions & 5 deletions debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,22 @@ func testDebugGCStatsBlocking(ch chan int) {
func TestDebugGCStatsDoubleRegister(t *testing.T) {
r := NewRegistry()
RegisterDebugGCStats(r)
zero := debugMetrics.GCStats.NumGC.Value() // Get a "zero" since GC may have run before these tests.
var storedGauge = (r.Get("debug.GCStats.LastGC")).(Gauge)

runtime.GC()
CaptureDebugGCStatsOnce(r)
if numGC := debugMetrics.GCStats.NumGC.Value(); 1 != numGC-zero {
t.Errorf("NumGC got %d, expected 1", numGC)

firstGC := storedGauge.Value()
if 0 == firstGC {
t.Errorf("firstGC got %d, expected > 0", firstGC)
}

time.Sleep(time.Millisecond)

RegisterDebugGCStats(r)
if numGC := debugMetrics.GCStats.NumGC.Value(); 1 != numGC-zero {
t.Errorf("NumGC got %d, expected 1", numGC-zero)
runtime.GC()
CaptureDebugGCStatsOnce(r)
if lastGC := storedGauge.Value(); firstGC == lastGC {
t.Errorf("lastGC got %d, expected a higher timestamp value", lastGC)
}
}
40 changes: 23 additions & 17 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ import (
"time"
)

func TestRuntimeMemStatsDoubleRegister(t *testing.T) {
r := NewRegistry()
RegisterRuntimeMemStats(r)
storedGauge := r.Get("runtime.MemStats.LastGC").(Gauge)

runtime.GC()
CaptureRuntimeMemStatsOnce(r)

firstGC := storedGauge.Value()
if 0 == firstGC {
t.Errorf("firstGC got %d, expected timestamp > 0", firstGC)
}

time.Sleep(time.Millisecond)

RegisterRuntimeMemStats(r)
runtime.GC()
CaptureRuntimeMemStatsOnce(r)
if lastGC := storedGauge.Value(); firstGC == lastGC {
t.Errorf("lastGC got %d, expected a higher timestamp value", lastGC)
}
}

func BenchmarkRuntimeMemStats(b *testing.B) {
r := NewRegistry()
RegisterRuntimeMemStats(r)
Expand Down Expand Up @@ -86,20 +109,3 @@ func testRuntimeMemStatsBlocking(ch chan int) {
}
}
}

func TestRuntimeMemStatsDoubleRegister(t *testing.T) {
r := NewRegistry()
RegisterRuntimeMemStats(r)
zero := runtimeMetrics.MemStats.NumGC.Value() // Get a "zero" since GC may have run before these tests.
runtime.GC()
CaptureRuntimeMemStatsOnce(r)

if count := runtimeMetrics.MemStats.NumGC.Value(); 1 != count-zero {
t.Errorf("NumGC got %d, expected 1", count-zero)
}

RegisterRuntimeMemStats(r)
if count := runtimeMetrics.MemStats.NumGC.Value(); 1 != count-zero {
t.Errorf("NumGC got %d, expected 1", count-zero)
}
}

0 comments on commit 2d03e0c

Please sign in to comment.