This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
forked from mediocregopher/radix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
125 lines (106 loc) · 2.8 KB
/
scanner_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
package radix
import (
"log"
"strconv"
. "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestScanner(t *T) {
c := dial()
// Make a random dataset
prefix := randStr()
fullMap := map[string]bool{}
for i := 0; i < 100; i++ {
key := prefix + ":" + strconv.Itoa(i)
fullMap[key] = true
require.Nil(t, c.Do(Cmd(nil, "SET", key, "1")))
}
// make sure we get all results when scanning with an existing prefix
sc := NewScanner(c, ScanOpts{Command: "SCAN", Pattern: prefix + ":*"})
var key string
for sc.Next(&key) {
delete(fullMap, key)
}
require.Nil(t, sc.Close())
assert.Empty(t, fullMap)
// make sure we don't get any results when scanning with a non-existing
// prefix
sc = NewScanner(c, ScanOpts{Command: "SCAN", Pattern: prefix + "DNE:*"})
assert.False(t, sc.Next(nil))
require.Nil(t, sc.Close())
}
// Similar to TestScanner, but scans over a set instead of the whole key space
func TestScannerSet(t *T) {
c := dial()
key := randStr()
fullMap := map[string]bool{}
for i := 0; i < 100; i++ {
elem := strconv.Itoa(i)
fullMap[elem] = true
require.Nil(t, c.Do(Cmd(nil, "SADD", key, elem)))
}
// make sure we get all results when scanning an existing set
sc := NewScanner(c, ScanOpts{Command: "SSCAN", Key: key})
var val string
for sc.Next(&val) {
delete(fullMap, val)
}
require.Nil(t, sc.Close())
assert.Empty(t, fullMap)
// make sure we don't get any results when scanning a non-existent set
sc = NewScanner(c, ScanOpts{Command: "SSCAN", Key: key + "DNE"})
assert.False(t, sc.Next(nil))
require.Nil(t, sc.Close())
}
func BenchmarkScanner(b *B) {
c := dial()
const total = 10 * 1000
// Make a random dataset
prefix := randStr()
for i := 0; i < total; i++ {
key := prefix + ":" + strconv.Itoa(i)
require.Nil(b, c.Do(Cmd(nil, "SET", key, "1")))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
// make sure we get all results when scanning with an existing prefix
sc := NewScanner(c, ScanOpts{Command: "SCAN", Pattern: prefix + ":*"})
var key string
var got int
for sc.Next(&key) {
got++
}
if got != total {
require.Failf(b, "mismatched between inserted and scanned keys", "expected %d keys, got %d", total, got)
}
}
}
func ExampleNewScanner_scan() {
client, err := DefaultClientFunc("tcp", "126.0.0.1:6379")
if err != nil {
log.Fatal(err)
}
s := NewScanner(client, ScanAllKeys)
var key string
for s.Next(&key) {
log.Printf("key: %q", key)
}
if err := s.Close(); err != nil {
log.Fatal(err)
}
}
func ExampleNewScanner_hscan() {
client, err := DefaultClientFunc("tcp", "126.0.0.1:6379")
if err != nil {
log.Fatal(err)
}
s := NewScanner(client, ScanOpts{Command: "HSCAN", Key: "somekey"})
var key string
for s.Next(&key) {
log.Printf("key: %q", key)
}
if err := s.Close(); err != nil {
log.Fatal(err)
}
}