race_test.go 7.3 KB

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