forked from bluele/gcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
50 lines (44 loc) · 912 Bytes
/
cache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package gcache_test
import (
"testing"
"time"
"sync"
"sync/atomic"
"github.com/bluele/gcache"
)
func TestLoaderFunc(t *testing.T) {
size := 2
var testCaches = []*gcache.CacheBuilder{
gcache.New(size).Simple(),
gcache.New(size).LRU(),
gcache.New(size).LFU(),
gcache.New(size).ARC(),
}
for _, builder := range testCaches {
var testCounter int64
counter := 1000
cache := builder.
LoaderFunc(func(key interface{}) (interface{}, error) {
time.Sleep(10 * time.Millisecond)
return atomic.AddInt64(&testCounter, 1), nil
}).
EvictedFunc(func(key, value interface{}) {
panic(key)
}).Build()
var wg sync.WaitGroup
for i := 0; i < counter; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := cache.Get(0)
if err != nil {
t.Error(err)
}
}()
}
wg.Wait()
if testCounter != 1 {
t.Errorf("testCounter != %v", testCounter)
}
}
}