| 96 | } |
| 97 | |
| 98 | func TestInMemoryCache_Set(t *testing.T) { |
| 99 | const testKey = "key" |
| 100 | |
| 101 | type testStruct struct { |
| 102 | Name string `json:"name"` |
| 103 | Age int `json:"age"` |
| 104 | } |
| 105 | |
| 106 | alice := testStruct{Name: "Alice", Age: 30} |
| 107 | |
| 108 | bob := testStruct{Name: "Bob", Age: 25} |
| 109 | |
| 110 | testCases := []struct { |
| 111 | name string |
| 112 | setup func(*lru.Cache[string, testStruct]) |
| 113 | value testStruct |
| 114 | assertions func(*testing.T, *lru.Cache[string, testStruct], error) |
| 115 | }{ |
| 116 | { |
| 117 | name: "initial write", |
| 118 | value: alice, |
| 119 | assertions: func( |
| 120 | t *testing.T, |
| 121 | c *lru.Cache[string, testStruct], |
| 122 | err error, |
| 123 | ) { |
| 124 | require.NoError(t, err) |
| 125 | value, found := c.Get(testKey) |
| 126 | require.True(t, found) |
| 127 | require.Equal(t, alice, value) |
| 128 | }, |
| 129 | }, |
| 130 | { |
| 131 | name: "overwrite", |
| 132 | setup: func(c *lru.Cache[string, testStruct]) { |
| 133 | c.Add(testKey, alice) |
| 134 | }, |
| 135 | value: bob, |
| 136 | assertions: func( |
| 137 | t *testing.T, |
| 138 | c *lru.Cache[string, testStruct], |
| 139 | err error, |
| 140 | ) { |
| 141 | require.NoError(t, err) |
| 142 | value, found := c.Get(testKey) |
| 143 | require.True(t, found) |
| 144 | require.Equal(t, bob, value) |
| 145 | }, |
| 146 | }, |
| 147 | { |
| 148 | name: "lru eviction when cache is full", |
| 149 | setup: func(c *lru.Cache[string, testStruct]) { |
| 150 | c.Add("alice-key", alice) |
| 151 | }, |
| 152 | value: bob, |
| 153 | assertions: func( |
| 154 | t *testing.T, |
| 155 | c *lru.Cache[string, testStruct], |