redis_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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_Persist(t *testing.T) {
  320. runOnRedis(t, func(client *Redis) {
  321. _, err := NewRedis(client.Addr, "").Persist("key")
  322. assert.NotNil(t, err)
  323. ok, err := client.Persist("key")
  324. assert.Nil(t, err)
  325. assert.False(t, ok)
  326. err = client.Set("key", "value")
  327. assert.Nil(t, err)
  328. ok, err = client.Persist("key")
  329. assert.Nil(t, err)
  330. assert.False(t, ok)
  331. err = NewRedis(client.Addr, "").Expire("key", 5)
  332. assert.NotNil(t, err)
  333. err = client.Expire("key", 5)
  334. assert.Nil(t, err)
  335. ok, err = client.Persist("key")
  336. assert.Nil(t, err)
  337. assert.True(t, ok)
  338. err = NewRedis(client.Addr, "").Expireat("key", time.Now().Unix()+5)
  339. assert.NotNil(t, err)
  340. err = client.Expireat("key", time.Now().Unix()+5)
  341. assert.Nil(t, err)
  342. ok, err = client.Persist("key")
  343. assert.Nil(t, err)
  344. assert.True(t, ok)
  345. })
  346. }
  347. func TestRedis_Ping(t *testing.T) {
  348. runOnRedis(t, func(client *Redis) {
  349. ok := client.Ping()
  350. assert.True(t, ok)
  351. })
  352. }
  353. func TestRedis_Scan(t *testing.T) {
  354. runOnRedis(t, func(client *Redis) {
  355. err := client.Set("key1", "value1")
  356. assert.Nil(t, err)
  357. err = client.Set("key2", "value2")
  358. assert.Nil(t, err)
  359. _, _, err = NewRedis(client.Addr, "").Scan(0, "*", 100)
  360. assert.NotNil(t, err)
  361. keys, _, err := client.Scan(0, "*", 100)
  362. assert.Nil(t, err)
  363. assert.ElementsMatch(t, []string{"key1", "key2"}, keys)
  364. })
  365. }
  366. func TestRedis_Sscan(t *testing.T) {
  367. runOnRedis(t, func(client *Redis) {
  368. key := "list"
  369. var list []string
  370. for i := 0; i < 1550; i++ {
  371. list = append(list, randomStr(i))
  372. }
  373. lens, err := client.Sadd(key, list)
  374. assert.Nil(t, err)
  375. assert.Equal(t, lens, 1550)
  376. var cursor uint64 = 0
  377. sum := 0
  378. for {
  379. _, _, err := NewRedis(client.Addr, "").Sscan(key, cursor, "", 100)
  380. assert.NotNil(t, err)
  381. keys, next, err := client.Sscan(key, cursor, "", 100)
  382. assert.Nil(t, err)
  383. sum += len(keys)
  384. if next == 0 {
  385. break
  386. }
  387. cursor = next
  388. }
  389. assert.Equal(t, sum, 1550)
  390. _, err = NewRedis(client.Addr, "").Del(key)
  391. assert.NotNil(t, err)
  392. _, err = client.Del(key)
  393. assert.Nil(t, err)
  394. })
  395. }
  396. func TestRedis_Set(t *testing.T) {
  397. runOnRedis(t, func(client *Redis) {
  398. _, err := NewRedis(client.Addr, "").Sadd("key", 1, 2, 3, 4)
  399. assert.NotNil(t, err)
  400. num, err := client.Sadd("key", 1, 2, 3, 4)
  401. assert.Nil(t, err)
  402. assert.Equal(t, 4, num)
  403. _, err = NewRedis(client.Addr, "").Scard("key")
  404. assert.NotNil(t, err)
  405. val, err := client.Scard("key")
  406. assert.Nil(t, err)
  407. assert.Equal(t, int64(4), val)
  408. _, err = NewRedis(client.Addr, "").Sismember("key", 2)
  409. assert.NotNil(t, err)
  410. ok, err := client.Sismember("key", 2)
  411. assert.Nil(t, err)
  412. assert.True(t, ok)
  413. _, err = NewRedis(client.Addr, "").Srem("key", 3, 4)
  414. assert.NotNil(t, err)
  415. num, err = client.Srem("key", 3, 4)
  416. assert.Nil(t, err)
  417. assert.Equal(t, 2, num)
  418. _, err = NewRedis(client.Addr, "").Smembers("key")
  419. assert.NotNil(t, err)
  420. vals, err := client.Smembers("key")
  421. assert.Nil(t, err)
  422. assert.ElementsMatch(t, []string{"1", "2"}, vals)
  423. _, err = NewRedis(client.Addr, "").Srandmember("key", 1)
  424. assert.NotNil(t, err)
  425. members, err := client.Srandmember("key", 1)
  426. assert.Nil(t, err)
  427. assert.Len(t, members, 1)
  428. assert.Contains(t, []string{"1", "2"}, members[0])
  429. _, err = NewRedis(client.Addr, "").Spop("key")
  430. assert.NotNil(t, err)
  431. member, err := client.Spop("key")
  432. assert.Nil(t, err)
  433. assert.Contains(t, []string{"1", "2"}, member)
  434. _, err = NewRedis(client.Addr, "").Smembers("key")
  435. assert.NotNil(t, err)
  436. vals, err = client.Smembers("key")
  437. assert.Nil(t, err)
  438. assert.NotContains(t, vals, member)
  439. _, err = NewRedis(client.Addr, "").Sadd("key1", 1, 2, 3, 4)
  440. assert.NotNil(t, err)
  441. num, err = client.Sadd("key1", 1, 2, 3, 4)
  442. assert.Nil(t, err)
  443. assert.Equal(t, 4, num)
  444. num, err = client.Sadd("key2", 2, 3, 4, 5)
  445. assert.Nil(t, err)
  446. assert.Equal(t, 4, num)
  447. _, err = NewRedis(client.Addr, "").Sunion("key1", "key2")
  448. assert.NotNil(t, err)
  449. vals, err = client.Sunion("key1", "key2")
  450. assert.Nil(t, err)
  451. assert.ElementsMatch(t, []string{"1", "2", "3", "4", "5"}, vals)
  452. _, err = NewRedis(client.Addr, "").Sunionstore("key3", "key1", "key2")
  453. assert.NotNil(t, err)
  454. num, err = client.Sunionstore("key3", "key1", "key2")
  455. assert.Nil(t, err)
  456. assert.Equal(t, 5, num)
  457. _, err = NewRedis(client.Addr, "").Sdiff("key1", "key2")
  458. assert.NotNil(t, err)
  459. vals, err = client.Sdiff("key1", "key2")
  460. assert.Nil(t, err)
  461. assert.EqualValues(t, []string{"1"}, vals)
  462. _, err = NewRedis(client.Addr, "").Sdiffstore("key4", "key1", "key2")
  463. assert.NotNil(t, err)
  464. num, err = client.Sdiffstore("key4", "key1", "key2")
  465. assert.Nil(t, err)
  466. assert.Equal(t, 1, num)
  467. })
  468. }
  469. func TestRedis_SetGetDel(t *testing.T) {
  470. runOnRedis(t, func(client *Redis) {
  471. err := NewRedis(client.Addr, "").Set("hello", "world")
  472. assert.NotNil(t, err)
  473. err = client.Set("hello", "world")
  474. assert.Nil(t, err)
  475. _, err = NewRedis(client.Addr, "").Get("hello")
  476. assert.NotNil(t, err)
  477. val, err := client.Get("hello")
  478. assert.Nil(t, err)
  479. assert.Equal(t, "world", val)
  480. ret, err := client.Del("hello")
  481. assert.Nil(t, err)
  482. assert.Equal(t, 1, ret)
  483. })
  484. }
  485. func TestRedis_SetExNx(t *testing.T) {
  486. runOnRedis(t, func(client *Redis) {
  487. err := NewRedis(client.Addr, "").Setex("hello", "world", 5)
  488. assert.NotNil(t, err)
  489. err = client.Setex("hello", "world", 5)
  490. assert.Nil(t, err)
  491. _, err = NewRedis(client.Addr, "").Setnx("hello", "newworld")
  492. assert.NotNil(t, err)
  493. ok, err := client.Setnx("hello", "newworld")
  494. assert.Nil(t, err)
  495. assert.False(t, ok)
  496. ok, err = client.Setnx("newhello", "newworld")
  497. assert.Nil(t, err)
  498. assert.True(t, ok)
  499. val, err := client.Get("hello")
  500. assert.Nil(t, err)
  501. assert.Equal(t, "world", val)
  502. val, err = client.Get("newhello")
  503. assert.Nil(t, err)
  504. assert.Equal(t, "newworld", val)
  505. ttl, err := client.Ttl("hello")
  506. assert.Nil(t, err)
  507. assert.True(t, ttl > 0)
  508. _, err = NewRedis(client.Addr, "").SetnxEx("newhello", "newworld", 5)
  509. assert.NotNil(t, err)
  510. ok, err = client.SetnxEx("newhello", "newworld", 5)
  511. assert.Nil(t, err)
  512. assert.False(t, ok)
  513. num, err := client.Del("newhello")
  514. assert.Nil(t, err)
  515. assert.Equal(t, 1, num)
  516. ok, err = client.SetnxEx("newhello", "newworld", 5)
  517. assert.Nil(t, err)
  518. assert.True(t, ok)
  519. val, err = client.Get("newhello")
  520. assert.Nil(t, err)
  521. assert.Equal(t, "newworld", val)
  522. })
  523. }
  524. func TestRedis_SetGetDelHashField(t *testing.T) {
  525. runOnRedis(t, func(client *Redis) {
  526. err := client.Hset("key", "field", "value")
  527. assert.Nil(t, err)
  528. _, err = NewRedis(client.Addr, "").Hget("key", "field")
  529. assert.NotNil(t, err)
  530. val, err := client.Hget("key", "field")
  531. assert.Nil(t, err)
  532. assert.Equal(t, "value", val)
  533. _, err = NewRedis(client.Addr, "").Hexists("key", "field")
  534. assert.NotNil(t, err)
  535. ok, err := client.Hexists("key", "field")
  536. assert.Nil(t, err)
  537. assert.True(t, ok)
  538. _, err = NewRedis(client.Addr, "").Hdel("key", "field")
  539. assert.NotNil(t, err)
  540. ret, err := client.Hdel("key", "field")
  541. assert.Nil(t, err)
  542. assert.True(t, ret)
  543. ok, err = client.Hexists("key", "field")
  544. assert.Nil(t, err)
  545. assert.False(t, ok)
  546. })
  547. }
  548. func TestRedis_SortedSet(t *testing.T) {
  549. runOnRedis(t, func(client *Redis) {
  550. ok, err := client.Zadd("key", 1, "value1")
  551. assert.Nil(t, err)
  552. assert.True(t, ok)
  553. ok, err = client.Zadd("key", 2, "value1")
  554. assert.Nil(t, err)
  555. assert.False(t, ok)
  556. val, err := client.Zscore("key", "value1")
  557. assert.Nil(t, err)
  558. assert.Equal(t, int64(2), val)
  559. _, err = NewRedis(client.Addr, "").Zincrby("key", 3, "value1")
  560. assert.NotNil(t, err)
  561. val, err = client.Zincrby("key", 3, "value1")
  562. assert.Nil(t, err)
  563. assert.Equal(t, int64(5), val)
  564. _, err = NewRedis(client.Addr, "").Zscore("key", "value1")
  565. assert.NotNil(t, err)
  566. val, err = client.Zscore("key", "value1")
  567. assert.Nil(t, err)
  568. assert.Equal(t, int64(5), val)
  569. _, err = NewRedis(client.Addr, "").Zadds("key")
  570. assert.NotNil(t, err)
  571. val, err = client.Zadds("key", Pair{
  572. Key: "value2",
  573. Score: 6,
  574. }, Pair{
  575. Key: "value3",
  576. Score: 7,
  577. })
  578. assert.Nil(t, err)
  579. assert.Equal(t, int64(2), val)
  580. _, err = NewRedis(client.Addr, "").ZRevRangeWithScores("key", 1, 3)
  581. assert.NotNil(t, err)
  582. pairs, err := client.ZRevRangeWithScores("key", 1, 3)
  583. assert.Nil(t, err)
  584. assert.EqualValues(t, []Pair{
  585. {
  586. Key: "value2",
  587. Score: 6,
  588. },
  589. {
  590. Key: "value1",
  591. Score: 5,
  592. },
  593. }, pairs)
  594. rank, err := client.Zrank("key", "value2")
  595. assert.Nil(t, err)
  596. assert.Equal(t, int64(1), rank)
  597. rank, err = client.Zrevrank("key", "value1")
  598. assert.Nil(t, err)
  599. assert.Equal(t, int64(2), rank)
  600. _, err = NewRedis(client.Addr, "").Zrank("key", "value4")
  601. assert.NotNil(t, err)
  602. _, err = client.Zrank("key", "value4")
  603. assert.Equal(t, Nil, err)
  604. _, err = NewRedis(client.Addr, "").Zrem("key", "value2", "value3")
  605. assert.NotNil(t, err)
  606. num, err := client.Zrem("key", "value2", "value3")
  607. assert.Nil(t, err)
  608. assert.Equal(t, 2, num)
  609. ok, err = client.Zadd("key", 6, "value2")
  610. assert.Nil(t, err)
  611. assert.True(t, ok)
  612. ok, err = client.Zadd("key", 7, "value3")
  613. assert.Nil(t, err)
  614. assert.True(t, ok)
  615. ok, err = client.Zadd("key", 8, "value4")
  616. assert.Nil(t, err)
  617. assert.True(t, ok)
  618. _, err = NewRedis(client.Addr, "").Zremrangebyscore("key", 6, 7)
  619. assert.NotNil(t, err)
  620. num, err = client.Zremrangebyscore("key", 6, 7)
  621. assert.Nil(t, err)
  622. assert.Equal(t, 2, num)
  623. ok, err = client.Zadd("key", 6, "value2")
  624. assert.Nil(t, err)
  625. assert.True(t, ok)
  626. _, err = NewRedis(client.Addr, "").Zadd("key", 7, "value3")
  627. assert.NotNil(t, err)
  628. ok, err = client.Zadd("key", 7, "value3")
  629. assert.Nil(t, err)
  630. assert.True(t, ok)
  631. _, err = NewRedis(client.Addr, "").Zcount("key", 6, 7)
  632. assert.NotNil(t, err)
  633. num, err = client.Zcount("key", 6, 7)
  634. assert.Nil(t, err)
  635. assert.Equal(t, 2, num)
  636. _, err = NewRedis(client.Addr, "").Zremrangebyrank("key", 1, 2)
  637. assert.NotNil(t, err)
  638. num, err = client.Zremrangebyrank("key", 1, 2)
  639. assert.Nil(t, err)
  640. assert.Equal(t, 2, num)
  641. _, err = NewRedis(client.Addr, "").Zcard("key")
  642. assert.NotNil(t, err)
  643. card, err := client.Zcard("key")
  644. assert.Nil(t, err)
  645. assert.Equal(t, 2, card)
  646. _, err = NewRedis(client.Addr, "").Zrange("key", 0, -1)
  647. assert.NotNil(t, err)
  648. vals, err := client.Zrange("key", 0, -1)
  649. assert.Nil(t, err)
  650. assert.EqualValues(t, []string{"value1", "value4"}, vals)
  651. _, err = NewRedis(client.Addr, "").Zrevrange("key", 0, -1)
  652. assert.NotNil(t, err)
  653. vals, err = client.Zrevrange("key", 0, -1)
  654. assert.Nil(t, err)
  655. assert.EqualValues(t, []string{"value4", "value1"}, vals)
  656. _, err = NewRedis(client.Addr, "").ZrangeWithScores("key", 0, -1)
  657. assert.NotNil(t, err)
  658. pairs, err = client.ZrangeWithScores("key", 0, -1)
  659. assert.Nil(t, err)
  660. assert.EqualValues(t, []Pair{
  661. {
  662. Key: "value1",
  663. Score: 5,
  664. },
  665. {
  666. Key: "value4",
  667. Score: 8,
  668. },
  669. }, pairs)
  670. _, err = NewRedis(client.Addr, "").ZrangebyscoreWithScores("key", 5, 8)
  671. assert.NotNil(t, err)
  672. pairs, err = client.ZrangebyscoreWithScores("key", 5, 8)
  673. assert.Nil(t, err)
  674. assert.EqualValues(t, []Pair{
  675. {
  676. Key: "value1",
  677. Score: 5,
  678. },
  679. {
  680. Key: "value4",
  681. Score: 8,
  682. },
  683. }, pairs)
  684. _, err = NewRedis(client.Addr, "").ZrangebyscoreWithScoresAndLimit(
  685. "key", 5, 8, 1, 1)
  686. assert.NotNil(t, err)
  687. pairs, err = client.ZrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1)
  688. assert.Nil(t, err)
  689. assert.EqualValues(t, []Pair{
  690. {
  691. Key: "value4",
  692. Score: 8,
  693. },
  694. }, pairs)
  695. pairs, err = client.ZrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 0)
  696. assert.Nil(t, err)
  697. assert.Equal(t, 0, len(pairs))
  698. _, err = NewRedis(client.Addr, "").ZrevrangebyscoreWithScores("key", 5, 8)
  699. assert.NotNil(t, err)
  700. pairs, err = client.ZrevrangebyscoreWithScores("key", 5, 8)
  701. assert.Nil(t, err)
  702. assert.EqualValues(t, []Pair{
  703. {
  704. Key: "value4",
  705. Score: 8,
  706. },
  707. {
  708. Key: "value1",
  709. Score: 5,
  710. },
  711. }, pairs)
  712. _, err = NewRedis(client.Addr, "").ZrevrangebyscoreWithScoresAndLimit(
  713. "key", 5, 8, 1, 1)
  714. assert.NotNil(t, err)
  715. pairs, err = client.ZrevrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 1)
  716. assert.Nil(t, err)
  717. assert.EqualValues(t, []Pair{
  718. {
  719. Key: "value1",
  720. Score: 5,
  721. },
  722. }, pairs)
  723. pairs, err = client.ZrevrangebyscoreWithScoresAndLimit("key", 5, 8, 1, 0)
  724. assert.Nil(t, err)
  725. assert.Equal(t, 0, len(pairs))
  726. _, err = NewRedis(client.Addr, "").Zrevrank("key", "value")
  727. assert.NotNil(t, err)
  728. client.Zadd("second", 2, "aa")
  729. client.Zadd("third", 3, "bbb")
  730. val, err = client.Zunionstore("union", ZStore{
  731. Weights: []float64{1, 2},
  732. Aggregate: "SUM",
  733. }, "second", "third")
  734. assert.Nil(t, err)
  735. assert.Equal(t, int64(2), val)
  736. vals, err = client.Zrange("union", 0, 10000)
  737. assert.Nil(t, err)
  738. assert.EqualValues(t, []string{"aa", "bbb"}, vals)
  739. ival, err := client.Zcard("union")
  740. assert.Nil(t, err)
  741. assert.Equal(t, 2, ival)
  742. })
  743. }
  744. func TestRedis_Pipelined(t *testing.T) {
  745. runOnRedis(t, func(client *Redis) {
  746. assert.NotNil(t, NewRedis(client.Addr, "").Pipelined(func(pipeliner Pipeliner) error {
  747. return nil
  748. }))
  749. err := client.Pipelined(
  750. func(pipe Pipeliner) error {
  751. pipe.Incr("pipelined_counter")
  752. pipe.Expire("pipelined_counter", time.Hour)
  753. pipe.ZAdd("zadd", Z{Score: 12, Member: "zadd"})
  754. return nil
  755. },
  756. )
  757. assert.Nil(t, err)
  758. _, err = NewRedis(client.Addr, "").Ttl("pipelined_counter")
  759. assert.NotNil(t, err)
  760. ttl, err := client.Ttl("pipelined_counter")
  761. assert.Nil(t, err)
  762. assert.Equal(t, 3600, ttl)
  763. value, err := client.Get("pipelined_counter")
  764. assert.Nil(t, err)
  765. assert.Equal(t, "1", value)
  766. score, err := client.Zscore("zadd", "zadd")
  767. assert.Nil(t, err)
  768. assert.Equal(t, int64(12), score)
  769. })
  770. }
  771. func TestRedisString(t *testing.T) {
  772. runOnRedis(t, func(client *Redis) {
  773. client.Ping()
  774. _, err := getRedis(NewRedis(client.Addr, ClusterType))
  775. assert.Nil(t, err)
  776. assert.Equal(t, client.Addr, client.String())
  777. assert.NotNil(t, NewRedis(client.Addr, "").Ping())
  778. })
  779. }
  780. func TestRedisScriptLoad(t *testing.T) {
  781. runOnRedis(t, func(client *Redis) {
  782. client.Ping()
  783. _, err := NewRedis(client.Addr, "").scriptLoad("foo")
  784. assert.NotNil(t, err)
  785. _, err = client.scriptLoad("foo")
  786. assert.NotNil(t, err)
  787. })
  788. }
  789. func TestRedisToPairs(t *testing.T) {
  790. pairs := toPairs([]red.Z{
  791. {
  792. Member: 1,
  793. Score: 1,
  794. },
  795. {
  796. Member: 2,
  797. Score: 2,
  798. },
  799. })
  800. assert.EqualValues(t, []Pair{
  801. {
  802. Key: "1",
  803. Score: 1,
  804. },
  805. {
  806. Key: "2",
  807. Score: 2,
  808. },
  809. }, pairs)
  810. }
  811. func TestRedisToStrings(t *testing.T) {
  812. vals := toStrings([]interface{}{1, 2})
  813. assert.EqualValues(t, []string{"1", "2"}, vals)
  814. }
  815. func TestRedisBlpop(t *testing.T) {
  816. runOnRedis(t, func(client *Redis) {
  817. client.Ping()
  818. var node mockedNode
  819. _, err := client.Blpop(nil, "foo")
  820. assert.NotNil(t, err)
  821. _, err = client.Blpop(node, "foo")
  822. assert.NotNil(t, err)
  823. })
  824. }
  825. func TestRedisBlpopEx(t *testing.T) {
  826. runOnRedis(t, func(client *Redis) {
  827. client.Ping()
  828. var node mockedNode
  829. _, _, err := client.BlpopEx(nil, "foo")
  830. assert.NotNil(t, err)
  831. _, _, err = client.BlpopEx(node, "foo")
  832. assert.NotNil(t, err)
  833. })
  834. }
  835. func TestRedisGeo(t *testing.T) {
  836. runOnRedis(t, func(client *Redis) {
  837. client.Ping()
  838. var geoLocation = []*GeoLocation{{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"}, {Longitude: 15.087269, Latitude: 37.502669, Name: "Catania"}}
  839. v, err := client.GeoAdd("sicily", geoLocation...)
  840. assert.Nil(t, err)
  841. assert.Equal(t, int64(2), v)
  842. v2, err := client.GeoDist("sicily", "Palermo", "Catania", "m")
  843. assert.Nil(t, err)
  844. assert.Equal(t, 166274, int(v2))
  845. // GeoHash not support
  846. v3, err := client.GeoPos("sicily", "Palermo", "Catania")
  847. assert.Nil(t, err)
  848. assert.Equal(t, int64(v3[0].Longitude), int64(13))
  849. assert.Equal(t, int64(v3[0].Latitude), int64(38))
  850. assert.Equal(t, int64(v3[1].Longitude), int64(15))
  851. assert.Equal(t, int64(v3[1].Latitude), int64(37))
  852. v4, err := client.GeoRadius("sicily", 15, 37, &red.GeoRadiusQuery{WithDist: true, Unit: "km", Radius: 200})
  853. assert.Nil(t, err)
  854. assert.Equal(t, int64(v4[0].Dist), int64(190))
  855. assert.Equal(t, int64(v4[1].Dist), int64(56))
  856. var geoLocation2 = []*GeoLocation{{Longitude: 13.583333, Latitude: 37.316667, Name: "Agrigento"}}
  857. v5, err := client.GeoAdd("sicily", geoLocation2...)
  858. assert.Nil(t, err)
  859. assert.Equal(t, int64(1), v5)
  860. v6, err := client.GeoRadiusByMember("sicily", "Agrigento", &red.GeoRadiusQuery{Unit: "km", Radius: 100})
  861. assert.Nil(t, err)
  862. assert.Equal(t, v6[0].Name, "Agrigento")
  863. assert.Equal(t, v6[1].Name, "Palermo")
  864. })
  865. }
  866. func runOnRedis(t *testing.T, fn func(client *Redis)) {
  867. s, err := miniredis.Run()
  868. assert.Nil(t, err)
  869. defer func() {
  870. client, err := clientManager.GetResource(s.Addr(), func() (io.Closer, error) {
  871. return nil, errors.New("should already exist")
  872. })
  873. if err != nil {
  874. t.Error(err)
  875. }
  876. if client != nil {
  877. client.Close()
  878. }
  879. }()
  880. fn(NewRedis(s.Addr(), NodeType))
  881. }
  882. type mockedNode struct {
  883. RedisNode
  884. }
  885. func (n mockedNode) BLPop(timeout time.Duration, keys ...string) *red.StringSliceCmd {
  886. return red.NewStringSliceCmd("foo", "bar")
  887. }