From ed346b29a38368dac0a663dec415082235185c62 Mon Sep 17 00:00:00 2001 From: Mauro Stettler Date: Tue, 10 Jan 2017 07:32:31 -0800 Subject: [PATCH] adds two more unit tests which are testing: - does aggmetric call the cache push callback every time a metric gets evicted from the ring buffer? - does aggmetric add the chunk into the store if the node is primary? - makes the devnullStore count how many times it's Add() got called - does aggmetric not add the chunk into the store if the node is not primary? --- mdata/aggmetric_test.go | 57 +++++++++++++++++++++++++++++++++++++++++ mdata/store_devnull.go | 6 +++++ 2 files changed, 63 insertions(+) diff --git a/mdata/aggmetric_test.go b/mdata/aggmetric_test.go index 427fc26862..6e53045e82 100644 --- a/mdata/aggmetric_test.go +++ b/mdata/aggmetric_test.go @@ -70,6 +70,63 @@ func (c *Checker) Verify(primary bool, from, to, first, last uint32) { cluster.Manager.SetPrimary(currentClusterStatus) } +func TestMetricPersistBeingPrimary(t *testing.T) { + testMetricPersistOptionalPrimary(t, true) +} + +func TestMetricPersistBeingSecondary(t *testing.T) { + testMetricPersistOptionalPrimary(t, false) +} + +func testMetricPersistOptionalPrimary(t *testing.T, primary bool) { + // always reset the counter when entering and leaving the test + dnstore.Reset() + defer dnstore.Reset() + + cluster.Init("default", "test", time.Now(), "http", 6060) + cluster.Manager.SetPrimary(primary) + + callCount := uint32(0) + calledCb := make(chan bool) + + mockCache := cache.MockCache{} + mockCache.CacheIfHotCb = func() { calledCb <- true } + + numChunks, chunkAddCount, chunkSpan := uint32(5), uint32(10), uint32(300) + agg := NewAggMetric(dnstore, &mockCache, "foo", chunkSpan, numChunks, 1, []AggSetting{}...) + + ts := uint32(1000) + for i := uint32(0); i < chunkAddCount; i++ { + agg.Add(ts, 1) + ts += chunkSpan + } + + timeout := time.After(1 * time.Second) + + for i := uint32(0); i < chunkAddCount-1; i++ { + select { + case <-timeout: + t.Fatalf("timed out waiting for a callback call") + case <-calledCb: + callCount = callCount + 1 + } + } + + if callCount < chunkAddCount-1 { + t.Fatalf("there should have been %d chunk pushes, but go %d", chunkAddCount-1, callCount) + } + + if primary { + if dnstore.AddCount != chunkAddCount-1 { + t.Fatalf("there should have been %d chunk adds on store, but go %d", chunkAddCount-1, dnstore.AddCount) + } + } else { + if dnstore.AddCount != 0 { + t.Fatalf("there should have been %d chunk adds on store, but go %d", 0, dnstore.AddCount) + } + } +} + func TestAggMetric(t *testing.T) { cluster.Init("default", "test", time.Now(), "http", 6060) diff --git a/mdata/store_devnull.go b/mdata/store_devnull.go index b591167c79..ceeb5931fd 100644 --- a/mdata/store_devnull.go +++ b/mdata/store_devnull.go @@ -3,6 +3,7 @@ package mdata import "github.com/raintank/metrictank/mdata/chunk" type devnullStore struct { + AddCount uint32 } func NewDevnullStore() *devnullStore { @@ -11,6 +12,11 @@ func NewDevnullStore() *devnullStore { } func (c *devnullStore) Add(cwr *ChunkWriteRequest) { + c.AddCount++ +} + +func (c *devnullStore) Reset() { + c.AddCount = 0 } func (c *devnullStore) Search(key string, start, end uint32) ([]chunk.IterGen, error) {