2014-10-25 07:19:14 +02:00
|
|
|
package ccache
|
|
|
|
|
|
|
|
import (
|
2014-10-25 07:21:10 +02:00
|
|
|
"strconv"
|
2014-10-25 07:19:14 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
2018-07-16 18:20:17 +02:00
|
|
|
|
|
|
|
. "github.com/karlseguin/expect"
|
2014-10-25 07:19:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2019-01-26 06:33:50 +01:00
|
|
|
Expect(cache.ItemCount()).To.Equal(0)
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2019-01-26 06:33:50 +01:00
|
|
|
Expect(cache.ItemCount()).To.Equal(1)
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2019-01-26 06:33:50 +01:00
|
|
|
Expect(cache.ItemCount()).To.Equal(3)
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
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")
|
2019-01-26 06:33:50 +01:00
|
|
|
Expect(cache.ItemCount()).To.Equal(1)
|
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")
|
2019-01-26 06:33:50 +01:00
|
|
|
Expect(cache.ItemCount()).To.Equal(2)
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
2018-07-16 18:20:17 +02:00
|
|
|
func (_ *LayeredCacheTests) OnDeleteCallbackCalled() {
|
|
|
|
|
|
|
|
onDeleteFnCalled := false
|
|
|
|
onDeleteFn := func(item *Item) {
|
|
|
|
|
|
|
|
if item.group == "spice" && item.key == "flow" {
|
|
|
|
onDeleteFnCalled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cache := Layered(Configure().OnDelete(onDeleteFn))
|
|
|
|
cache.Set("spice", "flow", "value-a", time.Minute)
|
|
|
|
cache.Set("spice", "must", "value-b", time.Minute)
|
|
|
|
cache.Set("leto", "sister", "ghanima", time.Minute)
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 10) // Run once to init
|
|
|
|
cache.Delete("spice", "flow")
|
|
|
|
time.Sleep(time.Millisecond * 10) // Wait for worker to pick up deleted items
|
|
|
|
|
|
|
|
Expect(cache.Get("spice", "flow")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("spice", "must").Value()).To.Equal("value-b")
|
|
|
|
Expect(cache.Get("spice", "worm")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("leto", "sister").Value()).To.Equal("ghanima")
|
|
|
|
|
|
|
|
Expect(onDeleteFnCalled).To.Equal(true)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-21 08:59:04 +01:00
|
|
|
func (_ LayeredCacheTests) GCsTheOldestItems() {
|
2014-10-25 07:19:14 +02:00
|
|
|
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)
|
2017-02-10 19:26:41 +01:00
|
|
|
gcLayeredCache(cache)
|
2014-10-25 07:19:14 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-21 08:59:04 +01:00
|
|
|
func (_ LayeredCacheTests) PromotedItemsDontGetPruned() {
|
2014-10-25 07:19:14 +02:00
|
|
|
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)
|
2017-02-10 19:26:41 +01:00
|
|
|
gcLayeredCache(cache)
|
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
|
|
|
}
|
|
|
|
|
2014-11-21 08:59:04 +01:00
|
|
|
func (_ LayeredCacheTests) TrackerDoesNotCleanupHeldInstance() {
|
2014-10-25 07:19:14 +02:00
|
|
|
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)
|
2017-02-10 19:26:41 +01:00
|
|
|
gcLayeredCache(cache)
|
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()
|
2017-02-10 19:26:41 +01:00
|
|
|
gcLayeredCache(cache)
|
2014-10-25 07:19:14 +02:00
|
|
|
Expect(cache.Get("0", "a")).To.Equal(nil)
|
|
|
|
}
|
|
|
|
|
2014-11-21 08:59:04 +01:00
|
|
|
func (_ LayeredCacheTests) RemovesOldestItemWhenFull() {
|
2014-11-21 09:06:27 +01:00
|
|
|
cache := Layered(Configure().MaxSize(5).ItemsToPrune(1))
|
2014-10-25 07:19:14 +02:00
|
|
|
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)
|
2020-02-05 15:05:05 +01:00
|
|
|
Expect(cache.GetDropped()).To.Equal(4)
|
|
|
|
Expect(cache.GetDropped()).To.Equal(0)
|
2014-10-25 07:19:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 14:22:30 +02:00
|
|
|
func (_ LayeredCacheTests) ResizeOnTheFly() {
|
|
|
|
cache := Layered(Configure().MaxSize(9).ItemsToPrune(1))
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
|
|
|
|
}
|
|
|
|
cache.SetMaxSize(3)
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
Expect(cache.GetDropped()).To.Equal(2)
|
|
|
|
Expect(cache.Get("0", "a")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("1", "a")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("2", "a").Value()).To.Equal(2)
|
|
|
|
Expect(cache.Get("3", "a").Value()).To.Equal(3)
|
|
|
|
Expect(cache.Get("4", "a").Value()).To.Equal(4)
|
|
|
|
|
|
|
|
cache.Set("5", "a", 5, time.Minute)
|
|
|
|
time.Sleep(time.Millisecond * 5)
|
|
|
|
Expect(cache.GetDropped()).To.Equal(1)
|
|
|
|
Expect(cache.Get("2", "a")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("3", "a").Value()).To.Equal(3)
|
|
|
|
Expect(cache.Get("4", "a").Value()).To.Equal(4)
|
|
|
|
Expect(cache.Get("5", "a").Value()).To.Equal(5)
|
|
|
|
|
|
|
|
cache.SetMaxSize(10)
|
|
|
|
cache.Set("6", "a", 6, time.Minute)
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
Expect(cache.GetDropped()).To.Equal(0)
|
|
|
|
Expect(cache.Get("3", "a").Value()).To.Equal(3)
|
|
|
|
Expect(cache.Get("4", "a").Value()).To.Equal(4)
|
|
|
|
Expect(cache.Get("5", "a").Value()).To.Equal(5)
|
|
|
|
Expect(cache.Get("6", "a").Value()).To.Equal(6)
|
|
|
|
}
|
|
|
|
|
2014-10-25 07:19:14 +02:00
|
|
|
func newLayered() *LayeredCache {
|
|
|
|
return Layered(Configure())
|
|
|
|
}
|
2014-11-21 08:59:04 +01:00
|
|
|
|
|
|
|
func (_ LayeredCacheTests) RemovesOldestItemWhenFullBySizer() {
|
2014-11-21 09:06:27 +01:00
|
|
|
cache := Layered(Configure().MaxSize(9).ItemsToPrune(2))
|
2014-11-21 08:59:04 +01:00
|
|
|
for i := 0; i < 7; i++ {
|
|
|
|
cache.Set("pri", strconv.Itoa(i), &SizedItem{i, 2}, time.Minute)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
Expect(cache.Get("pri", "0")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("pri", "1")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("pri", "2")).To.Equal(nil)
|
2014-12-28 05:11:32 +01:00
|
|
|
Expect(cache.Get("pri", "3")).To.Equal(nil)
|
|
|
|
Expect(cache.Get("pri", "4").Value().(*SizedItem).id).To.Equal(4)
|
2014-11-21 08:59:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ LayeredCacheTests) SetUpdatesSizeOnDelta() {
|
|
|
|
cache := Layered(Configure())
|
|
|
|
cache.Set("pri", "a", &SizedItem{0, 2}, time.Minute)
|
|
|
|
cache.Set("pri", "b", &SizedItem{0, 3}, time.Minute)
|
2014-12-28 05:11:32 +01:00
|
|
|
time.Sleep(time.Millisecond * 5)
|
2017-02-10 19:26:41 +01:00
|
|
|
checkLayeredSize(cache, 5)
|
2014-11-21 08:59:04 +01:00
|
|
|
cache.Set("pri", "b", &SizedItem{0, 3}, time.Minute)
|
2014-12-28 05:11:32 +01:00
|
|
|
time.Sleep(time.Millisecond * 5)
|
2017-02-10 19:26:41 +01:00
|
|
|
checkLayeredSize(cache, 5)
|
2014-11-21 08:59:04 +01:00
|
|
|
cache.Set("pri", "b", &SizedItem{0, 4}, time.Minute)
|
2014-12-28 05:11:32 +01:00
|
|
|
time.Sleep(time.Millisecond * 5)
|
2017-02-10 19:26:41 +01:00
|
|
|
checkLayeredSize(cache, 6)
|
2014-11-21 08:59:04 +01:00
|
|
|
cache.Set("pri", "b", &SizedItem{0, 2}, time.Minute)
|
|
|
|
cache.Set("sec", "b", &SizedItem{0, 3}, time.Minute)
|
2014-12-28 05:11:32 +01:00
|
|
|
time.Sleep(time.Millisecond * 5)
|
2017-02-10 19:26:41 +01:00
|
|
|
checkLayeredSize(cache, 7)
|
2014-11-21 08:59:04 +01:00
|
|
|
cache.Delete("pri", "b")
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
2017-02-10 19:26:41 +01:00
|
|
|
checkLayeredSize(cache, 5)
|
2014-11-21 08:59:04 +01:00
|
|
|
}
|
2014-11-21 09:45:11 +01:00
|
|
|
|
|
|
|
func (_ LayeredCacheTests) ReplaceDoesNotchangeSizeIfNotSet() {
|
|
|
|
cache := Layered(Configure())
|
|
|
|
cache.Set("pri", "1", &SizedItem{1, 2}, time.Minute)
|
|
|
|
cache.Set("pri", "2", &SizedItem{1, 2}, time.Minute)
|
|
|
|
cache.Set("pri", "3", &SizedItem{1, 2}, time.Minute)
|
|
|
|
cache.Replace("sec", "3", &SizedItem{1, 2})
|
2014-12-28 05:11:32 +01:00
|
|
|
time.Sleep(time.Millisecond * 5)
|
2017-02-10 19:26:41 +01:00
|
|
|
checkLayeredSize(cache, 6)
|
2014-11-21 09:45:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (_ LayeredCacheTests) ReplaceChangesSize() {
|
|
|
|
cache := Layered(Configure())
|
|
|
|
cache.Set("pri", "1", &SizedItem{1, 2}, time.Minute)
|
|
|
|
cache.Set("pri", "2", &SizedItem{1, 2}, time.Minute)
|
|
|
|
|
|
|
|
cache.Replace("pri", "2", &SizedItem{1, 2})
|
2014-12-28 05:11:32 +01:00
|
|
|
time.Sleep(time.Millisecond * 5)
|
2017-02-10 19:26:41 +01:00
|
|
|
checkLayeredSize(cache, 4)
|
2014-11-21 09:45:11 +01:00
|
|
|
|
|
|
|
cache.Replace("pri", "2", &SizedItem{1, 1})
|
2014-12-28 05:11:32 +01:00
|
|
|
time.Sleep(time.Millisecond * 5)
|
2017-02-10 19:26:41 +01:00
|
|
|
checkLayeredSize(cache, 3)
|
2014-11-21 09:45:11 +01:00
|
|
|
|
|
|
|
cache.Replace("pri", "2", &SizedItem{1, 3})
|
2014-12-28 05:11:32 +01:00
|
|
|
time.Sleep(time.Millisecond * 5)
|
2017-02-10 19:26:41 +01:00
|
|
|
checkLayeredSize(cache, 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkLayeredSize(cache *LayeredCache, sz int64) {
|
|
|
|
cache.Stop()
|
|
|
|
Expect(cache.size).To.Equal(sz)
|
|
|
|
cache.restart()
|
|
|
|
}
|
|
|
|
|
|
|
|
func gcLayeredCache(cache *LayeredCache) {
|
|
|
|
cache.Stop()
|
|
|
|
cache.gc()
|
|
|
|
cache.restart()
|
2014-11-21 09:45:11 +01:00
|
|
|
}
|