123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- package cache
- import (
- "context"
- "testing"
- "time"
- "github.com/stretchr/testify/require"
- )
- func TestLRU(t *testing.T) {
- testCache(t, &LRU{
- ItemTTL: time.Minute,
- })
- }
- func TestLRUEvict(t *testing.T) {
- ctx := context.TODO()
- isHelloEvicted := false
- c := LRU{
- MaxSize: 12,
- ItemTTL: time.Minute,
- OnEvict: func(key string, i Item) {
- isHelloEvicted = key == "/hello"
- require.Equal(t, String("hello"), i)
- },
- }
- c.Set(ctx, "/hello", String("hello"))
- require.Len(t, c.priority, 1)
- require.Equal(t, 1, c.Len())
- require.Equal(t, 5, c.Size())
- c.Set(ctx, "/world", String("world"))
- require.Len(t, c.priority, 2)
- require.Equal(t, 2, c.Len())
- require.Equal(t, 10, c.Size())
- c.Get(ctx, "/world")
- c.Set(ctx, "/goodbye", String("goodbye"))
- require.Len(t, c.priority, 2)
- require.Equal(t, 2, c.Len())
- require.Equal(t, 12, c.Size())
- require.True(t, isHelloEvicted)
- hello, isCached := c.Get(ctx, "/hello")
- require.False(t, isCached)
- require.Nil(t, hello)
- world, isCached := c.Get(ctx, "/world")
- require.True(t, isCached)
- require.Equal(t, String("world"), world)
- goodbye, isCached := c.Get(ctx, "/goodbye")
- require.True(t, isCached)
- require.Equal(t, String("goodbye"), goodbye)
- }
- func TestLRUSetSameKey(t *testing.T) {
- ctx := context.TODO()
- c := LRU{
- ItemTTL: time.Minute,
- }
- c.Set(ctx, "/test", String("test"))
- require.Len(t, c.priority, 1)
- require.Equal(t, 1, c.Len())
- require.Equal(t, 4, c.Size())
- c.Set(ctx, "/test", String("unit-test"))
- require.Len(t, c.priority, 2)
- require.Equal(t, 1, c.Len())
- require.Equal(t, 13, c.Size())
- }
- func TestSortLRUItems(t *testing.T) {
- now := time.Now()
- utests := []struct {
- scenario string
- now time.Time
- in []*lruItem
- out []*lruItem
- }{
- {
- scenario: "nil",
- now: now,
- },
- {
- scenario: "empty",
- now: now,
- in: []*lruItem{},
- out: []*lruItem{},
- },
- {
- scenario: "1 item",
- now: now,
- in: []*lruItem{
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- },
- out: []*lruItem{
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- },
- },
- {
- scenario: "2 items",
- now: now,
- in: []*lruItem{
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 42,
- expiresAt: now.Add(time.Second),
- },
- },
- out: []*lruItem{
- {
- count: 42,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- },
- },
- {
- scenario: "multiple items",
- now: now,
- in: []*lruItem{
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 42,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 7,
- expiresAt: now.Add(time.Second),
- },
- {
- count: -21,
- expiresAt: now.Add(time.Second),
- },
- },
- out: []*lruItem{
- {
- count: 42,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 7,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- {
- count: -21,
- expiresAt: now.Add(time.Second),
- },
- },
- },
- {
- scenario: "multiple items with expired ones",
- now: now,
- in: []*lruItem{
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 42,
- expiresAt: now.Add(-time.Second),
- },
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 7,
- expiresAt: now.Add(time.Second),
- },
- {
- count: -21,
- expiresAt: now.Add(time.Second),
- },
- },
- out: []*lruItem{
- {
- count: 7,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 1,
- expiresAt: now.Add(time.Second),
- },
- {
- count: 42,
- expiresAt: now.Add(-time.Second),
- },
- {
- count: -21,
- expiresAt: now.Add(time.Second),
- },
- },
- },
- }
- for _, u := range utests {
- t.Run(u.scenario, func(t *testing.T) {
- sortLRUItems(u.now, u.in)
- require.Equal(t, u.out, u.in)
- })
- }
- }
|