github.com/blend/go-sdk@v1.20220411.3/cache/lru_queue_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 "fmt" 12 "strconv" 13 "testing" 14 "time" 15 16 "github.com/blend/go-sdk/assert" 17 ) 18 19 func tv(index int) *Value { 20 return &Value{ 21 Key: index, 22 Value: strconv.Itoa(index), 23 Timestamp: time.Now().UTC(), 24 Expires: time.Date(2019, 06, 14, 12, index, 0, 0, time.UTC), 25 } 26 } 27 28 func TestLRUQueue(t *testing.T) { 29 assert := assert.New(t) 30 31 buffer := NewLRUQueue() 32 33 buffer.Push(tv(1)) 34 assert.Equal(1, buffer.Len()) 35 assert.Equal(1, buffer.Peek().Key) 36 assert.Equal(1, buffer.PeekBack().Key) 37 38 buffer.Push(tv(2)) 39 assert.Equal(2, buffer.Len()) 40 assert.Equal(1, buffer.Peek().Key) 41 assert.Equal(2, buffer.PeekBack().Key) 42 43 buffer.Push(tv(3)) 44 assert.Equal(3, buffer.Len()) 45 assert.Equal(1, buffer.Peek().Key) 46 assert.Equal(3, buffer.PeekBack().Key) 47 48 buffer.Push(tv(4)) 49 assert.Equal(4, buffer.Len()) 50 assert.Equal(1, buffer.Peek().Key) 51 assert.Equal(4, buffer.PeekBack().Key) 52 53 buffer.Push(tv(5)) 54 assert.Equal(5, buffer.Len()) 55 assert.Equal(1, buffer.Peek().Key) 56 assert.Equal(5, buffer.PeekBack().Key) 57 58 buffer.Push(tv(6)) 59 assert.Equal(6, buffer.Len()) 60 assert.Equal(1, buffer.Peek().Key) 61 assert.Equal(6, buffer.PeekBack().Key) 62 63 buffer.Push(tv(7)) 64 assert.Equal(7, buffer.Len()) 65 assert.Equal(1, buffer.Peek().Key) 66 assert.Equal(7, buffer.PeekBack().Key) 67 68 buffer.Push(tv(8)) 69 assert.Equal(8, buffer.Len()) 70 assert.Equal(1, buffer.Peek().Key) 71 assert.Equal(8, buffer.PeekBack().Key) 72 73 value := buffer.Pop() 74 assert.Equal(1, value.Key) 75 assert.Equal(7, buffer.Len()) 76 assert.Equal(2, buffer.Peek().Key) 77 assert.Equal(8, buffer.PeekBack().Key) 78 79 value = buffer.Pop() 80 assert.Equal(2, value.Key) 81 assert.Equal(6, buffer.Len()) 82 assert.Equal(3, buffer.Peek().Key) 83 assert.Equal(8, buffer.PeekBack().Key) 84 85 value = buffer.Pop() 86 assert.Equal(3, value.Key) 87 assert.Equal(5, buffer.Len()) 88 assert.Equal(4, buffer.Peek().Key) 89 assert.Equal(8, buffer.PeekBack().Key) 90 91 value = buffer.Pop() 92 assert.Equal(4, value.Key) 93 assert.Equal(4, buffer.Len()) 94 assert.Equal(5, buffer.Peek().Key) 95 assert.Equal(8, buffer.PeekBack().Key) 96 97 value = buffer.Pop() 98 assert.Equal(5, value.Key) 99 assert.Equal(3, buffer.Len()) 100 assert.Equal(6, buffer.Peek().Key) 101 assert.Equal(8, buffer.PeekBack().Key) 102 103 value = buffer.Pop() 104 assert.Equal(6, value.Key) 105 assert.Equal(2, buffer.Len()) 106 assert.Equal(7, buffer.Peek().Key) 107 assert.Equal(8, buffer.PeekBack().Key) 108 109 value = buffer.Pop() 110 assert.Equal(7, value.Key) 111 assert.Equal(1, buffer.Len()) 112 assert.Equal(8, buffer.Peek().Key) 113 assert.Equal(8, buffer.PeekBack().Key) 114 115 value = buffer.Pop() 116 assert.Equal(8, value.Key) 117 assert.Equal(0, buffer.Len()) 118 assert.Nil(buffer.Peek()) 119 assert.Nil(buffer.PeekBack()) 120 121 before := buffer.Capacity() 122 buffer.TrimExcess() 123 after := buffer.Capacity() 124 assert.True(before > after) 125 126 } 127 128 func TestLRUQueueClear(t *testing.T) { 129 assert := assert.New(t) 130 131 buffer := NewLRUQueue() 132 for x := 0; x < 8; x++ { 133 buffer.Push(tv(x)) 134 } 135 assert.Equal(8, buffer.Len()) 136 buffer.Clear() 137 assert.Equal(0, buffer.Len()) 138 assert.Nil(buffer.Peek()) 139 assert.Nil(buffer.PeekBack()) 140 } 141 142 func TestLRUQueueFix(t *testing.T) { 143 assert := assert.New(t) 144 145 buffer := NewLRUQueue() 146 for x := 0; x < 8; x++ { 147 buffer.Push(tv(x)) 148 } 149 150 tv4e := time.Date(2018, 06, 14, 12, 4, 0, 0, time.UTC) 151 tv4 := tv(4) 152 tv4.Expires = tv4e 153 154 // fix tv4 to have the earliest expires 155 buffer.Fix(tv4) 156 157 // it should now be the head of the queue 158 head := buffer.Peek() 159 assert.Equal(tv4e, head.Expires, fmt.Sprintf("Head is actually %v", head.Key)) 160 161 tv6e := time.Date(2017, 06, 14, 12, 4, 0, 0, time.UTC) 162 tv6 := tv(6) 163 tv6.Expires = tv6e 164 165 buffer.Fix(tv6) 166 167 head = buffer.Peek() 168 assert.Equal(tv6e, head.Expires) 169 } 170 171 func TestLRUQueueRemove(t *testing.T) { 172 assert := assert.New(t) 173 174 buffer := NewLRUQueue() 175 for x := 0; x < 8; x++ { 176 buffer.Push(tv(x)) 177 } 178 179 buffer.Remove(4) 180 181 var didFind bool 182 buffer.Each(func(v *Value) bool { 183 if v.Key == 4 { 184 didFind = true 185 return false 186 } 187 return true 188 }) 189 assert.False(didFind) 190 191 assert.Equal(tv(0).Expires, buffer.Peek().Expires) 192 assert.Equal(tv(7).Expires, buffer.PeekBack().Expires) 193 } 194 195 func TestLRUQueueConsume(t *testing.T) { 196 assert := assert.New(t) 197 198 buffer := NewLRUQueue() 199 200 for x := 0; x < 20; x++ { 201 buffer.Push(tv(x)) 202 } 203 204 var called int 205 buffer.Consume(func(v *Value) bool { 206 // stop after 10 207 if v.Key.(int) > 10 { 208 return false 209 } 210 211 if v.Key.(int) == called { 212 called++ 213 } 214 return true 215 }) 216 217 assert.Equal(10, called) 218 }