redis_test.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. package redis
  2. import (
  3. "errors"
  4. "io"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "github.com/alicebob/miniredis/v2"
  9. red "github.com/go-redis/redis"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestRedis_Exists(t *testing.T) {
  13. runOnRedis(t, func(client *Redis) {
  14. _, err := NewRedis(client.Addr, "").Exists("a")
  15. assert.NotNil(t, err)
  16. ok, err := client.Exists("a")
  17. assert.Nil(t, err)
  18. assert.False(t, ok)
  19. assert.Nil(t, client.Set("a", "b"))
  20. ok, err = client.Exists("a")
  21. assert.Nil(t, err)
  22. assert.True(t, ok)
  23. })
  24. }
  25. func TestRedis_Eval(t *testing.T) {
  26. runOnRedis(t, func(client *Redis) {
  27. _, err := NewRedis(client.Addr, "").Eval(`redis.call("EXISTS", KEYS[1])`, []string{"notexist"})
  28. assert.NotNil(t, err)
  29. _, err = client.Eval(`redis.call("EXISTS", KEYS[1])`, []string{"notexist"})
  30. assert.Equal(t, Nil, err)
  31. err = client.Set("key1", "value1")
  32. assert.Nil(t, err)
  33. _, err = client.Eval(`redis.call("EXISTS", KEYS[1])`, []string{"key1"})
  34. assert.Equal(t, Nil, err)
  35. val, err := client.Eval(`return redis.call("EXISTS", KEYS[1])`, []string{"key1"})
  36. assert.Nil(t, err)
  37. assert.Equal(t, int64(1), val)
  38. })
  39. }
  40. func TestRedis_GeoHash(t *testing.T) {
  41. runOnRedis(t, func(client *Redis) {
  42. _, err := client.GeoHash("parent", "child1", "child2")
  43. assert.NotNil(t, err)
  44. })
  45. }
  46. func TestRedis_Hgetall(t *testing.T) {
  47. runOnRedis(t, func(client *Redis) {
  48. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  49. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  50. _, err := NewRedis(client.Addr, "").Hgetall("a")
  51. assert.NotNil(t, err)
  52. vals, err := client.Hgetall("a")
  53. assert.Nil(t, err)
  54. assert.EqualValues(t, map[string]string{
  55. "aa": "aaa",
  56. "bb": "bbb",
  57. }, vals)
  58. })
  59. }
  60. func TestRedis_Hvals(t *testing.T) {
  61. runOnRedis(t, func(client *Redis) {
  62. assert.NotNil(t, NewRedis(client.Addr, "").Hset("a", "aa", "aaa"))
  63. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  64. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  65. _, err := NewRedis(client.Addr, "").Hvals("a")
  66. assert.NotNil(t, err)
  67. vals, err := client.Hvals("a")
  68. assert.Nil(t, err)
  69. assert.ElementsMatch(t, []string{"aaa", "bbb"}, vals)
  70. })
  71. }
  72. func TestRedis_Hsetnx(t *testing.T) {
  73. runOnRedis(t, func(client *Redis) {
  74. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  75. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  76. _, err := NewRedis(client.Addr, "").Hsetnx("a", "bb", "ccc")
  77. assert.NotNil(t, err)
  78. ok, err := client.Hsetnx("a", "bb", "ccc")
  79. assert.Nil(t, err)
  80. assert.False(t, ok)
  81. ok, err = client.Hsetnx("a", "dd", "ddd")
  82. assert.Nil(t, err)
  83. assert.True(t, ok)
  84. vals, err := client.Hvals("a")
  85. assert.Nil(t, err)
  86. assert.ElementsMatch(t, []string{"aaa", "bbb", "ddd"}, vals)
  87. })
  88. }
  89. func TestRedis_HdelHlen(t *testing.T) {
  90. runOnRedis(t, func(client *Redis) {
  91. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  92. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  93. _, err := NewRedis(client.Addr, "").Hlen("a")
  94. assert.NotNil(t, err)
  95. num, err := client.Hlen("a")
  96. assert.Nil(t, err)
  97. assert.Equal(t, 2, num)
  98. val, err := client.Hdel("a", "aa")
  99. assert.Nil(t, err)
  100. assert.True(t, val)
  101. vals, err := client.Hvals("a")
  102. assert.Nil(t, err)
  103. assert.ElementsMatch(t, []string{"bbb"}, vals)
  104. })
  105. }
  106. func TestRedis_HIncrBy(t *testing.T) {
  107. runOnRedis(t, func(client *Redis) {
  108. _, err := NewRedis(client.Addr, "").Hincrby("key", "field", 2)
  109. assert.NotNil(t, err)
  110. val, err := client.Hincrby("key", "field", 2)
  111. assert.Nil(t, err)
  112. assert.Equal(t, 2, val)
  113. val, err = client.Hincrby("key", "field", 3)
  114. assert.Nil(t, err)
  115. assert.Equal(t, 5, val)
  116. })
  117. }
  118. func TestRedis_Hkeys(t *testing.T) {
  119. runOnRedis(t, func(client *Redis) {
  120. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  121. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  122. _, err := NewRedis(client.Addr, "").Hkeys("a")
  123. assert.NotNil(t, err)
  124. vals, err := client.Hkeys("a")
  125. assert.Nil(t, err)
  126. assert.ElementsMatch(t, []string{"aa", "bb"}, vals)
  127. })
  128. }
  129. func TestRedis_Hmget(t *testing.T) {
  130. runOnRedis(t, func(client *Redis) {
  131. assert.Nil(t, client.Hset("a", "aa", "aaa"))
  132. assert.Nil(t, client.Hset("a", "bb", "bbb"))
  133. _, err := NewRedis(client.Addr, "").Hmget("a", "aa", "bb")
  134. assert.NotNil(t, err)
  135. vals, err := client.Hmget("a", "aa", "bb")
  136. assert.Nil(t, err)
  137. assert.EqualValues(t, []string{"aaa", "bbb"}, vals)
  138. vals, err = client.Hmget("a", "aa", "no", "bb")
  139. assert.Nil(t, err)
  140. assert.EqualValues(t, []string{"aaa", "", "bbb"}, vals)
  141. })
  142. }
  143. func TestRedis_Hmset(t *testing.T) {
  144. runOnRedis(t, func(client *Redis) {
  145. assert.NotNil(t, NewRedis(client.Addr, "").Hmset("a", nil))
  146. assert.Nil(t, client.Hmset("a", map[string]string{
  147. "aa": "aaa",
  148. "bb": "bbb",
  149. }))
  150. vals, err := client.Hmget("a", "aa", "bb")
  151. assert.Nil(t, err)
  152. assert.EqualValues(t, []string{"aaa", "bbb"}, vals)
  153. })
  154. }
  155. func TestRedis_Hscan(t *testing.T) {
  156. runOnRedis(t, func(client *Redis) {
  157. key := "hash:test"
  158. fieldsAndValues := make(map[string]string)
  159. for i := 0; i < 1550; i++ {
  160. fieldsAndValues["filed_"+strconv.Itoa(i)] = randomStr(i)
  161. }
  162. err := client.Hmset(key, fieldsAndValues)
  163. assert.Nil(t, err)
  164. var cursor uint64 = 0
  165. sum := 0
  166. for {
  167. _, _, err := NewRedis(client.Addr, "").Hscan(key, cursor, "*", 100)
  168. assert.NotNil(t, err)
  169. reMap, next, err := client.Hscan(key, cursor, "*", 100)
  170. assert.Nil(t, err)
  171. sum += len(reMap)
  172. if next == 0 {
  173. break
  174. }
  175. cursor = next
  176. }
  177. assert.Equal(t, sum, 3100)
  178. _, err = NewRedis(client.Addr, "").Del(key)
  179. assert.NotNil(t, err)
  180. _, err = client.Del(key)
  181. assert.Nil(t, err)
  182. })
  183. }
  184. func TestRedis_Incr(t *testing.T) {
  185. runOnRedis(t, func(client *Redis) {
  186. _, err := NewRedis(client.Addr, "").Incr("a")
  187. assert.NotNil(t, err)
  188. val, err := client.Incr("a")
  189. assert.Nil(t, err)
  190. assert.Equal(t, int64(1), val)
  191. val, err = client.Incr("a")
  192. assert.Nil(t, err)
  193. assert.Equal(t, int64(2), val)
  194. })
  195. }
  196. func TestRedis_IncrBy(t *testing.T) {
  197. runOnRedis(t, func(client *Redis) {
  198. _, err := NewRedis(client.Addr, "").Incrby("a", 2)
  199. assert.NotNil(t, err)
  200. val, err := client.Incrby("a", 2)
  201. assert.Nil(t, err)
  202. assert.Equal(t, int64(2), val)
  203. val, err = client.Incrby("a", 3)
  204. assert.Nil(t, err)
  205. assert.Equal(t, int64(5), val)
  206. })
  207. }
  208. func TestRedis_Keys(t *testing.T) {
  209. runOnRedis(t, func(client *Redis) {
  210. err := client.Set("key1", "value1")
  211. assert.Nil(t, err)
  212. err = client.Set("key2", "value2")
  213. assert.Nil(t, err)
  214. _, err = NewRedis(client.Addr, "").Keys("*")
  215. assert.NotNil(t, err)
  216. keys, err := client.Keys("*")
  217. assert.Nil(t, err)
  218. assert.ElementsMatch(t, []string{"key1", "key2"}, keys)
  219. })
  220. }
  221. func TestRedis_HyperLogLog(t *testing.T) {
  222. runOnRedis(t, func(client *Redis) {
  223. client.Ping()
  224. r := NewRedis(client.Addr, "")
  225. _, err := r.Pfadd("key1")
  226. assert.NotNil(t, err)
  227. _, err = r.Pfcount("*")
  228. assert.NotNil(t, err)
  229. err = r.Pfmerge("*")
  230. assert.NotNil(t, err)
  231. })
  232. }
  233. func TestRedis_List(t *testing.T) {
  234. runOnRedis(t, func(client *Redis) {
  235. _, err := NewRedis(client.Addr, "").Lpush("key", "value1", "value2")
  236. assert.NotNil(t, err)
  237. val, err := client.Lpush("key", "value1", "value2")
  238. assert.Nil(t, err)
  239. assert.Equal(t, 2, val)
  240. _, err = NewRedis(client.Addr, "").Rpush("key", "value3", "value4")
  241. assert.NotNil(t, err)
  242. val, err = client.Rpush("key", "value3", "value4")
  243. assert.Nil(t, err)
  244. assert.Equal(t, 4, val)
  245. _, err = NewRedis(client.Addr, "").Llen("key")
  246. assert.NotNil(t, err)
  247. val, err = client.Llen("key")
  248. assert.Nil(t, err)
  249. assert.Equal(t, 4, val)
  250. vals, err := client.Lrange("key", 0, 10)
  251. assert.Nil(t, err)
  252. assert.EqualValues(t, []string{"value2", "value1", "value3", "value4"}, vals)
  253. _, err = NewRedis(client.Addr, "").Lpop("key")
  254. assert.NotNil(t, err)
  255. v, err := client.Lpop("key")
  256. assert.Nil(t, err)
  257. assert.Equal(t, "value2", v)
  258. val, err = client.Lpush("key", "value1", "value2")
  259. assert.Nil(t, err)
  260. assert.Equal(t, 5, val)
  261. _, err = NewRedis(client.Addr, "").Rpop("key")
  262. assert.NotNil(t, err)
  263. v, err = client.Rpop("key")
  264. assert.Nil(t, err)
  265. assert.Equal(t, "value4", v)
  266. val, err = client.Rpush("key", "value4", "value3", "value3")
  267. assert.Nil(t, err)
  268. assert.Equal(t, 7, val)
  269. _, err = NewRedis(client.Addr, "").Lrem("key", 2, "value1")
  270. assert.NotNil(t, err)
  271. n, err := client.Lrem("key", 2, "value1")
  272. assert.Nil(t, err)
  273. assert.Equal(t, 2, n)
  274. _, err = NewRedis(client.Addr, "").Lrange("key", 0, 10)
  275. assert.NotNil(t, err)
  276. vals, err = client.Lrange("key", 0, 10)
  277. assert.Nil(t, err)
  278. assert.EqualValues(t, []string{"value2", "value3", "value4", "value3", "value3"}, vals)
  279. n, err = client.Lrem("key", -2, "value3")
  280. assert.Nil(t, err)
  281. assert.Equal(t, 2, n)
  282. vals, err = client.Lrange("key", 0, 10)
  283. assert.Nil(t, err)
  284. assert.EqualValues(t, []string{"value2", "value3", "value4"}, vals)
  285. })
  286. }
  287. func TestRedis_Mget(t *testing.T) {
  288. runOnRedis(t, func(client *Redis) {
  289. err := client.Set("key1", "value1")
  290. assert.Nil(t, err)
  291. err = client.Set("key2", "value2")
  292. assert.Nil(t, err)
  293. _, err = NewRedis(client.Addr, "").Mget("key1", "key0", "key2", "key3")
  294. assert.NotNil(t, err)
  295. vals, err := client.Mget("key1", "key0", "key2", "key3")
  296. assert.Nil(t, err)
  297. assert.EqualValues(t, []string{"value1", "", "value2", ""}, vals)
  298. })
  299. }
  300. func TestRedis_SetBit(t *testing.T) {
  301. runOnRedis(t, func(client *Redis) {
  302. err := NewRedis(client.Addr, "").SetBit("key", 1, 1)
  303. assert.NotNil(t, err)
  304. err = client.SetBit("key", 1, 1)
  305. assert.Nil(t, err)
  306. })
  307. }
  308. func TestRedis_GetBit(t *testing.T) {
  309. runOnRedis(t, func(client *Redis) {
  310. err := client.SetBit("key", 2, 1)
  311. assert.Nil(t, err)
  312. _, err = NewRedis(client.Addr, "").GetBit("key", 2)
  313. assert.NotNil(t, err)
  314. val, err := client.GetBit("key", 2)
  315. assert.Nil(t, err)
  316. assert.Equal(t, 1, val)
  317. })
  318. }
  319. func TestRedis_BitCount(t *testing.T) {
  320. runOnRedis(t, func(client *Redis) {
  321. for i := 0; i < 11; i++ {
  322. err := client.SetBit("key", int64(i), 1)
  323. assert.Nil(t, err)
  324. }
  325. _, err := NewRedis(client.Addr, "").BitCount("key", 0, -1)
  326. assert.NotNil(t, err)
  327. val, err := client.BitCount("key", 0, -1)
  328. assert.Nil(t, err)
  329. assert.Equal(t, int64(11), val)
  330. val, err = client.BitCount("key", 0, 0)
  331. assert.Nil(t, err)
  332. assert.Equal(t, int64(8), val)
  333. val, err = client.BitCount("key", 1, 1)
  334. assert.Nil(t, err)
  335. assert.Equal(t, int64(3), val)
  336. val, err = client.BitCount("key", 0, 1)
  337. assert.Nil(t, err)
  338. assert.Equal(t, int64(11), val)
  339. val, err = client.BitCount("key", 2, 2)
  340. assert.Nil(t, err)
  341. assert.Equal(t, int64(0), val)
  342. })
  343. }
  344. func TestRedis_BitOpAnd(t *testing.T) {
  345. runOnRedis(t, func(client *Redis) {
  346. err := client.Set("key1", "0")
  347. assert.Nil(t, err)
  348. err = client.Set("key2", "1")
  349. assert.Nil(t, err)
  350. _, err = NewRedis(client.Addr, "").BitOpAnd("destKey", "key1", "key2")
  351. assert.NotNil(t, err)
  352. val, err := client.BitOpAnd("destKey", "key1", "key2")
  353. assert.Nil(t, err)
  354. assert.Equal(t, int64(1), val)
  355. valStr, err := client.Get("destKey")
  356. assert.Nil(t, err)
  357. //destKey binary 110000 ascii 0
  358. assert.Equal(t, "0", valStr)
  359. })
  360. }
  361. func TestRedis_BitOpNot(t *testing.T) {
  362. runOnRedis(t, func(client *Redis) {
  363. err := client.Set("key1", "\u0000")
  364. assert.Nil(t, err)
  365. _, err = NewRedis(client.Addr, "").BitOpNot("destKey", "key1")
  366. assert.NotNil(t, err)
  367. val, err := client.BitOpNot("destKey", "key1")
  368. assert.Nil(t, err)
  369. assert.Equal(t, int64(1), val)
  370. valStr, err := client.Get("destKey")
  371. assert.Nil(t, err)
  372. assert.Equal(t, "\xff", valStr)
  373. })
  374. }
  375. func TestRedis_BitOpOr(t *testing.T) {
  376. runOnRedis(t, func(client *Redis) {
  377. err := client.Set("key1", "1")
  378. assert.Nil(t, err)
  379. err = client.Set("key2", "0")
  380. assert.Nil(t, err)
  381. _, err = NewRedis(client.Addr, "").BitOpOr("destKey", "key1", "key2")
  382. assert.NotNil(t, err)
  383. val, err := client.BitOpOr("destKey", "key1", "key2")
  384. assert.Nil(t, err)
  385. assert.Equal(t, int64(1), val)
  386. valStr, err := client.Get("destKey")
  387. assert.Nil(t, err)
  388. assert.Equal(t, "1", valStr)
  389. })
  390. }
  391. func TestRedis_BitOpXor(t *testing.T) {
  392. runOnRedis(t, func(client *Redis) {
  393. err := client.Set("key1", "\xff")
  394. assert.Nil(t, err)
  395. err = client.Set("key2", "\x0f")
  396. assert.Nil(t, err)
  397. _, err = NewRedis(client.Addr, "").BitOpXor("destKey", "key1", "key2")
  398. assert.NotNil(t, err)
  399. val, err := client.BitOpXor("destKey", "key1", "key2")
  400. assert.Nil(t, err)
  401. assert.Equal(t, int64(1), val)
  402. valStr, err := client.Get("destKey")
  403. assert.Nil(t, err)
  404. assert.Equal(t, "\xf0", valStr)
  405. })
  406. }
  407. func TestRedis_BitPos(t *testing.T) {
  408. runOnRedis(t, func(client *Redis) {
  409. //11111111 11110000 00000000
  410. err := client.Set("key", "\xff\xf0\x00")
  411. assert.Nil(t, err)
  412. _, err = NewRedis(client.Addr, "").BitPos("key", 0, 0, -1)
  413. assert.NotNil(t, err)
  414. val, err := client.BitPos("key", 0, 0, 2)
  415. assert.Nil(t, err)
  416. assert.Equal(t, int64(12), val)
  417. val, err = client.BitPos("key", 1, 0, 2)
  418. assert.Nil(t, err)
  419. assert.Equal(t, int64(0), val)
  420. val, err = client.BitPos("key", 0, 1, 2)
  421. assert.Nil(t, err)
  422. assert.Equal(t, int64(12), val)
  423. val, err = client.BitPos("key", 1, 1, 2)
  424. assert.Nil(t, err)
  425. assert.Equal(t, int64(8), val)
  426. val, err = client.BitPos("key", 1, 2, 2)
  427. assert.Nil(t, err)
  428. assert.Equal(t, int64(-1), val)
  429. })
  430. }
  431. func TestRedis_Persist(t *testing.T) {
  432. runOnRedis(t, func(client *Redis) {
  433. _, err := NewRedis(client.Addr, "").Persist("key")
  434. assert.NotNil(t, err)
  435. ok, err := client.Persist("key")
  436. assert.Nil(t, err)
  437. assert.False(t, ok)
  438. err = client.Set("key", "value")
  439. assert.Nil(t, err)
  440. ok, err = client.Persist("key")
  441. assert.Nil(t, err)
  442. assert.False(t, ok)
  443. err = NewRedis(client.Addr, "").Expire("key", 5)
  444. assert.NotNil(t, err)
  445. err = client.Expire("key", 5)
  446. assert.Nil(t, err)
  447. ok, err = client.Persist("key")
  448. assert.Nil(t, err)
  449. assert.True(t, ok)
  450. err = NewRedis(client.Addr, "").Expireat("key", time.Now().Unix()+5)
  451. assert.NotNil(t, err)
  452. err = client.Expireat("key", time.Now().Unix()+5)
  453. assert.Nil(t, err)
  454. ok, err = client.Persist("key")
  455. assert.Nil(t, err)
  456. assert.True(t, ok)
  457. })
  458. }
  459. func TestRedis_Ping(t *testing.T) {
  460. runOnRedis(t, func(client *Redis) {
  461. ok := client.Ping()
  462. assert.True(t, ok)
  463. })
  464. }
  465. func TestRedis_Scan(t *testing.T) {
  466. runOnRedis(t, func(client *Redis) {
  467. err := client.Set("key1", "value1")
  468. assert.Nil(t, err)
  469. err = client.Set("key2", "value2")
  470. assert.Nil(t, err)
  471. _, _, err = NewRedis(client.Addr, "").Scan(0, "*", 100)
  472. assert.NotNil(t, err)
  473. keys, _, err := client.Scan(0, "*", 100)
  474. assert.Nil(t, err)
  475. assert.ElementsMatch(t, []string{"key1", "key2"}, keys)
  476. })
  477. }
  478. func TestRedis_Sscan(t *testing.T) {
  479. runOnRedis(t, func(client *Redis) {
  480. key := "list"
  481. var list []string
  482. for i := 0; i < 1550; i++ {
  483. list = append(list, randomStr(i))
  484. }
  485. lens, err := client.Sadd(key, list)
  486. assert.Nil(t, err)
  487. assert.Equal(t, lens, 1550)
  488. var cursor uint64 = 0
  489. sum := 0
  490. for {
  491. _, _, err := NewRedis(client.Addr, "").Sscan(key, cursor, "", 100)
  492. assert.NotNil(t, err)
  493. keys, next, err := client.Sscan(key, cursor, "", 100)
  494. assert.Nil(t, err)
  495. sum += len(keys)
  496. if next == 0 {
  497. break
  498. }
  499. cursor = next
  500. }
  501. assert.Equal(t, sum, 1550)
  502. _, err = NewRedis(client.Addr, "").Del(key)
  503. assert.NotNil(t, err)
  504. _, err = client.Del(key)
  505. assert.Nil(t, err)
  506. })
  507. }
  508. func TestRedis_Set(t *testing.T) {
  509. runOnRedis(t, func(client *Redis) {
  510. _, err := NewRedis(client.Addr, "").Sadd("key", 1, 2, 3, 4)
  511. assert.NotNil(t, err)
  512. num, err := client.Sadd("key", 1, 2, 3, 4)
  513. assert.Nil(t, err)
  514. assert.Equal(t, 4, num)
  515. _, err = NewRedis(client.Addr, "").Scard("key")
  516. assert.NotNil(t, err)
  517. val, err := client.Scard("key")
  518. assert.Nil(t, err)
  519. assert.Equal(t, int64(4), val)
  520. _, err = NewRedis(client.Addr, "").Sismember("key", 2)
  521. assert.NotNil(t, err)
  522. ok, err := client.Sismember("key", 2)
  523. assert.Nil(t, err)
  524. assert.True(t, ok)
  525. _, err = NewRedis(client.Addr, "").Srem("key", 3, 4)
  526. assert.NotNil(t, err)
  527. num, err = client.Srem("key", 3, 4)
  528. assert.Nil(t, err)
  529. assert.Equal(t, 2, num)
  530. _, err = NewRedis(client.Addr, "").Smembers("key")
  531. assert.NotNil(t, err)
  532. vals, err := client.Smembers("key")
  533. assert.Nil(t, err)
  534. assert.ElementsMatch(t, []string{"1", "2"}, vals)
  535. _, err = NewRedis(client.Addr, "").Srandmember("key", 1)
  536. assert.NotNil(t, err)
  537. members, err := client.Srandmember("key", 1)
  538. assert.Nil(t, err)
  539. assert.Len(t, members, 1)
  540. assert.Contains(t, []string{"1", "2"}, members[0])
  541. _, err = NewRedis(client.Addr, "").Spop("key")
  542. assert.NotNil(t, err)
  543. member, err := client.Spop("key")
  544. assert.Nil(t, err)
  545. assert.Contains(t, []string{"1", "2"}, member)
  546. _, err = NewRedis(client.Addr, "").Smembers("key")
  547. assert.NotNil(t, err)
  548. vals, err = client.Smembers("key")
  549. assert.Nil(t, err)
  550. assert.NotContains(t, vals, member)
  551. _, err = NewRedis(client.Addr, "").Sadd("key1", 1, 2, 3, 4)
  552. assert.NotNil(t, err)
  553. num, err = client.Sadd("key1", 1, 2, 3, 4)
  554. assert.Nil(t, err)
  555. assert.Equal(t, 4, num)
  556. num, err = client.Sadd("key2", 2, 3, 4, 5)
  557. assert.Nil(t, err)
  558. assert.Equal(t, 4, num)
  559. _, err = NewRedis(client.Addr, "").Sunion("key1", "key2")
  560. assert.NotNil(t, err)
  561. vals, err = client.Sunion("key1", "key2")
  562. assert.Nil(t, err)
  563. assert.ElementsMatch(t, []string{"1", "2", "3", "4", "5"}, vals)
  564. _, err = NewRedis(client.Addr, "").Sunionstore("key3", "key1", "key2")
  565. assert.NotNil(t, err)
  566. num, err = client.Sunionstore("key3", "key1", "key2")
  567. assert.Nil(t, err)
  568. assert.Equal(t, 5, num)
  569. _, err = NewRedis(client.Addr, "").Sdiff("key1", "key2")
  570. assert.NotNil(t, err)
  571. vals, err = client.Sdiff("key1", "key2")
  572. assert.Nil(t, err)
  573. assert.EqualValues(t, []string{"1"}, vals)
  574. _, err = NewRedis(client.Addr, "").Sdiffstore("key4", "key1", "key2")
  575. assert.NotNil(t, err)
  576. num, err = client.Sdiffstore("key4", "key1", "key2")
  577. assert.Nil(t, err)
  578. assert.Equal(t, 1, num)
  579. })
  580. }
  581. func TestRedis_SetGetDel(t *testing.T) {
  582. runOnRedis(t, func(client *Redis) {
  583. err := NewRedis(client.Addr, "").Set("hello", "world")
  584. assert.NotNil(t, err)
  585. err = client.Set("hello", "world")
  586. assert.Nil(t, err)
  587. _, err = NewRedis(client.Addr, "").Get("hello")
  588. assert.NotNil(t, err)
  589. val, err := client.Get("hello")
  590. assert.Nil(t, err)
  591. assert.Equal(t, "world", val)
  592. ret, err := client.Del("hello")
  593. assert.Nil(t, err)
  594. assert.Equal(t, 1, ret)
  595. })
  596. }
  597. func TestRedis_SetExNx(t *testing.T) {
  598. runOnRedis(t, func(client *Redis) {
  599. err := NewRedis(client.Addr, "").Setex("hello", "world", 5)
  600. assert.NotNil(t, err)
  601. err = client.Setex("hello", "world", 5)
  602. assert.Nil(t, err)
  603. _, err = NewRedis(client.Addr, "").Setnx("hello", "newworld")
  604. assert.NotNil(t, err)
  605. ok, err := client.Setnx("hello", "newworld")
  606. assert.Nil(t, err)
  607. assert.False(t, ok)
  608. ok, err = client.Setnx("newhello", "newworld")
  609. assert.Nil(t, err)
  610. assert.True(t, ok)
  611. val, err := client.Get("hello")
  612. assert.Nil(t, err)
  613. assert.Equal(t, "world", val)
  614. val, err = client.Get("newhello")
  615. assert.Nil(t, err)
  616. assert.Equal(t, "newworld", val)
  617. ttl, err := client.Ttl("hello")
  618. assert.Nil(t, err)
  619. assert.True(t, ttl > 0)
  620. _, err = NewRedis(client.Addr, "").SetnxEx("newhello", "newworld", 5)
  621. assert.NotNil(t, err)
  622. ok, err = client.SetnxEx("newhello", "newworld", 5)
  623. assert.Nil(t, err)
  624. assert.False(t, ok)
  625. num, err := client.Del("newhello")
  626. assert.Nil(t, err)
  627. assert.Equal(t, 1, num)
  628. ok, err = client.SetnxEx("newhello", "newworld", 5)
  629. assert.Nil(t, err)
  630. assert.True(t, ok)
  631. val, err = client.Get("newhello")
  632. assert.Nil(t, err)
  633. assert.Equal(t, "newworld", val)
  634. })
  635. }
  636. func TestRedis_SetGetDelHashField(t *testing.T) {
  637. runOnRedis(t, func(client *Redis) {
  638. err := client.Hset("key", "field", "value")
  639. assert.Nil(t, err)
  640. _, err = NewRedis(client.Addr, "").Hget("key", "field")
  641. assert.NotNil(t, err)
  642. val, err := client.Hget("key", "field")
  643. assert.Nil(t, err)
  644. assert.Equal(t, "value", val)
  645. _, err = NewRedis(client.Addr, "").Hexists("key", "field")
  646. assert.NotNil(t, err)
  647. ok, err := client.Hexists("key", "field")
  648. assert.Nil(t, err)
  649. assert.True(t, ok)
  650. _, err = NewRedis(client.Addr, "").Hdel("key", "field")
  651. assert.NotNil(t, err)
  652. ret, err := client.Hdel("key", "field")
  653. assert.Nil(t, err)
  654. assert.True(t, ret)
  655. ok, err = client.Hexists("key", "field")
  656. assert.Nil(t, err)
  657. assert.False(t, ok)
  658. })
  659. }
  660. func TestRedis_SortedSet(t *testing.T) {
  661. runOnRedis(t, func(client *Redis) {
  662. ok, err := client.Zadd("key", 1, "value1")
  663. assert.Nil(t, err)
  664. assert.True(t, ok)
  665. ok, err = client.Zadd("key", 2, "value1")
  666. assert.Nil(t, err)
  667. assert.False(t, ok)
  668. val, err := client.Zscore("key", "value1")
  669. assert.Nil(t, err)
  670. assert.Equal(t, int64(2), val)
  671. _, err = NewRedis(client.Addr, "").Zincrby("key", 3, "value1")
  672. assert.NotNil(t, err)
  673. val, err = client.Zincrby("key", 3, "value1")
  674. assert.Nil(t, err)
  675. assert.Equal(t, int64(5), val)
  676. _, err = NewRedis(client.Addr, "").Zscore("key", "value1")
  677. assert.NotNil(t, err)
  678. val, err = client.Zscore("key", "value1")
  679. assert.Nil(t, err)
  680. assert.Equal(t, int64(5), val)
  681. _, err = NewRedis(client.Addr, "").Zadds("key")
  682. assert.NotNil(t, err)
  683. val, err = client.Zadds("key", Pair{
  684. Key: "value2",
  685. Score: 6,
  686. }, Pair{
  687. Key: "value3",
  688. Score: 7,
  689. })
  690. assert.Nil(t, err)
  691. assert.Equal(t, int64(2), val)
  692. _, err = NewRedis(client.Addr, "").ZRevRangeWithScores("key", 1, 3)
  693. assert.NotNil(t, err)
  694. pairs, err := client.ZRevRangeWithScores("key", 1, 3)
  695. assert.Nil(t, err)
  696. assert.EqualValues(t, []Pair{
  697. {
  698. Key: "value2",
  699. Score: 6,
  700. },
  701. {
  702. Key: "value1",
  703. Score: 5,
  704. },
  705. }, pairs)
  706. rank, err := client.Zrank("key", "value2")
  707. assert.Nil(t, err)
  708. assert.Equal(t, int64(1), rank)
  709. rank, err = client.Zrevrank("key", "value1")
  710. assert.Nil(t, err)
  711. assert.Equal(t, int64(2), rank)
  712. _, err = NewRedis(client.Addr, "").Zrank("key", "value4")
  713. assert.NotNil(t, err)
  714. _, err = client.Zrank("key", "value4")
  715. assert.Equal(t, Nil, err)
  716. _, err = NewRedis(client.Addr, "").Zrem("key", "value2", "value3")
  717. assert.NotNil(t, err)
  718. num, err := client.Zrem("key", "value2", "value3")
  719. assert.Nil(t, err)
  720. assert.Equal(t, 2, num)
  721. ok, err = client.Zadd("key", 6, "value2")
  722. assert.Nil(t, err)
  723. assert.True(t, ok)
  724. ok, err = client.Zadd("key", 7, "value3")
  725. assert.Nil(t, err)
  726. assert.True(t, ok)
  727. ok, err = client.Zadd("key", 8, "value4")
  728. assert.Nil(t, err)
  729. assert.True(t, ok)
  730. _, err = NewRedis(client.Addr, "").Zremrangebyscore("key", 6, 7)
  731. assert.NotNil(t, err)
  732. num, err = client.Zremrangebyscore("key", 6, 7)
  733. assert.Nil(t, err)
  734. assert.Equal(t, 2, num)
  735. ok, err = client.Zadd("key", 6, "value2")
  736. assert.Nil(t, err)
  737. assert.True(t, ok)
  738. _, err = NewRedis(client.Addr, "").Zadd("key", 7, "value3")
  739. assert.NotNil(t, err)
  740. ok, err = client.Zadd("key", 7, "value3")
  741. assert.Nil(t, err)
  742. assert.True(t, ok)
  743. _, err = NewRedis(client.Addr, "").Zcount("key", 6, 7)
  744. assert.NotNil(t, err)
  745. num, err = client.Zcount("key", 6, 7)
  746. assert.Nil(t, err)
  747. assert.Equal(t, 2, num)
  748. _, err = NewRedis(client.Addr, "").Zremrangebyrank("key", 1, 2)
  749. assert.NotNil(t, err)
  750. num, err = client.Zremrangebyrank("key", 1, 2)
  751. assert.Nil(t, err)
  752. assert.Equal(t, 2, num)
  753. _, err = NewRedis(client.Addr, "").Zcard("key")
  754. assert.NotNil(t, err)
  755. card, err := client.Zcard("key")
  756. assert.Nil(t, err)
  757. assert.Equal(t, 2, card)
  758. _, err = NewRedis(client.Addr, "").Zrange("key", 0, -1)
  759. assert.NotNil(t, err)
  760. vals, err := client.Zrange("key", 0, -1)
  761. assert.Nil(t, err)
  762. assert.EqualValues(t, []string{"value1", "value4"}, vals)
  763. _, err = NewRedis(client.Addr, "").Zrevrange("key", 0, -1)
  764. assert.NotNil(t, err)
  765. vals, err = client.Zrevrange("key", 0, -1)
  766. assert.Nil(t, err)
  767. assert.EqualValues(t, []string{"value4", "value1"}, vals)
  768. _, err = NewRedis(client.Addr, "").ZrangeWithScores("key", 0, -1)
  769. assert.NotNil(t, err)
  770. pairs, err = client.ZrangeWithScores("key", 0, -1)
  771. assert.Nil(t, err)
  772. assert.EqualValues(t, []Pair{
  773. {
  774. Key: "value1",
  775. Score: 5,
  776. },
  777. {
  778. Key: "value4",
  779. Score: 8,
  780. },
  781. }, pairs)
  782. _, err = NewRedis(client.Addr, "").ZrangebyscoreWithScores("key", 5, 8)
  783. assert.NotNil(t, err)
  784. pairs, err = client.ZrangebyscoreWithScores("key", 5, 8)
  785. assert.Nil(t, err)
  786. assert.EqualValues(t, []Pair{
  787. {
  788. Key: "value1",
  789. Score: 5,
  790. },
  791. {
  792. Key: "value4",
  793. Score: 8,
  794. },
  795. }, pairs)
  796. _, err = NewRedis(client.Addr, "").ZrangebyscoreWithScoresAndLimit(
  797. "key", 5, 8, 1, 1)
  798. assert.NotNil(t, err)
  799. pairs, err = client.ZrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1)
  800. assert.Nil(t, err)
  801. assert.EqualValues(t, []Pair{
  802. {
  803. Key: "value4",
  804. Score: 8,
  805. },
  806. }, pairs)
  807. pairs, err = client.ZrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 0)
  808. assert.Nil(t, err)
  809. assert.Equal(t, 0, len(pairs))
  810. _, err = NewRedis(client.Addr, "").ZrevrangebyscoreWithScores("key", 5, 8)
  811. assert.NotNil(t, err)
  812. pairs, err = client.ZrevrangebyscoreWithScores("key", 5, 8)
  813. assert.Nil(t, err)
  814. assert.EqualValues(t, []Pair{
  815. {
  816. Key: "value4",
  817. Score: 8,
  818. },
  819. {
  820. Key: "value1",
  821. Score: 5,
  822. },
  823. }, pairs)
  824. _, err = NewRedis(client.Addr, "").ZrevrangebyscoreWithScoresAndLimit(
  825. "key", 5, 8, 1, 1)
  826. assert.NotNil(t, err)
  827. pairs, err = client.ZrevrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1)
  828. assert.Nil(t, err)
  829. assert.EqualValues(t, []Pair{
  830. {
  831. Key: "value1",
  832. Score: 5,
  833. },
  834. }, pairs)
  835. pairs, err = client.ZrevrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 0)
  836. assert.Nil(t, err)
  837. assert.Equal(t, 0, len(pairs))
  838. _, err = NewRedis(client.Addr, "").Zrevrank("key", "value")
  839. assert.NotNil(t, err)
  840. client.Zadd("second", 2, "aa")
  841. client.Zadd("third", 3, "bbb")
  842. val, err = client.Zunionstore("union", ZStore{
  843. Weights: []float64{1, 2},
  844. Aggregate: "SUM",
  845. }, "second", "third")
  846. assert.Nil(t, err)
  847. assert.Equal(t, int64(2), val)
  848. vals, err = client.Zrange("union", 0, 10000)
  849. assert.Nil(t, err)
  850. assert.EqualValues(t, []string{"aa", "bbb"}, vals)
  851. ival, err := client.Zcard("union")
  852. assert.Nil(t, err)
  853. assert.Equal(t, 2, ival)
  854. })
  855. }
  856. func TestRedis_Pipelined(t *testing.T) {
  857. runOnRedis(t, func(client *Redis) {
  858. assert.NotNil(t, NewRedis(client.Addr, "").Pipelined(func(pipeliner Pipeliner) error {
  859. return nil
  860. }))
  861. err := client.Pipelined(
  862. func(pipe Pipeliner) error {
  863. pipe.Incr("pipelined_counter")
  864. pipe.Expire("pipelined_counter", time.Hour)
  865. pipe.ZAdd("zadd", Z{Score: 12, Member: "zadd"})
  866. return nil
  867. },
  868. )
  869. assert.Nil(t, err)
  870. _, err = NewRedis(client.Addr, "").Ttl("pipelined_counter")
  871. assert.NotNil(t, err)
  872. ttl, err := client.Ttl("pipelined_counter")
  873. assert.Nil(t, err)
  874. assert.Equal(t, 3600, ttl)
  875. value, err := client.Get("pipelined_counter")
  876. assert.Nil(t, err)
  877. assert.Equal(t, "1", value)
  878. score, err := client.Zscore("zadd", "zadd")
  879. assert.Nil(t, err)
  880. assert.Equal(t, int64(12), score)
  881. })
  882. }
  883. func TestRedisString(t *testing.T) {
  884. runOnRedis(t, func(client *Redis) {
  885. client.Ping()
  886. _, err := getRedis(NewRedis(client.Addr, ClusterType))
  887. assert.Nil(t, err)
  888. assert.Equal(t, client.Addr, client.String())
  889. assert.NotNil(t, NewRedis(client.Addr, "").Ping())
  890. })
  891. }
  892. func TestRedisScriptLoad(t *testing.T) {
  893. runOnRedis(t, func(client *Redis) {
  894. client.Ping()
  895. _, err := NewRedis(client.Addr, "").ScriptLoad("foo")
  896. assert.NotNil(t, err)
  897. _, err = client.ScriptLoad("foo")
  898. assert.NotNil(t, err)
  899. })
  900. }
  901. func TestRedisEvalSha(t *testing.T) {
  902. runOnRedis(t, func(client *Redis) {
  903. client.Ping()
  904. scriptHash, err := client.ScriptLoad(`return redis.call("EXISTS", KEYS[1])`)
  905. assert.Nil(t, err)
  906. result, err := client.EvalSha(scriptHash, []string{"key1"})
  907. assert.Nil(t, err)
  908. assert.Equal(t, int64(0), result)
  909. })
  910. }
  911. func TestRedisToPairs(t *testing.T) {
  912. pairs := toPairs([]red.Z{
  913. {
  914. Member: 1,
  915. Score: 1,
  916. },
  917. {
  918. Member: 2,
  919. Score: 2,
  920. },
  921. })
  922. assert.EqualValues(t, []Pair{
  923. {
  924. Key: "1",
  925. Score: 1,
  926. },
  927. {
  928. Key: "2",
  929. Score: 2,
  930. },
  931. }, pairs)
  932. }
  933. func TestRedisToStrings(t *testing.T) {
  934. vals := toStrings([]interface{}{1, 2})
  935. assert.EqualValues(t, []string{"1", "2"}, vals)
  936. }
  937. func TestRedisBlpop(t *testing.T) {
  938. runOnRedis(t, func(client *Redis) {
  939. client.Ping()
  940. var node mockedNode
  941. _, err := client.Blpop(nil, "foo")
  942. assert.NotNil(t, err)
  943. _, err = client.Blpop(node, "foo")
  944. assert.NotNil(t, err)
  945. })
  946. }
  947. func TestRedisBlpopEx(t *testing.T) {
  948. runOnRedis(t, func(client *Redis) {
  949. client.Ping()
  950. var node mockedNode
  951. _, _, err := client.BlpopEx(nil, "foo")
  952. assert.NotNil(t, err)
  953. _, _, err = client.BlpopEx(node, "foo")
  954. assert.NotNil(t, err)
  955. })
  956. }
  957. func TestRedisGeo(t *testing.T) {
  958. runOnRedis(t, func(client *Redis) {
  959. client.Ping()
  960. var geoLocation = []*GeoLocation{{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"}, {Longitude: 15.087269, Latitude: 37.502669, Name: "Catania"}}
  961. v, err := client.GeoAdd("sicily", geoLocation...)
  962. assert.Nil(t, err)
  963. assert.Equal(t, int64(2), v)
  964. v2, err := client.GeoDist("sicily", "Palermo", "Catania", "m")
  965. assert.Nil(t, err)
  966. assert.Equal(t, 166274, int(v2))
  967. // GeoHash not support
  968. v3, err := client.GeoPos("sicily", "Palermo", "Catania")
  969. assert.Nil(t, err)
  970. assert.Equal(t, int64(v3[0].Longitude), int64(13))
  971. assert.Equal(t, int64(v3[0].Latitude), int64(38))
  972. assert.Equal(t, int64(v3[1].Longitude), int64(15))
  973. assert.Equal(t, int64(v3[1].Latitude), int64(37))
  974. v4, err := client.GeoRadius("sicily", 15, 37, &red.GeoRadiusQuery{WithDist: true, Unit: "km", Radius: 200})
  975. assert.Nil(t, err)
  976. assert.Equal(t, int64(v4[0].Dist), int64(190))
  977. assert.Equal(t, int64(v4[1].Dist), int64(56))
  978. var geoLocation2 = []*GeoLocation{{Longitude: 13.583333, Latitude: 37.316667, Name: "Agrigento"}}
  979. v5, err := client.GeoAdd("sicily", geoLocation2...)
  980. assert.Nil(t, err)
  981. assert.Equal(t, int64(1), v5)
  982. v6, err := client.GeoRadiusByMember("sicily", "Agrigento", &red.GeoRadiusQuery{Unit: "km", Radius: 100})
  983. assert.Nil(t, err)
  984. assert.Equal(t, v6[0].Name, "Agrigento")
  985. assert.Equal(t, v6[1].Name, "Palermo")
  986. })
  987. }
  988. func runOnRedis(t *testing.T, fn func(client *Redis)) {
  989. s, err := miniredis.Run()
  990. assert.Nil(t, err)
  991. defer func() {
  992. client, err := clientManager.GetResource(s.Addr(), func() (io.Closer, error) {
  993. return nil, errors.New("should already exist")
  994. })
  995. if err != nil {
  996. t.Error(err)
  997. }
  998. if client != nil {
  999. client.Close()
  1000. }
  1001. }()
  1002. fn(NewRedis(s.Addr(), NodeType))
  1003. }
  1004. type mockedNode struct {
  1005. RedisNode
  1006. }
  1007. func (n mockedNode) BLPop(timeout time.Duration, keys ...string) *red.StringSliceCmd {
  1008. return red.NewStringSliceCmd("foo", "bar")
  1009. }