Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -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>
39 changes: 39 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"testing"
"time"
"fmt"
)

type TestStruct struct {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
})
}
}