-
Notifications
You must be signed in to change notification settings - Fork 7
/
sampler_test.go
94 lines (79 loc) · 1.57 KB
/
sampler_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
package main
import (
"os"
"strconv"
"testing"
"fmt"
"github.com/go-redis/redis"
)
var client = redis.NewClient(&redis.Options{Addr: "localhost:6379"})
func TestSampleArguments(t *testing.T) {
cases := []struct {
redisUrl string
samples int
results int
err string
}{
{
"redis://localhost",
0,
10,
"number of samples must be > 0",
},
{
"redis://localhost",
10,
0,
"number of results must be > 0",
},
{
"localhost",
1,
1,
"invalid redis URL scheme: ",
},
}
for i, c := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
_, err := Sample(c.redisUrl, c.samples, c.results)
if err.Error() != c.err {
t.Errorf("expected: %#v\nresult: %#v", c.err, err)
}
})
}
}
func TestSample(t *testing.T) {
output, err := Sample("redis://localhost:6379", 1, 1)
if err == nil {
t.Errorf("expected an error but got: %#v", output)
}
err = client.Set("key", "value", 0).Err()
if err != nil {
t.Errorf("SET: error: %#v\n", err)
}
output, err = Sample("redis://localhost:6379", 10, 1)
if err != nil {
t.Errorf("unexpected error: %#v", err)
}
if output != "key: 100.00% (10)" {
t.Errorf("unexpected output: %#v", output)
}
}
func TestMain(m *testing.M) {
dbSize, err := client.DBSize().Result()
if err != nil {
fmt.Printf("DBSIZE: error: %#v\n", err)
os.Exit(1)
}
if dbSize > 0 {
fmt.Println("redis database is not empty")
os.Exit(1)
}
returnCode := m.Run()
err = client.FlushDB().Err()
if err != nil {
fmt.Printf("FLUSHDB: error: %#v\n", err)
os.Exit(1)
}
os.Exit(returnCode)
}