-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompose_test.go
126 lines (103 loc) · 2.76 KB
/
compose_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cachestore_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/goware/cachestore"
"github.com/goware/cachestore/memlru"
"github.com/stretchr/testify/require"
)
func TestCompose(t *testing.T) {
n := 20
m1s, err := memlru.NewWithSize[string](n, cachestore.WithDefaultKeyExpiry(6*time.Second))
require.NoError(t, err)
m2s, err := memlru.NewWithSize[string](n, cachestore.WithDefaultKeyExpiry(9*time.Second))
require.NoError(t, err)
cs, err := cachestore.Compose(m1s, m2s)
require.NoError(t, err)
ctx := context.Background()
for i := 0; i < n; i++ {
err := m1s.Set(ctx, fmt.Sprintf("foo:%d", i), fmt.Sprintf("a%d", i))
require.NoError(t, err)
}
for i := 0; i < n; i++ {
err := cs.Set(ctx, fmt.Sprintf("foo:%d", i), fmt.Sprintf("b%d", i))
require.NoError(t, err)
}
// Get some keys
{
key := "foo:10"
val, ok, err := m1s.Get(ctx, key)
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, "b10", val)
val, ok, err = m2s.Get(ctx, key)
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, "b10", val)
}
// Overwrite random keys
{
m1s.Set(ctx, "foo:8", "c8")
m2s.Set(ctx, "foo:8", "d8")
m2s.Set(ctx, "foo:7", "d7")
m2s.Set(ctx, "foo:6", "d6")
cs.Set(ctx, "foo:6", "c6")
val, ok, err := cs.Get(ctx, "foo:8")
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, "c8", val)
val, ok, err = cs.Get(ctx, "foo:7")
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, "b7", val)
val, ok, err = cs.Get(ctx, "foo:6")
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, "c6", val)
}
// Batch get, etc.
{
// first lets normalize all the values again
for i := 0; i < n; i++ {
err := cs.Set(ctx, fmt.Sprintf("foo:%d", i), fmt.Sprintf("z%d", i))
require.NoError(t, err)
}
// then lets delete some random keys in both sets
// so when we query we should get all values
m1s.Delete(ctx, "foo:2")
m1s.Delete(ctx, "foo:5")
m1s.Delete(ctx, "foo:8")
m2s.Delete(ctx, "foo:3")
m2s.Delete(ctx, "foo:4")
m2s.Delete(ctx, "foo:10")
vals, exists, err := cs.BatchGet(ctx, []string{
"foo:0", "foo:1", "foo:2", "foo:3", "foo:4", "foo:5", "foo:6", "foo:7", "foo:8", "foo:9", "foo:10",
})
require.NoError(t, err)
for _, e := range exists {
require.True(t, e)
}
for i, v := range vals {
require.NotEmpty(t, v)
require.Equal(t, fmt.Sprintf("z%d", i), v)
}
}
// Wait for expiry..
time.Sleep(7 * time.Second)
{
val, ok, err := m1s.Get(ctx, "foo:1")
require.NoError(t, err)
require.False(t, ok)
require.Empty(t, val)
val, ok, err = m2s.Get(ctx, "foo:1")
require.NoError(t, err)
require.True(t, ok)
require.NotEmpty(t, val)
val, ok, err = cs.Get(ctx, "foo:1")
require.NoError(t, err)
require.True(t, ok)
require.NotEmpty(t, val)
}
}