@@ -189,6 +189,10 @@ func Test_Cache_set(t *testing.T) {
189189 Key : existingKey ,
190190 TTL : DefaultTTL ,
191191 },
192+ "Set with existing key and PreviousOrDefaultTTL" : {
193+ Key : existingKey ,
194+ TTL : PreviousOrDefaultTTL ,
195+ },
192196 "Set with new key and eviction caused by small capacity" : {
193197 Capacity : 3 ,
194198 Key : newKey ,
@@ -232,6 +236,14 @@ func Test_Cache_set(t *testing.T) {
232236 },
233237 ExpectFns : true ,
234238 },
239+ "Set with new key and PreviousOrDefaultTTL" : {
240+ Key : newKey ,
241+ TTL : PreviousOrDefaultTTL ,
242+ Metrics : Metrics {
243+ Insertions : 1 ,
244+ },
245+ ExpectFns : true ,
246+ },
235247 }
236248
237249 for cn , c := range cc {
@@ -245,6 +257,9 @@ func Test_Cache_set(t *testing.T) {
245257 evictionFnsCalls int
246258 )
247259
260+ // calculated based on how addToCache sets ttl
261+ existingKeyTTL := time .Hour + time .Minute
262+
248263 cache := prepCache (time .Hour , evictedKey , existingKey , "test3" )
249264 cache .options .capacity = c .Capacity
250265 cache .options .ttl = time .Minute * 20
@@ -295,6 +310,13 @@ func Test_Cache_set(t *testing.T) {
295310 assert .Equal (t , c .TTL , item .ttl )
296311 assert .WithinDuration (t , time .Now (), item .expiresAt , c .TTL )
297312 assert .Equal (t , c .Key , cache .items .expQueue [0 ].Value .(* Item [string , string ]).key )
313+ case c .TTL == PreviousOrDefaultTTL :
314+ expectedTTL := cache .options .ttl
315+ if c .Key == existingKey {
316+ expectedTTL = existingKeyTTL
317+ }
318+ assert .Equal (t , expectedTTL , item .ttl )
319+ assert .WithinDuration (t , time .Now (), item .expiresAt , expectedTTL )
298320 default :
299321 assert .Equal (t , c .TTL , item .ttl )
300322 assert .Zero (t , item .expiresAt )
0 commit comments