-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
59 lines (50 loc) · 1.09 KB
/
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
51
52
53
54
55
56
57
58
59
// Copyright (c) 2022 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package cache_test
import (
"fmt"
"sync"
"testing"
"time"
"github.com/tunabay/go-cache"
)
func TestGet_1(t *testing.T) {
cfunc := func(key string) (string, time.Time, error) {
time.Sleep(time.Second >> 2) // slow creation
if key == "KEY-0" {
return "", time.Time{}, fmt.Errorf("test error")
}
return fmt.Sprintf("VALUE(%s)", key), time.Now().Add(time.Second >> 2), nil
}
c := cache.New[string, string](cfunc)
lookup := func(keyNo int) {
key := fmt.Sprintf("KEY-%d", keyNo%4)
val, cached, _, err := c.Get(key)
var tag string
if cached {
tag = " (cached)"
}
if err != nil {
t.Logf("%q -> ERROR: %v%s", key, err, tag)
return
}
t.Logf("%q -> %q%s", key, val, tag)
}
var wg sync.WaitGroup
for i := 0; i < 12; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
lookup(n % 4)
}(i)
}
wg.Wait()
c.CheckAndExpire()
lookup(0)
lookup(1)
lookup(2)
lookup(3)
lookup(3)
c.CheckAndExpire()
}