-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
66 lines (58 loc) · 1.36 KB
/
utils_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
package golimiter
import (
"net"
"testing"
"time"
"github.com/Bose/minisentinel"
"github.com/alicebob/miniredis"
)
func testRedisPorts(t *testing.T) error {
const timeout = 50
conn, err := net.DialTimeout("tcp", "localhost:6379", 50*time.Millisecond)
if err != nil {
t.Log("Redis Connection error:", err)
return err
}
t.Log("Redis connection successful")
conn.Close()
conn2, err := net.DialTimeout("tcp", "localhost:26379", 50*time.Millisecond)
if err != nil {
t.Log("Sentinel Connection error:", err)
return err
}
t.Log("Sentinel connection successful")
conn2.Close()
return nil
}
type testRedis struct {
m *miniredis.Miniredis
s *minisentinel.Sentinel
}
func (t testRedis) Close() {
if t.m != nil {
t.m.Close()
}
if t.s != nil {
t.s.Close()
}
}
func initTestRedis(t *testing.T) (testRedis, error) {
if err := testRedisPorts(t); err == nil {
t.Log("Using Redis/Sentinel that's already running...")
return testRedis{}, err
}
m := miniredis.NewMiniRedis()
// m.RequireAuth("super-secret") // not required, but demonstrates how-to
err := m.StartAddr(":6379")
if err != nil {
return testRedis{}, err
}
s := minisentinel.NewSentinel(m, minisentinel.WithReplica(m))
err = s.StartAddr(":26379")
if err != nil {
m.Close()
return testRedis{}, err
}
t.Log("Using miniredis and minisentinel...")
return testRedis{m: m, s: s}, nil
}