remove dependency on expect library
This commit is contained in:
@@ -7,75 +7,69 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/karlseguin/expect"
|
||||
"github.com/karlseguin/ccache/v3/assert"
|
||||
)
|
||||
|
||||
type LayeredCacheTests struct{}
|
||||
|
||||
func Test_LayeredCache(t *testing.T) {
|
||||
Expectify(new(LayeredCacheTests), t)
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) GetsANonExistantValue() {
|
||||
func Test_LayedCache_GetsANonExistantValue(t *testing.T) {
|
||||
cache := newLayered[string]()
|
||||
Expect(cache.Get("spice", "flow")).To.Equal(nil)
|
||||
Expect(cache.ItemCount()).To.Equal(0)
|
||||
assert.Equal(t, cache.Get("spice", "flow"), nil)
|
||||
assert.Equal(t, cache.ItemCount(), 0)
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) SetANewValue() {
|
||||
func Test_LayedCache_SetANewValue(t *testing.T) {
|
||||
cache := newLayered[string]()
|
||||
cache.Set("spice", "flow", "a value", time.Minute)
|
||||
Expect(cache.Get("spice", "flow").Value()).To.Equal("a value")
|
||||
Expect(cache.Get("spice", "stop")).To.Equal(nil)
|
||||
Expect(cache.ItemCount()).To.Equal(1)
|
||||
assert.Equal(t, cache.Get("spice", "flow").Value(), "a value")
|
||||
assert.Equal(t, cache.Get("spice", "stop"), nil)
|
||||
assert.Equal(t, cache.ItemCount(), 1)
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) SetsMultipleValueWithinTheSameLayer() {
|
||||
func Test_LayedCache_SetsMultipleValueWithinTheSameLayer(t *testing.T) {
|
||||
cache := newLayered[string]()
|
||||
cache.Set("spice", "flow", "value-a", time.Minute)
|
||||
cache.Set("spice", "must", "value-b", time.Minute)
|
||||
cache.Set("leto", "sister", "ghanima", time.Minute)
|
||||
Expect(cache.Get("spice", "flow").Value()).To.Equal("value-a")
|
||||
Expect(cache.Get("spice", "must").Value()).To.Equal("value-b")
|
||||
Expect(cache.Get("spice", "worm")).To.Equal(nil)
|
||||
assert.Equal(t, cache.Get("spice", "flow").Value(), "value-a")
|
||||
assert.Equal(t, cache.Get("spice", "must").Value(), "value-b")
|
||||
assert.Equal(t, cache.Get("spice", "worm"), nil)
|
||||
|
||||
Expect(cache.Get("leto", "sister").Value()).To.Equal("ghanima")
|
||||
Expect(cache.Get("leto", "brother")).To.Equal(nil)
|
||||
Expect(cache.Get("baron", "friend")).To.Equal(nil)
|
||||
Expect(cache.ItemCount()).To.Equal(3)
|
||||
assert.Equal(t, cache.Get("leto", "sister").Value(), "ghanima")
|
||||
assert.Equal(t, cache.Get("leto", "brother"), nil)
|
||||
assert.Equal(t, cache.Get("baron", "friend"), nil)
|
||||
assert.Equal(t, cache.ItemCount(), 3)
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) ReplaceDoesNothingIfKeyDoesNotExist() {
|
||||
func Test_LayedCache_ReplaceDoesNothingIfKeyDoesNotExist(t *testing.T) {
|
||||
cache := newLayered[string]()
|
||||
Expect(cache.Replace("spice", "flow", "value-a")).To.Equal(false)
|
||||
Expect(cache.Get("spice", "flow")).To.Equal(nil)
|
||||
assert.Equal(t, cache.Replace("spice", "flow", "value-a"), false)
|
||||
assert.Equal(t, cache.Get("spice", "flow"), nil)
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) ReplaceUpdatesTheValue() {
|
||||
func Test_LayedCache_ReplaceUpdatesTheValue(t *testing.T) {
|
||||
cache := newLayered[string]()
|
||||
cache.Set("spice", "flow", "value-a", time.Minute)
|
||||
Expect(cache.Replace("spice", "flow", "value-b")).To.Equal(true)
|
||||
Expect(cache.Get("spice", "flow").Value()).To.Equal("value-b")
|
||||
Expect(cache.ItemCount()).To.Equal(1)
|
||||
assert.Equal(t, cache.Replace("spice", "flow", "value-b"), true)
|
||||
assert.Equal(t, cache.Get("spice", "flow").Value(), "value-b")
|
||||
assert.Equal(t, cache.ItemCount(), 1)
|
||||
//not sure how to test that the TTL hasn't changed sort of a sleep..
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) DeletesAValue() {
|
||||
func Test_LayedCache_DeletesAValue(t *testing.T) {
|
||||
cache := newLayered[string]()
|
||||
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)
|
||||
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(cache.ItemCount()).To.Equal(2)
|
||||
assert.Equal(t, cache.Get("spice", "flow"), nil)
|
||||
assert.Equal(t, cache.Get("spice", "must").Value(), "value-b")
|
||||
assert.Equal(t, cache.Get("spice", "worm"), nil)
|
||||
assert.Equal(t, cache.Get("leto", "sister").Value(), "ghanima")
|
||||
assert.Equal(t, cache.ItemCount(), 2)
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) DeletesAPrefix() {
|
||||
func Test_LayedCache_DeletesAPrefix(t *testing.T) {
|
||||
cache := newLayered[string]()
|
||||
Expect(cache.ItemCount()).To.Equal(0)
|
||||
assert.Equal(t, cache.ItemCount(), 0)
|
||||
|
||||
cache.Set("spice", "aaa", "1", time.Minute)
|
||||
cache.Set("spice", "aab", "2", time.Minute)
|
||||
@@ -83,23 +77,23 @@ func (_ *LayeredCacheTests) DeletesAPrefix() {
|
||||
cache.Set("leto", "aac", "3", time.Minute)
|
||||
cache.Set("spice", "ac", "4", time.Minute)
|
||||
cache.Set("spice", "z5", "7", time.Minute)
|
||||
Expect(cache.ItemCount()).To.Equal(6)
|
||||
assert.Equal(t, cache.ItemCount(), 6)
|
||||
|
||||
Expect(cache.DeletePrefix("spice", "9a")).To.Equal(0)
|
||||
Expect(cache.ItemCount()).To.Equal(6)
|
||||
assert.Equal(t, cache.DeletePrefix("spice", "9a"), 0)
|
||||
assert.Equal(t, cache.ItemCount(), 6)
|
||||
|
||||
Expect(cache.DeletePrefix("spice", "aa")).To.Equal(3)
|
||||
Expect(cache.Get("spice", "aaa")).To.Equal(nil)
|
||||
Expect(cache.Get("spice", "aab")).To.Equal(nil)
|
||||
Expect(cache.Get("spice", "aac")).To.Equal(nil)
|
||||
Expect(cache.Get("spice", "ac").Value()).To.Equal("4")
|
||||
Expect(cache.Get("spice", "z5").Value()).To.Equal("7")
|
||||
Expect(cache.ItemCount()).To.Equal(3)
|
||||
assert.Equal(t, cache.DeletePrefix("spice", "aa"), 3)
|
||||
assert.Equal(t, cache.Get("spice", "aaa"), nil)
|
||||
assert.Equal(t, cache.Get("spice", "aab"), nil)
|
||||
assert.Equal(t, cache.Get("spice", "aac"), nil)
|
||||
assert.Equal(t, cache.Get("spice", "ac").Value(), "4")
|
||||
assert.Equal(t, cache.Get("spice", "z5").Value(), "7")
|
||||
assert.Equal(t, cache.ItemCount(), 3)
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) DeletesAFunc() {
|
||||
func Test_LayedCache_DeletesAFunc(t *testing.T) {
|
||||
cache := newLayered[int]()
|
||||
Expect(cache.ItemCount()).To.Equal(0)
|
||||
assert.Equal(t, cache.ItemCount(), 0)
|
||||
|
||||
cache.Set("spice", "a", 1, time.Minute)
|
||||
cache.Set("leto", "b", 2, time.Minute)
|
||||
@@ -107,26 +101,26 @@ func (_ *LayeredCacheTests) DeletesAFunc() {
|
||||
cache.Set("spice", "d", 4, time.Minute)
|
||||
cache.Set("spice", "e", 5, time.Minute)
|
||||
cache.Set("spice", "f", 6, time.Minute)
|
||||
Expect(cache.ItemCount()).To.Equal(6)
|
||||
assert.Equal(t, cache.ItemCount(), 6)
|
||||
|
||||
Expect(cache.DeleteFunc("spice", func(key string, item *Item[int]) bool {
|
||||
assert.Equal(t, cache.DeleteFunc("spice", func(key string, item *Item[int]) bool {
|
||||
return false
|
||||
})).To.Equal(0)
|
||||
Expect(cache.ItemCount()).To.Equal(6)
|
||||
}), 0)
|
||||
assert.Equal(t, cache.ItemCount(), 6)
|
||||
|
||||
Expect(cache.DeleteFunc("spice", func(key string, item *Item[int]) bool {
|
||||
assert.Equal(t, cache.DeleteFunc("spice", func(key string, item *Item[int]) bool {
|
||||
return item.Value() < 4
|
||||
})).To.Equal(2)
|
||||
Expect(cache.ItemCount()).To.Equal(4)
|
||||
}), 2)
|
||||
assert.Equal(t, cache.ItemCount(), 4)
|
||||
|
||||
Expect(cache.DeleteFunc("spice", func(key string, item *Item[int]) bool {
|
||||
assert.Equal(t, cache.DeleteFunc("spice", func(key string, item *Item[int]) bool {
|
||||
return key == "d"
|
||||
})).To.Equal(1)
|
||||
Expect(cache.ItemCount()).To.Equal(3)
|
||||
}), 1)
|
||||
assert.Equal(t, cache.ItemCount(), 3)
|
||||
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) OnDeleteCallbackCalled() {
|
||||
func Test_LayedCache_OnDeleteCallbackCalled(t *testing.T) {
|
||||
onDeleteFnCalled := int32(0)
|
||||
onDeleteFn := func(item *Item[string]) {
|
||||
if item.group == "spice" && item.key == "flow" {
|
||||
@@ -143,27 +137,27 @@ func (_ *LayeredCacheTests) OnDeleteCallbackCalled() {
|
||||
cache.Delete("spice", "flow")
|
||||
cache.SyncUpdates()
|
||||
|
||||
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")
|
||||
assert.Equal(t, cache.Get("spice", "flow"), nil)
|
||||
assert.Equal(t, cache.Get("spice", "must").Value(), "value-b")
|
||||
assert.Equal(t, cache.Get("spice", "worm"), nil)
|
||||
assert.Equal(t, cache.Get("leto", "sister").Value(), "ghanima")
|
||||
|
||||
Expect(atomic.LoadInt32(&onDeleteFnCalled)).To.Eql(1)
|
||||
assert.Equal(t, atomic.LoadInt32(&onDeleteFnCalled), 1)
|
||||
}
|
||||
|
||||
func (_ *LayeredCacheTests) DeletesALayer() {
|
||||
func Test_LayedCache_DeletesALayer(t *testing.T) {
|
||||
cache := newLayered[string]()
|
||||
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)
|
||||
Expect(cache.Get("leto", "sister").Value()).To.Equal("ghanima")
|
||||
assert.Equal(t, cache.Get("spice", "flow"), nil)
|
||||
assert.Equal(t, cache.Get("spice", "must"), nil)
|
||||
assert.Equal(t, cache.Get("spice", "worm"), nil)
|
||||
assert.Equal(t, cache.Get("leto", "sister").Value(), "ghanima")
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) GCsTheOldestItems() {
|
||||
func Test_LayeredCache_GCsTheOldestItems(t *testing.T) {
|
||||
cache := Layered[int](Configure[int]().ItemsToPrune(10))
|
||||
cache.Set("xx", "a", 23, time.Minute)
|
||||
for i := 0; i < 500; i++ {
|
||||
@@ -173,14 +167,14 @@ func (_ LayeredCacheTests) GCsTheOldestItems() {
|
||||
//let the items get promoted (and added to our list)
|
||||
cache.SyncUpdates()
|
||||
cache.GC()
|
||||
Expect(cache.Get("xx", "a")).To.Equal(nil)
|
||||
Expect(cache.Get("xx", "b").Value()).To.Equal(9001)
|
||||
Expect(cache.Get("8", "a")).To.Equal(nil)
|
||||
Expect(cache.Get("9", "a").Value()).To.Equal(9)
|
||||
Expect(cache.Get("10", "a").Value()).To.Equal(10)
|
||||
assert.Equal(t, cache.Get("xx", "a"), nil)
|
||||
assert.Equal(t, cache.Get("xx", "b").Value(), 9001)
|
||||
assert.Equal(t, cache.Get("8", "a"), nil)
|
||||
assert.Equal(t, cache.Get("9", "a").Value(), 9)
|
||||
assert.Equal(t, cache.Get("10", "a").Value(), 10)
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) PromotedItemsDontGetPruned() {
|
||||
func Test_LayeredCache_PromotedItemsDontGetPruned(t *testing.T) {
|
||||
cache := Layered[int](Configure[int]().ItemsToPrune(10).GetsPerPromote(1))
|
||||
for i := 0; i < 500; i++ {
|
||||
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
|
||||
@@ -189,12 +183,12 @@ func (_ LayeredCacheTests) PromotedItemsDontGetPruned() {
|
||||
cache.Get("9", "a")
|
||||
cache.SyncUpdates()
|
||||
cache.GC()
|
||||
Expect(cache.Get("9", "a").Value()).To.Equal(9)
|
||||
Expect(cache.Get("10", "a")).To.Equal(nil)
|
||||
Expect(cache.Get("11", "a").Value()).To.Equal(11)
|
||||
assert.Equal(t, cache.Get("9", "a").Value(), 9)
|
||||
assert.Equal(t, cache.Get("10", "a"), nil)
|
||||
assert.Equal(t, cache.Get("11", "a").Value(), 11)
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) TrackerDoesNotCleanupHeldInstance() {
|
||||
func Test_LayeredCache_TrackerDoesNotCleanupHeldInstance(t *testing.T) {
|
||||
cache := Layered[int](Configure[int]().ItemsToPrune(10).Track())
|
||||
item0 := cache.TrackingSet("0", "a", 0, time.Minute)
|
||||
for i := 1; i < 11; i++ {
|
||||
@@ -203,16 +197,16 @@ func (_ LayeredCacheTests) TrackerDoesNotCleanupHeldInstance() {
|
||||
item1 := cache.TrackingGet("1", "a")
|
||||
cache.SyncUpdates()
|
||||
cache.GC()
|
||||
Expect(cache.Get("0", "a").Value()).To.Equal(0)
|
||||
Expect(cache.Get("1", "a").Value()).To.Equal(1)
|
||||
assert.Equal(t, cache.Get("0", "a").Value(), 0)
|
||||
assert.Equal(t, cache.Get("1", "a").Value(), 1)
|
||||
item0.Release()
|
||||
item1.Release()
|
||||
cache.GC()
|
||||
Expect(cache.Get("0", "a")).To.Equal(nil)
|
||||
Expect(cache.Get("1", "a")).To.Equal(nil)
|
||||
assert.Equal(t, cache.Get("0", "a"), nil)
|
||||
assert.Equal(t, cache.Get("1", "a"), nil)
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) RemovesOldestItemWhenFull() {
|
||||
func Test_LayeredCache_RemovesOldestItemWhenFull(t *testing.T) {
|
||||
cache := Layered[int](Configure[int]().MaxSize(5).ItemsToPrune(1))
|
||||
cache.Set("xx", "a", 23, time.Minute)
|
||||
for i := 0; i < 7; i++ {
|
||||
@@ -220,17 +214,17 @@ func (_ LayeredCacheTests) RemovesOldestItemWhenFull() {
|
||||
}
|
||||
cache.Set("xx", "b", 9001, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
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)
|
||||
Expect(cache.Get("3", "a").Value()).To.Equal(3)
|
||||
Expect(cache.Get("xx", "b").Value()).To.Equal(9001)
|
||||
Expect(cache.GetDropped()).To.Equal(4)
|
||||
Expect(cache.GetDropped()).To.Equal(0)
|
||||
assert.Equal(t, cache.Get("xx", "a"), nil)
|
||||
assert.Equal(t, cache.Get("0", "a"), nil)
|
||||
assert.Equal(t, cache.Get("1", "a"), nil)
|
||||
assert.Equal(t, cache.Get("2", "a"), nil)
|
||||
assert.Equal(t, cache.Get("3", "a").Value(), 3)
|
||||
assert.Equal(t, cache.Get("xx", "b").Value(), 9001)
|
||||
assert.Equal(t, cache.GetDropped(), 4)
|
||||
assert.Equal(t, cache.GetDropped(), 0)
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) ResizeOnTheFly() {
|
||||
func Test_LayeredCache_ResizeOnTheFly(t *testing.T) {
|
||||
cache := Layered[int](Configure[int]().MaxSize(9).ItemsToPrune(1))
|
||||
for i := 0; i < 5; i++ {
|
||||
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
|
||||
@@ -239,120 +233,120 @@ func (_ LayeredCacheTests) ResizeOnTheFly() {
|
||||
|
||||
cache.SetMaxSize(3)
|
||||
cache.SyncUpdates()
|
||||
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)
|
||||
assert.Equal(t, cache.GetDropped(), 2)
|
||||
assert.Equal(t, cache.Get("0", "a"), nil)
|
||||
assert.Equal(t, cache.Get("1", "a"), nil)
|
||||
assert.Equal(t, cache.Get("2", "a").Value(), 2)
|
||||
assert.Equal(t, cache.Get("3", "a").Value(), 3)
|
||||
assert.Equal(t, cache.Get("4", "a").Value(), 4)
|
||||
|
||||
cache.Set("5", "a", 5, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
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)
|
||||
assert.Equal(t, cache.GetDropped(), 1)
|
||||
assert.Equal(t, cache.Get("2", "a"), nil)
|
||||
assert.Equal(t, cache.Get("3", "a").Value(), 3)
|
||||
assert.Equal(t, cache.Get("4", "a").Value(), 4)
|
||||
assert.Equal(t, cache.Get("5", "a").Value(), 5)
|
||||
|
||||
cache.SetMaxSize(10)
|
||||
cache.Set("6", "a", 6, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
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)
|
||||
assert.Equal(t, cache.GetDropped(), 0)
|
||||
assert.Equal(t, cache.Get("3", "a").Value(), 3)
|
||||
assert.Equal(t, cache.Get("4", "a").Value(), 4)
|
||||
assert.Equal(t, cache.Get("5", "a").Value(), 5)
|
||||
assert.Equal(t, cache.Get("6", "a").Value(), 6)
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) RemovesOldestItemWhenFullBySizer() {
|
||||
func Test_LayeredCache_RemovesOldestItemWhenFullBySizer(t *testing.T) {
|
||||
cache := Layered[*SizedItem](Configure[*SizedItem]().MaxSize(9).ItemsToPrune(2))
|
||||
for i := 0; i < 7; i++ {
|
||||
cache.Set("pri", strconv.Itoa(i), &SizedItem{i, 2}, time.Minute)
|
||||
}
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.Get("pri", "0")).To.Equal(nil)
|
||||
Expect(cache.Get("pri", "1")).To.Equal(nil)
|
||||
Expect(cache.Get("pri", "2")).To.Equal(nil)
|
||||
Expect(cache.Get("pri", "3")).To.Equal(nil)
|
||||
Expect(cache.Get("pri", "4").Value().id).To.Equal(4)
|
||||
assert.Equal(t, cache.Get("pri", "0"), nil)
|
||||
assert.Equal(t, cache.Get("pri", "1"), nil)
|
||||
assert.Equal(t, cache.Get("pri", "2"), nil)
|
||||
assert.Equal(t, cache.Get("pri", "3"), nil)
|
||||
assert.Equal(t, cache.Get("pri", "4").Value().id, 4)
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) SetUpdatesSizeOnDelta() {
|
||||
func Test_LayeredCache_SetUpdatesSizeOnDelta(t *testing.T) {
|
||||
cache := Layered[*SizedItem](Configure[*SizedItem]())
|
||||
cache.Set("pri", "a", &SizedItem{0, 2}, time.Minute)
|
||||
cache.Set("pri", "b", &SizedItem{0, 3}, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.GetSize()).To.Eql(5)
|
||||
assert.Equal(t, cache.GetSize(), 5)
|
||||
cache.Set("pri", "b", &SizedItem{0, 3}, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.GetSize()).To.Eql(5)
|
||||
assert.Equal(t, cache.GetSize(), 5)
|
||||
cache.Set("pri", "b", &SizedItem{0, 4}, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.GetSize()).To.Eql(6)
|
||||
assert.Equal(t, cache.GetSize(), 6)
|
||||
cache.Set("pri", "b", &SizedItem{0, 2}, time.Minute)
|
||||
cache.Set("sec", "b", &SizedItem{0, 3}, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.GetSize()).To.Eql(7)
|
||||
assert.Equal(t, cache.GetSize(), 7)
|
||||
cache.Delete("pri", "b")
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.GetSize()).To.Eql(5)
|
||||
assert.Equal(t, cache.GetSize(), 5)
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) ReplaceDoesNotchangeSizeIfNotSet() {
|
||||
func Test_LayeredCache_ReplaceDoesNotchangeSizeIfNotSet(t *testing.T) {
|
||||
cache := Layered[*SizedItem](Configure[*SizedItem]())
|
||||
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})
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.GetSize()).To.Eql(6)
|
||||
assert.Equal(t, cache.GetSize(), 6)
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) ReplaceChangesSize() {
|
||||
func Test_LayeredCache_ReplaceChangesSize(t *testing.T) {
|
||||
cache := Layered[*SizedItem](Configure[*SizedItem]())
|
||||
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})
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.GetSize()).To.Eql(4)
|
||||
assert.Equal(t, cache.GetSize(), 4)
|
||||
|
||||
cache.Replace("pri", "2", &SizedItem{1, 1})
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.GetSize()).To.Eql(3)
|
||||
assert.Equal(t, cache.GetSize(), 3)
|
||||
|
||||
cache.Replace("pri", "2", &SizedItem{1, 3})
|
||||
cache.SyncUpdates()
|
||||
Expect(cache.GetSize()).To.Eql(5)
|
||||
assert.Equal(t, cache.GetSize(), 5)
|
||||
}
|
||||
|
||||
func (_ LayeredCacheTests) EachFunc() {
|
||||
func Test_LayeredCache_EachFunc(t *testing.T) {
|
||||
cache := Layered[int](Configure[int]().MaxSize(3).ItemsToPrune(1))
|
||||
Expect(forEachKeysLayered[int](cache, "1")).To.Equal([]string{})
|
||||
assert.List(t, forEachKeysLayered[int](cache, "1"), []string{})
|
||||
|
||||
cache.Set("1", "a", 1, time.Minute)
|
||||
Expect(forEachKeysLayered[int](cache, "1")).To.Equal([]string{"a"})
|
||||
assert.List(t, forEachKeysLayered[int](cache, "1"), []string{"a"})
|
||||
|
||||
cache.Set("1", "b", 2, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
Expect(forEachKeysLayered[int](cache, "1")).To.Equal([]string{"a", "b"})
|
||||
assert.List(t, forEachKeysLayered[int](cache, "1"), []string{"a", "b"})
|
||||
|
||||
cache.Set("1", "c", 3, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
Expect(forEachKeysLayered[int](cache, "1")).To.Equal([]string{"a", "b", "c"})
|
||||
assert.List(t, forEachKeysLayered[int](cache, "1"), []string{"a", "b", "c"})
|
||||
|
||||
cache.Set("1", "d", 4, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
Expect(forEachKeysLayered[int](cache, "1")).To.Equal([]string{"b", "c", "d"})
|
||||
assert.List(t, forEachKeysLayered[int](cache, "1"), []string{"b", "c", "d"})
|
||||
|
||||
// iteration is non-deterministic, all we know for sure is "stop" should not be in there
|
||||
cache.Set("1", "stop", 5, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
Expect(forEachKeysLayered[int](cache, "1")).Not.To.Contain("stop")
|
||||
assert.DoesNotContain(t, forEachKeysLayered[int](cache, "1"), "stop")
|
||||
|
||||
cache.Set("1", "e", 6, time.Minute)
|
||||
cache.SyncUpdates()
|
||||
Expect(forEachKeysLayered[int](cache, "1")).Not.To.Contain("stop")
|
||||
assert.DoesNotContain(t, forEachKeysLayered[int](cache, "1"), "stop")
|
||||
}
|
||||
|
||||
func newLayered[T any]() *LayeredCache[T] {
|
||||
|
Reference in New Issue
Block a user