2016-03-16 17:57:24 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2018-03-08 11:16:53 +03:00
|
|
|
"sync/atomic"
|
2016-03-16 17:57:24 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis"
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("races", func() {
|
|
|
|
var client *redis.Client
|
2016-03-17 19:00:47 +03:00
|
|
|
var C, N int
|
2016-03-16 17:57:24 +03:00
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
client = redis.NewClient(redisOptions())
|
2017-06-17 12:53:16 +03:00
|
|
|
Expect(client.FlushDB().Err()).To(BeNil())
|
2016-03-17 19:00:47 +03:00
|
|
|
|
|
|
|
C, N = 10, 1000
|
|
|
|
if testing.Short() {
|
|
|
|
C = 4
|
|
|
|
N = 100
|
|
|
|
}
|
2016-03-16 17:57:24 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
AfterEach(func() {
|
|
|
|
err := client.Close()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should echo", func() {
|
|
|
|
perform(C, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
msg := fmt.Sprintf("echo %d %d", id, i)
|
|
|
|
echo, err := client.Echo(msg).Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(echo).To(Equal(msg))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should incr", func() {
|
|
|
|
key := "TestIncrFromGoroutines"
|
|
|
|
|
|
|
|
perform(C, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
err := client.Incr(key).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
val, err := client.Get(key).Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal(int64(C * N)))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should handle many keys", func() {
|
|
|
|
perform(C, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
err := client.Set(
|
|
|
|
fmt.Sprintf("keys.key-%d-%d", id, i),
|
|
|
|
fmt.Sprintf("hello-%d-%d", id, i),
|
|
|
|
0,
|
|
|
|
).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
keys := client.Keys("keys.*")
|
|
|
|
Expect(keys.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(len(keys.Val())).To(Equal(C * N))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should handle many keys 2", func() {
|
|
|
|
perform(C, func(id int) {
|
|
|
|
keys := []string{"non-existent-key"}
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
key := fmt.Sprintf("keys.key-%d", i)
|
|
|
|
keys = append(keys, key)
|
|
|
|
|
|
|
|
err := client.Set(key, fmt.Sprintf("hello-%d", i), 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
keys = append(keys, "non-existent-key")
|
|
|
|
|
|
|
|
vals, err := client.MGet(keys...).Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(len(vals)).To(Equal(N + 2))
|
|
|
|
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
Expect(vals[i+1]).To(Equal(fmt.Sprintf("hello-%d", i)))
|
|
|
|
}
|
|
|
|
|
|
|
|
Expect(vals[0]).To(BeNil())
|
|
|
|
Expect(vals[N+1]).To(BeNil())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should handle big vals in Get", func() {
|
2016-11-09 11:04:37 +03:00
|
|
|
C, N = 4, 100
|
|
|
|
|
2017-09-30 09:21:59 +03:00
|
|
|
bigVal := bigVal()
|
2016-03-16 17:57:24 +03:00
|
|
|
|
|
|
|
err := client.Set("key", bigVal, 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
// Reconnect to get new connection.
|
|
|
|
Expect(client.Close()).To(BeNil())
|
|
|
|
client = redis.NewClient(redisOptions())
|
|
|
|
|
|
|
|
perform(C, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
2016-11-09 11:04:37 +03:00
|
|
|
got, err := client.Get("key").Bytes()
|
2016-03-16 17:57:24 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(got).To(Equal(bigVal))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should handle big vals in Set", func() {
|
2016-03-17 19:00:47 +03:00
|
|
|
C, N = 4, 100
|
2016-11-09 11:04:37 +03:00
|
|
|
|
2017-09-30 09:21:59 +03:00
|
|
|
bigVal := bigVal()
|
2016-03-16 17:57:24 +03:00
|
|
|
perform(C, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
err := client.Set("key", bigVal, 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should select db", func() {
|
|
|
|
err := client.Set("db", 1, 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
perform(C, func(id int) {
|
|
|
|
opt := redisOptions()
|
2016-06-05 12:45:39 +03:00
|
|
|
opt.DB = id
|
2016-03-16 17:57:24 +03:00
|
|
|
client := redis.NewClient(opt)
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
err := client.Set("db", id, 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
n, err := client.Get("db").Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(id)))
|
|
|
|
}
|
|
|
|
err := client.Close()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
n, err := client.Get("db").Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(1)))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should select DB with read timeout", func() {
|
|
|
|
perform(C, func(id int) {
|
|
|
|
opt := redisOptions()
|
2016-06-05 12:45:39 +03:00
|
|
|
opt.DB = id
|
2016-03-16 17:57:24 +03:00
|
|
|
opt.ReadTimeout = time.Nanosecond
|
|
|
|
client := redis.NewClient(opt)
|
|
|
|
|
|
|
|
perform(C, func(id int) {
|
|
|
|
err := client.Ping().Err()
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.(net.Error).Timeout()).To(BeTrue())
|
|
|
|
})
|
|
|
|
|
|
|
|
err := client.Close()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
|
|
|
})
|
2016-04-09 11:23:58 +03:00
|
|
|
|
|
|
|
It("should Watch/Unwatch", func() {
|
|
|
|
err := client.Set("key", "0", 0).Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
perform(C, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
2016-05-02 15:54:15 +03:00
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
|
|
|
val, err := tx.Get("key").Result()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).NotTo(Equal(redis.Nil))
|
2016-04-09 11:23:58 +03:00
|
|
|
|
2016-05-02 15:54:15 +03:00
|
|
|
num, err := strconv.ParseInt(val, 10, 64)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2016-04-09 11:23:58 +03:00
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
cmds, err := tx.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-10-13 14:36:15 +03:00
|
|
|
pipe.Set("key", strconv.FormatInt(num+1, 10), 0)
|
2016-05-02 15:54:15 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
Expect(cmds).To(HaveLen(1))
|
|
|
|
return err
|
|
|
|
}, "key")
|
2016-04-09 11:23:58 +03:00
|
|
|
if err == redis.TxFailedErr {
|
|
|
|
i--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
val, err := client.Get("key").Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(val).To(Equal(int64(C * N)))
|
|
|
|
})
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
It("should Pipeline", func() {
|
|
|
|
perform(C, func(id int) {
|
|
|
|
pipe := client.Pipeline()
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
pipe.Echo(fmt.Sprint(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
cmds, err := pipe.Exec()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cmds).To(HaveLen(N))
|
|
|
|
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
Expect(cmds[i].(*redis.StringCmd).Val()).To(Equal(fmt.Sprint(i)))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should Pipeline", func() {
|
|
|
|
pipe := client.Pipeline()
|
|
|
|
perform(N, func(id int) {
|
|
|
|
pipe.Incr("key")
|
|
|
|
})
|
|
|
|
|
|
|
|
cmds, err := pipe.Exec()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cmds).To(HaveLen(N))
|
|
|
|
|
|
|
|
n, err := client.Get("key").Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(N)))
|
|
|
|
})
|
2018-02-20 11:05:11 +03:00
|
|
|
|
|
|
|
It("should TxPipeline", func() {
|
|
|
|
pipe := client.TxPipeline()
|
|
|
|
perform(N, func(id int) {
|
|
|
|
pipe.Incr("key")
|
|
|
|
})
|
|
|
|
|
|
|
|
cmds, err := pipe.Exec()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(cmds).To(HaveLen(N))
|
|
|
|
|
|
|
|
n, err := client.Get("key").Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(N)))
|
|
|
|
})
|
2018-03-08 11:16:53 +03:00
|
|
|
|
|
|
|
It("should BLPop", func() {
|
|
|
|
var received uint32
|
|
|
|
wg := performAsync(C, func(id int) {
|
|
|
|
for {
|
|
|
|
v, err := client.BLPop(3*time.Second, "list").Result()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
Expect(v).To(Equal([]string{"list", "hello"}))
|
|
|
|
atomic.AddUint32(&received, 1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
perform(C, func(id int) {
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
err := client.LPush("list", "hello").Err()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
Expect(received).To(Equal(uint32(C * N)))
|
|
|
|
})
|
2016-03-16 17:57:24 +03:00
|
|
|
})
|
2017-09-30 09:21:59 +03:00
|
|
|
|
|
|
|
func bigVal() []byte {
|
|
|
|
return bytes.Repeat([]byte{'*'}, 1<<17) // 128kb
|
|
|
|
}
|