| 1 |
package redis_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"net" |
| 7 |
"strconv" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
. "github.com/onsi/ginkgo" |
| 12 |
. "github.com/onsi/gomega" |
| 13 |
|
| 14 |
"gopkg.in/redis.v5" |
| 15 |
) |
| 16 |
|
| 17 |
var _ = Describe("races", func() { |
| 18 |
var client *redis.Client |
| 19 |
var C, N int |
| 20 |
|
| 21 |
BeforeEach(func() { |
| 22 |
client = redis.NewClient(redisOptions()) |
| 23 |
Expect(client.FlushDb().Err()).To(BeNil()) |
| 24 |
|
| 25 |
C, N = 10, 1000 |
| 26 |
if testing.Short() { |
| 27 |
C = 4 |
| 28 |
N = 100 |
| 29 |
} |
| 30 |
}) |
| 31 |
|
| 32 |
AfterEach(func() { |
| 33 |
err := client.Close() |
| 34 |
Expect(err).NotTo(HaveOccurred()) |
| 35 |
}) |
| 36 |
|
| 37 |
It("should echo", func() { |
| 38 |
perform(C, func(id int) { |
| 39 |
for i := 0; i < N; i++ { |
| 40 |
msg := fmt.Sprintf("echo %d %d", id, i) |
| 41 |
echo, err := client.Echo(msg).Result() |
| 42 |
Expect(err).NotTo(HaveOccurred()) |
| 43 |
Expect(echo).To(Equal(msg)) |
| 44 |
} |
| 45 |
}) |
| 46 |
}) |
| 47 |
|
| 48 |
It("should incr", func() { |
| 49 |
key := "TestIncrFromGoroutines" |
| 50 |
|
| 51 |
perform(C, func(id int) { |
| 52 |
for i := 0; i < N; i++ { |
| 53 |
err := client.Incr(key).Err() |
| 54 |
Expect(err).NotTo(HaveOccurred()) |
| 55 |
} |
| 56 |
}) |
| 57 |
|
| 58 |
val, err := client.Get(key).Int64() |
| 59 |
Expect(err).NotTo(HaveOccurred()) |
| 60 |
Expect(val).To(Equal(int64(C * N))) |
| 61 |
}) |
| 62 |
|
| 63 |
It("should handle many keys", func() { |
| 64 |
perform(C, func(id int) { |
| 65 |
for i := 0; i < N; i++ { |
| 66 |
err := client.Set( |
| 67 |
fmt.Sprintf("keys.key-%d-%d", id, i), |
| 68 |
fmt.Sprintf("hello-%d-%d", id, i), |
| 69 |
0, |
| 70 |
).Err() |
| 71 |
Expect(err).NotTo(HaveOccurred()) |
| 72 |
} |
| 73 |
}) |
| 74 |
|
| 75 |
keys := client.Keys("keys.*") |
| 76 |
Expect(keys.Err()).NotTo(HaveOccurred()) |
| 77 |
Expect(len(keys.Val())).To(Equal(C * N)) |
| 78 |
}) |
| 79 |
|
| 80 |
It("should handle many keys 2", func() { |
| 81 |
perform(C, func(id int) { |
| 82 |
keys := []string{"non-existent-key"} |
| 83 |
for i := 0; i < N; i++ { |
| 84 |
key := fmt.Sprintf("keys.key-%d", i) |
| 85 |
keys = append(keys, key) |
| 86 |
|
| 87 |
err := client.Set(key, fmt.Sprintf("hello-%d", i), 0).Err() |
| 88 |
Expect(err).NotTo(HaveOccurred()) |
| 89 |
} |
| 90 |
keys = append(keys, "non-existent-key") |
| 91 |
|
| 92 |
vals, err := client.MGet(keys...).Result() |
| 93 |
Expect(err).NotTo(HaveOccurred()) |
| 94 |
Expect(len(vals)).To(Equal(N + 2)) |
| 95 |
|
| 96 |
for i := 0; i < N; i++ { |
| 97 |
Expect(vals[i+1]).To(Equal(fmt.Sprintf("hello-%d", i))) |
| 98 |
} |
| 99 |
|
| 100 |
Expect(vals[0]).To(BeNil()) |
| 101 |
Expect(vals[N+1]).To(BeNil()) |
| 102 |
}) |
| 103 |
}) |
| 104 |
|
| 105 |
It("should handle big vals in Get", func() { |
| 106 |
C, N = 4, 100 |
| 107 |
|
| 108 |
bigVal := bytes.Repeat([]byte{'*'}, 1<<17) // 128kb |
| 109 |
|
| 110 |
err := client.Set("key", bigVal, 0).Err() |
| 111 |
Expect(err).NotTo(HaveOccurred()) |
| 112 |
|
| 113 |
// Reconnect to get new connection. |
| 114 |
Expect(client.Close()).To(BeNil()) |
| 115 |
client = redis.NewClient(redisOptions()) |
| 116 |
|
| 117 |
perform(C, func(id int) { |
| 118 |
for i := 0; i < N; i++ { |
| 119 |
got, err := client.Get("key").Bytes() |
| 120 |
Expect(err).NotTo(HaveOccurred()) |
| 121 |
Expect(got).To(Equal(bigVal)) |
| 122 |
} |
| 123 |
}) |
| 124 |
}) |
| 125 |
|
| 126 |
It("should handle big vals in Set", func() { |
| 127 |
C, N = 4, 100 |
| 128 |
|
| 129 |
bigVal := bytes.Repeat([]byte{'*'}, 1<<17) // 128kb |
| 130 |
|
| 131 |
perform(C, func(id int) { |
| 132 |
for i := 0; i < N; i++ { |
| 133 |
err := client.Set("key", bigVal, 0).Err() |
| 134 |
Expect(err).NotTo(HaveOccurred()) |
| 135 |
} |
| 136 |
}) |
| 137 |
}) |
| 138 |
|
| 139 |
It("should PubSub", func() { |
| 140 |
connPool := client.Pool() |
| 141 |
|
| 142 |
perform(C, func(id int) { |
| 143 |
for i := 0; i < N; i++ { |
| 144 |
pubsub, err := client.Subscribe(fmt.Sprintf("mychannel%d", id)) |
| 145 |
Expect(err).NotTo(HaveOccurred()) |
| 146 |
|
| 147 |
go func() { |
| 148 |
defer GinkgoRecover() |
| 149 |
|
| 150 |
time.Sleep(time.Millisecond) |
| 151 |
err := pubsub.Close() |
| 152 |
Expect(err).NotTo(HaveOccurred()) |
| 153 |
}() |
| 154 |
|
| 155 |
_, err = pubsub.ReceiveMessage() |
| 156 |
Expect(err.Error()).To(ContainSubstring("closed")) |
| 157 |
|
| 158 |
val := "echo" + strconv.Itoa(i) |
| 159 |
echo, err := client.Echo(val).Result() |
| 160 |
Expect(err).NotTo(HaveOccurred()) |
| 161 |
Expect(echo).To(Equal(val)) |
| 162 |
} |
| 163 |
}) |
| 164 |
|
| 165 |
Expect(connPool.Len()).To(Equal(connPool.FreeLen())) |
| 166 |
Expect(connPool.Len()).To(BeNumerically("<=", 10)) |
| 167 |
}) |
| 168 |
|
| 169 |
It("should select db", func() { |
| 170 |
err := client.Set("db", 1, 0).Err() |
| 171 |
Expect(err).NotTo(HaveOccurred()) |
| 172 |
|
| 173 |
perform(C, func(id int) { |
| 174 |
opt := redisOptions() |
| 175 |
opt.DB = id |
| 176 |
client := redis.NewClient(opt) |
| 177 |
for i := 0; i < N; i++ { |
| 178 |
err := client.Set("db", id, 0).Err() |
| 179 |
Expect(err).NotTo(HaveOccurred()) |
| 180 |
|
| 181 |
n, err := client.Get("db").Int64() |
| 182 |
Expect(err).NotTo(HaveOccurred()) |
| 183 |
Expect(n).To(Equal(int64(id))) |
| 184 |
} |
| 185 |
err := client.Close() |
| 186 |
Expect(err).NotTo(HaveOccurred()) |
| 187 |
}) |
| 188 |
|
| 189 |
n, err := client.Get("db").Int64() |
| 190 |
Expect(err).NotTo(HaveOccurred()) |
| 191 |
Expect(n).To(Equal(int64(1))) |
| 192 |
}) |
| 193 |
|
| 194 |
It("should select DB with read timeout", func() { |
| 195 |
perform(C, func(id int) { |
| 196 |
opt := redisOptions() |
| 197 |
opt.DB = id |
| 198 |
opt.ReadTimeout = time.Nanosecond |
| 199 |
client := redis.NewClient(opt) |
| 200 |
|
| 201 |
perform(C, func(id int) { |
| 202 |
err := client.Ping().Err() |
| 203 |
Expect(err).To(HaveOccurred()) |
| 204 |
Expect(err.(net.Error).Timeout()).To(BeTrue()) |
| 205 |
}) |
| 206 |
|
| 207 |
err := client.Close() |
| 208 |
Expect(err).NotTo(HaveOccurred()) |
| 209 |
}) |
| 210 |
}) |
| 211 |
|
| 212 |
It("should Watch/Unwatch", func() { |
| 213 |
err := client.Set("key", "0", 0).Err() |
| 214 |
Expect(err).NotTo(HaveOccurred()) |
| 215 |
|
| 216 |
perform(C, func(id int) { |
| 217 |
for i := 0; i < N; i++ { |
| 218 |
err := client.Watch(func(tx *redis.Tx) error { |
| 219 |
val, err := tx.Get("key").Result() |
| 220 |
Expect(err).NotTo(HaveOccurred()) |
| 221 |
Expect(val).NotTo(Equal(redis.Nil)) |
| 222 |
|
| 223 |
num, err := strconv.ParseInt(val, 10, 64) |
| 224 |
Expect(err).NotTo(HaveOccurred()) |
| 225 |
|
| 226 |
cmds, err := tx.Pipelined(func(pipe *redis.Pipeline) error { |
| 227 |
pipe.Set("key", strconv.FormatInt(num+1, 10), 0) |
| 228 |
return nil |
| 229 |
}) |
| 230 |
Expect(cmds).To(HaveLen(1)) |
| 231 |
return err |
| 232 |
}, "key") |
| 233 |
if err == redis.TxFailedErr { |
| 234 |
i-- |
| 235 |
continue |
| 236 |
} |
| 237 |
Expect(err).NotTo(HaveOccurred()) |
| 238 |
} |
| 239 |
}) |
| 240 |
|
| 241 |
val, err := client.Get("key").Int64() |
| 242 |
Expect(err).NotTo(HaveOccurred()) |
| 243 |
Expect(val).To(Equal(int64(C * N))) |
| 244 |
}) |
| 245 |
|
| 246 |
It("should Pipeline", func() { |
| 247 |
perform(C, func(id int) { |
| 248 |
pipe := client.Pipeline() |
| 249 |
for i := 0; i < N; i++ { |
| 250 |
pipe.Echo(fmt.Sprint(i)) |
| 251 |
} |
| 252 |
|
| 253 |
cmds, err := pipe.Exec() |
| 254 |
Expect(err).NotTo(HaveOccurred()) |
| 255 |
Expect(cmds).To(HaveLen(N)) |
| 256 |
|
| 257 |
for i := 0; i < N; i++ { |
| 258 |
Expect(cmds[i].(*redis.StringCmd).Val()).To(Equal(fmt.Sprint(i))) |
| 259 |
} |
| 260 |
}) |
| 261 |
}) |
| 262 |
|
| 263 |
It("should Pipeline", func() { |
| 264 |
pipe := client.Pipeline() |
| 265 |
perform(N, func(id int) { |
| 266 |
pipe.Incr("key") |
| 267 |
}) |
| 268 |
|
| 269 |
cmds, err := pipe.Exec() |
| 270 |
Expect(err).NotTo(HaveOccurred()) |
| 271 |
Expect(cmds).To(HaveLen(N)) |
| 272 |
|
| 273 |
n, err := client.Get("key").Int64() |
| 274 |
Expect(err).NotTo(HaveOccurred()) |
| 275 |
Expect(n).To(Equal(int64(N))) |
| 276 |
}) |
| 277 |
}) |