github.com/heucuva/go-wlru@v0.9.2/cache_test.go (about)

     1  package wlru_test
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  	"context"
     7  
     8  	"github.com/heucuva/go-wlru"
     9  )
    10  
    11  func TestPruning(t *testing.T) {
    12  	c := &wlru.Cache{Capacity: 2}
    13  
    14  	input := []wlru.Entry{
    15  		{"a", "hello", false, false},
    16  		{"b", "world", false, false},
    17  		{"c", "matey", false, false},
    18  		{"d", "ahoy", false, false},
    19  	}
    20  
    21  	expected := []wlru.Entry{
    22  		input[3],
    23  		input[2],
    24  	}
    25  
    26  	for _, entry := range input {
    27  		if err := c.Set(entry.Key, entry.Value, entry.IsPermanent); err != nil {
    28  			t.Fatal(err)
    29  		}
    30  	}
    31  
    32  	compareSnapshot(t, c.SnapshotList(), expected)
    33  }
    34  
    35  func TestPermanency(t *testing.T) {
    36  	c := &wlru.Cache{Capacity: 2}
    37  
    38  	input := []wlru.Entry{
    39  		{"a", "hello", true, false},
    40  		{"b", "world", true, false},
    41  		{"c", "matey", false, false},
    42  		{"d", "ahoy", false, false},
    43  	}
    44  
    45  	expected := []wlru.Entry{
    46  		input[1],
    47  		input[0],
    48  	}
    49  
    50  	for _, entry := range input {
    51  		if err := c.Set(entry.Key, entry.Value, entry.IsPermanent); err != nil {
    52  			t.Fatal(err)
    53  		}
    54  	}
    55  
    56  	compareSnapshot(t, c.SnapshotList(), expected)
    57  }
    58  
    59  func TestExpiration(t *testing.T) {
    60  	c := &wlru.Cache{}
    61  
    62  	input := []wlru.Entry{
    63  		{"a", "hello", false, false},
    64  		{"b", "world", false, true},
    65  		{"c", "matey", false, false},
    66  		{"d", "ahoy", false, true},
    67  	}
    68  
    69  	expectedBeforeRemove := []wlru.Entry{
    70  		input[3],
    71  		input[2],
    72  		input[1],
    73  		input[0],
    74  	}
    75  
    76  	expectedAfterPrune := []wlru.Entry{
    77  		input[2],
    78  		input[0],
    79  	}
    80  
    81  	expiredCtx, cancel := context.WithCancel(context.Background())
    82  
    83  	for _, entry := range input {
    84  		ctx := context.Background()
    85  		if entry.IsExpired {
    86  			ctx = expiredCtx
    87  		}
    88  		if err := c.SetWithContext(ctx, entry.Key, entry.Value, entry.IsPermanent); err != nil {
    89  			t.Fatal(err)
    90  		}
    91  	}
    92  
    93  	cancel()
    94  
    95  	compareSnapshot(t, c.SnapshotList(), expectedBeforeRemove)
    96  
    97  	c.RemoveExpired()
    98  
    99  	compareSnapshot(t, c.SnapshotList(), expectedAfterPrune)
   100  }
   101  
   102  func TestUnbounded(t *testing.T) {
   103  	c := &wlru.Cache{}
   104  
   105  	input := []wlru.Entry{
   106  		{"a", "hello", false, false},
   107  		{"b", "world", false, false},
   108  		{"c", "matey", false, false},
   109  		{"d", "ahoy", false, false},
   110  	}
   111  
   112  	expected := []wlru.Entry{
   113  		input[3],
   114  		input[2],
   115  		input[1],
   116  		input[0],
   117  	}
   118  
   119  	for _, entry := range input {
   120  		if err := c.Set(entry.Key, entry.Value, entry.IsPermanent); err != nil {
   121  			t.Fatal(err)
   122  		}
   123  	}
   124  
   125  	compareSnapshot(t, c.SnapshotList(), expected)
   126  }
   127  
   128  func compareSnapshot(t *testing.T, snapshot, expected []wlru.Entry) {
   129  	if len(snapshot) != len(expected) {
   130  		t.Fatalf("unexpected snapshot size %d != expected %d", len(snapshot), len(expected))
   131  	}
   132  	for i, entry := range snapshot {
   133  		ex := expected[i]
   134  		if entry.Key != ex.Key {
   135  			t.Fatalf("[%d] unexpected key %q != expected %q", i, entry.Key, ex.Key)
   136  		}
   137  		if entry.Value != ex.Value {
   138  			t.Fatalf("[%d] unexpected value %q != expected %q", i, entry.Value, ex.Value)
   139  		}
   140  		if entry.IsPermanent != ex.IsPermanent {
   141  			t.Fatalf("[%d] unexpected isPermanent %v != expected %v", i, entry.IsPermanent, ex.IsPermanent)
   142  		}
   143  		if entry.IsExpired != ex.IsExpired {
   144  			t.Fatalf("[%d] unexpected isExpired %v != expected %v", i, entry.IsExpired, ex.IsExpired)
   145  		}
   146  	}
   147  }
   148  
   149  func BenchmarkSet100k(b *testing.B) {
   150  	c := &wlru.Cache{}
   151  
   152  	for i := 0; i < 100000; i++ {
   153  		if err := c.Set(i, rand.Int63(), false); err != nil {
   154  			b.Fatal(err)
   155  		}
   156  	}
   157  }
   158  
   159  func BenchmarkSet1M(b *testing.B) {
   160  	c := &wlru.Cache{}
   161  
   162  	for i := 0; i < 1000000; i++ {
   163  		if err := c.Set(i, rand.Int63(), false); err != nil {
   164  			b.Fatal(err)
   165  		}
   166  	}
   167  }