-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshard_test.go
108 lines (91 loc) · 1.92 KB
/
shard_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
package geche
import (
"fmt"
"strconv"
"sync"
"testing"
)
func ExampleNewSharded() {
numShards := 4
c := NewSharded[int](
func() Geche[int, string] {
return NewMapCache[int, string]()
},
numShards,
&NumberMapper[int]{},
)
// Set 4000 records in 4 parallel goroutines.
wg := sync.WaitGroup{}
wg.Add(numShards)
for i := 0; i < numShards; i++ {
go func(i int) {
defer wg.Done()
for j := i * 1000; j < i*1000+1000; j++ {
c.Set(j, strconv.Itoa(j))
}
}(i)
}
wg.Wait()
for i := 0; i < 10; i++ {
v, _ := c.Get(i*1000 + 500)
fmt.Println(v)
}
// Output: 500
// 1500
// 2500
// 3500
}
func TestSharded(t *testing.T) {
c := NewSharded[int](
func() Geche[int, string] {
return NewMapCache[int, string]()
},
4,
&NumberMapper[int]{},
)
for i := 0; i < 1000; i++ {
c.Set(i, strconv.Itoa(i))
}
for i := 0; i < 1000; i++ {
v, err := c.Get(i)
if err != nil {
t.Fatalf("unexpected error in Get: %v", err)
}
if v != strconv.Itoa(i) {
t.Fatalf("key %d with unexpected value %q", i, v)
}
}
for i := 0; i < 1000; i++ {
old, inserted := c.SetIfPresent(i, "modified")
if !inserted {
t.Fatalf("SetIfPresent returned inserted=false for existing key %d", i)
}
if old != strconv.Itoa(i) {
t.Fatalf("SetIfPresent returned unexpected old value, expected=%d, got=%s", i, old)
}
}
for i := 1000; i < 2000; i++ {
_, inserted := c.SetIfPresent(i, "modified")
if inserted {
t.Fatalf("SetIfPresent returned inserted=true for non-existing key %d", i)
}
}
for i := 0; i < 1000; i++ {
v, err := c.Get(i)
if err != nil {
t.Fatalf("unexpected error in Get: %v", err)
}
if v != "modified" {
t.Fatalf("key %d with unexpected value %q", i, v)
}
}
for i := 1000; i < 2000; i++ {
_, err := c.Get(i)
if err == nil {
t.Fatalf("expected error in Get")
}
}
if len(c.shards) != 4 {
t.Errorf("expected number of shards to be 4 but got %d", len(c.shards))
}
}