forked from go-redsync/redsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redsync_test.go
149 lines (127 loc) · 3.18 KB
/
redsync_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package redsync
import (
"os"
"testing"
"time"
goredislib "github.com/go-redis/redis"
goredislib_v7 "github.com/go-redis/redis/v7"
goredislib_v8 "github.com/go-redis/redis/v8"
"github.com/go-redsync/redsync/v4/redis"
"github.com/go-redsync/redsync/v4/redis/goredis"
goredis_v7 "github.com/go-redsync/redsync/v4/redis/goredis/v7"
goredis_v8 "github.com/go-redsync/redsync/v4/redis/goredis/v8"
"github.com/go-redsync/redsync/v4/redis/redigo"
redigolib "github.com/gomodule/redigo/redis"
"github.com/stvp/tempredis"
)
var servers []*tempredis.Server
type testCase struct {
poolCount int
pools []redis.Pool
}
func makeCases(poolCount int) map[string]*testCase {
return map[string]*testCase{
"redigo": {
poolCount,
newMockPoolsRedigo(poolCount),
},
"goredis": {
poolCount,
newMockPoolsGoredis(poolCount),
},
"goredis_v7": {
poolCount,
newMockPoolsGoredisV7(poolCount),
},
"goredis_v8": {
poolCount,
newMockPoolsGoredisV8(poolCount),
},
}
}
// Maintain separate blocks of servers for each type of driver
const ServerPools = 4
const ServerPoolSize = 8
const RedigoBlock = 0
const GoredisBlock = 1
const GoredisV7Block = 2
const GoredisV8Block = 3
func TestMain(m *testing.M) {
for i := 0; i < ServerPoolSize*ServerPools; i++ {
server, err := tempredis.Start(tempredis.Config{})
if err != nil {
panic(err)
}
servers = append(servers, server)
}
result := m.Run()
for _, server := range servers {
_ = server.Term()
}
os.Exit(result)
}
func TestRedsync(t *testing.T) {
for k, v := range makeCases(8) {
t.Run(k, func(t *testing.T) {
rs := New(v.pools...)
mutex := rs.NewMutex("test-redsync")
_ = mutex.Lock()
assertAcquired(t, v.pools, mutex)
})
}
}
func newMockPoolsRedigo(n int) []redis.Pool {
pools := make([]redis.Pool, n)
offset := RedigoBlock * ServerPoolSize
for i := 0; i < n; i++ {
server := servers[i+offset]
pools[i] = redigo.NewPool(&redigolib.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redigolib.Conn, error) {
return redigolib.Dial("unix", server.Socket())
},
TestOnBorrow: func(c redigolib.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
})
}
return pools
}
func newMockPoolsGoredis(n int) []redis.Pool {
pools := make([]redis.Pool, n)
offset := GoredisBlock * ServerPoolSize
for i := 0; i < n; i++ {
client := goredislib.NewClient(&goredislib.Options{
Network: "unix",
Addr: servers[i+offset].Socket(),
})
pools[i] = goredis.NewPool(client)
}
return pools
}
func newMockPoolsGoredisV7(n int) []redis.Pool {
pools := make([]redis.Pool, n)
offset := GoredisV7Block * ServerPoolSize
for i := 0; i < n; i++ {
client := goredislib_v7.NewClient(&goredislib_v7.Options{
Network: "unix",
Addr: servers[i+offset].Socket(),
})
pools[i] = goredis_v7.NewPool(client)
}
return pools
}
func newMockPoolsGoredisV8(n int) []redis.Pool {
pools := make([]redis.Pool, n)
offset := GoredisV8Block * ServerPoolSize
for i := 0; i < n; i++ {
client := goredislib_v8.NewClient(&goredislib_v8.Options{
Network: "unix",
Addr: servers[i+offset].Socket(),
})
pools[i] = goredis_v8.NewPool(client)
}
return pools
}