Skip to content

Commit

Permalink
lru: add evict test
Browse files Browse the repository at this point in the history
Test evict cached keys in least-recently-used way.
  • Loading branch information
lorneli committed Oct 31, 2017
1 parent b710c84 commit 5c411b2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package lru

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -71,3 +72,26 @@ func TestRemove(t *testing.T) {
t.Fatal("TestRemove returned a removed entry")
}
}

func TestEvict(t *testing.T) {
evictedKeys := make([]Key, 0)
onEvictedFun := func(key Key, value interface{}) {
evictedKeys = append(evictedKeys, key)
}

lru := New(20)
lru.OnEvicted = onEvictedFun
for i := 0; i < 22; i++ {
lru.Add(fmt.Sprintf("myKey%d", i), 1234)
}

if len(evictedKeys) != 2 {
t.Fatalf("got %d evicted keys; want 2", len(evictedKeys))
}
if evictedKeys[0] != Key("myKey0") {
t.Fatalf("got %v in first evicted key; want %s", evictedKeys[0], "myKey0")
}
if evictedKeys[1] != Key("myKey1") {
t.Fatalf("got %v in second evicted key; want %s", evictedKeys[1], "myKey1")
}
}

0 comments on commit 5c411b2

Please sign in to comment.