-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcache_ext_test.go
48 lines (43 loc) · 1.36 KB
/
hcache_ext_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
package hcache
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHnlq715LRUCache(t *testing.T) {
hc, err := NewHnlq715LRUCache(5, time.Second, Config{HandleTimeoutWindow: time.Second, HandleConcurrent: 100})
assert.True(t, err == nil, err)
o, err := hc.GetOrSetWithHandle(context.Background(), "test1", func(ctx context.Context) (interface{}, error) {
return 1, nil
})
assert.True(t, err == nil, err)
assert.True(t, o.(int) == 1, o)
for i := 0; i < 6; i++ {
key := fmt.Sprintf("key%d", i)
hc.GetOrSetWithHandle(context.Background(), key, func(ctx context.Context) (interface{}, error) {
return i, nil
})
}
assert.True(t, hc.Contains("key0") == false, hc.Contains("key0"))
inHandle := false
o, err = hc.GetOrSetWithHandle(context.Background(), "key0", func(ctx context.Context) (interface{}, error) {
inHandle = true
return 8, nil
})
assert.True(t, inHandle, inHandle)
assert.True(t, err == nil, err)
assert.True(t, o.(int) == 8, o)
tick := time.NewTicker(time.Second)
<-tick.C
assert.True(t, hc.Contains("key0") == false, hc.Contains("key0"))
inHandle = false
o, err = hc.GetOrSetWithHandle(context.Background(), "key0", func(ctx context.Context) (interface{}, error) {
inHandle = true
return 9, nil
})
assert.True(t, inHandle, inHandle)
assert.True(t, err == nil, err)
assert.True(t, o.(int) == 9, o)
}