redis_test.go 26 KB

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