store_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. package kv
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/alicebob/miniredis/v2"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tal-tech/go-zero/core/hash"
  8. "github.com/tal-tech/go-zero/core/stores/cache"
  9. "github.com/tal-tech/go-zero/core/stores/redis"
  10. "github.com/tal-tech/go-zero/core/stringx"
  11. )
  12. var s1, _ = miniredis.Run()
  13. var s2, _ = miniredis.Run()
  14. func TestRedis_Exists(t *testing.T) {
  15. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  16. _, err := store.Exists("foo")
  17. assert.NotNil(t, err)
  18. runOnCluster(t, func(client Store) {
  19. ok, err := client.Exists("a")
  20. assert.Nil(t, err)
  21. assert.False(t, ok)
  22. assert.Nil(t, client.Set("a", "b"))
  23. ok, err = client.Exists("a")
  24. assert.Nil(t, err)
  25. assert.True(t, ok)
  26. })
  27. }
  28. func TestRedis_Eval(t *testing.T) {
  29. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  30. _, err := store.Eval(`redis.call("EXISTS", KEYS[1])`, "key1")
  31. assert.NotNil(t, err)
  32. runOnCluster(t, func(client Store) {
  33. _, err := client.Eval(`redis.call("EXISTS", KEYS[1])`, "notexist")
  34. assert.Equal(t, redis.Nil, err)
  35. err = client.Set("key1", "value1")
  36. assert.Nil(t, err)
  37. _, err = client.Eval(`redis.call("EXISTS", KEYS[1])`, "key1")
  38. assert.Equal(t, redis.Nil, err)
  39. val, err := client.Eval(`return redis.call("EXISTS", KEYS[1])`, "key1")
  40. assert.Nil(t, err)
  41. assert.Equal(t, int64(1), val)
  42. })
  43. }
  44. func TestRedis_Hgetall(t *testing.T) {
  45. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  46. err := store.Hset("a", "aa", "aaa")
  47. assert.NotNil(t, err)
  48. _, err = store.Hgetall("a")
  49. assert.NotNil(t, err)
  50. runOnCluster(t, func(client Store) {
  51. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  52. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  53. vals, err := client.Hgetall("a")
  54. assert.Nil(t, err)
  55. assert.EqualValues(t, map[string]string{
  56. "aa": "aaa",
  57. "bb": "bbb",
  58. }, vals)
  59. })
  60. }
  61. func TestRedis_Hvals(t *testing.T) {
  62. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  63. _, err := store.Hvals("a")
  64. assert.NotNil(t, err)
  65. runOnCluster(t, func(client Store) {
  66. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  67. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  68. vals, err := client.Hvals("a")
  69. assert.Nil(t, err)
  70. assert.ElementsMatch(t, []string{"aaa", "bbb"}, vals)
  71. })
  72. }
  73. func TestRedis_Hsetnx(t *testing.T) {
  74. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  75. _, err := store.Hsetnx("a", "dd", "ddd")
  76. assert.NotNil(t, err)
  77. runOnCluster(t, func(client Store) {
  78. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  79. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  80. ok, err := client.Hsetnx("a", "bb", "ccc")
  81. assert.Nil(t, err)
  82. assert.False(t, ok)
  83. ok, err = client.Hsetnx("a", "dd", "ddd")
  84. assert.Nil(t, err)
  85. assert.True(t, ok)
  86. vals, err := client.Hvals("a")
  87. assert.Nil(t, err)
  88. assert.ElementsMatch(t, []string{"aaa", "bbb", "ddd"}, vals)
  89. })
  90. }
  91. func TestRedis_HdelHlen(t *testing.T) {
  92. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  93. _, err := store.Hdel("a", "aa")
  94. assert.NotNil(t, err)
  95. _, err = store.Hlen("a")
  96. assert.NotNil(t, err)
  97. runOnCluster(t, func(client Store) {
  98. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  99. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  100. num, err := client.Hlen("a")
  101. assert.Nil(t, err)
  102. assert.Equal(t, 2, num)
  103. val, err := client.Hdel("a", "aa")
  104. assert.Nil(t, err)
  105. assert.True(t, val)
  106. vals, err := client.Hvals("a")
  107. assert.Nil(t, err)
  108. assert.ElementsMatch(t, []string{"bbb"}, vals)
  109. })
  110. }
  111. func TestRedis_HIncrBy(t *testing.T) {
  112. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  113. _, err := store.Hincrby("key", "field", 3)
  114. assert.NotNil(t, err)
  115. runOnCluster(t, func(client Store) {
  116. val, err := client.Hincrby("key", "field", 2)
  117. assert.Nil(t, err)
  118. assert.Equal(t, 2, val)
  119. val, err = client.Hincrby("key", "field", 3)
  120. assert.Nil(t, err)
  121. assert.Equal(t, 5, val)
  122. })
  123. }
  124. func TestRedis_Hkeys(t *testing.T) {
  125. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  126. _, err := store.Hkeys("a")
  127. assert.NotNil(t, err)
  128. runOnCluster(t, func(client Store) {
  129. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  130. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  131. vals, err := client.Hkeys("a")
  132. assert.Nil(t, err)
  133. assert.ElementsMatch(t, []string{"aa", "bb"}, vals)
  134. })
  135. }
  136. func TestRedis_Hmget(t *testing.T) {
  137. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  138. _, err := store.Hmget("a", "aa", "bb")
  139. assert.NotNil(t, err)
  140. runOnCluster(t, func(client Store) {
  141. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  142. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  143. vals, err := client.Hmget("a", "aa", "bb")
  144. assert.Nil(t, err)
  145. assert.EqualValues(t, []string{"aaa", "bbb"}, vals)
  146. vals, err = client.Hmget("a", "aa", "no", "bb")
  147. assert.Nil(t, err)
  148. assert.EqualValues(t, []string{"aaa", "", "bbb"}, vals)
  149. })
  150. }
  151. func TestRedis_Hmset(t *testing.T) {
  152. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  153. err := store.Hmset("a", map[string]string{
  154. "aa": "aaa",
  155. })
  156. assert.NotNil(t, err)
  157. runOnCluster(t, func(client Store) {
  158. assert.Nil(t, client.Hmset("a", map[string]string{
  159. "aa": "aaa",
  160. "bb": "bbb",
  161. }))
  162. vals, err := client.Hmget("a", "aa", "bb")
  163. assert.Nil(t, err)
  164. assert.EqualValues(t, []string{"aaa", "bbb"}, vals)
  165. })
  166. }
  167. func TestRedis_Incr(t *testing.T) {
  168. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  169. _, err := store.Incr("a")
  170. assert.NotNil(t, err)
  171. runOnCluster(t, func(client Store) {
  172. val, err := client.Incr("a")
  173. assert.Nil(t, err)
  174. assert.Equal(t, int64(1), val)
  175. val, err = client.Incr("a")
  176. assert.Nil(t, err)
  177. assert.Equal(t, int64(2), val)
  178. })
  179. }
  180. func TestRedis_IncrBy(t *testing.T) {
  181. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  182. _, err := store.Incrby("a", 2)
  183. assert.NotNil(t, err)
  184. runOnCluster(t, func(client Store) {
  185. val, err := client.Incrby("a", 2)
  186. assert.Nil(t, err)
  187. assert.Equal(t, int64(2), val)
  188. val, err = client.Incrby("a", 3)
  189. assert.Nil(t, err)
  190. assert.Equal(t, int64(5), val)
  191. })
  192. }
  193. func TestRedis_List(t *testing.T) {
  194. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  195. _, err := store.Lpush("key", "value1", "value2")
  196. assert.NotNil(t, err)
  197. _, err = store.Rpush("key", "value3", "value4")
  198. assert.NotNil(t, err)
  199. _, err = store.Llen("key")
  200. assert.NotNil(t, err)
  201. _, err = store.Lrange("key", 0, 10)
  202. assert.NotNil(t, err)
  203. _, err = store.Lpop("key")
  204. assert.NotNil(t, err)
  205. _, err = store.Lrem("key", 0, "val")
  206. assert.NotNil(t, err)
  207. runOnCluster(t, func(client Store) {
  208. val, err := client.Lpush("key", "value1", "value2")
  209. assert.Nil(t, err)
  210. assert.Equal(t, 2, val)
  211. val, err = client.Rpush("key", "value3", "value4")
  212. assert.Nil(t, err)
  213. assert.Equal(t, 4, val)
  214. val, err = client.Llen("key")
  215. assert.Nil(t, err)
  216. assert.Equal(t, 4, val)
  217. vals, err := client.Lrange("key", 0, 10)
  218. assert.Nil(t, err)
  219. assert.EqualValues(t, []string{"value2", "value1", "value3", "value4"}, vals)
  220. v, err := client.Lpop("key")
  221. assert.Nil(t, err)
  222. assert.Equal(t, "value2", v)
  223. val, err = client.Lpush("key", "value1", "value2")
  224. assert.Nil(t, err)
  225. assert.Equal(t, 5, val)
  226. val, err = client.Rpush("key", "value3", "value3")
  227. assert.Nil(t, err)
  228. assert.Equal(t, 7, val)
  229. n, err := client.Lrem("key", 2, "value1")
  230. assert.Nil(t, err)
  231. assert.Equal(t, 2, n)
  232. vals, err = client.Lrange("key", 0, 10)
  233. assert.Nil(t, err)
  234. assert.EqualValues(t, []string{"value2", "value3", "value4", "value3", "value3"}, vals)
  235. n, err = client.Lrem("key", -2, "value3")
  236. assert.Nil(t, err)
  237. assert.Equal(t, 2, n)
  238. vals, err = client.Lrange("key", 0, 10)
  239. assert.Nil(t, err)
  240. assert.EqualValues(t, []string{"value2", "value3", "value4"}, vals)
  241. })
  242. }
  243. func TestRedis_Persist(t *testing.T) {
  244. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  245. _, err := store.Persist("key")
  246. assert.NotNil(t, err)
  247. err = store.Expire("key", 5)
  248. assert.NotNil(t, err)
  249. err = store.Expireat("key", time.Now().Unix()+5)
  250. assert.NotNil(t, err)
  251. runOnCluster(t, func(client Store) {
  252. ok, err := client.Persist("key")
  253. assert.Nil(t, err)
  254. assert.False(t, ok)
  255. err = client.Set("key", "value")
  256. assert.Nil(t, err)
  257. ok, err = client.Persist("key")
  258. assert.Nil(t, err)
  259. assert.False(t, ok)
  260. err = client.Expire("key", 5)
  261. assert.Nil(t, err)
  262. ok, err = client.Persist("key")
  263. assert.Nil(t, err)
  264. assert.True(t, ok)
  265. err = client.Expireat("key", time.Now().Unix()+5)
  266. assert.Nil(t, err)
  267. ok, err = client.Persist("key")
  268. assert.Nil(t, err)
  269. assert.True(t, ok)
  270. })
  271. }
  272. func TestRedis_Sscan(t *testing.T) {
  273. key := "list"
  274. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  275. _, err := store.Sadd(key, nil)
  276. assert.NotNil(t, err)
  277. _, _, err = store.Sscan(key, 0, "", 100)
  278. assert.NotNil(t, err)
  279. _, err = store.Del(key)
  280. assert.NotNil(t, err)
  281. runOnCluster(t, func(client Store) {
  282. var list []string
  283. for i := 0; i < 1550; i++ {
  284. list = append(list, stringx.Randn(i))
  285. }
  286. lens, err := client.Sadd(key, list)
  287. assert.Nil(t, err)
  288. assert.Equal(t, lens, 1550)
  289. var cursor uint64 = 0
  290. sum := 0
  291. for {
  292. keys, next, err := client.Sscan(key, cursor, "", 100)
  293. assert.Nil(t, err)
  294. sum += len(keys)
  295. if next == 0 {
  296. break
  297. }
  298. cursor = next
  299. }
  300. assert.Equal(t, sum, 1550)
  301. _, err = client.Del(key)
  302. assert.Nil(t, err)
  303. })
  304. }
  305. func TestRedis_Set(t *testing.T) {
  306. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  307. _, err := store.Scard("key")
  308. assert.NotNil(t, err)
  309. _, err = store.Sismember("key", 2)
  310. assert.NotNil(t, err)
  311. _, err = store.Srem("key", 3, 4)
  312. assert.NotNil(t, err)
  313. _, err = store.Smembers("key")
  314. assert.NotNil(t, err)
  315. _, err = store.Srandmember("key", 1)
  316. assert.NotNil(t, err)
  317. _, err = store.Spop("key")
  318. assert.NotNil(t, err)
  319. runOnCluster(t, func(client Store) {
  320. num, err := client.Sadd("key", 1, 2, 3, 4)
  321. assert.Nil(t, err)
  322. assert.Equal(t, 4, num)
  323. val, err := client.Scard("key")
  324. assert.Nil(t, err)
  325. assert.Equal(t, int64(4), val)
  326. ok, err := client.Sismember("key", 2)
  327. assert.Nil(t, err)
  328. assert.True(t, ok)
  329. num, err = client.Srem("key", 3, 4)
  330. assert.Nil(t, err)
  331. assert.Equal(t, 2, num)
  332. vals, err := client.Smembers("key")
  333. assert.Nil(t, err)
  334. assert.ElementsMatch(t, []string{"1", "2"}, vals)
  335. members, err := client.Srandmember("key", 1)
  336. assert.Nil(t, err)
  337. assert.Len(t, members, 1)
  338. assert.Contains(t, []string{"1", "2"}, members[0])
  339. member, err := client.Spop("key")
  340. assert.Nil(t, err)
  341. assert.Contains(t, []string{"1", "2"}, member)
  342. vals, err = client.Smembers("key")
  343. assert.Nil(t, err)
  344. assert.NotContains(t, vals, member)
  345. num, err = client.Sadd("key1", 1, 2, 3, 4)
  346. assert.Nil(t, err)
  347. assert.Equal(t, 4, num)
  348. num, err = client.Sadd("key2", 2, 3, 4, 5)
  349. assert.Nil(t, err)
  350. assert.Equal(t, 4, num)
  351. })
  352. }
  353. func TestRedis_SetGetDel(t *testing.T) {
  354. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  355. err := store.Set("hello", "world")
  356. assert.NotNil(t, err)
  357. _, err = store.Get("hello")
  358. assert.NotNil(t, err)
  359. _, err = store.Del("hello")
  360. assert.NotNil(t, err)
  361. runOnCluster(t, func(client Store) {
  362. err := client.Set("hello", "world")
  363. assert.Nil(t, err)
  364. val, err := client.Get("hello")
  365. assert.Nil(t, err)
  366. assert.Equal(t, "world", val)
  367. ret, err := client.Del("hello")
  368. assert.Nil(t, err)
  369. assert.Equal(t, 1, ret)
  370. })
  371. }
  372. func TestRedis_SetExNx(t *testing.T) {
  373. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  374. err := store.Setex("hello", "world", 5)
  375. assert.NotNil(t, err)
  376. _, err = store.Setnx("newhello", "newworld")
  377. assert.NotNil(t, err)
  378. _, err = store.Ttl("hello")
  379. assert.NotNil(t, err)
  380. _, err = store.SetnxEx("newhello", "newworld", 5)
  381. assert.NotNil(t, err)
  382. runOnCluster(t, func(client Store) {
  383. err := client.Setex("hello", "world", 5)
  384. assert.Nil(t, err)
  385. ok, err := client.Setnx("hello", "newworld")
  386. assert.Nil(t, err)
  387. assert.False(t, ok)
  388. ok, err = client.Setnx("newhello", "newworld")
  389. assert.Nil(t, err)
  390. assert.True(t, ok)
  391. val, err := client.Get("hello")
  392. assert.Nil(t, err)
  393. assert.Equal(t, "world", val)
  394. val, err = client.Get("newhello")
  395. assert.Nil(t, err)
  396. assert.Equal(t, "newworld", val)
  397. ttl, err := client.Ttl("hello")
  398. assert.Nil(t, err)
  399. assert.True(t, ttl > 0)
  400. ok, err = client.SetnxEx("newhello", "newworld", 5)
  401. assert.Nil(t, err)
  402. assert.False(t, ok)
  403. num, err := client.Del("newhello")
  404. assert.Nil(t, err)
  405. assert.Equal(t, 1, num)
  406. ok, err = client.SetnxEx("newhello", "newworld", 5)
  407. assert.Nil(t, err)
  408. assert.True(t, ok)
  409. val, err = client.Get("newhello")
  410. assert.Nil(t, err)
  411. assert.Equal(t, "newworld", val)
  412. })
  413. }
  414. func TestRedis_SetGetDelHashField(t *testing.T) {
  415. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  416. err := store.Hset("key", "field", "value")
  417. assert.NotNil(t, err)
  418. _, err = store.Hget("key", "field")
  419. assert.NotNil(t, err)
  420. _, err = store.Hexists("key", "field")
  421. assert.NotNil(t, err)
  422. _, err = store.Hdel("key", "field")
  423. assert.NotNil(t, err)
  424. runOnCluster(t, func(client Store) {
  425. err := client.Hset("key", "field", "value")
  426. assert.Nil(t, err)
  427. val, err := client.Hget("key", "field")
  428. assert.Nil(t, err)
  429. assert.Equal(t, "value", val)
  430. ok, err := client.Hexists("key", "field")
  431. assert.Nil(t, err)
  432. assert.True(t, ok)
  433. ret, err := client.Hdel("key", "field")
  434. assert.Nil(t, err)
  435. assert.True(t, ret)
  436. ok, err = client.Hexists("key", "field")
  437. assert.Nil(t, err)
  438. assert.False(t, ok)
  439. })
  440. }
  441. func TestRedis_SortedSet(t *testing.T) {
  442. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  443. _, err := store.Zadd("key", 1, "value1")
  444. assert.NotNil(t, err)
  445. _, err = store.Zscore("key", "value1")
  446. assert.NotNil(t, err)
  447. _, err = store.Zcount("key", 6, 7)
  448. assert.NotNil(t, err)
  449. _, err = store.Zincrby("key", 3, "value1")
  450. assert.NotNil(t, err)
  451. _, err = store.Zrank("key", "value2")
  452. assert.NotNil(t, err)
  453. _, err = store.Zrem("key", "value2", "value3")
  454. assert.NotNil(t, err)
  455. _, err = store.Zremrangebyscore("key", 6, 7)
  456. assert.NotNil(t, err)
  457. _, err = store.Zremrangebyrank("key", 1, 2)
  458. assert.NotNil(t, err)
  459. _, err = store.Zcard("key")
  460. assert.NotNil(t, err)
  461. _, err = store.Zrange("key", 0, -1)
  462. assert.NotNil(t, err)
  463. _, err = store.Zrevrange("key", 0, -1)
  464. assert.NotNil(t, err)
  465. _, err = store.ZrangeWithScores("key", 0, -1)
  466. assert.NotNil(t, err)
  467. _, err = store.ZrangebyscoreWithScores("key", 5, 8)
  468. assert.NotNil(t, err)
  469. _, err = store.ZrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1)
  470. assert.NotNil(t, err)
  471. _, err = store.ZrevrangebyscoreWithScores("key", 5, 8)
  472. assert.NotNil(t, err)
  473. _, err = store.ZrevrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1)
  474. assert.NotNil(t, err)
  475. _, err = store.Zrevrank("key", "value")
  476. assert.NotNil(t, err)
  477. _, err = store.Zadds("key", redis.Pair{
  478. Key: "value2",
  479. Score: 6,
  480. }, redis.Pair{
  481. Key: "value3",
  482. Score: 7,
  483. })
  484. assert.NotNil(t, err)
  485. runOnCluster(t, func(client Store) {
  486. ok, err := client.Zadd("key", 1, "value1")
  487. assert.Nil(t, err)
  488. assert.True(t, ok)
  489. ok, err = client.Zadd("key", 2, "value1")
  490. assert.Nil(t, err)
  491. assert.False(t, ok)
  492. val, err := client.Zscore("key", "value1")
  493. assert.Nil(t, err)
  494. assert.Equal(t, int64(2), val)
  495. val, err = client.Zincrby("key", 3, "value1")
  496. assert.Nil(t, err)
  497. assert.Equal(t, int64(5), val)
  498. val, err = client.Zscore("key", "value1")
  499. assert.Nil(t, err)
  500. assert.Equal(t, int64(5), val)
  501. ok, err = client.Zadd("key", 6, "value2")
  502. assert.Nil(t, err)
  503. assert.True(t, ok)
  504. ok, err = client.Zadd("key", 7, "value3")
  505. assert.Nil(t, err)
  506. assert.True(t, ok)
  507. rank, err := client.Zrank("key", "value2")
  508. assert.Nil(t, err)
  509. assert.Equal(t, int64(1), rank)
  510. _, err = client.Zrank("key", "value4")
  511. assert.Equal(t, redis.Nil, err)
  512. num, err := client.Zrem("key", "value2", "value3")
  513. assert.Nil(t, err)
  514. assert.Equal(t, 2, num)
  515. ok, err = client.Zadd("key", 6, "value2")
  516. assert.Nil(t, err)
  517. assert.True(t, ok)
  518. ok, err = client.Zadd("key", 7, "value3")
  519. assert.Nil(t, err)
  520. assert.True(t, ok)
  521. ok, err = client.Zadd("key", 8, "value4")
  522. assert.Nil(t, err)
  523. assert.True(t, ok)
  524. num, err = client.Zremrangebyscore("key", 6, 7)
  525. assert.Nil(t, err)
  526. assert.Equal(t, 2, num)
  527. ok, err = client.Zadd("key", 6, "value2")
  528. assert.Nil(t, err)
  529. assert.True(t, ok)
  530. ok, err = client.Zadd("key", 7, "value3")
  531. assert.Nil(t, err)
  532. assert.True(t, ok)
  533. num, err = client.Zcount("key", 6, 7)
  534. assert.Nil(t, err)
  535. assert.Equal(t, 2, num)
  536. num, err = client.Zremrangebyrank("key", 1, 2)
  537. assert.Nil(t, err)
  538. assert.Equal(t, 2, num)
  539. card, err := client.Zcard("key")
  540. assert.Nil(t, err)
  541. assert.Equal(t, 2, card)
  542. vals, err := client.Zrange("key", 0, -1)
  543. assert.Nil(t, err)
  544. assert.EqualValues(t, []string{"value1", "value4"}, vals)
  545. vals, err = client.Zrevrange("key", 0, -1)
  546. assert.Nil(t, err)
  547. assert.EqualValues(t, []string{"value4", "value1"}, vals)
  548. pairs, err := client.ZrangeWithScores("key", 0, -1)
  549. assert.Nil(t, err)
  550. assert.EqualValues(t, []redis.Pair{
  551. {
  552. Key: "value1",
  553. Score: 5,
  554. },
  555. {
  556. Key: "value4",
  557. Score: 8,
  558. },
  559. }, pairs)
  560. pairs, err = client.ZrangebyscoreWithScores("key", 5, 8)
  561. assert.Nil(t, err)
  562. assert.EqualValues(t, []redis.Pair{
  563. {
  564. Key: "value1",
  565. Score: 5,
  566. },
  567. {
  568. Key: "value4",
  569. Score: 8,
  570. },
  571. }, pairs)
  572. pairs, err = client.ZrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1)
  573. assert.Nil(t, err)
  574. assert.EqualValues(t, []redis.Pair{
  575. {
  576. Key: "value4",
  577. Score: 8,
  578. },
  579. }, pairs)
  580. pairs, err = client.ZrevrangebyscoreWithScores("key", 5, 8)
  581. assert.Nil(t, err)
  582. assert.EqualValues(t, []redis.Pair{
  583. {
  584. Key: "value4",
  585. Score: 8,
  586. },
  587. {
  588. Key: "value1",
  589. Score: 5,
  590. },
  591. }, pairs)
  592. pairs, err = client.ZrevrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1)
  593. assert.Nil(t, err)
  594. assert.EqualValues(t, []redis.Pair{
  595. {
  596. Key: "value1",
  597. Score: 5,
  598. },
  599. }, pairs)
  600. rank, err = client.Zrevrank("key", "value1")
  601. assert.Nil(t, err)
  602. assert.Equal(t, int64(1), rank)
  603. val, err = client.Zadds("key", redis.Pair{
  604. Key: "value2",
  605. Score: 6,
  606. }, redis.Pair{
  607. Key: "value3",
  608. Score: 7,
  609. })
  610. assert.Nil(t, err)
  611. assert.Equal(t, int64(2), val)
  612. })
  613. }
  614. func TestRedis_HyperLogLog(t *testing.T) {
  615. store := clusterStore{dispatcher: hash.NewConsistentHash()}
  616. _, err := store.Pfadd("key")
  617. assert.NotNil(t, err)
  618. _, err = store.Pfcount("key")
  619. assert.NotNil(t, err)
  620. runOnCluster(t, func(cluster Store) {
  621. _, err := cluster.Pfadd("key")
  622. assert.NotNil(t, err)
  623. _, err = cluster.Pfcount("key")
  624. assert.NotNil(t, err)
  625. })
  626. }
  627. func runOnCluster(t *testing.T, fn func(cluster Store)) {
  628. s1.FlushAll()
  629. s2.FlushAll()
  630. store := NewStore([]cache.NodeConf{
  631. {
  632. RedisConf: redis.RedisConf{
  633. Host: s1.Addr(),
  634. Type: redis.NodeType,
  635. },
  636. Weight: 100,
  637. },
  638. {
  639. RedisConf: redis.RedisConf{
  640. Host: s2.Addr(),
  641. Type: redis.NodeType,
  642. },
  643. Weight: 100,
  644. },
  645. })
  646. fn(store)
  647. }