github.com/eagleql/xray-core@v1.4.4/common/cache/lru_test.go (about)

     1  package cache_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/eagleql/xray-core/common/cache"
     7  )
     8  
     9  func TestLruReplaceValue(t *testing.T) {
    10  	lru := NewLru(2)
    11  	lru.Put(2, 6)
    12  	lru.Put(1, 5)
    13  	lru.Put(1, 2)
    14  	v, _ := lru.Get(1)
    15  	if v != 2 {
    16  		t.Error("should get 2", v)
    17  	}
    18  	v, _ = lru.Get(2)
    19  	if v != 6 {
    20  		t.Error("should get 6", v)
    21  	}
    22  }
    23  
    24  func TestLruRemoveOld(t *testing.T) {
    25  	lru := NewLru(2)
    26  	v, ok := lru.Get(2)
    27  	if ok {
    28  		t.Error("should get nil", v)
    29  	}
    30  	lru.Put(1, 1)
    31  	lru.Put(2, 2)
    32  	v, _ = lru.Get(1)
    33  	if v != 1 {
    34  		t.Error("should get 1", v)
    35  	}
    36  	lru.Put(3, 3)
    37  	v, ok = lru.Get(2)
    38  	if ok {
    39  		t.Error("should get nil", v)
    40  	}
    41  	lru.Put(4, 4)
    42  	v, ok = lru.Get(1)
    43  	if ok {
    44  		t.Error("should get nil", v)
    45  	}
    46  	v, _ = lru.Get(3)
    47  	if v != 3 {
    48  		t.Error("should get 3", v)
    49  	}
    50  	v, _ = lru.Get(4)
    51  	if v != 4 {
    52  		t.Error("should get 4", v)
    53  	}
    54  }
    55  
    56  func TestGetKeyFromValue(t *testing.T) {
    57  	lru := NewLru(2)
    58  	lru.Put(3, 3)
    59  	lru.Put(2, 2)
    60  	lru.GetKeyFromValue(3)
    61  	lru.Put(1, 1)
    62  	v, ok := lru.GetKeyFromValue(2)
    63  	if ok {
    64  		t.Error("should get nil", v)
    65  	}
    66  	v, _ = lru.GetKeyFromValue(3)
    67  	if v != 3 {
    68  		t.Error("should get 3", v)
    69  	}
    70  }
    71  
    72  func TestPeekKeyFromValue(t *testing.T) {
    73  	lru := NewLru(2)
    74  	lru.Put(3, 3)
    75  	lru.Put(2, 2)
    76  	lru.PeekKeyFromValue(3)
    77  	lru.Put(1, 1)
    78  	v, ok := lru.PeekKeyFromValue(3)
    79  	if ok {
    80  		t.Error("should get nil", v)
    81  	}
    82  	v, _ = lru.PeekKeyFromValue(2)
    83  	if v != 2 {
    84  		t.Error("should get 2", v)
    85  	}
    86  }