github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/utils/lru_test.go (about)

     1  // This files was copied/modified from https://github.com/hashicorp/golang-lru
     2  // which was (see below)
     3  
     4  // This package provides a simple LRU cache. It is based on the
     5  // LRU implementation in groupcache:
     6  // https://github.com/golang/groupcache/tree/master/lru
     7  
     8  package utils
     9  
    10  import "testing"
    11  import "time"
    12  
    13  func TestLRU(t *testing.T) {
    14  	evictCounter := 0
    15  	onEvicted := func(k interface{}, v interface{}) {
    16  		evictCounter += 1
    17  	}
    18  	l, err := NewLruWithEvict(128, onEvicted)
    19  	if err != nil {
    20  		t.Fatalf("err: %v", err)
    21  	}
    22  
    23  	for i := 0; i < 256; i++ {
    24  		l.Add(i, i)
    25  	}
    26  	if l.Len() != 128 {
    27  		t.Fatalf("bad len: %v", l.Len())
    28  	}
    29  
    30  	if evictCounter != 128 {
    31  		t.Fatalf("bad evict count: %v", evictCounter)
    32  	}
    33  
    34  	for i, k := range l.Keys() {
    35  		if v, ok := l.Get(k); !ok || v != k || v != i+128 {
    36  			t.Fatalf("bad key: %v", k)
    37  		}
    38  	}
    39  	for i := 0; i < 128; i++ {
    40  		_, ok := l.Get(i)
    41  		if ok {
    42  			t.Fatalf("should be evicted")
    43  		}
    44  	}
    45  	for i := 128; i < 256; i++ {
    46  		_, ok := l.Get(i)
    47  		if !ok {
    48  			t.Fatalf("should not be evicted")
    49  		}
    50  	}
    51  	for i := 128; i < 192; i++ {
    52  		l.Remove(i)
    53  		_, ok := l.Get(i)
    54  		if ok {
    55  			t.Fatalf("should be deleted")
    56  		}
    57  	}
    58  
    59  	l.Get(192) // expect 192 to be last key in l.Keys()
    60  
    61  	for i, k := range l.Keys() {
    62  		if (i < 63 && k != i+193) || (i == 63 && k != 192) {
    63  			t.Fatalf("out of order key: %v", k)
    64  		}
    65  	}
    66  
    67  	l.Purge()
    68  	if l.Len() != 0 {
    69  		t.Fatalf("bad len: %v", l.Len())
    70  	}
    71  	if _, ok := l.Get(200); ok {
    72  		t.Fatalf("should contain nothing")
    73  	}
    74  }
    75  
    76  // test that Add return true/false if an eviction occurred
    77  func TestLRUAdd(t *testing.T) {
    78  	evictCounter := 0
    79  	onEvicted := func(k interface{}, v interface{}) {
    80  		evictCounter += 1
    81  	}
    82  
    83  	l, err := NewLruWithEvict(1, onEvicted)
    84  	if err != nil {
    85  		t.Fatalf("err: %v", err)
    86  	}
    87  
    88  	if l.Add(1, 1) || evictCounter != 0 {
    89  		t.Errorf("should not have an eviction")
    90  	}
    91  	if !l.Add(2, 2) || evictCounter != 1 {
    92  		t.Errorf("should have an eviction")
    93  	}
    94  }
    95  
    96  func TestLRUExpire(t *testing.T) {
    97  	l := NewLru(128)
    98  
    99  	l.AddWithExpiresInSecs(1, 1, 1)
   100  	l.AddWithExpiresInSecs(2, 2, 1)
   101  	l.AddWithExpiresInSecs(3, 3, 0)
   102  
   103  	time.Sleep(time.Millisecond * 2100)
   104  
   105  	if r1, ok := l.Get(1); ok {
   106  		t.Fatal(r1)
   107  	}
   108  
   109  	if _, ok2 := l.Get(3); !ok2 {
   110  		t.Fatal("should exist")
   111  	}
   112  }