Merge pull request #90 from lorneli/lru-dev

lru: add evict test
This commit is contained in:
Brad Fitzpatrick 2017-11-01 15:31:31 -05:00 committed by GitHub
commit 84a468cf14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,7 @@ limitations under the License.
package lru package lru
import ( import (
"fmt"
"testing" "testing"
) )
@ -71,3 +72,26 @@ func TestRemove(t *testing.T) {
t.Fatal("TestRemove returned a removed entry") 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")
}
}