| 1 |
package redis_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"path/filepath" |
| 10 |
"sync" |
| 11 |
"sync/atomic" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
. "github.com/onsi/ginkgo" |
| 16 |
. "github.com/onsi/gomega" |
| 17 |
|
| 18 |
"gopkg.in/redis.v5" |
| 19 |
) |
| 20 |
|
| 21 |
const ( |
| 22 |
redisPort = "6380" |
| 23 |
redisAddr = ":" + redisPort |
| 24 |
redisSecondaryPort = "6381" |
| 25 |
) |
| 26 |
|
| 27 |
const ( |
| 28 |
ringShard1Port = "6390" |
| 29 |
ringShard2Port = "6391" |
| 30 |
) |
| 31 |
|
| 32 |
const ( |
| 33 |
sentinelName = "mymaster" |
| 34 |
sentinelMasterPort = "8123" |
| 35 |
sentinelSlave1Port = "8124" |
| 36 |
sentinelSlave2Port = "8125" |
| 37 |
sentinelPort = "8126" |
| 38 |
) |
| 39 |
|
| 40 |
var ( |
| 41 |
redisMain *redisProcess |
| 42 |
ringShard1, ringShard2 *redisProcess |
| 43 |
sentinelMaster, sentinelSlave1, sentinelSlave2, sentinel *redisProcess |
| 44 |
) |
| 45 |
|
| 46 |
var cluster = &clusterScenario{ |
| 47 |
ports: []string{"8220", "8221", "8222", "8223", "8224", "8225"}, |
| 48 |
nodeIds: make([]string, 6), |
| 49 |
processes: make(map[string]*redisProcess, 6), |
| 50 |
clients: make(map[string]*redis.Client, 6), |
| 51 |
} |
| 52 |
|
| 53 |
var _ = BeforeSuite(func() { |
| 54 |
var err error |
| 55 |
|
| 56 |
redisMain, err = startRedis(redisPort) |
| 57 |
Expect(err).NotTo(HaveOccurred()) |
| 58 |
|
| 59 |
ringShard1, err = startRedis(ringShard1Port) |
| 60 |
Expect(err).NotTo(HaveOccurred()) |
| 61 |
|
| 62 |
ringShard2, err = startRedis(ringShard2Port) |
| 63 |
Expect(err).NotTo(HaveOccurred()) |
| 64 |
|
| 65 |
sentinelMaster, err = startRedis(sentinelMasterPort) |
| 66 |
Expect(err).NotTo(HaveOccurred()) |
| 67 |
|
| 68 |
sentinel, err = startSentinel(sentinelPort, sentinelName, sentinelMasterPort) |
| 69 |
Expect(err).NotTo(HaveOccurred()) |
| 70 |
|
| 71 |
sentinelSlave1, err = startRedis( |
| 72 |
sentinelSlave1Port, "--slaveof", "127.0.0.1", sentinelMasterPort) |
| 73 |
Expect(err).NotTo(HaveOccurred()) |
| 74 |
|
| 75 |
sentinelSlave2, err = startRedis( |
| 76 |
sentinelSlave2Port, "--slaveof", "127.0.0.1", sentinelMasterPort) |
| 77 |
Expect(err).NotTo(HaveOccurred()) |
| 78 |
|
| 79 |
Expect(startCluster(cluster)).NotTo(HaveOccurred()) |
| 80 |
}) |
| 81 |
|
| 82 |
var _ = AfterSuite(func() { |
| 83 |
Expect(redisMain.Close()).NotTo(HaveOccurred()) |
| 84 |
|
| 85 |
Expect(ringShard1.Close()).NotTo(HaveOccurred()) |
| 86 |
Expect(ringShard2.Close()).NotTo(HaveOccurred()) |
| 87 |
|
| 88 |
Expect(sentinel.Close()).NotTo(HaveOccurred()) |
| 89 |
Expect(sentinelSlave1.Close()).NotTo(HaveOccurred()) |
| 90 |
Expect(sentinelSlave2.Close()).NotTo(HaveOccurred()) |
| 91 |
Expect(sentinelMaster.Close()).NotTo(HaveOccurred()) |
| 92 |
|
| 93 |
Expect(stopCluster(cluster)).NotTo(HaveOccurred()) |
| 94 |
}) |
| 95 |
|
| •96 |
//func TestGinkgoSuite(t *testing.T) { |
| 97 |
// RegisterFailHandler(Fail) |
| 98 |
// RunSpecs(t, "gopkg.in/redis.v5") |
| 99 |
//} |
| 100 |
|
| 101 |
//------------------------------------------------------------------------------ |
| 102 |
|
| 103 |
func redisOptions() *redis.Options { |
| 104 |
return &redis.Options{ |
| 105 |
Addr: redisAddr, |
| 106 |
DB: 15, |
| 107 |
DialTimeout: 10 * time.Second, |
| 108 |
ReadTimeout: 30 * time.Second, |
| 109 |
WriteTimeout: 30 * time.Second, |
| 110 |
PoolSize: 10, |
| 111 |
PoolTimeout: 30 * time.Second, |
| 112 |
IdleTimeout: 500 * time.Millisecond, |
| 113 |
IdleCheckFrequency: 500 * time.Millisecond, |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func redisClusterOptions() *redis.ClusterOptions { |
| 118 |
return &redis.ClusterOptions{ |
| 119 |
DialTimeout: 10 * time.Second, |
| 120 |
ReadTimeout: 30 * time.Second, |
| 121 |
WriteTimeout: 30 * time.Second, |
| 122 |
PoolSize: 10, |
| 123 |
PoolTimeout: 30 * time.Second, |
| 124 |
IdleTimeout: 500 * time.Millisecond, |
| 125 |
IdleCheckFrequency: 500 * time.Millisecond, |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func redisRingOptions() *redis.RingOptions { |
| 130 |
return &redis.RingOptions{ |
| 131 |
Addrs: map[string]string{ |
| 132 |
"ringShardOne": ":" + ringShard1Port, |
| 133 |
"ringShardTwo": ":" + ringShard2Port, |
| 134 |
}, |
| 135 |
DialTimeout: 10 * time.Second, |
| 136 |
ReadTimeout: 30 * time.Second, |
| 137 |
WriteTimeout: 30 * time.Second, |
| 138 |
PoolSize: 10, |
| 139 |
PoolTimeout: 30 * time.Second, |
| 140 |
IdleTimeout: 500 * time.Millisecond, |
| 141 |
IdleCheckFrequency: 500 * time.Millisecond, |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func perform(n int, cbs ...func(int)) { |
| 146 |
var wg sync.WaitGroup |
| 147 |
for _, cb := range cbs { |
| 148 |
for i := 0; i < n; i++ { |
| 149 |
wg.Add(1) |
| 150 |
go func(cb func(int), i int) { |
| 151 |
defer GinkgoRecover() |
| 152 |
defer wg.Done() |
| 153 |
|
| 154 |
cb(i) |
| 155 |
}(cb, i) |
| 156 |
} |
| 157 |
} |
| 158 |
wg.Wait() |
| 159 |
} |
| 160 |
|
| 161 |
func eventually(fn func() error, timeout time.Duration) error { |
| 162 |
var exit int32 |
| 163 |
errCh := make(chan error) |
| 164 |
done := make(chan struct{}) |
| 165 |
|
| 166 |
go func() { |
| 167 |
defer GinkgoRecover() |
| 168 |
|
| 169 |
for atomic.LoadInt32(&exit) == 0 { |
| 170 |
err := fn() |
| 171 |
if err == nil { |
| 172 |
close(done) |
| 173 |
return |
| 174 |
} |
| 175 |
select { |
| 176 |
case errCh <- err: |
| 177 |
default: |
| 178 |
} |
| 179 |
time.Sleep(timeout / 100) |
| 180 |
} |
| 181 |
}() |
| 182 |
|
| 183 |
select { |
| 184 |
case <-done: |
| 185 |
return nil |
| 186 |
case <-time.After(timeout): |
| 187 |
atomic.StoreInt32(&exit, 1) |
| 188 |
select { |
| 189 |
case err := <-errCh: |
| 190 |
return err |
| 191 |
default: |
| 192 |
return fmt.Errorf("timeout after %s", timeout) |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func execCmd(name string, args ...string) (*os.Process, error) { |
| 198 |
cmd := exec.Command(name, args...) |
| 199 |
if testing.Verbose() { |
| 200 |
cmd.Stdout = os.Stdout |
| 201 |
cmd.Stderr = os.Stderr |
| 202 |
} |
| 203 |
return cmd.Process, cmd.Start() |
| 204 |
} |
| 205 |
|
| 206 |
func connectTo(port string) (*redis.Client, error) { |
| 207 |
client := redis.NewClient(&redis.Options{ |
| 208 |
Addr: ":" + port, |
| 209 |
}) |
| 210 |
|
| 211 |
err := eventually(func() error { |
| 212 |
return client.Ping().Err() |
| 213 |
}, 30*time.Second) |
| 214 |
if err != nil { |
| 215 |
return nil, err |
| 216 |
} |
| 217 |
|
| 218 |
return client, nil |
| 219 |
} |
| 220 |
|
| 221 |
type redisProcess struct { |
| 222 |
*os.Process |
| 223 |
*redis.Client |
| 224 |
} |
| 225 |
|
| 226 |
func (p *redisProcess) Close() error { |
| 227 |
if err := p.Kill(); err != nil { |
| 228 |
return err |
| 229 |
} |
| 230 |
|
| 231 |
err := eventually(func() error { |
| 232 |
if err := p.Client.Ping().Err(); err != nil { |
| 233 |
return nil |
| 234 |
} |
| 235 |
return errors.New("client is not shutdown") |
| 236 |
}, 10*time.Second) |
| 237 |
if err != nil { |
| 238 |
return err |
| 239 |
} |
| 240 |
|
| 241 |
p.Client.Close() |
| 242 |
return nil |
| 243 |
} |
| 244 |
|
| 245 |
var ( |
| 246 |
redisServerBin, _ = filepath.Abs(filepath.Join("testdata", "redis", "src", "redis-server")) |
| 247 |
redisServerConf, _ = filepath.Abs(filepath.Join("testdata", "redis.conf")) |
| 248 |
) |
| 249 |
|
| 250 |
func redisDir(port string) (string, error) { |
| 251 |
dir, err := filepath.Abs(filepath.Join("testdata", "instances", port)) |
| 252 |
if err != nil { |
| 253 |
return "", err |
| 254 |
} |
| 255 |
if err := os.RemoveAll(dir); err != nil { |
| 256 |
return "", err |
| 257 |
} |
| 258 |
if err := os.MkdirAll(dir, 0775); err != nil { |
| 259 |
return "", err |
| 260 |
} |
| 261 |
return dir, nil |
| 262 |
} |
| 263 |
|
| 264 |
func startRedis(port string, args ...string) (*redisProcess, error) { |
| 265 |
dir, err := redisDir(port) |
| 266 |
if err != nil { |
| 267 |
return nil, err |
| 268 |
} |
| 269 |
if err = exec.Command("cp", "-f", redisServerConf, dir).Run(); err != nil { |
| 270 |
return nil, err |
| 271 |
} |
| 272 |
|
| 273 |
baseArgs := []string{filepath.Join(dir, "redis.conf"), "--port", port, "--dir", dir} |
| 274 |
process, err := execCmd(redisServerBin, append(baseArgs, args...)...) |
| 275 |
if err != nil { |
| 276 |
return nil, err |
| 277 |
} |
| 278 |
|
| 279 |
client, err := connectTo(port) |
| 280 |
if err != nil { |
| 281 |
process.Kill() |
| 282 |
return nil, err |
| 283 |
} |
| 284 |
return &redisProcess{process, client}, err |
| 285 |
} |
| 286 |
|
| 287 |
func startSentinel(port, masterName, masterPort string) (*redisProcess, error) { |
| 288 |
dir, err := redisDir(port) |
| 289 |
if err != nil { |
| 290 |
return nil, err |
| 291 |
} |
| 292 |
process, err := execCmd(redisServerBin, os.DevNull, "--sentinel", "--port", port, "--dir", dir) |
| 293 |
if err != nil { |
| 294 |
return nil, err |
| 295 |
} |
| 296 |
client, err := connectTo(port) |
| 297 |
if err != nil { |
| 298 |
process.Kill() |
| 299 |
return nil, err |
| 300 |
} |
| 301 |
for _, cmd := range []*redis.StatusCmd{ |
| 302 |
redis.NewStatusCmd("SENTINEL", "MONITOR", masterName, "127.0.0.1", masterPort, "1"), |
| 303 |
redis.NewStatusCmd("SENTINEL", "SET", masterName, "down-after-milliseconds", "500"), |
| 304 |
redis.NewStatusCmd("SENTINEL", "SET", masterName, "failover-timeout", "1000"), |
| 305 |
redis.NewStatusCmd("SENTINEL", "SET", masterName, "parallel-syncs", "1"), |
| 306 |
} { |
| 307 |
client.Process(cmd) |
| 308 |
if err := cmd.Err(); err != nil { |
| 309 |
process.Kill() |
| 310 |
return nil, err |
| 311 |
} |
| 312 |
} |
| 313 |
return &redisProcess{process, client}, nil |
| 314 |
} |
| 315 |
|
| 316 |
//------------------------------------------------------------------------------ |
| 317 |
|
| 318 |
type badConnError string |
| 319 |
|
| 320 |
func (e badConnError) Error() string { return string(e) } |
| 321 |
func (e badConnError) Timeout() bool { return false } |
| 322 |
func (e badConnError) Temporary() bool { return false } |
| 323 |
|
| 324 |
type badConn struct { |
| 325 |
net.TCPConn |
| 326 |
|
| 327 |
readDelay, writeDelay time.Duration |
| 328 |
readErr, writeErr error |
| 329 |
} |
| 330 |
|
| 331 |
var _ net.Conn = &badConn{} |
| 332 |
|
| 333 |
func (cn *badConn) Read([]byte) (int, error) { |
| 334 |
if cn.readDelay != 0 { |
| 335 |
time.Sleep(cn.readDelay) |
| 336 |
} |
| 337 |
if cn.readErr != nil { |
| 338 |
return 0, cn.readErr |
| 339 |
} |
| 340 |
return 0, badConnError("bad connection") |
| 341 |
} |
| 342 |
|
| 343 |
func (cn *badConn) Write([]byte) (int, error) { |
| 344 |
if cn.writeDelay != 0 { |
| 345 |
time.Sleep(cn.writeDelay) |
| 346 |
} |
| 347 |
if cn.writeErr != nil { |
| 348 |
return 0, cn.writeErr |
| 349 |
} |
| 350 |
return 0, badConnError("bad connection") |
| 351 |
} |