-
Notifications
You must be signed in to change notification settings - Fork 12
/
pool_test.go
96 lines (92 loc) · 2.72 KB
/
pool_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
package redis_bloom_go
import (
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewMultiHostPool(t *testing.T) {
type args struct {
hosts []string
authPass *string
}
tests := []struct {
name string
args args
wantPoolSize int
wantConntNil bool
}{
{"same connection string", args{[]string{"localhost:6379", "localhost:6379"}, nil}, 2, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got *MultiHostPool
got = NewMultiHostPool(tt.args.hosts, tt.args.authPass)
if len(got.hosts) != tt.wantPoolSize {
t.Errorf("NewMultiHostPool() = %v, want %v", got, tt.wantPoolSize)
}
if gotConn := got.Get(); tt.wantConntNil == false && gotConn == nil {
t.Errorf("NewMultiHostPool().Get() = %v, want %v", gotConn, tt.wantConntNil)
}
})
}
}
func TestMultiHostPool_Close(t *testing.T) {
host, password := getTestConnectionDetails()
// Test a simple flow
if password == "" {
oneMulti := NewMultiHostPool([]string{host}, nil)
conn := oneMulti.Get()
assert.NotNil(t, conn)
err := oneMulti.Close()
assert.Nil(t, err)
err = oneMulti.Close()
assert.NotNil(t, conn)
severalMulti := NewMultiHostPool([]string{host, host}, nil)
connMulti := severalMulti.Get()
assert.NotNil(t, connMulti)
err = severalMulti.Close()
assert.Nil(t, err)
}
// Exhaustive test
dial := func() (redis.Conn, error) {
return redis.Dial("tcp", host, redis.DialPassword(password))
}
pool1 := &redis.Pool{Dial: dial, MaxIdle: maxConns}
pool2 := &redis.Pool{Dial: dial, MaxIdle: maxConns}
pool3 := &redis.Pool{Dial: dial, MaxIdle: maxConns}
//Close pull3 prior to enforce error
pool3.Close()
pool4 := &redis.Pool{Dial: dial, MaxIdle: maxConns}
type fields struct {
pools map[string]*redis.Pool
hosts []string
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{"empty", fields{map[string]*redis.Pool{}, []string{}}, false},
{"normal", fields{map[string]*redis.Pool{"hostpool1": pool1}, []string{"hostpool1"}}, false},
{"pool3-already-close", fields{map[string]*redis.Pool{"hostpool2": pool2, "hostpool3": pool3, "hostpool4": pool4}, []string{"hostpool2", "hostpool3", "hostpool3"}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &MultiHostPool{
pools: tt.fields.pools,
hosts: tt.fields.hosts,
}
if err := p.Close(); (err != nil) != tt.wantErr {
t.Errorf("Close() error = %v, wantErr %v", err, tt.wantErr)
}
// ensure all connections are really closed
if !tt.wantErr {
for _, pool := range p.pools {
if _, err := pool.Get().Do("PING"); err == nil {
t.Errorf("expected error after connection closed. Got %v", err)
}
}
}
})
}
}