@@ -2,24 +2,18 @@ package cache
22
33import (
44 "testing"
5+
6+ "github.com/stretchr/testify/require"
57)
68
79func TestCacheInsert (t * testing.T ) {
810 cache := NewCache (1 )
9- insertError := cache .Insert ("A" , "" , 1 )
10-
11- if insertError != nil {
12- t .Fatalf ("Cache insert resulted in unexpected error: %s" , insertError )
13- }
11+ require .True (t , cache .Insert ("A" , "" , 1 ))
1412}
1513
1614func TestCacheInsertWithinBudget (t * testing.T ) {
1715 cache := NewCache (1 )
18- insertError := cache .Insert ("A" , "" , 2 )
19-
20- if insertError != nil {
21- t .Fatalf ("Cache insert resulted in unexpected error: %s" , insertError )
22- }
16+ require .True (t , cache .Insert ("A" , "" , 2 ))
2317}
2418
2519func TestCacheInsertUpdatesWeight (t * testing.T ) {
@@ -28,19 +22,13 @@ func TestCacheInsertUpdatesWeight(t *testing.T) {
2822 _ = cache .Insert ("B" , "" , 1 )
2923 _ = cache .Insert ("budget_exceeded" , "" , 1 )
3024
31- if cache .GetWeight () != 2 {
32- t .Fatal ("Cache with budget 2 did not correctly set weight after evicting one of three nodes" )
33- }
25+ require .Equal (t , 2 , cache .GetWeight ())
3426}
3527
3628func TestCacheInsertDuplicateRejected (t * testing.T ) {
3729 cache := NewCache (2 )
38- _ = cache .Insert ("dupe" , "" , 1 )
39- dupeError := cache .Insert ("dupe" , "" , 1 )
40-
41- if dupeError == nil {
42- t .Fatal ("Cache insert of duplicate key did not result in any err" )
43- }
30+ require .True (t , cache .Insert ("dupe" , "" , 1 ))
31+ require .False (t , cache .Insert ("dupe" , "" , 1 ))
4432}
4533
4634func TestCacheInsertEvictsLeastRecentlyUsed (t * testing.T ) {
@@ -51,43 +39,34 @@ func TestCacheInsertEvictsLeastRecentlyUsed(t *testing.T) {
5139 _ = cache .Insert ("B" , "" , 1 )
5240
5341 _ , foundEvicted := cache .Retrieve ("evicted" )
54- if foundEvicted {
55- t .Fatal ("Cache insert did not trigger eviction after weight exceedance" )
56- }
42+ require .False (t , foundEvicted )
5743
5844 // double check that only 1 one was evicted and not any extra
5945 _ , foundA := cache .Retrieve ("A" )
46+ require .True (t , foundA )
6047 _ , foundB := cache .Retrieve ("B" )
61-
62- if ! foundA || ! foundB {
63- t .Fatal ("Cache insert evicted more than necessary" )
64- }
48+ require .True (t , foundB )
6549}
6650
6751func TestCacheInsertEvictsLeastRecentlyRetrieved (t * testing.T ) {
6852 cache := NewCache (2 )
6953 _ = cache .Insert ("A" , "" , 1 )
7054 _ = cache .Insert ("evicted" , "" , 1 )
7155
72- // retrieve the oldest node, promoting it head so it is not evicted
56+ // retrieve the oldest node, promoting it head, so it is not evicted
7357 cache .Retrieve ("A" )
7458
7559 // insert once more, exceeding weight capacity
7660 _ = cache .Insert ("B" , "" , 1 )
7761 // now the least recently used key should be evicted
7862 _ , foundEvicted := cache .Retrieve ("evicted" )
79- if foundEvicted {
80- t .Fatal ("Cache insert did not evict least recently used after weight exceedance" )
81- }
63+ require .False (t , foundEvicted )
8264}
8365
8466func TestClear (t * testing.T ) {
8567 cache := NewCache (1 )
8668 _ = cache .Insert ("cleared" , "" , 1 )
8769 cache .Clear ()
8870 _ , found := cache .Retrieve ("cleared" )
89-
90- if found {
91- t .Fatal ("Still able to retrieve nodes after cache was cleared" )
92- }
71+ require .False (t , found )
9372}
0 commit comments