From 709dc4b1de9e19ca39244c1b076822c01a452a2f Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 16 May 2019 10:37:37 +0200 Subject: [PATCH] Fix race in TestGoCollectorMemStats Related to #573 (but only partially fixes it). Signed-off-by: beorn7 --- prometheus/go_collector_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/prometheus/go_collector_test.go b/prometheus/go_collector_test.go index 5a89b25..f55aff9 100644 --- a/prometheus/go_collector_test.go +++ b/prometheus/go_collector_test.go @@ -196,9 +196,11 @@ func TestGoCollectorMemStats(t *testing.T) { } checkCollect(1) // Now msLast is set. + c.msMtx.Lock() if want, got := uint64(1), c.msLast.Alloc; want != got { t.Errorf("unexpected of msLast.Alloc, want %d, got %d", want, got) } + c.msMtx.Unlock() // Scenario 2: msRead responds fast, previous memstats available, new // value collected. @@ -207,9 +209,11 @@ func TestGoCollectorMemStats(t *testing.T) { } checkCollect(2) // msLast is set, too. + c.msMtx.Lock() if want, got := uint64(2), c.msLast.Alloc; want != got { t.Errorf("unexpected of msLast.Alloc, want %d, got %d", want, got) } + c.msMtx.Unlock() // Scenario 3: msRead responds slowly, previous memstats available, old // value collected. @@ -220,9 +224,11 @@ func TestGoCollectorMemStats(t *testing.T) { checkCollect(2) // After waiting, new value is still set in msLast. time.Sleep(12 * time.Millisecond) + c.msMtx.Lock() if want, got := uint64(3), c.msLast.Alloc; want != got { t.Errorf("unexpected of msLast.Alloc, want %d, got %d", want, got) } + c.msMtx.Unlock() // Scenario 4: msRead responds slowly, previous memstats is too old, new // value collected. @@ -231,7 +237,9 @@ func TestGoCollectorMemStats(t *testing.T) { ms.Alloc = 4 } checkCollect(4) + c.msMtx.Lock() if want, got := uint64(4), c.msLast.Alloc; want != got { t.Errorf("unexpected of msLast.Alloc, want %d, got %d", want, got) } + c.msMtx.Unlock() }