@@ -216,6 +216,40 @@ func TestNATSKV_NilKVGracefulDegradation(t *testing.T) {
216216 require .NoError (t , nkv .Purge (ctx ))
217217}
218218
219+ func TestNATSKV_MaxBytesEvictsOldEntries (t * testing.T ) {
220+ nc := startEmbeddedNATS (t )
221+
222+ // With MaxBytes set, the backing stream is updated to DiscardOld so that
223+ // the oldest entries are evicted when the bucket is full.
224+ const maxBytes int64 = 10 * 1024
225+ c , err := New [[]byte ](
226+ WithTTL (time .Minute ),
227+ WithNATS (nc , "test-maxbytes" ),
228+ WithMaxBytes (maxBytes ),
229+ )
230+ require .NoError (t , err )
231+
232+ ctx := context .Background ()
233+ payload := make ([]byte , 1024 )
234+
235+ // Write 20 entries, well beyond the 10 KiB limit
236+ for i := range 20 {
237+ key := "key-" + strings .Repeat ("x" , i )
238+ require .NoError (t , c .Set (ctx , key , payload ), "Set should not fail even when bucket is full" )
239+ }
240+
241+ // The latest entry should still be retrievable
242+ lastKey := "key-" + strings .Repeat ("x" , 19 )
243+ _ , ok , err := c .Get (ctx , lastKey )
244+ require .NoError (t , err )
245+ assert .True (t , ok , "most recent entry should still be in the cache" )
246+
247+ // The earliest entries should have been evicted
248+ _ , ok , err = c .Get (ctx , "key-" )
249+ require .NoError (t , err )
250+ assert .False (t , ok , "oldest entry should have been evicted" )
251+ }
252+
219253func TestNew_WithNATSReturnsNATSBackend (t * testing.T ) {
220254 nc := startEmbeddedNATS (t )
221255 c , err := New [string ](
0 commit comments