2014-10-25 07:19:14 +02:00
|
|
|
package ccache
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/karlseguin/expect"
|
2014-10-25 07:21:10 +02:00
|
|
|
"strconv"
|
2014-10-25 07:19:14 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LayeredCacheTests struct{}
|
|
|
|
|
|
|
|
func Test_LayeredCache(t *testing.T) {
|
|
|
|
Expectify(new(LayeredCacheTests), t)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *LayeredCacheTests) GetsANonExistantValue() {
|
2014-10-25 07:19:14 +02:00
|
|
|
cache := newLayered()
|
|
|
|
Expect(cache.Get("spice", "flow")).To.Equal(nil)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *LayeredCacheTests) SetANewValue() {
|
2014-10-25 07:19:14 +02:00
|
|
|
cache := newLayered()
|
|
|
|
cache.Set("spice", "flow", "a value", time.Minute)
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("spice", "flow").Value()).To.Equal("a value")
|
2014-10-25 07:19:14 +02:00
|
|
|
Expect(cache.Get("spice", "stop")).To.Equal(nil)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *LayeredCacheTests) SetsMultipleValueWithinTheSameLayer() {
|
2014-10-25 07:19:14 +02:00
|
|
|
cache := newLayered()
|
|
|
|
cache.Set("spice", "flow", "value-a", time.Minute)
|
|
|
|
cache.Set("spice", "must", "value-b", time.Minute)
|
|
|
|
cache.Set("leto", "sister", "ghanima", time.Minute)
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("spice", "flow").Value()).To.Equal("value-a")
|
|
|
|
Expect(cache.Get("spice", "must").Value()).To.Equal("value-b")
|
2014-10-25 07:19:14 +02:00
|
|
|
Expect(cache.Get("spice", "worm")).To.Equal(nil)
|
|
|
|
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("leto", "sister").Value()).To.Equal("ghanima")
|
2014-10-25 07:19:14 +02:00
|
|
|
Expect(cache.Get("leto", "brother")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("baron", "friend")).To.Equal(nil)
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *LayeredCacheTests) ReplaceDoesNothingIfKeyDoesNotExist() {
|
2014-11-13 16:23:52 +01:00
|
|
|
cache := newLayered()
|
|
|
|
Expect(cache.Replace("spice", "flow", "value-a")).To.Equal(false)
|
2014-11-13 16:26:05 +01:00
|
|
|
Expect(cache.Get("spice", "flow")).To.Equal(nil)
|
2014-11-13 16:23:52 +01:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *LayeredCacheTests) ReplaceUpdatesTheValue() {
|
2014-11-13 16:23:52 +01:00
|
|
|
cache := newLayered()
|
|
|
|
cache.Set("spice", "flow", "value-a", time.Minute)
|
|
|
|
Expect(cache.Replace("spice", "flow", "value-b")).To.Equal(true)
|
|
|
|
Expect(cache.Get("spice", "flow").Value().(string)).To.Equal("value-b")
|
2014-11-13 16:26:05 +01:00
|
|
|
//not sure how to test that the TTL hasn't changed sort of a sleep..
|
2014-11-13 16:23:52 +01:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *LayeredCacheTests) DeletesAValue() {
|
2014-10-25 07:19:14 +02:00
|
|
|
cache := newLayered()
|
|
|
|
cache.Set("spice", "flow", "value-a", time.Minute)
|
|
|
|
cache.Set("spice", "must", "value-b", time.Minute)
|
|
|
|
cache.Set("leto", "sister", "ghanima", time.Minute)
|
|
|
|
cache.Delete("spice", "flow")
|
|
|
|
Expect(cache.Get("spice", "flow")).To.Equal(nil)
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("spice", "must").Value()).To.Equal("value-b")
|
2014-10-25 07:19:14 +02:00
|
|
|
Expect(cache.Get("spice", "worm")).To.Equal(nil)
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("leto", "sister").Value()).To.Equal("ghanima")
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-14 01:41:22 +01:00
|
|
|
func (_ *LayeredCacheTests) DeletesALayer() {
|
2014-10-25 07:19:14 +02:00
|
|
|
cache := newLayered()
|
|
|
|
cache.Set("spice", "flow", "value-a", time.Minute)
|
|
|
|
cache.Set("spice", "must", "value-b", time.Minute)
|
|
|
|
cache.Set("leto", "sister", "ghanima", time.Minute)
|
|
|
|
cache.DeleteAll("spice")
|
|
|
|
Expect(cache.Get("spice", "flow")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("spice", "must")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("spice", "worm")).To.Equal(nil)
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("leto", "sister").Value()).To.Equal("ghanima")
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LayeredCacheTests) GCsTheOldestItems() {
|
|
|
|
cache := Layered(Configure().ItemsToPrune(10))
|
|
|
|
cache.Set("xx", "a", 23, time.Minute)
|
|
|
|
for i := 0; i < 500; i++ {
|
|
|
|
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
|
|
|
|
}
|
|
|
|
cache.Set("xx", "b", 9001, time.Minute)
|
|
|
|
//let the items get promoted (and added to our list)
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
cache.gc()
|
|
|
|
Expect(cache.Get("xx", "a")).To.Equal(nil)
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("xx", "b").Value()).To.Equal(9001)
|
2014-10-25 07:19:14 +02:00
|
|
|
Expect(cache.Get("8", "a")).To.Equal(nil)
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("9", "a").Value()).To.Equal(9)
|
|
|
|
Expect(cache.Get("10", "a").Value()).To.Equal(10)
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LayeredCacheTests) PromotedItemsDontGetPruned() {
|
|
|
|
cache := Layered(Configure().ItemsToPrune(10).GetsPerPromote(1))
|
|
|
|
for i := 0; i < 500; i++ {
|
|
|
|
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 10) //run the worker once to init the list
|
|
|
|
cache.Get("9", "a")
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
cache.gc()
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("9", "a").Value()).To.Equal(9)
|
2014-10-25 07:19:14 +02:00
|
|
|
Expect(cache.Get("10", "a")).To.Equal(nil)
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("11", "a").Value()).To.Equal(11)
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LayeredCacheTests) TrackerDoesNotCleanupHeldInstance() {
|
|
|
|
cache := Layered(Configure().ItemsToPrune(10).Track())
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
|
|
|
|
}
|
|
|
|
item := cache.TrackingGet("0", "a")
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
cache.gc()
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("0", "a").Value()).To.Equal(0)
|
2014-10-25 07:19:14 +02:00
|
|
|
Expect(cache.Get("1", "a")).To.Equal(nil)
|
|
|
|
item.Release()
|
|
|
|
cache.gc()
|
|
|
|
Expect(cache.Get("0", "a")).To.Equal(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LayeredCacheTests) RemovesOldestItemWhenFull() {
|
|
|
|
cache := Layered(Configure().MaxItems(5).ItemsToPrune(1))
|
|
|
|
cache.Set("xx", "a", 23, time.Minute)
|
|
|
|
for i := 0; i < 7; i++ {
|
|
|
|
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
|
|
|
|
}
|
|
|
|
cache.Set("xx", "b", 9001, time.Minute)
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
Expect(cache.Get("xx", "a")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("0", "a")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("1", "a")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("2", "a")).To.Equal(nil)
|
2014-10-25 12:15:47 +02:00
|
|
|
Expect(cache.Get("3", "a").Value()).To.Equal(3)
|
|
|
|
Expect(cache.Get("xx", "b").Value()).To.Equal(9001)
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLayered() *LayeredCache {
|
|
|
|
return Layered(Configure())
|
|
|
|
}
|