store_test.go 18 KB

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