sm
/
cache
1
0
Fork 0
This commit is contained in:
Christoph Petrausch 2017-03-29 08:08:40 +00:00 committed by GitHub
commit d07c004c68
2 changed files with 40 additions and 0 deletions

View File

@ -6,3 +6,4 @@ code was contributed.)
Dustin Sallings <dustin@spy.net>
Jason Mooberry <jasonmoo@me.com>
Sergey Shepelev <temotor@gmail.com>
Christoph Petrausch <chrobbert@gmail.com>

View File

@ -8,6 +8,7 @@ import (
"sync"
"testing"
"time"
"fmt"
)
type TestStruct struct {
@ -1247,6 +1248,29 @@ func TestOnEvicted(t *testing.T) {
}
}
func TestCacheGetAllNotExpiredItems(t *testing.T) {
tc := New(time.Minute*1, 0)
tc.Set("a", "a", DefaultExpiration)
tc.Set("b", "b", DefaultExpiration)
tc.Set("c", "c", time.Millisecond*1)
time.Sleep(time.Millisecond*2)
allNotExpiredItems := tc.Items()
if len(allNotExpiredItems) != 2 {
t.Error("There are more or less items in the result than the two unexpired.")
}
for _, key := range []string{"a", "b"} {
if _, ok := tc.Get(key); !ok{
t.Error("Could not find unexpired item %s", key)
}
}
if _, ok := tc.Get("c"); ok {
t.Error("Found expired item c.")
}
if &allNotExpiredItems == &tc.cache.items {
t.Error("Returned map is equal to internal map.")
}
}
func TestCacheSerialization(t *testing.T) {
tc := New(DefaultExpiration, 0)
testFillAndSerialize(t, tc)
@ -1676,3 +1700,18 @@ func BenchmarkDeleteExpiredLoop(b *testing.B) {
tc.DeleteExpired()
}
}
func BenchmarkGetAllNotExpiredItems(b *testing.B) {
for i:= 0; i < 20; i++ {
b.Run(fmt.Sprintf("BenchmarkGetAllNotExpiredItemsWith %d000 Items", i), func(b *testing.B){
tc := New(20*time.Minute, 0)
for j:= 0; j < i*1000; j++{
tc.Set(strconv.Itoa(i), "bar", DefaultExpiration)
}
b.ResetTimer()
for j:= 0; j < b.N; j++{
tc.Items()
}
})
}
}