-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose_test.go
More file actions
100 lines (82 loc) · 2.1 KB
/
compose_test.go
File metadata and controls
100 lines (82 loc) · 2.1 KB
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
package cachestore_test
import (
"context"
"fmt"
"testing"
cachestore "github.com/goware/cachestore2"
)
func TestCompose(t *testing.T) {
n := 20
m1s := NewMockStore[string]()
m2s := NewMockStore[string]()
cs, err := cachestore.ComposeStores(m1s, m2s)
assertNoError(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))
assertNoError(t, err)
}
for i := 0; i < n; i++ {
err := cs.Set(ctx, fmt.Sprintf("foo:%d", i), fmt.Sprintf("b%d", i))
assertNoError(t, err)
}
// Get some keys
{
key := "foo:10"
val, ok, err := m1s.Get(ctx, key)
assertNoError(t, err)
assertTrue(t, ok)
assertEqual(t, "b10", val)
val, ok, err = m2s.Get(ctx, key)
assertNoError(t, err)
assertTrue(t, ok)
assertEqual(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")
assertNoError(t, err)
assertTrue(t, ok)
assertEqual(t, "c8", val)
val, ok, err = cs.Get(ctx, "foo:7")
assertNoError(t, err)
assertTrue(t, ok)
assertEqual(t, "b7", val)
val, ok, err = cs.Get(ctx, "foo:6")
assertNoError(t, err)
assertTrue(t, ok)
assertEqual(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))
assertNoError(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",
})
assertNoError(t, err)
for _, e := range exists {
assertTrue(t, e)
}
for i, v := range vals {
assertTrue(t, len(v) > 0)
assertEqual(t, fmt.Sprintf("z%d", i), v)
}
}
}