redis_test.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 TestRedisToPairs(t *testing.T) {
  902. pairs := toPairs([]red.Z{
  903. {
  904. Member: 1,
  905. Score: 1,
  906. },
  907. {
  908. Member: 2,
  909. Score: 2,
  910. },
  911. })
  912. assert.EqualValues(t, []Pair{
  913. {
  914. Key: "1",
  915. Score: 1,
  916. },
  917. {
  918. Key: "2",
  919. Score: 2,
  920. },
  921. }, pairs)
  922. }
  923. func TestRedisToStrings(t *testing.T) {
  924. vals := toStrings([]interface{}{1, 2})
  925. assert.EqualValues(t, []string{"1", "2"}, vals)
  926. }
  927. func TestRedisBlpop(t *testing.T) {
  928. runOnRedis(t, func(client *Redis) {
  929. client.Ping()
  930. var node mockedNode
  931. _, err := client.Blpop(nil, "foo")
  932. assert.NotNil(t, err)
  933. _, err = client.Blpop(node, "foo")
  934. assert.NotNil(t, err)
  935. })
  936. }
  937. func TestRedisBlpopEx(t *testing.T) {
  938. runOnRedis(t, func(client *Redis) {
  939. client.Ping()
  940. var node mockedNode
  941. _, _, err := client.BlpopEx(nil, "foo")
  942. assert.NotNil(t, err)
  943. _, _, err = client.BlpopEx(node, "foo")
  944. assert.NotNil(t, err)
  945. })
  946. }
  947. func TestRedisGeo(t *testing.T) {
  948. runOnRedis(t, func(client *Redis) {
  949. client.Ping()
  950. var geoLocation = []*GeoLocation{{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"}, {Longitude: 15.087269, Latitude: 37.502669, Name: "Catania"}}
  951. v, err := client.GeoAdd("sicily", geoLocation...)
  952. assert.Nil(t, err)
  953. assert.Equal(t, int64(2), v)
  954. v2, err := client.GeoDist("sicily", "Palermo", "Catania", "m")
  955. assert.Nil(t, err)
  956. assert.Equal(t, 166274, int(v2))
  957. // GeoHash not support
  958. v3, err := client.GeoPos("sicily", "Palermo", "Catania")
  959. assert.Nil(t, err)
  960. assert.Equal(t, int64(v3[0].Longitude), int64(13))
  961. assert.Equal(t, int64(v3[0].Latitude), int64(38))
  962. assert.Equal(t, int64(v3[1].Longitude), int64(15))
  963. assert.Equal(t, int64(v3[1].Latitude), int64(37))
  964. v4, err := client.GeoRadius("sicily", 15, 37, &red.GeoRadiusQuery{WithDist: true, Unit: "km", Radius: 200})
  965. assert.Nil(t, err)
  966. assert.Equal(t, int64(v4[0].Dist), int64(190))
  967. assert.Equal(t, int64(v4[1].Dist), int64(56))
  968. var geoLocation2 = []*GeoLocation{{Longitude: 13.583333, Latitude: 37.316667, Name: "Agrigento"}}
  969. v5, err := client.GeoAdd("sicily", geoLocation2...)
  970. assert.Nil(t, err)
  971. assert.Equal(t, int64(1), v5)
  972. v6, err := client.GeoRadiusByMember("sicily", "Agrigento", &red.GeoRadiusQuery{Unit: "km", Radius: 100})
  973. assert.Nil(t, err)
  974. assert.Equal(t, v6[0].Name, "Agrigento")
  975. assert.Equal(t, v6[1].Name, "Palermo")
  976. })
  977. }
  978. func runOnRedis(t *testing.T, fn func(client *Redis)) {
  979. s, err := miniredis.Run()
  980. assert.Nil(t, err)
  981. defer func() {
  982. client, err := clientManager.GetResource(s.Addr(), func() (io.Closer, error) {
  983. return nil, errors.New("should already exist")
  984. })
  985. if err != nil {
  986. t.Error(err)
  987. }
  988. if client != nil {
  989. client.Close()
  990. }
  991. }()
  992. fn(NewRedis(s.Addr(), NodeType))
  993. }
  994. type mockedNode struct {
  995. RedisNode
  996. }
  997. func (n mockedNode) BLPop(timeout time.Duration, keys ...string) *red.StringSliceCmd {
  998. return red.NewStringSliceCmd("foo", "bar")
  999. }