Jelajahi Sumber

Fixed #252 - fixed test to work with the actual registry metric...

eranharel 7 tahun lalu
induk
melakukan
2d03e0c978
2 mengubah file dengan 35 tambahan dan 22 penghapusan
  1. 12 5
      debug_test.go
  2. 23 17
      runtime_test.go

+ 12 - 5
debug_test.go

@@ -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)
 	}
 }

+ 23 - 17
runtime_test.go

@@ -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)
@@ -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)
-	}
-}