github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/cachedmap/cachedmap_test.go (about)

     1  // Copyright 2024 The Inspektor Gadget authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cachedmap
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  const defaultTestTTL = 2 * time.Second
    27  
    28  func TestNewCachedMap(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	cache := NewCachedMap[int, string](defaultTestTTL).(*cachedMap[int, string])
    32  	require.NotNil(t, cache)
    33  	t.Cleanup(func() {
    34  		cache.Close()
    35  	})
    36  
    37  	assert.Len(t, cache.current, 0)
    38  	assert.Len(t, cache.old, 0)
    39  }
    40  
    41  func TestResourceCacheAdd(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	cache := NewCachedMap[int, string](defaultTestTTL).(*cachedMap[int, string])
    45  	t.Cleanup(func() {
    46  		cache.Close()
    47  	})
    48  
    49  	key := 1
    50  	value := "value"
    51  	cache.Add(key, value)
    52  	assert.Len(t, cache.current, 1)
    53  	assert.Len(t, cache.old, 0)
    54  	assert.Equal(t, value, cache.current[key])
    55  }
    56  
    57  func TestResourceCacheOverwrite(t *testing.T) {
    58  	t.Parallel()
    59  
    60  	cache := NewCachedMap[int, string](defaultTestTTL).(*cachedMap[int, string])
    61  	t.Cleanup(func() {
    62  		cache.Close()
    63  	})
    64  
    65  	key := 1
    66  	value := "value"
    67  	cache.Add(key, value)
    68  	assert.Len(t, cache.current, 1)
    69  	assert.Len(t, cache.old, 0)
    70  	assert.Equal(t, value, cache.current[key])
    71  
    72  	value2 := "value2"
    73  	cache.Add(key, value2)
    74  	assert.Len(t, cache.current, 1)
    75  	assert.Len(t, cache.old, 0)
    76  	assert.Equal(t, value2, cache.current[key])
    77  }
    78  
    79  func TestResourceCacheRemove(t *testing.T) {
    80  	t.Parallel()
    81  
    82  	cache := NewCachedMap[int, string](defaultTestTTL).(*cachedMap[int, string])
    83  	t.Cleanup(func() {
    84  		cache.Close()
    85  	})
    86  
    87  	key := 1
    88  	value := "value"
    89  	cache.Add(key, value)
    90  	assert.Len(t, cache.current, 1)
    91  	assert.Len(t, cache.old, 0)
    92  	assert.Equal(t, value, cache.current[key])
    93  
    94  	cache.Remove(key)
    95  	assert.Len(t, cache.current, 0)
    96  	assert.Len(t, cache.old, 1)
    97  	assert.Equal(t, value, cache.old[key].obj)
    98  }
    99  
   100  func TestResourceCachePruneOldObjects(t *testing.T) {
   101  	t.Parallel()
   102  
   103  	cache := NewCachedMap[int, string](defaultTestTTL).(*cachedMap[int, string])
   104  	t.Cleanup(func() {
   105  		cache.Close()
   106  	})
   107  
   108  	key := 1
   109  	value := "value"
   110  	cache.Add(key, value)
   111  	assert.Len(t, cache.current, 1)
   112  	assert.Len(t, cache.old, 0)
   113  	assert.Equal(t, value, cache.current[key])
   114  
   115  	cache.Remove(key)
   116  	assert.Len(t, cache.current, 0)
   117  	assert.Len(t, cache.old, 1)
   118  	assert.Equal(t, value, cache.old[key].obj)
   119  
   120  	// Manually subtract the TTL from the timestamp
   121  	temp := cache.old[key]
   122  	temp.deletionTimestamp = temp.deletionTimestamp.Add(-defaultTestTTL)
   123  	cache.old[key] = temp
   124  	cache.pruneOldObjects()
   125  	assert.Len(t, cache.current, 0)
   126  	assert.Len(t, cache.old, 0)
   127  }
   128  
   129  func TestResourceCacheValues(t *testing.T) {
   130  	t.Parallel()
   131  
   132  	cache := NewCachedMap[int, string](defaultTestTTL).(*cachedMap[int, string])
   133  	t.Cleanup(func() {
   134  		cache.Close()
   135  	})
   136  
   137  	key := 1
   138  	value := "value"
   139  	cache.Add(key, value)
   140  	sliceFromCurrent := cache.Values()
   141  	cache.Remove(key)
   142  	sliceFromOld := cache.Values()
   143  
   144  	// Manually subtract the TTL from the timestamp
   145  	temp := cache.old[key]
   146  	temp.deletionTimestamp = temp.deletionTimestamp.Add(-defaultTestTTL)
   147  	cache.old[key] = temp
   148  	cache.pruneOldObjects()
   149  
   150  	sliceAfterPrune := cache.Values()
   151  
   152  	require.Len(t, sliceFromCurrent, 1)
   153  	require.Len(t, sliceFromOld, 1)
   154  	assert.Equal(t, value, sliceFromCurrent[0])
   155  	assert.Equal(t, value, sliceFromOld[0])
   156  	assert.Len(t, sliceAfterPrune, 0)
   157  }
   158  
   159  func TestResourceCacheGet(t *testing.T) {
   160  	t.Parallel()
   161  
   162  	cache := NewCachedMap[int, string](defaultTestTTL).(*cachedMap[int, string])
   163  	t.Cleanup(func() {
   164  		cache.Close()
   165  	})
   166  
   167  	key := 1
   168  	value := "value"
   169  	cache.Add(key, value)
   170  	assert.Len(t, cache.current, 1)
   171  	assert.Len(t, cache.old, 0)
   172  	assert.Equal(t, value, cache.current[key])
   173  
   174  	objFromCache, found := cache.Get(key)
   175  	require.True(t, found)
   176  	assert.Equal(t, value, objFromCache)
   177  }
   178  
   179  func TestResourceCacheGetNotFound(t *testing.T) {
   180  	t.Parallel()
   181  
   182  	cache := NewCachedMap[int, string](defaultTestTTL).(*cachedMap[int, string])
   183  
   184  	key := 1
   185  	value := "value"
   186  	cache.Add(key, value)
   187  	assert.Len(t, cache.current, 1)
   188  	assert.Len(t, cache.old, 0)
   189  	assert.Equal(t, value, cache.current[key])
   190  
   191  	_, found := cache.Get(2)
   192  	assert.False(t, found)
   193  }
   194  
   195  func TestResourceCacheGetCmp(t *testing.T) {
   196  	t.Parallel()
   197  
   198  	cache := NewCachedMap[int, string](defaultTestTTL).(*cachedMap[int, string])
   199  
   200  	key1 := 1
   201  	value1 := "value1"
   202  	cache.Add(key1, value1)
   203  	assert.Len(t, cache.current, 1)
   204  	assert.Len(t, cache.old, 0)
   205  	assert.Equal(t, value1, cache.current[key1])
   206  
   207  	key2 := 2
   208  	value2 := "value2"
   209  	cache.Add(key2, value2)
   210  	assert.Len(t, cache.current, 2)
   211  	assert.Len(t, cache.old, 0)
   212  	assert.Equal(t, value2, cache.current[key2])
   213  
   214  	objFromCache, found := cache.GetCmp(func(obj string) bool {
   215  		return strings.HasSuffix(obj, "2")
   216  	})
   217  	require.True(t, found)
   218  	assert.Equal(t, value2, objFromCache)
   219  }