This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
/
cluster_client_test.go
133 lines (112 loc) · 2.98 KB
/
cluster_client_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
126
127
128
129
130
131
132
133
package main
import (
"errors"
"fmt"
"sync"
"testing"
"github.com/garyburd/redigo/redis"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func NewMockRedisPool() *MockRedisPool {
pool := &MockRedisPool{}
return pool
}
func TestChooseFewestConns(t *testing.T) {
pools := make(map[string]Pool)
mockPool := NewMockRedisPool()
mockPool.Active = 10
pools["host1:6379"] = mockPool
assert.Equal(t, mockPool, chooseFewestConns(pools), "chose the correct pool")
mockPool2 := NewMockRedisPool()
mockPool2.Active = 1
pools["host2:6379"] = mockPool2
mockPool3 := NewMockRedisPool()
mockPool3.Active = 0
pools["host3:6379"] = mockPool3
assert.Equal(t, mockPool3, chooseFewestConns(pools), "chose the correct pool")
}
func TestParseRedirection(t *testing.T) {
errType := "MOVED"
slot := uint16(123)
host := "server:6379"
redir := fmt.Sprintf("%s %d %s", errType, slot, host)
redirErr, err := _parseRedirection(redir)
assert.Nil(t, err)
assert.Equal(t, movedError, redirErr.errType, "redir errType matches")
assert.Equal(t, slot, redirErr.slot, "redir slot matches")
assert.Equal(t, host, redirErr.host, "redir host matches")
}
func TestParseIfRedir(t *testing.T) {
errType := "MOVED"
slot := uint16(123)
host := "server:6379"
redir := errors.New(fmt.Sprintf("%s %d %s", errType, slot, host))
redirErr, isRedir := parseIfRedir(redir)
assert.IsType(t, redirectionError{}, redirErr)
assert.True(t, isRedir)
nonRedir := errors.New("TOFU 123")
redirErr, isRedir = parseIfRedir(nonRedir)
assert.IsType(t, redirectionError{}, redirErr)
assert.Zero(t, redirErr.errType, "empty type returned")
assert.Zero(t, redirErr.slot, "empty slot returned")
assert.False(t, isRedir)
}
type MockConn struct {
mock.Mock
Mutex sync.Mutex
}
func (m *MockConn) Do(cmd string, cmdArgs ...interface{}) (interface{}, error) {
m.Mutex.Lock()
defer m.Mutex.Unlock()
var args mock.Arguments
if len(cmdArgs) == 0 {
args = m.Mock.Called(cmd)
} else {
args = m.Mock.Called(cmd, cmdArgs)
}
return args.Get(0), args.Error(1)
}
func (m *MockConn) Send(cmd string, cmdArgs ...interface{}) error {
m.Mutex.Lock()
defer m.Mutex.Unlock()
if len(cmdArgs) == 0 {
return m.Mock.Called(cmd).Error(0)
}
return m.Mock.Called(cmd, cmdArgs).Error(0)
}
func (m *MockConn) Receive() (interface{}, error) {
m.Mutex.Lock()
defer m.Mutex.Unlock()
args := m.Mock.Called()
return args.Get(0), args.Error(1)
}
func (m *MockConn) Flush() error {
m.Mutex.Lock()
defer m.Mutex.Unlock()
return m.Mock.Called().Error(0)
}
func (m *MockConn) Err() error {
m.Mutex.Lock()
defer m.Mutex.Unlock()
return m.Mock.Called().Error(0)
}
func (m *MockConn) Close() error {
m.Mutex.Lock()
defer m.Mutex.Unlock()
return m.Mock.Called().Error(0)
}
type MockRedisPool struct {
mock.Mock
Conn *MockConn
Active int
}
func (m *MockRedisPool) Get() redis.Conn {
return m.Conn
}
func (m *MockRedisPool) ActiveCount() int {
return m.Active
}
func (m *MockRedisPool) Close() error {
return m.Mock.Called().Error(0)
}