Skip to content

Commit 84a468c

Browse files
authored
Merge pull request #90 from lorneli/lru-dev
lru: add evict test
2 parents b710c84 + 5c411b2 commit 84a468c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lru/lru_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package lru
1818

1919
import (
20+
"fmt"
2021
"testing"
2122
)
2223

@@ -71,3 +72,26 @@ func TestRemove(t *testing.T) {
7172
t.Fatal("TestRemove returned a removed entry")
7273
}
7374
}
75+
76+
func TestEvict(t *testing.T) {
77+
evictedKeys := make([]Key, 0)
78+
onEvictedFun := func(key Key, value interface{}) {
79+
evictedKeys = append(evictedKeys, key)
80+
}
81+
82+
lru := New(20)
83+
lru.OnEvicted = onEvictedFun
84+
for i := 0; i < 22; i++ {
85+
lru.Add(fmt.Sprintf("myKey%d", i), 1234)
86+
}
87+
88+
if len(evictedKeys) != 2 {
89+
t.Fatalf("got %d evicted keys; want 2", len(evictedKeys))
90+
}
91+
if evictedKeys[0] != Key("myKey0") {
92+
t.Fatalf("got %v in first evicted key; want %s", evictedKeys[0], "myKey0")
93+
}
94+
if evictedKeys[1] != Key("myKey1") {
95+
t.Fatalf("got %v in second evicted key; want %s", evictedKeys[1], "myKey1")
96+
}
97+
}

0 commit comments

Comments
 (0)