Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Updated cache test to use chrono for time warp
Browse files Browse the repository at this point in the history
  • Loading branch information
nqn committed Nov 16, 2015
1 parent 9dab28c commit 3c5e95c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
9 changes: 5 additions & 4 deletions control/plugin/client/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ func (c *cache) get(ns string, version int) interface{} {
cell *cachecell
ok bool
)

key := fmt.Sprintf("%v:%v", ns, version)
if cell, ok = c.table[key]; ok && time.Since(cell.time) < GlobalCacheExpiration {
if cell, ok = c.table[key]; ok && core.Chrono.Now().Sub(cell.time) < GlobalCacheExpiration {
cell.hits++
cacheLog.WithFields(log.Fields{
"namespace": key,
Expand Down Expand Up @@ -91,11 +92,11 @@ func (c *cache) put(ns string, version int, m interface{}) {
switch metric := m.(type) {
case core.Metric:
if _, ok := c.table[key]; ok {
c.table[key].time = time.Now()
c.table[key].time = core.Chrono.Now()
c.table[key].metric = metric
} else {
c.table[key] = &cachecell{
time: time.Now(),
time: core.Chrono.Now(),
metric: metric,
}
}
Expand All @@ -105,7 +106,7 @@ func (c *cache) put(ns string, version int, m interface{}) {
c.table[key].metrics = metric
} else {
c.table[key] = &cachecell{
time: time.Now(),
time: core.Chrono.Now(),
metrics: metric,
}
}
Expand Down
18 changes: 16 additions & 2 deletions control/plugin/client/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
. "github.com/smartystreets/goconvey/convey"

"github.com/intelsdi-x/pulse/control/plugin"
"github.com/intelsdi-x/pulse/core"
)

func TestCache(t *testing.T) {
Expand Down Expand Up @@ -76,26 +77,39 @@ func TestCache(t *testing.T) {
So(mc.table["/foo/bar:1"].hits, ShouldEqual, 1)
})
Convey("ticks miss count when a cache entry is still a hit", func() {
// Make sure global clock is restored after test.
defer core.Chrono.Reset()
defer core.Chrono.Continue()

// Use artificial time: pause to get base time.
core.Chrono.Pause()

mc := &cache{
table: make(map[string]*cachecell),
}
foo := &plugin.PluginMetricType{
Namespace_: []string{"foo", "bar"},
}

mc.put("/foo/bar", 1, foo)
time.Sleep(250 * time.Millisecond)
core.Chrono.Forward(250 * time.Millisecond)
mc.get("/foo/bar", 1)
So(mc.table["/foo/bar:1"].hits, ShouldEqual, 1)
})
Convey("ticks miss count when a cache entry is missed", func() {
defer core.Chrono.Reset()
defer core.Chrono.Continue()

core.Chrono.Pause()

mc := &cache{
table: make(map[string]*cachecell),
}
foo := &plugin.PluginMetricType{
Namespace_: []string{"foo", "bar"},
}
mc.put("/foo/bar", 1, foo)
time.Sleep(301 * time.Millisecond)
core.Chrono.Forward(301 * time.Millisecond)
mc.get("/foo/bar", 1)
So(mc.table["/foo/bar:1"].misses, ShouldEqual, 1)
})
Expand Down

0 comments on commit 3c5e95c

Please sign in to comment.