Files

105 lines
2.4 KiB
Go
Raw Permalink Normal View History

2021-01-16 16:38:01 +01:00
package sync
import (
"github.com/stretchr/testify/assert"
"strconv"
2021-01-18 14:12:10 +01:00
"sync"
2021-01-16 16:38:01 +01:00
"testing"
"time"
)
2021-01-20 18:32:33 +01:00
func cacheRunner(size int) (*Cache, func(f func(v string))) {
2021-01-16 16:38:01 +01:00
c := NewCache(size)
2021-01-20 18:32:33 +01:00
run := func(f func(v string)) {
2021-01-18 14:12:10 +01:00
wg := sync.WaitGroup{}
for i := 0; i < size; i++ {
wg.Add(1)
2021-01-20 18:32:33 +01:00
go func(v string) {
f(v)
2021-01-18 14:12:10 +01:00
wg.Done()
2021-01-20 18:32:33 +01:00
}(strconv.Itoa(i))
2021-01-18 14:12:10 +01:00
}
wg.Wait()
2021-01-16 16:38:01 +01:00
}
2021-01-18 14:12:10 +01:00
return &c, run
}
func BenchmarkCache(b *testing.B) {
b.ReportAllocs()
size := 1024
c, cr := cacheRunner(size)
2021-01-20 18:32:33 +01:00
cr(func(v string) { c.Store(v, v, time.Now().Add(100*time.Millisecond)) })
cr(func(v string) { c.Delete(v) })
2021-01-18 14:12:10 +01:00
}
func TestCache(t *testing.T) {
size := 1024
c, cr := cacheRunner(size)
2021-01-20 18:32:33 +01:00
cr(func(v string) { c.Store(v, v, time.Now().Add(100*time.Millisecond)) })
2021-01-18 14:12:10 +01:00
assert.Equal(t, size, int(c.length), "length is atomic")
2021-01-16 16:38:01 +01:00
2021-01-20 18:32:33 +01:00
cr(func(v string) { c.Delete(v) })
2021-01-18 14:12:10 +01:00
assert.Equal(t, 0, int(c.length), "delete is atomic")
2021-01-16 16:38:01 +01:00
2021-01-20 18:32:33 +01:00
cr(func(v string) {
2021-01-18 16:08:08 +01:00
time.Sleep(101 * time.Millisecond)
2021-01-18 14:12:10 +01:00
c.evict()
})
assert.Equal(t, 0, int(c.length), "evict is atomic")
2021-01-16 16:38:01 +01:00
}
2021-01-18 14:12:10 +01:00
func TestCache_Load(t *testing.T) {
size := 1024
c, cr := cacheRunner(size)
2021-01-16 16:38:01 +01:00
2021-01-20 18:32:33 +01:00
cr(func(v string) {
c.Store(v, v, time.Now().Add(10*time.Second))
2021-01-18 14:12:10 +01:00
})
2021-01-16 16:38:01 +01:00
2021-01-20 18:32:33 +01:00
cr(func(v string) {
assert.Equal(t, v, c.Load(v).V, "entry value is the same")
2021-01-18 14:12:10 +01:00
})
2021-01-16 16:38:01 +01:00
2021-01-20 18:32:33 +01:00
cr(func(v string) {
assert.Nil(t, c.Load(v+strconv.Itoa(size)), "entry is nil if unknown")
2021-01-18 14:12:10 +01:00
})
2021-01-20 18:32:33 +01:00
cr(func(v string) {
2021-01-18 16:08:08 +01:00
wait := 100 * time.Millisecond
2021-01-20 18:32:33 +01:00
c.Store(v, v, time.Now().Add(wait))
2021-01-18 14:12:10 +01:00
time.Sleep(wait + 1)
2021-01-20 18:32:33 +01:00
assert.Nil(t, c.Load(v), "entry is nil if it's expired")
2021-01-18 14:12:10 +01:00
})
2021-01-16 16:38:01 +01:00
}
2021-01-18 14:12:10 +01:00
func TestCache_Store(t *testing.T) {
c, cr := cacheRunner(1024)
2021-01-20 18:32:33 +01:00
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")
2021-01-18 14:12:10 +01:00
})
2021-01-20 18:32:33 +01:00
cr(func(v string) {
2021-01-18 16:08:08 +01:00
replacedExpiration := time.Now().Add(10 * time.Minute)
2021-01-20 18:32:33 +01:00
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")
2021-01-18 14:12:10 +01:00
})
}
func TestCache_Delete(t *testing.T) {
c, cr := cacheRunner(1024)
2021-01-20 18:32:33 +01:00
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")
2021-01-18 14:12:10 +01:00
})
2021-01-16 16:38:01 +01:00
2021-01-18 14:12:10 +01:00
assert.Equal(t, 0, int(c.length), "removing a entry decreases the cache size")
2021-01-16 16:38:01 +01:00
}