github.com/blend/go-sdk@v1.20220411.3/cache/lru_heap_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package cache 9 10 import ( 11 "testing" 12 "time" 13 14 "github.com/blend/go-sdk/assert" 15 ) 16 17 func TestLRUHeap(t *testing.T) { 18 assert := assert.New(t) 19 20 t0 := time.Date(2019, 06, 13, 12, 10, 9, 8, time.UTC) 21 t1 := time.Date(2019, 06, 14, 12, 10, 9, 8, time.UTC) 22 t2 := time.Date(2019, 06, 15, 12, 10, 9, 8, time.UTC) 23 t3 := time.Date(2019, 06, 16, 12, 10, 9, 8, time.UTC) 24 t4 := time.Date(2019, 06, 17, 12, 10, 9, 8, time.UTC) 25 t5 := time.Date(2019, 06, 18, 12, 10, 9, 8, time.UTC) 26 27 h := NewLRUHeap() 28 h.Push(&Value{ 29 Key: "5", 30 Expires: t5, 31 }) 32 assert.Len(h.Values, 1) 33 assert.Equal(1, h.Len()) 34 h.Push(&Value{ 35 Key: "2", 36 Expires: t2, 37 }) 38 assert.Len(h.Values, 2) 39 h.Push(&Value{ 40 Key: "3", 41 Expires: t3, 42 }) 43 assert.Len(h.Values, 3) 44 h.Push(&Value{ 45 Key: "0", 46 Expires: t0, 47 }) 48 assert.Len(h.Values, 4) 49 h.Push(&Value{ 50 Key: "4", 51 Expires: t4, 52 }) 53 assert.Len(h.Values, 5) 54 h.Push(&Value{ 55 Key: "1", 56 Expires: t1, 57 }) 58 assert.Len(h.Values, 6) 59 assert.Equal(t0, h.Values[0].Expires) 60 61 popped := h.Pop() 62 assert.Equal(t0, popped.Expires) 63 assert.Equal(t1, h.Values[0].Expires) 64 65 popped = h.Pop() 66 assert.Equal(t1, popped.Expires) 67 assert.Equal(t2, h.Values[0].Expires) 68 69 popped = h.Pop() 70 assert.Equal(t2, popped.Expires) 71 assert.Equal(t3, h.Values[0].Expires) 72 73 popped = h.Pop() 74 assert.Equal(t3, popped.Expires) 75 assert.Equal(t4, h.Values[0].Expires) 76 77 popped = h.Pop() 78 assert.Equal(t4, popped.Expires) 79 assert.Equal(t5, h.Values[0].Expires) 80 81 popped = h.Pop() 82 assert.Equal(t5, popped.Expires) 83 assert.Empty(h.Values) 84 85 popped = h.Pop() 86 assert.Nil(popped) 87 } 88 89 func TestLRUHeapEmpty(t *testing.T) { 90 assert := assert.New(t) 91 92 h := NewLRUHeap() 93 h.Fix(nil) 94 h.Remove(nil) 95 assert.Nil(h.Pop()) 96 assert.Nil(h.Peek()) 97 } 98 99 func TestLRUHeapConsumeUntil(t *testing.T) { 100 assert := assert.New(t) 101 102 t0 := time.Date(2019, 06, 13, 12, 10, 9, 8, time.UTC) 103 t1 := time.Date(2019, 06, 14, 12, 10, 9, 8, time.UTC) 104 t2 := time.Date(2019, 06, 15, 12, 10, 9, 8, time.UTC) 105 t3 := time.Date(2019, 06, 16, 12, 10, 9, 8, time.UTC) 106 t4 := time.Date(2019, 06, 17, 12, 10, 9, 8, time.UTC) 107 t5 := time.Date(2019, 06, 18, 12, 10, 9, 8, time.UTC) 108 109 h := NewLRUHeap() 110 h.Push(&Value{Key: "5", Expires: t5}) 111 h.Push(&Value{Key: "2", Expires: t2}) 112 h.Push(&Value{Key: "3", Expires: t3}) 113 h.Push(&Value{Key: "0", Expires: t0}) 114 h.Push(&Value{Key: "4", Expires: t4}) 115 h.Push(&Value{Key: "1", Expires: t1}) 116 assert.Len(h.Values, 6) 117 118 h.Consume(func(v *Value) bool { 119 return v.Expires.Before(t3) 120 }) 121 assert.Len(h.Values, 3, "consumeUntil should have removed (3) items") 122 } 123 124 func TestLRUHeapFix(t *testing.T) { 125 assert := assert.New(t) 126 127 t0 := time.Date(2019, 06, 13, 12, 10, 9, 8, time.UTC) 128 t1 := time.Date(2019, 06, 14, 12, 10, 9, 8, time.UTC) 129 t2 := time.Date(2019, 06, 15, 12, 10, 9, 8, time.UTC) 130 t3 := time.Date(2018, 06, 15, 12, 10, 9, 8, time.UTC) 131 132 h := NewLRUHeap() 133 h.Push(&Value{Key: "1", Expires: t0}) 134 h.Push(&Value{Key: "2", Expires: t1}) 135 h.Push(&Value{Key: "3", Expires: t2}) 136 assert.Equal(t0, h.Peek().Expires) 137 138 // do ths fix 139 h.Fix(&Value{Key: "3", Expires: t3}) 140 141 assert.Equal(t3, h.Peek().Expires) 142 } 143 144 func TestLRUHeapRemove(t *testing.T) { 145 assert := assert.New(t) 146 147 t0 := time.Date(2019, 06, 13, 12, 10, 9, 8, time.UTC) 148 t1 := time.Date(2019, 06, 14, 12, 10, 9, 8, time.UTC) 149 t2 := time.Date(2019, 06, 15, 12, 10, 9, 8, time.UTC) 150 151 h := NewLRUHeap() 152 h.Push(&Value{Key: "1", Expires: t0}) 153 h.Push(&Value{Key: "2", Expires: t1}) 154 h.Push(&Value{Key: "3", Expires: t2}) 155 assert.Equal(t0, h.Peek().Expires) 156 157 h.Remove("1") 158 assert.Equal(t1, h.Peek().Expires) 159 }