update roles cache to use sync.cache

add tests for roles cache
add changelog
use strings in sync cache tests
This commit is contained in:
Florian Schade
2021-01-21 00:04:25 +01:00
parent 03867f4230
commit 03c1416a4a
4 changed files with 102 additions and 77 deletions
+31 -31
View File
@@ -8,16 +8,16 @@ import (
"time"
)
func cacheRunner(size int) (*Cache, func(f func(i int))) {
func cacheRunner(size int) (*Cache, func(f func(v string))) {
c := NewCache(size)
run := func(f func(i int)) {
run := func(f func(v string)) {
wg := sync.WaitGroup{}
for i := 0; i < size; i++ {
wg.Add(1)
go func(i int) {
f(i)
go func(v string) {
f(v)
wg.Done()
}(i)
}(strconv.Itoa(i))
}
wg.Wait()
}
@@ -30,21 +30,21 @@ func BenchmarkCache(b *testing.B) {
size := 1024
c, cr := cacheRunner(size)
cr(func(i int) { c.Store(strconv.Itoa(i), i, time.Now().Add(100*time.Millisecond)) })
cr(func(i int) { c.Delete(strconv.Itoa(i)) })
cr(func(v string) { c.Store(v, v, time.Now().Add(100*time.Millisecond)) })
cr(func(v string) { c.Delete(v) })
}
func TestCache(t *testing.T) {
size := 1024
c, cr := cacheRunner(size)
cr(func(i int) { c.Store(strconv.Itoa(i), i, time.Now().Add(100*time.Millisecond)) })
cr(func(v string) { c.Store(v, v, time.Now().Add(100*time.Millisecond)) })
assert.Equal(t, size, int(c.length), "length is atomic")
cr(func(i int) { c.Delete(strconv.Itoa(i)) })
cr(func(v string) { c.Delete(v) })
assert.Equal(t, 0, int(c.length), "delete is atomic")
cr(func(i int) {
cr(func(v string) {
time.Sleep(101 * time.Millisecond)
c.evict()
})
@@ -55,50 +55,50 @@ func TestCache_Load(t *testing.T) {
size := 1024
c, cr := cacheRunner(size)
cr(func(i int) {
c.Store(strconv.Itoa(i), i, time.Now().Add(10*time.Second))
cr(func(v string) {
c.Store(v, v, time.Now().Add(10*time.Second))
})
cr(func(i int) {
assert.Equal(t, i, c.Load(strconv.Itoa(i)).V, "entry value is the same")
cr(func(v string) {
assert.Equal(t, v, c.Load(v).V, "entry value is the same")
})
cr(func(i int) {
assert.Nil(t, c.Load(strconv.Itoa(i+size)), "entry is nil if unknown")
cr(func(v string) {
assert.Nil(t, c.Load(v+strconv.Itoa(size)), "entry is nil if unknown")
})
cr(func(i int) {
cr(func(v string) {
wait := 100 * time.Millisecond
c.Store(strconv.Itoa(i), i, time.Now().Add(wait))
c.Store(v, v, time.Now().Add(wait))
time.Sleep(wait + 1)
assert.Nil(t, c.Load(strconv.Itoa(i)), "entry is nil if it's expired")
assert.Nil(t, c.Load(v), "entry is nil if it's expired")
})
}
func TestCache_Store(t *testing.T) {
c, cr := cacheRunner(1024)
cr(func(i int) {
c.Store(strconv.Itoa(i), i, time.Now().Add(100*time.Millisecond))
assert.Equal(t, i, c.Load(strconv.Itoa(i)).V, "new entries can be added")
cr(func(v string) {
c.Store(v, v, time.Now().Add(100*time.Millisecond))
assert.Equal(t, v, c.Load(v).V, "new entries can be added")
})
cr(func(i int) {
cr(func(v string) {
replacedExpiration := time.Now().Add(10 * time.Minute)
c.Store(strconv.Itoa(i), "old", time.Now().Add(10*time.Minute))
c.Store(strconv.Itoa(i), "updated", replacedExpiration)
assert.Equal(t, "updated", c.Load(strconv.Itoa(i)).V, "entry values can be updated")
assert.Equal(t, replacedExpiration, c.Load(strconv.Itoa(i)).expiration, "entry expiration can be updated")
c.Store(v, "old", time.Now().Add(10*time.Minute))
c.Store(v, "updated", replacedExpiration)
assert.Equal(t, "updated", c.Load(v).V, "entry values can be updated")
assert.Equal(t, replacedExpiration, c.Load(v).expiration, "entry expiration can be updated")
})
}
func TestCache_Delete(t *testing.T) {
c, cr := cacheRunner(1024)
cr(func(i int) {
c.Store(strconv.Itoa(i), i, time.Now().Add(100*time.Millisecond))
c.Delete(strconv.Itoa(i))
assert.Nil(t, c.Load(strconv.Itoa(i)), "entries can be deleted")
cr(func(v string) {
c.Store(v, v, time.Now().Add(100*time.Millisecond))
c.Delete(v)
assert.Nil(t, c.Load(v), "entries can be deleted")
})
assert.Equal(t, 0, int(c.length), "removing a entry decreases the cache size")