Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
adds two more unit tests which are testing:
Browse files Browse the repository at this point in the history
- 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?
  • Loading branch information
replay committed Jan 17, 2017
1 parent 19001f2 commit ed346b2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
57 changes: 57 additions & 0 deletions mdata/aggmetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions mdata/store_devnull.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mdata
import "github.com/raintank/metrictank/mdata/chunk"

type devnullStore struct {
AddCount uint32
}

func NewDevnullStore() *devnullStore {
Expand All @@ -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) {
Expand Down

0 comments on commit ed346b2

Please sign in to comment.