lru: add evict test

Test evict cached keys in least-recently-used way.
This commit is contained in:
lorneli 2017-10-31 22:00:35 +08:00
parent b710c8433b
commit 5c411b28ac

View File

@ -17,6 +17,7 @@ limitations under the License.
package lru
import (
"fmt"
"testing"
)
@ -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")
}
}