github.com/livekit/protocol@v1.39.3/utils/time_size_cache_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/livekit/protocol/utils/mono"
    10  )
    11  
    12  func TestTimeSizeCache(t *testing.T) {
    13  	cache := NewTimeSizeCache[string](TimeSizeCacheParams{
    14  		TTL:     5 * time.Second,
    15  		MaxSize: 10,
    16  	})
    17  
    18  	now := mono.Now()
    19  	one := "one"
    20  	cache.AddAt(&one, len(one), now.UnixNano())
    21  	items := cache.Get()
    22  	require.Equal(t, 1, len(items))
    23  	require.Equal(t, one, *items[0])
    24  	require.Equal(t, 3, cache.itemsSize)
    25  
    26  	now = now.Add(time.Second)
    27  	two := "two"
    28  	cache.AddAt(&two, len(two), now.UnixNano())
    29  	items = cache.Get()
    30  	require.Equal(t, 2, len(items))
    31  	require.Equal(t, one, *items[0])
    32  	require.Equal(t, two, *items[1])
    33  	require.Equal(t, 6, cache.itemsSize)
    34  
    35  	// this should evict the "one" based on size
    36  	now = now.Add(time.Second)
    37  	three := "three"
    38  	cache.AddAt(&three, len(three), now.UnixNano())
    39  	items = cache.Get()
    40  	require.Equal(t, 2, len(items))
    41  	require.Equal(t, two, *items[0])
    42  	require.Equal(t, three, *items[1])
    43  	require.Equal(t, 8, cache.itemsSize)
    44  
    45  	// this should evict the "two" based on size
    46  	now = now.Add(time.Second)
    47  	four := "four"
    48  	cache.AddAt(&four, len(four), now.UnixNano())
    49  	items = cache.Get()
    50  	require.Equal(t, 2, len(items))
    51  	require.Equal(t, three, *items[0])
    52  	require.Equal(t, four, *items[1])
    53  	require.Equal(t, 9, cache.itemsSize)
    54  
    55  	// this should not evict any element as existing elements should not have aged enough
    56  	now = now.Add(time.Second)
    57  	cache.PruneAt(now.UnixNano())
    58  	items = cache.Get()
    59  	require.Equal(t, 2, len(items))
    60  	require.Equal(t, 9, cache.itemsSize)
    61  
    62  	// should evict "three" based on age
    63  	now = now.Add(4 * time.Second)
    64  	cache.PruneAt(now.UnixNano())
    65  	items = cache.Get()
    66  	require.Equal(t, 1, len(items))
    67  	require.Equal(t, 4, cache.itemsSize)
    68  
    69  	// should evict all
    70  	now = now.Add(time.Second)
    71  	cache.PruneAt(now.UnixNano())
    72  	items = cache.Get()
    73  	require.Equal(t, 0, len(items))
    74  	require.Equal(t, 0, cache.itemsSize)
    75  
    76  	// add something that fits
    77  	cache.Add(&four, len(four))
    78  	items = cache.Get()
    79  	require.Equal(t, 1, len(items))
    80  	require.Equal(t, four, *items[0])
    81  	require.Equal(t, 4, cache.itemsSize)
    82  
    83  	// add something which will exceed size threshold by itself,
    84  	// should clean up all items in cache as items are FIFO and the "four" added above
    85  	// gets removed first and the long item added also should get removed as it does not fit.
    86  	reallyLong := "really-long"
    87  	cache.Add(&reallyLong, len(reallyLong))
    88  	items = cache.Get()
    89  	require.Equal(t, 0, len(items))
    90  	require.Equal(t, 0, cache.itemsSize)
    91  }