commands_test.go 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941
  1. package redis_test
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "time"
  7. . "github.com/onsi/ginkgo"
  8. . "github.com/onsi/gomega"
  9. "github.com/go-redis/redis"
  10. "github.com/go-redis/redis/internal/proto"
  11. )
  12. var _ = Describe("Commands", func() {
  13. var client *redis.Client
  14. BeforeEach(func() {
  15. client = redis.NewClient(redisOptions())
  16. Expect(client.FlushDB().Err()).NotTo(HaveOccurred())
  17. })
  18. AfterEach(func() {
  19. Expect(client.Close()).NotTo(HaveOccurred())
  20. })
  21. Describe("server", func() {
  22. It("should Auth", func() {
  23. cmds, err := client.Pipelined(func(pipe redis.Pipeliner) error {
  24. pipe.Auth("password")
  25. pipe.Auth("")
  26. return nil
  27. })
  28. Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set"))
  29. Expect(cmds[0].Err()).To(MatchError("ERR Client sent AUTH, but no password is set"))
  30. Expect(cmds[1].Err()).To(MatchError("ERR Client sent AUTH, but no password is set"))
  31. stats := client.PoolStats()
  32. Expect(stats.Hits).To(Equal(uint32(1)))
  33. Expect(stats.Misses).To(Equal(uint32(1)))
  34. Expect(stats.Timeouts).To(Equal(uint32(0)))
  35. Expect(stats.TotalConns).To(Equal(uint32(1)))
  36. Expect(stats.IdleConns).To(Equal(uint32(1)))
  37. })
  38. It("should Echo", func() {
  39. pipe := client.Pipeline()
  40. echo := pipe.Echo("hello")
  41. _, err := pipe.Exec()
  42. Expect(err).NotTo(HaveOccurred())
  43. Expect(echo.Err()).NotTo(HaveOccurred())
  44. Expect(echo.Val()).To(Equal("hello"))
  45. })
  46. It("should Ping", func() {
  47. ping := client.Ping()
  48. Expect(ping.Err()).NotTo(HaveOccurred())
  49. Expect(ping.Val()).To(Equal("PONG"))
  50. })
  51. It("should Wait", func() {
  52. const wait = 3 * time.Second
  53. // assume testing on single redis instance
  54. start := time.Now()
  55. val, err := client.Wait(1, wait).Result()
  56. Expect(err).NotTo(HaveOccurred())
  57. Expect(val).To(Equal(int64(0)))
  58. Expect(time.Now()).To(BeTemporally("~", start.Add(wait), 3*time.Second))
  59. })
  60. It("should Select", func() {
  61. pipe := client.Pipeline()
  62. sel := pipe.Select(1)
  63. _, err := pipe.Exec()
  64. Expect(err).NotTo(HaveOccurred())
  65. Expect(sel.Err()).NotTo(HaveOccurred())
  66. Expect(sel.Val()).To(Equal("OK"))
  67. })
  68. It("should SwapDB", func() {
  69. pipe := client.Pipeline()
  70. sel := pipe.SwapDB(1, 2)
  71. _, err := pipe.Exec()
  72. Expect(err).NotTo(HaveOccurred())
  73. Expect(sel.Err()).NotTo(HaveOccurred())
  74. Expect(sel.Val()).To(Equal("OK"))
  75. })
  76. It("should BgRewriteAOF", func() {
  77. Skip("flaky test")
  78. val, err := client.BgRewriteAOF().Result()
  79. Expect(err).NotTo(HaveOccurred())
  80. Expect(val).To(ContainSubstring("Background append only file rewriting"))
  81. })
  82. It("should BgSave", func() {
  83. Skip("flaky test")
  84. // workaround for "ERR Can't BGSAVE while AOF log rewriting is in progress"
  85. Eventually(func() string {
  86. return client.BgSave().Val()
  87. }, "30s").Should(Equal("Background saving started"))
  88. })
  89. It("should ClientKill", func() {
  90. r := client.ClientKill("1.1.1.1:1111")
  91. Expect(r.Err()).To(MatchError("ERR No such client"))
  92. Expect(r.Val()).To(Equal(""))
  93. })
  94. It("should ClientKillByFilter", func() {
  95. r := client.ClientKillByFilter("TYPE", "test")
  96. Expect(r.Err()).To(MatchError("ERR Unknown client type 'test'"))
  97. Expect(r.Val()).To(Equal(int64(0)))
  98. })
  99. It("should ClientID", func() {
  100. err := client.ClientID().Err()
  101. Expect(err).NotTo(HaveOccurred())
  102. Expect(client.ClientID().Val()).To(BeNumerically(">=", 0))
  103. })
  104. It("should ClientUnblock", func() {
  105. id := client.ClientID().Val()
  106. r, err := client.ClientUnblock(id).Result()
  107. Expect(err).NotTo(HaveOccurred())
  108. Expect(r).To(Equal(int64(0)))
  109. })
  110. It("should ClientUnblockWithError", func() {
  111. id := client.ClientID().Val()
  112. r, err := client.ClientUnblockWithError(id).Result()
  113. Expect(err).NotTo(HaveOccurred())
  114. Expect(r).To(Equal(int64(0)))
  115. })
  116. It("should ClientPause", func() {
  117. err := client.ClientPause(time.Second).Err()
  118. Expect(err).NotTo(HaveOccurred())
  119. start := time.Now()
  120. err = client.Ping().Err()
  121. Expect(err).NotTo(HaveOccurred())
  122. Expect(time.Now()).To(BeTemporally("~", start.Add(time.Second), 800*time.Millisecond))
  123. })
  124. It("should ClientSetName and ClientGetName", func() {
  125. pipe := client.Pipeline()
  126. set := pipe.ClientSetName("theclientname")
  127. get := pipe.ClientGetName()
  128. _, err := pipe.Exec()
  129. Expect(err).NotTo(HaveOccurred())
  130. Expect(set.Err()).NotTo(HaveOccurred())
  131. Expect(set.Val()).To(BeTrue())
  132. Expect(get.Err()).NotTo(HaveOccurred())
  133. Expect(get.Val()).To(Equal("theclientname"))
  134. })
  135. It("should ConfigGet", func() {
  136. val, err := client.ConfigGet("*").Result()
  137. Expect(err).NotTo(HaveOccurred())
  138. Expect(val).NotTo(BeEmpty())
  139. })
  140. It("should ConfigResetStat", func() {
  141. r := client.ConfigResetStat()
  142. Expect(r.Err()).NotTo(HaveOccurred())
  143. Expect(r.Val()).To(Equal("OK"))
  144. })
  145. It("should ConfigSet", func() {
  146. configGet := client.ConfigGet("maxmemory")
  147. Expect(configGet.Err()).NotTo(HaveOccurred())
  148. Expect(configGet.Val()).To(HaveLen(2))
  149. Expect(configGet.Val()[0]).To(Equal("maxmemory"))
  150. configSet := client.ConfigSet("maxmemory", configGet.Val()[1].(string))
  151. Expect(configSet.Err()).NotTo(HaveOccurred())
  152. Expect(configSet.Val()).To(Equal("OK"))
  153. })
  154. It("should ConfigRewrite", func() {
  155. configRewrite := client.ConfigRewrite()
  156. Expect(configRewrite.Err()).NotTo(HaveOccurred())
  157. Expect(configRewrite.Val()).To(Equal("OK"))
  158. })
  159. It("should DBSize", func() {
  160. size, err := client.DBSize().Result()
  161. Expect(err).NotTo(HaveOccurred())
  162. Expect(size).To(Equal(int64(0)))
  163. })
  164. It("should Info", func() {
  165. info := client.Info()
  166. Expect(info.Err()).NotTo(HaveOccurred())
  167. Expect(info.Val()).NotTo(Equal(""))
  168. })
  169. It("should Info cpu", func() {
  170. info := client.Info("cpu")
  171. Expect(info.Err()).NotTo(HaveOccurred())
  172. Expect(info.Val()).NotTo(Equal(""))
  173. Expect(info.Val()).To(ContainSubstring(`used_cpu_sys`))
  174. })
  175. It("should LastSave", func() {
  176. lastSave := client.LastSave()
  177. Expect(lastSave.Err()).NotTo(HaveOccurred())
  178. Expect(lastSave.Val()).NotTo(Equal(0))
  179. })
  180. It("should Save", func() {
  181. // workaround for "ERR Background save already in progress"
  182. Eventually(func() string {
  183. return client.Save().Val()
  184. }, "10s").Should(Equal("OK"))
  185. })
  186. It("should SlaveOf", func() {
  187. slaveOf := client.SlaveOf("localhost", "8888")
  188. Expect(slaveOf.Err()).NotTo(HaveOccurred())
  189. Expect(slaveOf.Val()).To(Equal("OK"))
  190. slaveOf = client.SlaveOf("NO", "ONE")
  191. Expect(slaveOf.Err()).NotTo(HaveOccurred())
  192. Expect(slaveOf.Val()).To(Equal("OK"))
  193. })
  194. It("should Time", func() {
  195. tm, err := client.Time().Result()
  196. Expect(err).NotTo(HaveOccurred())
  197. Expect(tm).To(BeTemporally("~", time.Now(), 3*time.Second))
  198. })
  199. It("should Command", func() {
  200. cmds, err := client.Command().Result()
  201. Expect(err).NotTo(HaveOccurred())
  202. Expect(len(cmds)).To(BeNumerically("~", 200, 20))
  203. cmd := cmds["mget"]
  204. Expect(cmd.Name).To(Equal("mget"))
  205. Expect(cmd.Arity).To(Equal(int8(-2)))
  206. Expect(cmd.Flags).To(ContainElement("readonly"))
  207. Expect(cmd.FirstKeyPos).To(Equal(int8(1)))
  208. Expect(cmd.LastKeyPos).To(Equal(int8(-1)))
  209. Expect(cmd.StepCount).To(Equal(int8(1)))
  210. cmd = cmds["ping"]
  211. Expect(cmd.Name).To(Equal("ping"))
  212. Expect(cmd.Arity).To(Equal(int8(-1)))
  213. Expect(cmd.Flags).To(ContainElement("stale"))
  214. Expect(cmd.Flags).To(ContainElement("fast"))
  215. Expect(cmd.FirstKeyPos).To(Equal(int8(0)))
  216. Expect(cmd.LastKeyPos).To(Equal(int8(0)))
  217. Expect(cmd.StepCount).To(Equal(int8(0)))
  218. })
  219. })
  220. Describe("debugging", func() {
  221. It("should DebugObject", func() {
  222. err := client.DebugObject("foo").Err()
  223. Expect(err).To(MatchError("ERR no such key"))
  224. err = client.Set("foo", "bar", 0).Err()
  225. Expect(err).NotTo(HaveOccurred())
  226. s, err := client.DebugObject("foo").Result()
  227. Expect(err).NotTo(HaveOccurred())
  228. Expect(s).To(ContainSubstring("serializedlength:4"))
  229. })
  230. It("should MemoryUsage", func() {
  231. err := client.MemoryUsage("foo").Err()
  232. Expect(err).To(Equal(redis.Nil))
  233. err = client.Set("foo", "bar", 0).Err()
  234. Expect(err).NotTo(HaveOccurred())
  235. n, err := client.MemoryUsage("foo").Result()
  236. Expect(err).NotTo(HaveOccurred())
  237. Expect(n).To(Equal(int64(50)))
  238. n, err = client.MemoryUsage("foo", 0).Result()
  239. Expect(err).NotTo(HaveOccurred())
  240. Expect(n).To(Equal(int64(50)))
  241. })
  242. })
  243. Describe("keys", func() {
  244. It("should Del", func() {
  245. err := client.Set("key1", "Hello", 0).Err()
  246. Expect(err).NotTo(HaveOccurred())
  247. err = client.Set("key2", "World", 0).Err()
  248. Expect(err).NotTo(HaveOccurred())
  249. n, err := client.Del("key1", "key2", "key3").Result()
  250. Expect(err).NotTo(HaveOccurred())
  251. Expect(n).To(Equal(int64(2)))
  252. })
  253. It("should Unlink", func() {
  254. err := client.Set("key1", "Hello", 0).Err()
  255. Expect(err).NotTo(HaveOccurred())
  256. err = client.Set("key2", "World", 0).Err()
  257. Expect(err).NotTo(HaveOccurred())
  258. n, err := client.Unlink("key1", "key2", "key3").Result()
  259. Expect(err).NotTo(HaveOccurred())
  260. Expect(n).To(Equal(int64(2)))
  261. })
  262. It("should Dump", func() {
  263. set := client.Set("key", "hello", 0)
  264. Expect(set.Err()).NotTo(HaveOccurred())
  265. Expect(set.Val()).To(Equal("OK"))
  266. dump := client.Dump("key")
  267. Expect(dump.Err()).NotTo(HaveOccurred())
  268. Expect(dump.Val()).NotTo(BeEmpty())
  269. })
  270. It("should Exists", func() {
  271. set := client.Set("key1", "Hello", 0)
  272. Expect(set.Err()).NotTo(HaveOccurred())
  273. Expect(set.Val()).To(Equal("OK"))
  274. n, err := client.Exists("key1").Result()
  275. Expect(err).NotTo(HaveOccurred())
  276. Expect(n).To(Equal(int64(1)))
  277. n, err = client.Exists("key2").Result()
  278. Expect(err).NotTo(HaveOccurred())
  279. Expect(n).To(Equal(int64(0)))
  280. n, err = client.Exists("key1", "key2").Result()
  281. Expect(err).NotTo(HaveOccurred())
  282. Expect(n).To(Equal(int64(1)))
  283. n, err = client.Exists("key1", "key1").Result()
  284. Expect(err).NotTo(HaveOccurred())
  285. Expect(n).To(Equal(int64(2)))
  286. })
  287. It("should Expire", func() {
  288. set := client.Set("key", "Hello", 0)
  289. Expect(set.Err()).NotTo(HaveOccurred())
  290. Expect(set.Val()).To(Equal("OK"))
  291. expire := client.Expire("key", 10*time.Second)
  292. Expect(expire.Err()).NotTo(HaveOccurred())
  293. Expect(expire.Val()).To(Equal(true))
  294. ttl := client.TTL("key")
  295. Expect(ttl.Err()).NotTo(HaveOccurred())
  296. Expect(ttl.Val()).To(Equal(10 * time.Second))
  297. set = client.Set("key", "Hello World", 0)
  298. Expect(set.Err()).NotTo(HaveOccurred())
  299. Expect(set.Val()).To(Equal("OK"))
  300. ttl = client.TTL("key")
  301. Expect(ttl.Err()).NotTo(HaveOccurred())
  302. Expect(ttl.Val() < 0).To(Equal(true))
  303. })
  304. It("should ExpireAt", func() {
  305. set := client.Set("key", "Hello", 0)
  306. Expect(set.Err()).NotTo(HaveOccurred())
  307. Expect(set.Val()).To(Equal("OK"))
  308. n, err := client.Exists("key").Result()
  309. Expect(err).NotTo(HaveOccurred())
  310. Expect(n).To(Equal(int64(1)))
  311. expireAt := client.ExpireAt("key", time.Now().Add(-time.Hour))
  312. Expect(expireAt.Err()).NotTo(HaveOccurred())
  313. Expect(expireAt.Val()).To(Equal(true))
  314. n, err = client.Exists("key").Result()
  315. Expect(err).NotTo(HaveOccurred())
  316. Expect(n).To(Equal(int64(0)))
  317. })
  318. It("should Keys", func() {
  319. mset := client.MSet("one", "1", "two", "2", "three", "3", "four", "4")
  320. Expect(mset.Err()).NotTo(HaveOccurred())
  321. Expect(mset.Val()).To(Equal("OK"))
  322. keys := client.Keys("*o*")
  323. Expect(keys.Err()).NotTo(HaveOccurred())
  324. Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "two"}))
  325. keys = client.Keys("t??")
  326. Expect(keys.Err()).NotTo(HaveOccurred())
  327. Expect(keys.Val()).To(Equal([]string{"two"}))
  328. keys = client.Keys("*")
  329. Expect(keys.Err()).NotTo(HaveOccurred())
  330. Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "three", "two"}))
  331. })
  332. It("should Migrate", func() {
  333. migrate := client.Migrate("localhost", redisSecondaryPort, "key", 0, 0)
  334. Expect(migrate.Err()).NotTo(HaveOccurred())
  335. Expect(migrate.Val()).To(Equal("NOKEY"))
  336. set := client.Set("key", "hello", 0)
  337. Expect(set.Err()).NotTo(HaveOccurred())
  338. Expect(set.Val()).To(Equal("OK"))
  339. migrate = client.Migrate("localhost", redisSecondaryPort, "key", 0, 0)
  340. Expect(migrate.Err()).To(MatchError("IOERR error or timeout writing to target instance"))
  341. Expect(migrate.Val()).To(Equal(""))
  342. })
  343. It("should Move", func() {
  344. move := client.Move("key", 2)
  345. Expect(move.Err()).NotTo(HaveOccurred())
  346. Expect(move.Val()).To(Equal(false))
  347. set := client.Set("key", "hello", 0)
  348. Expect(set.Err()).NotTo(HaveOccurred())
  349. Expect(set.Val()).To(Equal("OK"))
  350. move = client.Move("key", 2)
  351. Expect(move.Err()).NotTo(HaveOccurred())
  352. Expect(move.Val()).To(Equal(true))
  353. get := client.Get("key")
  354. Expect(get.Err()).To(Equal(redis.Nil))
  355. Expect(get.Val()).To(Equal(""))
  356. pipe := client.Pipeline()
  357. pipe.Select(2)
  358. get = pipe.Get("key")
  359. pipe.FlushDB()
  360. _, err := pipe.Exec()
  361. Expect(err).NotTo(HaveOccurred())
  362. Expect(get.Val()).To(Equal("hello"))
  363. })
  364. It("should Object", func() {
  365. set := client.Set("key", "hello", 0)
  366. Expect(set.Err()).NotTo(HaveOccurred())
  367. Expect(set.Val()).To(Equal("OK"))
  368. refCount := client.ObjectRefCount("key")
  369. Expect(refCount.Err()).NotTo(HaveOccurred())
  370. Expect(refCount.Val()).To(Equal(int64(1)))
  371. err := client.ObjectEncoding("key").Err()
  372. Expect(err).NotTo(HaveOccurred())
  373. idleTime := client.ObjectIdleTime("key")
  374. Expect(idleTime.Err()).NotTo(HaveOccurred())
  375. Expect(idleTime.Val()).To(Equal(time.Duration(0)))
  376. })
  377. It("should Persist", func() {
  378. set := client.Set("key", "Hello", 0)
  379. Expect(set.Err()).NotTo(HaveOccurred())
  380. Expect(set.Val()).To(Equal("OK"))
  381. expire := client.Expire("key", 10*time.Second)
  382. Expect(expire.Err()).NotTo(HaveOccurred())
  383. Expect(expire.Val()).To(Equal(true))
  384. ttl := client.TTL("key")
  385. Expect(ttl.Err()).NotTo(HaveOccurred())
  386. Expect(ttl.Val()).To(Equal(10 * time.Second))
  387. persist := client.Persist("key")
  388. Expect(persist.Err()).NotTo(HaveOccurred())
  389. Expect(persist.Val()).To(Equal(true))
  390. ttl = client.TTL("key")
  391. Expect(ttl.Err()).NotTo(HaveOccurred())
  392. Expect(ttl.Val() < 0).To(Equal(true))
  393. })
  394. It("should PExpire", func() {
  395. set := client.Set("key", "Hello", 0)
  396. Expect(set.Err()).NotTo(HaveOccurred())
  397. Expect(set.Val()).To(Equal("OK"))
  398. expiration := 900 * time.Millisecond
  399. pexpire := client.PExpire("key", expiration)
  400. Expect(pexpire.Err()).NotTo(HaveOccurred())
  401. Expect(pexpire.Val()).To(Equal(true))
  402. ttl := client.TTL("key")
  403. Expect(ttl.Err()).NotTo(HaveOccurred())
  404. Expect(ttl.Val()).To(Equal(time.Second))
  405. pttl := client.PTTL("key")
  406. Expect(pttl.Err()).NotTo(HaveOccurred())
  407. Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
  408. })
  409. It("should PExpireAt", func() {
  410. set := client.Set("key", "Hello", 0)
  411. Expect(set.Err()).NotTo(HaveOccurred())
  412. Expect(set.Val()).To(Equal("OK"))
  413. expiration := 900 * time.Millisecond
  414. pexpireat := client.PExpireAt("key", time.Now().Add(expiration))
  415. Expect(pexpireat.Err()).NotTo(HaveOccurred())
  416. Expect(pexpireat.Val()).To(Equal(true))
  417. ttl := client.TTL("key")
  418. Expect(ttl.Err()).NotTo(HaveOccurred())
  419. Expect(ttl.Val()).To(Equal(time.Second))
  420. pttl := client.PTTL("key")
  421. Expect(pttl.Err()).NotTo(HaveOccurred())
  422. Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
  423. })
  424. It("should PTTL", func() {
  425. set := client.Set("key", "Hello", 0)
  426. Expect(set.Err()).NotTo(HaveOccurred())
  427. Expect(set.Val()).To(Equal("OK"))
  428. expiration := time.Second
  429. expire := client.Expire("key", expiration)
  430. Expect(expire.Err()).NotTo(HaveOccurred())
  431. Expect(set.Val()).To(Equal("OK"))
  432. pttl := client.PTTL("key")
  433. Expect(pttl.Err()).NotTo(HaveOccurred())
  434. Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
  435. })
  436. It("should RandomKey", func() {
  437. randomKey := client.RandomKey()
  438. Expect(randomKey.Err()).To(Equal(redis.Nil))
  439. Expect(randomKey.Val()).To(Equal(""))
  440. set := client.Set("key", "hello", 0)
  441. Expect(set.Err()).NotTo(HaveOccurred())
  442. Expect(set.Val()).To(Equal("OK"))
  443. randomKey = client.RandomKey()
  444. Expect(randomKey.Err()).NotTo(HaveOccurred())
  445. Expect(randomKey.Val()).To(Equal("key"))
  446. })
  447. It("should Rename", func() {
  448. set := client.Set("key", "hello", 0)
  449. Expect(set.Err()).NotTo(HaveOccurred())
  450. Expect(set.Val()).To(Equal("OK"))
  451. status := client.Rename("key", "key1")
  452. Expect(status.Err()).NotTo(HaveOccurred())
  453. Expect(status.Val()).To(Equal("OK"))
  454. get := client.Get("key1")
  455. Expect(get.Err()).NotTo(HaveOccurred())
  456. Expect(get.Val()).To(Equal("hello"))
  457. })
  458. It("should RenameNX", func() {
  459. set := client.Set("key", "hello", 0)
  460. Expect(set.Err()).NotTo(HaveOccurred())
  461. Expect(set.Val()).To(Equal("OK"))
  462. renameNX := client.RenameNX("key", "key1")
  463. Expect(renameNX.Err()).NotTo(HaveOccurred())
  464. Expect(renameNX.Val()).To(Equal(true))
  465. get := client.Get("key1")
  466. Expect(get.Err()).NotTo(HaveOccurred())
  467. Expect(get.Val()).To(Equal("hello"))
  468. })
  469. It("should Restore", func() {
  470. err := client.Set("key", "hello", 0).Err()
  471. Expect(err).NotTo(HaveOccurred())
  472. dump := client.Dump("key")
  473. Expect(dump.Err()).NotTo(HaveOccurred())
  474. err = client.Del("key").Err()
  475. Expect(err).NotTo(HaveOccurred())
  476. restore, err := client.Restore("key", 0, dump.Val()).Result()
  477. Expect(err).NotTo(HaveOccurred())
  478. Expect(restore).To(Equal("OK"))
  479. type_, err := client.Type("key").Result()
  480. Expect(err).NotTo(HaveOccurred())
  481. Expect(type_).To(Equal("string"))
  482. val, err := client.Get("key").Result()
  483. Expect(err).NotTo(HaveOccurred())
  484. Expect(val).To(Equal("hello"))
  485. })
  486. It("should RestoreReplace", func() {
  487. err := client.Set("key", "hello", 0).Err()
  488. Expect(err).NotTo(HaveOccurred())
  489. dump := client.Dump("key")
  490. Expect(dump.Err()).NotTo(HaveOccurred())
  491. restore, err := client.RestoreReplace("key", 0, dump.Val()).Result()
  492. Expect(err).NotTo(HaveOccurred())
  493. Expect(restore).To(Equal("OK"))
  494. type_, err := client.Type("key").Result()
  495. Expect(err).NotTo(HaveOccurred())
  496. Expect(type_).To(Equal("string"))
  497. val, err := client.Get("key").Result()
  498. Expect(err).NotTo(HaveOccurred())
  499. Expect(val).To(Equal("hello"))
  500. })
  501. It("should Sort", func() {
  502. size, err := client.LPush("list", "1").Result()
  503. Expect(err).NotTo(HaveOccurred())
  504. Expect(size).To(Equal(int64(1)))
  505. size, err = client.LPush("list", "3").Result()
  506. Expect(err).NotTo(HaveOccurred())
  507. Expect(size).To(Equal(int64(2)))
  508. size, err = client.LPush("list", "2").Result()
  509. Expect(err).NotTo(HaveOccurred())
  510. Expect(size).To(Equal(int64(3)))
  511. els, err := client.Sort("list", &redis.Sort{
  512. Offset: 0,
  513. Count: 2,
  514. Order: "ASC",
  515. }).Result()
  516. Expect(err).NotTo(HaveOccurred())
  517. Expect(els).To(Equal([]string{"1", "2"}))
  518. })
  519. It("should Sort and Get", func() {
  520. size, err := client.LPush("list", "1").Result()
  521. Expect(err).NotTo(HaveOccurred())
  522. Expect(size).To(Equal(int64(1)))
  523. size, err = client.LPush("list", "3").Result()
  524. Expect(err).NotTo(HaveOccurred())
  525. Expect(size).To(Equal(int64(2)))
  526. size, err = client.LPush("list", "2").Result()
  527. Expect(err).NotTo(HaveOccurred())
  528. Expect(size).To(Equal(int64(3)))
  529. err = client.Set("object_2", "value2", 0).Err()
  530. Expect(err).NotTo(HaveOccurred())
  531. {
  532. els, err := client.Sort("list", &redis.Sort{
  533. Get: []string{"object_*"},
  534. }).Result()
  535. Expect(err).NotTo(HaveOccurred())
  536. Expect(els).To(Equal([]string{"", "value2", ""}))
  537. }
  538. {
  539. els, err := client.SortInterfaces("list", &redis.Sort{
  540. Get: []string{"object_*"},
  541. }).Result()
  542. Expect(err).NotTo(HaveOccurred())
  543. Expect(els).To(Equal([]interface{}{nil, "value2", nil}))
  544. }
  545. })
  546. It("should Sort and Store", func() {
  547. size, err := client.LPush("list", "1").Result()
  548. Expect(err).NotTo(HaveOccurred())
  549. Expect(size).To(Equal(int64(1)))
  550. size, err = client.LPush("list", "3").Result()
  551. Expect(err).NotTo(HaveOccurred())
  552. Expect(size).To(Equal(int64(2)))
  553. size, err = client.LPush("list", "2").Result()
  554. Expect(err).NotTo(HaveOccurred())
  555. Expect(size).To(Equal(int64(3)))
  556. n, err := client.SortStore("list", "list2", &redis.Sort{
  557. Offset: 0,
  558. Count: 2,
  559. Order: "ASC",
  560. }).Result()
  561. Expect(err).NotTo(HaveOccurred())
  562. Expect(n).To(Equal(int64(2)))
  563. els, err := client.LRange("list2", 0, -1).Result()
  564. Expect(err).NotTo(HaveOccurred())
  565. Expect(els).To(Equal([]string{"1", "2"}))
  566. })
  567. It("should Touch", func() {
  568. set1 := client.Set("touch1", "hello", 0)
  569. Expect(set1.Err()).NotTo(HaveOccurred())
  570. Expect(set1.Val()).To(Equal("OK"))
  571. set2 := client.Set("touch2", "hello", 0)
  572. Expect(set2.Err()).NotTo(HaveOccurred())
  573. Expect(set2.Val()).To(Equal("OK"))
  574. touch := client.Touch("touch1", "touch2", "touch3")
  575. Expect(touch.Err()).NotTo(HaveOccurred())
  576. Expect(touch.Val()).To(Equal(int64(2)))
  577. })
  578. It("should TTL", func() {
  579. ttl := client.TTL("key")
  580. Expect(ttl.Err()).NotTo(HaveOccurred())
  581. Expect(ttl.Val() < 0).To(Equal(true))
  582. set := client.Set("key", "hello", 0)
  583. Expect(set.Err()).NotTo(HaveOccurred())
  584. Expect(set.Val()).To(Equal("OK"))
  585. expire := client.Expire("key", 60*time.Second)
  586. Expect(expire.Err()).NotTo(HaveOccurred())
  587. Expect(expire.Val()).To(Equal(true))
  588. ttl = client.TTL("key")
  589. Expect(ttl.Err()).NotTo(HaveOccurred())
  590. Expect(ttl.Val()).To(Equal(60 * time.Second))
  591. })
  592. It("should Type", func() {
  593. set := client.Set("key", "hello", 0)
  594. Expect(set.Err()).NotTo(HaveOccurred())
  595. Expect(set.Val()).To(Equal("OK"))
  596. type_ := client.Type("key")
  597. Expect(type_.Err()).NotTo(HaveOccurred())
  598. Expect(type_.Val()).To(Equal("string"))
  599. })
  600. })
  601. Describe("scanning", func() {
  602. It("should Scan", func() {
  603. for i := 0; i < 1000; i++ {
  604. set := client.Set(fmt.Sprintf("key%d", i), "hello", 0)
  605. Expect(set.Err()).NotTo(HaveOccurred())
  606. }
  607. keys, cursor, err := client.Scan(0, "", 0).Result()
  608. Expect(err).NotTo(HaveOccurred())
  609. Expect(keys).NotTo(BeEmpty())
  610. Expect(cursor).NotTo(BeZero())
  611. })
  612. It("should SScan", func() {
  613. for i := 0; i < 1000; i++ {
  614. sadd := client.SAdd("myset", fmt.Sprintf("member%d", i))
  615. Expect(sadd.Err()).NotTo(HaveOccurred())
  616. }
  617. keys, cursor, err := client.SScan("myset", 0, "", 0).Result()
  618. Expect(err).NotTo(HaveOccurred())
  619. Expect(keys).NotTo(BeEmpty())
  620. Expect(cursor).NotTo(BeZero())
  621. })
  622. It("should HScan", func() {
  623. for i := 0; i < 1000; i++ {
  624. sadd := client.HSet("myhash", fmt.Sprintf("key%d", i), "hello")
  625. Expect(sadd.Err()).NotTo(HaveOccurred())
  626. }
  627. keys, cursor, err := client.HScan("myhash", 0, "", 0).Result()
  628. Expect(err).NotTo(HaveOccurred())
  629. Expect(keys).NotTo(BeEmpty())
  630. Expect(cursor).NotTo(BeZero())
  631. })
  632. It("should ZScan", func() {
  633. for i := 0; i < 1000; i++ {
  634. err := client.ZAdd("myset", redis.Z{
  635. Score: float64(i),
  636. Member: fmt.Sprintf("member%d", i),
  637. }).Err()
  638. Expect(err).NotTo(HaveOccurred())
  639. }
  640. keys, cursor, err := client.ZScan("myset", 0, "", 0).Result()
  641. Expect(err).NotTo(HaveOccurred())
  642. Expect(keys).NotTo(BeEmpty())
  643. Expect(cursor).NotTo(BeZero())
  644. })
  645. })
  646. Describe("strings", func() {
  647. It("should Append", func() {
  648. n, err := client.Exists("key").Result()
  649. Expect(err).NotTo(HaveOccurred())
  650. Expect(n).To(Equal(int64(0)))
  651. append := client.Append("key", "Hello")
  652. Expect(append.Err()).NotTo(HaveOccurred())
  653. Expect(append.Val()).To(Equal(int64(5)))
  654. append = client.Append("key", " World")
  655. Expect(append.Err()).NotTo(HaveOccurred())
  656. Expect(append.Val()).To(Equal(int64(11)))
  657. get := client.Get("key")
  658. Expect(get.Err()).NotTo(HaveOccurred())
  659. Expect(get.Val()).To(Equal("Hello World"))
  660. })
  661. It("should BitCount", func() {
  662. set := client.Set("key", "foobar", 0)
  663. Expect(set.Err()).NotTo(HaveOccurred())
  664. Expect(set.Val()).To(Equal("OK"))
  665. bitCount := client.BitCount("key", nil)
  666. Expect(bitCount.Err()).NotTo(HaveOccurred())
  667. Expect(bitCount.Val()).To(Equal(int64(26)))
  668. bitCount = client.BitCount("key", &redis.BitCount{
  669. Start: 0,
  670. End: 0,
  671. })
  672. Expect(bitCount.Err()).NotTo(HaveOccurred())
  673. Expect(bitCount.Val()).To(Equal(int64(4)))
  674. bitCount = client.BitCount("key", &redis.BitCount{
  675. Start: 1,
  676. End: 1,
  677. })
  678. Expect(bitCount.Err()).NotTo(HaveOccurred())
  679. Expect(bitCount.Val()).To(Equal(int64(6)))
  680. })
  681. It("should BitOpAnd", func() {
  682. set := client.Set("key1", "1", 0)
  683. Expect(set.Err()).NotTo(HaveOccurred())
  684. Expect(set.Val()).To(Equal("OK"))
  685. set = client.Set("key2", "0", 0)
  686. Expect(set.Err()).NotTo(HaveOccurred())
  687. Expect(set.Val()).To(Equal("OK"))
  688. bitOpAnd := client.BitOpAnd("dest", "key1", "key2")
  689. Expect(bitOpAnd.Err()).NotTo(HaveOccurred())
  690. Expect(bitOpAnd.Val()).To(Equal(int64(1)))
  691. get := client.Get("dest")
  692. Expect(get.Err()).NotTo(HaveOccurred())
  693. Expect(get.Val()).To(Equal("0"))
  694. })
  695. It("should BitOpOr", func() {
  696. set := client.Set("key1", "1", 0)
  697. Expect(set.Err()).NotTo(HaveOccurred())
  698. Expect(set.Val()).To(Equal("OK"))
  699. set = client.Set("key2", "0", 0)
  700. Expect(set.Err()).NotTo(HaveOccurred())
  701. Expect(set.Val()).To(Equal("OK"))
  702. bitOpOr := client.BitOpOr("dest", "key1", "key2")
  703. Expect(bitOpOr.Err()).NotTo(HaveOccurred())
  704. Expect(bitOpOr.Val()).To(Equal(int64(1)))
  705. get := client.Get("dest")
  706. Expect(get.Err()).NotTo(HaveOccurred())
  707. Expect(get.Val()).To(Equal("1"))
  708. })
  709. It("should BitOpXor", func() {
  710. set := client.Set("key1", "\xff", 0)
  711. Expect(set.Err()).NotTo(HaveOccurred())
  712. Expect(set.Val()).To(Equal("OK"))
  713. set = client.Set("key2", "\x0f", 0)
  714. Expect(set.Err()).NotTo(HaveOccurred())
  715. Expect(set.Val()).To(Equal("OK"))
  716. bitOpXor := client.BitOpXor("dest", "key1", "key2")
  717. Expect(bitOpXor.Err()).NotTo(HaveOccurred())
  718. Expect(bitOpXor.Val()).To(Equal(int64(1)))
  719. get := client.Get("dest")
  720. Expect(get.Err()).NotTo(HaveOccurred())
  721. Expect(get.Val()).To(Equal("\xf0"))
  722. })
  723. It("should BitOpNot", func() {
  724. set := client.Set("key1", "\x00", 0)
  725. Expect(set.Err()).NotTo(HaveOccurred())
  726. Expect(set.Val()).To(Equal("OK"))
  727. bitOpNot := client.BitOpNot("dest", "key1")
  728. Expect(bitOpNot.Err()).NotTo(HaveOccurred())
  729. Expect(bitOpNot.Val()).To(Equal(int64(1)))
  730. get := client.Get("dest")
  731. Expect(get.Err()).NotTo(HaveOccurred())
  732. Expect(get.Val()).To(Equal("\xff"))
  733. })
  734. It("should BitPos", func() {
  735. err := client.Set("mykey", "\xff\xf0\x00", 0).Err()
  736. Expect(err).NotTo(HaveOccurred())
  737. pos, err := client.BitPos("mykey", 0).Result()
  738. Expect(err).NotTo(HaveOccurred())
  739. Expect(pos).To(Equal(int64(12)))
  740. pos, err = client.BitPos("mykey", 1).Result()
  741. Expect(err).NotTo(HaveOccurred())
  742. Expect(pos).To(Equal(int64(0)))
  743. pos, err = client.BitPos("mykey", 0, 2).Result()
  744. Expect(err).NotTo(HaveOccurred())
  745. Expect(pos).To(Equal(int64(16)))
  746. pos, err = client.BitPos("mykey", 1, 2).Result()
  747. Expect(err).NotTo(HaveOccurred())
  748. Expect(pos).To(Equal(int64(-1)))
  749. pos, err = client.BitPos("mykey", 0, -1).Result()
  750. Expect(err).NotTo(HaveOccurred())
  751. Expect(pos).To(Equal(int64(16)))
  752. pos, err = client.BitPos("mykey", 1, -1).Result()
  753. Expect(err).NotTo(HaveOccurred())
  754. Expect(pos).To(Equal(int64(-1)))
  755. pos, err = client.BitPos("mykey", 0, 2, 1).Result()
  756. Expect(err).NotTo(HaveOccurred())
  757. Expect(pos).To(Equal(int64(-1)))
  758. pos, err = client.BitPos("mykey", 0, 0, -3).Result()
  759. Expect(err).NotTo(HaveOccurred())
  760. Expect(pos).To(Equal(int64(-1)))
  761. pos, err = client.BitPos("mykey", 0, 0, 0).Result()
  762. Expect(err).NotTo(HaveOccurred())
  763. Expect(pos).To(Equal(int64(-1)))
  764. })
  765. It("should Decr", func() {
  766. set := client.Set("key", "10", 0)
  767. Expect(set.Err()).NotTo(HaveOccurred())
  768. Expect(set.Val()).To(Equal("OK"))
  769. decr := client.Decr("key")
  770. Expect(decr.Err()).NotTo(HaveOccurred())
  771. Expect(decr.Val()).To(Equal(int64(9)))
  772. set = client.Set("key", "234293482390480948029348230948", 0)
  773. Expect(set.Err()).NotTo(HaveOccurred())
  774. Expect(set.Val()).To(Equal("OK"))
  775. decr = client.Decr("key")
  776. Expect(decr.Err()).To(MatchError("ERR value is not an integer or out of range"))
  777. Expect(decr.Val()).To(Equal(int64(0)))
  778. })
  779. It("should DecrBy", func() {
  780. set := client.Set("key", "10", 0)
  781. Expect(set.Err()).NotTo(HaveOccurred())
  782. Expect(set.Val()).To(Equal("OK"))
  783. decrBy := client.DecrBy("key", 5)
  784. Expect(decrBy.Err()).NotTo(HaveOccurred())
  785. Expect(decrBy.Val()).To(Equal(int64(5)))
  786. })
  787. It("should Get", func() {
  788. get := client.Get("_")
  789. Expect(get.Err()).To(Equal(redis.Nil))
  790. Expect(get.Val()).To(Equal(""))
  791. set := client.Set("key", "hello", 0)
  792. Expect(set.Err()).NotTo(HaveOccurred())
  793. Expect(set.Val()).To(Equal("OK"))
  794. get = client.Get("key")
  795. Expect(get.Err()).NotTo(HaveOccurred())
  796. Expect(get.Val()).To(Equal("hello"))
  797. })
  798. It("should GetBit", func() {
  799. setBit := client.SetBit("key", 7, 1)
  800. Expect(setBit.Err()).NotTo(HaveOccurred())
  801. Expect(setBit.Val()).To(Equal(int64(0)))
  802. getBit := client.GetBit("key", 0)
  803. Expect(getBit.Err()).NotTo(HaveOccurred())
  804. Expect(getBit.Val()).To(Equal(int64(0)))
  805. getBit = client.GetBit("key", 7)
  806. Expect(getBit.Err()).NotTo(HaveOccurred())
  807. Expect(getBit.Val()).To(Equal(int64(1)))
  808. getBit = client.GetBit("key", 100)
  809. Expect(getBit.Err()).NotTo(HaveOccurred())
  810. Expect(getBit.Val()).To(Equal(int64(0)))
  811. })
  812. It("should GetRange", func() {
  813. set := client.Set("key", "This is a string", 0)
  814. Expect(set.Err()).NotTo(HaveOccurred())
  815. Expect(set.Val()).To(Equal("OK"))
  816. getRange := client.GetRange("key", 0, 3)
  817. Expect(getRange.Err()).NotTo(HaveOccurred())
  818. Expect(getRange.Val()).To(Equal("This"))
  819. getRange = client.GetRange("key", -3, -1)
  820. Expect(getRange.Err()).NotTo(HaveOccurred())
  821. Expect(getRange.Val()).To(Equal("ing"))
  822. getRange = client.GetRange("key", 0, -1)
  823. Expect(getRange.Err()).NotTo(HaveOccurred())
  824. Expect(getRange.Val()).To(Equal("This is a string"))
  825. getRange = client.GetRange("key", 10, 100)
  826. Expect(getRange.Err()).NotTo(HaveOccurred())
  827. Expect(getRange.Val()).To(Equal("string"))
  828. })
  829. It("should GetSet", func() {
  830. incr := client.Incr("key")
  831. Expect(incr.Err()).NotTo(HaveOccurred())
  832. Expect(incr.Val()).To(Equal(int64(1)))
  833. getSet := client.GetSet("key", "0")
  834. Expect(getSet.Err()).NotTo(HaveOccurred())
  835. Expect(getSet.Val()).To(Equal("1"))
  836. get := client.Get("key")
  837. Expect(get.Err()).NotTo(HaveOccurred())
  838. Expect(get.Val()).To(Equal("0"))
  839. })
  840. It("should Incr", func() {
  841. set := client.Set("key", "10", 0)
  842. Expect(set.Err()).NotTo(HaveOccurred())
  843. Expect(set.Val()).To(Equal("OK"))
  844. incr := client.Incr("key")
  845. Expect(incr.Err()).NotTo(HaveOccurred())
  846. Expect(incr.Val()).To(Equal(int64(11)))
  847. get := client.Get("key")
  848. Expect(get.Err()).NotTo(HaveOccurred())
  849. Expect(get.Val()).To(Equal("11"))
  850. })
  851. It("should IncrBy", func() {
  852. set := client.Set("key", "10", 0)
  853. Expect(set.Err()).NotTo(HaveOccurred())
  854. Expect(set.Val()).To(Equal("OK"))
  855. incrBy := client.IncrBy("key", 5)
  856. Expect(incrBy.Err()).NotTo(HaveOccurred())
  857. Expect(incrBy.Val()).To(Equal(int64(15)))
  858. })
  859. It("should IncrByFloat", func() {
  860. set := client.Set("key", "10.50", 0)
  861. Expect(set.Err()).NotTo(HaveOccurred())
  862. Expect(set.Val()).To(Equal("OK"))
  863. incrByFloat := client.IncrByFloat("key", 0.1)
  864. Expect(incrByFloat.Err()).NotTo(HaveOccurred())
  865. Expect(incrByFloat.Val()).To(Equal(10.6))
  866. set = client.Set("key", "5.0e3", 0)
  867. Expect(set.Err()).NotTo(HaveOccurred())
  868. Expect(set.Val()).To(Equal("OK"))
  869. incrByFloat = client.IncrByFloat("key", 2.0e2)
  870. Expect(incrByFloat.Err()).NotTo(HaveOccurred())
  871. Expect(incrByFloat.Val()).To(Equal(float64(5200)))
  872. })
  873. It("should IncrByFloatOverflow", func() {
  874. incrByFloat := client.IncrByFloat("key", 996945661)
  875. Expect(incrByFloat.Err()).NotTo(HaveOccurred())
  876. Expect(incrByFloat.Val()).To(Equal(float64(996945661)))
  877. })
  878. It("should MSetMGet", func() {
  879. mSet := client.MSet("key1", "hello1", "key2", "hello2")
  880. Expect(mSet.Err()).NotTo(HaveOccurred())
  881. Expect(mSet.Val()).To(Equal("OK"))
  882. mGet := client.MGet("key1", "key2", "_")
  883. Expect(mGet.Err()).NotTo(HaveOccurred())
  884. Expect(mGet.Val()).To(Equal([]interface{}{"hello1", "hello2", nil}))
  885. })
  886. It("should MSetNX", func() {
  887. mSetNX := client.MSetNX("key1", "hello1", "key2", "hello2")
  888. Expect(mSetNX.Err()).NotTo(HaveOccurred())
  889. Expect(mSetNX.Val()).To(Equal(true))
  890. mSetNX = client.MSetNX("key2", "hello1", "key3", "hello2")
  891. Expect(mSetNX.Err()).NotTo(HaveOccurred())
  892. Expect(mSetNX.Val()).To(Equal(false))
  893. })
  894. It("should Set with expiration", func() {
  895. err := client.Set("key", "hello", 100*time.Millisecond).Err()
  896. Expect(err).NotTo(HaveOccurred())
  897. val, err := client.Get("key").Result()
  898. Expect(err).NotTo(HaveOccurred())
  899. Expect(val).To(Equal("hello"))
  900. Eventually(func() error {
  901. return client.Get("foo").Err()
  902. }, "1s", "100ms").Should(Equal(redis.Nil))
  903. })
  904. It("should SetGet", func() {
  905. set := client.Set("key", "hello", 0)
  906. Expect(set.Err()).NotTo(HaveOccurred())
  907. Expect(set.Val()).To(Equal("OK"))
  908. get := client.Get("key")
  909. Expect(get.Err()).NotTo(HaveOccurred())
  910. Expect(get.Val()).To(Equal("hello"))
  911. })
  912. It("should SetNX", func() {
  913. setNX := client.SetNX("key", "hello", 0)
  914. Expect(setNX.Err()).NotTo(HaveOccurred())
  915. Expect(setNX.Val()).To(Equal(true))
  916. setNX = client.SetNX("key", "hello2", 0)
  917. Expect(setNX.Err()).NotTo(HaveOccurred())
  918. Expect(setNX.Val()).To(Equal(false))
  919. get := client.Get("key")
  920. Expect(get.Err()).NotTo(HaveOccurred())
  921. Expect(get.Val()).To(Equal("hello"))
  922. })
  923. It("should SetNX with expiration", func() {
  924. isSet, err := client.SetNX("key", "hello", time.Second).Result()
  925. Expect(err).NotTo(HaveOccurred())
  926. Expect(isSet).To(Equal(true))
  927. isSet, err = client.SetNX("key", "hello2", time.Second).Result()
  928. Expect(err).NotTo(HaveOccurred())
  929. Expect(isSet).To(Equal(false))
  930. val, err := client.Get("key").Result()
  931. Expect(err).NotTo(HaveOccurred())
  932. Expect(val).To(Equal("hello"))
  933. })
  934. It("should SetXX", func() {
  935. isSet, err := client.SetXX("key", "hello2", 0).Result()
  936. Expect(err).NotTo(HaveOccurred())
  937. Expect(isSet).To(Equal(false))
  938. err = client.Set("key", "hello", 0).Err()
  939. Expect(err).NotTo(HaveOccurred())
  940. isSet, err = client.SetXX("key", "hello2", 0).Result()
  941. Expect(err).NotTo(HaveOccurred())
  942. Expect(isSet).To(Equal(true))
  943. val, err := client.Get("key").Result()
  944. Expect(err).NotTo(HaveOccurred())
  945. Expect(val).To(Equal("hello2"))
  946. })
  947. It("should SetXX with expiration", func() {
  948. isSet, err := client.SetXX("key", "hello2", time.Second).Result()
  949. Expect(err).NotTo(HaveOccurred())
  950. Expect(isSet).To(Equal(false))
  951. err = client.Set("key", "hello", time.Second).Err()
  952. Expect(err).NotTo(HaveOccurred())
  953. isSet, err = client.SetXX("key", "hello2", time.Second).Result()
  954. Expect(err).NotTo(HaveOccurred())
  955. Expect(isSet).To(Equal(true))
  956. val, err := client.Get("key").Result()
  957. Expect(err).NotTo(HaveOccurred())
  958. Expect(val).To(Equal("hello2"))
  959. })
  960. It("should SetRange", func() {
  961. set := client.Set("key", "Hello World", 0)
  962. Expect(set.Err()).NotTo(HaveOccurred())
  963. Expect(set.Val()).To(Equal("OK"))
  964. range_ := client.SetRange("key", 6, "Redis")
  965. Expect(range_.Err()).NotTo(HaveOccurred())
  966. Expect(range_.Val()).To(Equal(int64(11)))
  967. get := client.Get("key")
  968. Expect(get.Err()).NotTo(HaveOccurred())
  969. Expect(get.Val()).To(Equal("Hello Redis"))
  970. })
  971. It("should StrLen", func() {
  972. set := client.Set("key", "hello", 0)
  973. Expect(set.Err()).NotTo(HaveOccurred())
  974. Expect(set.Val()).To(Equal("OK"))
  975. strLen := client.StrLen("key")
  976. Expect(strLen.Err()).NotTo(HaveOccurred())
  977. Expect(strLen.Val()).To(Equal(int64(5)))
  978. strLen = client.StrLen("_")
  979. Expect(strLen.Err()).NotTo(HaveOccurred())
  980. Expect(strLen.Val()).To(Equal(int64(0)))
  981. })
  982. })
  983. Describe("hashes", func() {
  984. It("should HDel", func() {
  985. hSet := client.HSet("hash", "key", "hello")
  986. Expect(hSet.Err()).NotTo(HaveOccurred())
  987. hDel := client.HDel("hash", "key")
  988. Expect(hDel.Err()).NotTo(HaveOccurred())
  989. Expect(hDel.Val()).To(Equal(int64(1)))
  990. hDel = client.HDel("hash", "key")
  991. Expect(hDel.Err()).NotTo(HaveOccurred())
  992. Expect(hDel.Val()).To(Equal(int64(0)))
  993. })
  994. It("should HExists", func() {
  995. hSet := client.HSet("hash", "key", "hello")
  996. Expect(hSet.Err()).NotTo(HaveOccurred())
  997. hExists := client.HExists("hash", "key")
  998. Expect(hExists.Err()).NotTo(HaveOccurred())
  999. Expect(hExists.Val()).To(Equal(true))
  1000. hExists = client.HExists("hash", "key1")
  1001. Expect(hExists.Err()).NotTo(HaveOccurred())
  1002. Expect(hExists.Val()).To(Equal(false))
  1003. })
  1004. It("should HGet", func() {
  1005. hSet := client.HSet("hash", "key", "hello")
  1006. Expect(hSet.Err()).NotTo(HaveOccurred())
  1007. hGet := client.HGet("hash", "key")
  1008. Expect(hGet.Err()).NotTo(HaveOccurred())
  1009. Expect(hGet.Val()).To(Equal("hello"))
  1010. hGet = client.HGet("hash", "key1")
  1011. Expect(hGet.Err()).To(Equal(redis.Nil))
  1012. Expect(hGet.Val()).To(Equal(""))
  1013. })
  1014. It("should HGetAll", func() {
  1015. err := client.HSet("hash", "key1", "hello1").Err()
  1016. Expect(err).NotTo(HaveOccurred())
  1017. err = client.HSet("hash", "key2", "hello2").Err()
  1018. Expect(err).NotTo(HaveOccurred())
  1019. m, err := client.HGetAll("hash").Result()
  1020. Expect(err).NotTo(HaveOccurred())
  1021. Expect(m).To(Equal(map[string]string{"key1": "hello1", "key2": "hello2"}))
  1022. })
  1023. It("should HIncrBy", func() {
  1024. hSet := client.HSet("hash", "key", "5")
  1025. Expect(hSet.Err()).NotTo(HaveOccurred())
  1026. hIncrBy := client.HIncrBy("hash", "key", 1)
  1027. Expect(hIncrBy.Err()).NotTo(HaveOccurred())
  1028. Expect(hIncrBy.Val()).To(Equal(int64(6)))
  1029. hIncrBy = client.HIncrBy("hash", "key", -1)
  1030. Expect(hIncrBy.Err()).NotTo(HaveOccurred())
  1031. Expect(hIncrBy.Val()).To(Equal(int64(5)))
  1032. hIncrBy = client.HIncrBy("hash", "key", -10)
  1033. Expect(hIncrBy.Err()).NotTo(HaveOccurred())
  1034. Expect(hIncrBy.Val()).To(Equal(int64(-5)))
  1035. })
  1036. It("should HIncrByFloat", func() {
  1037. hSet := client.HSet("hash", "field", "10.50")
  1038. Expect(hSet.Err()).NotTo(HaveOccurred())
  1039. Expect(hSet.Val()).To(Equal(true))
  1040. hIncrByFloat := client.HIncrByFloat("hash", "field", 0.1)
  1041. Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
  1042. Expect(hIncrByFloat.Val()).To(Equal(10.6))
  1043. hSet = client.HSet("hash", "field", "5.0e3")
  1044. Expect(hSet.Err()).NotTo(HaveOccurred())
  1045. Expect(hSet.Val()).To(Equal(false))
  1046. hIncrByFloat = client.HIncrByFloat("hash", "field", 2.0e2)
  1047. Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
  1048. Expect(hIncrByFloat.Val()).To(Equal(float64(5200)))
  1049. })
  1050. It("should HKeys", func() {
  1051. hkeys := client.HKeys("hash")
  1052. Expect(hkeys.Err()).NotTo(HaveOccurred())
  1053. Expect(hkeys.Val()).To(Equal([]string{}))
  1054. hset := client.HSet("hash", "key1", "hello1")
  1055. Expect(hset.Err()).NotTo(HaveOccurred())
  1056. hset = client.HSet("hash", "key2", "hello2")
  1057. Expect(hset.Err()).NotTo(HaveOccurred())
  1058. hkeys = client.HKeys("hash")
  1059. Expect(hkeys.Err()).NotTo(HaveOccurred())
  1060. Expect(hkeys.Val()).To(Equal([]string{"key1", "key2"}))
  1061. })
  1062. It("should HLen", func() {
  1063. hSet := client.HSet("hash", "key1", "hello1")
  1064. Expect(hSet.Err()).NotTo(HaveOccurred())
  1065. hSet = client.HSet("hash", "key2", "hello2")
  1066. Expect(hSet.Err()).NotTo(HaveOccurred())
  1067. hLen := client.HLen("hash")
  1068. Expect(hLen.Err()).NotTo(HaveOccurred())
  1069. Expect(hLen.Val()).To(Equal(int64(2)))
  1070. })
  1071. It("should HMGet", func() {
  1072. err := client.HSet("hash", "key1", "hello1").Err()
  1073. Expect(err).NotTo(HaveOccurred())
  1074. err = client.HSet("hash", "key2", "hello2").Err()
  1075. Expect(err).NotTo(HaveOccurred())
  1076. vals, err := client.HMGet("hash", "key1", "key2", "_").Result()
  1077. Expect(err).NotTo(HaveOccurred())
  1078. Expect(vals).To(Equal([]interface{}{"hello1", "hello2", nil}))
  1079. })
  1080. It("should HMSet", func() {
  1081. ok, err := client.HMSet("hash", map[string]interface{}{
  1082. "key1": "hello1",
  1083. "key2": "hello2",
  1084. }).Result()
  1085. Expect(err).NotTo(HaveOccurred())
  1086. Expect(ok).To(Equal("OK"))
  1087. v, err := client.HGet("hash", "key1").Result()
  1088. Expect(err).NotTo(HaveOccurred())
  1089. Expect(v).To(Equal("hello1"))
  1090. v, err = client.HGet("hash", "key2").Result()
  1091. Expect(err).NotTo(HaveOccurred())
  1092. Expect(v).To(Equal("hello2"))
  1093. })
  1094. It("should HSet", func() {
  1095. hSet := client.HSet("hash", "key", "hello")
  1096. Expect(hSet.Err()).NotTo(HaveOccurred())
  1097. Expect(hSet.Val()).To(Equal(true))
  1098. hGet := client.HGet("hash", "key")
  1099. Expect(hGet.Err()).NotTo(HaveOccurred())
  1100. Expect(hGet.Val()).To(Equal("hello"))
  1101. })
  1102. It("should HSetNX", func() {
  1103. hSetNX := client.HSetNX("hash", "key", "hello")
  1104. Expect(hSetNX.Err()).NotTo(HaveOccurred())
  1105. Expect(hSetNX.Val()).To(Equal(true))
  1106. hSetNX = client.HSetNX("hash", "key", "hello")
  1107. Expect(hSetNX.Err()).NotTo(HaveOccurred())
  1108. Expect(hSetNX.Val()).To(Equal(false))
  1109. hGet := client.HGet("hash", "key")
  1110. Expect(hGet.Err()).NotTo(HaveOccurred())
  1111. Expect(hGet.Val()).To(Equal("hello"))
  1112. })
  1113. It("should HVals", func() {
  1114. err := client.HSet("hash", "key1", "hello1").Err()
  1115. Expect(err).NotTo(HaveOccurred())
  1116. err = client.HSet("hash", "key2", "hello2").Err()
  1117. Expect(err).NotTo(HaveOccurred())
  1118. v, err := client.HVals("hash").Result()
  1119. Expect(err).NotTo(HaveOccurred())
  1120. Expect(v).To(Equal([]string{"hello1", "hello2"}))
  1121. var slice []string
  1122. err = client.HVals("hash").ScanSlice(&slice)
  1123. Expect(err).NotTo(HaveOccurred())
  1124. Expect(slice).To(Equal([]string{"hello1", "hello2"}))
  1125. })
  1126. })
  1127. Describe("hyperloglog", func() {
  1128. It("should PFMerge", func() {
  1129. pfAdd := client.PFAdd("hll1", "1", "2", "3", "4", "5")
  1130. Expect(pfAdd.Err()).NotTo(HaveOccurred())
  1131. pfCount := client.PFCount("hll1")
  1132. Expect(pfCount.Err()).NotTo(HaveOccurred())
  1133. Expect(pfCount.Val()).To(Equal(int64(5)))
  1134. pfAdd = client.PFAdd("hll2", "a", "b", "c", "d", "e")
  1135. Expect(pfAdd.Err()).NotTo(HaveOccurred())
  1136. pfMerge := client.PFMerge("hllMerged", "hll1", "hll2")
  1137. Expect(pfMerge.Err()).NotTo(HaveOccurred())
  1138. pfCount = client.PFCount("hllMerged")
  1139. Expect(pfCount.Err()).NotTo(HaveOccurred())
  1140. Expect(pfCount.Val()).To(Equal(int64(10)))
  1141. pfCount = client.PFCount("hll1", "hll2")
  1142. Expect(pfCount.Err()).NotTo(HaveOccurred())
  1143. Expect(pfCount.Val()).To(Equal(int64(10)))
  1144. })
  1145. })
  1146. Describe("lists", func() {
  1147. It("should BLPop", func() {
  1148. rPush := client.RPush("list1", "a", "b", "c")
  1149. Expect(rPush.Err()).NotTo(HaveOccurred())
  1150. bLPop := client.BLPop(0, "list1", "list2")
  1151. Expect(bLPop.Err()).NotTo(HaveOccurred())
  1152. Expect(bLPop.Val()).To(Equal([]string{"list1", "a"}))
  1153. })
  1154. It("should BLPopBlocks", func() {
  1155. started := make(chan bool)
  1156. done := make(chan bool)
  1157. go func() {
  1158. defer GinkgoRecover()
  1159. started <- true
  1160. bLPop := client.BLPop(0, "list")
  1161. Expect(bLPop.Err()).NotTo(HaveOccurred())
  1162. Expect(bLPop.Val()).To(Equal([]string{"list", "a"}))
  1163. done <- true
  1164. }()
  1165. <-started
  1166. select {
  1167. case <-done:
  1168. Fail("BLPop is not blocked")
  1169. case <-time.After(time.Second):
  1170. // ok
  1171. }
  1172. rPush := client.RPush("list", "a")
  1173. Expect(rPush.Err()).NotTo(HaveOccurred())
  1174. select {
  1175. case <-done:
  1176. // ok
  1177. case <-time.After(time.Second):
  1178. Fail("BLPop is still blocked")
  1179. }
  1180. })
  1181. It("should BLPop timeout", func() {
  1182. val, err := client.BLPop(time.Second, "list1").Result()
  1183. Expect(err).To(Equal(redis.Nil))
  1184. Expect(val).To(BeNil())
  1185. Expect(client.Ping().Err()).NotTo(HaveOccurred())
  1186. stats := client.PoolStats()
  1187. Expect(stats.Hits).To(Equal(uint32(1)))
  1188. Expect(stats.Misses).To(Equal(uint32(2)))
  1189. Expect(stats.Timeouts).To(Equal(uint32(0)))
  1190. })
  1191. It("should BRPop", func() {
  1192. rPush := client.RPush("list1", "a", "b", "c")
  1193. Expect(rPush.Err()).NotTo(HaveOccurred())
  1194. bRPop := client.BRPop(0, "list1", "list2")
  1195. Expect(bRPop.Err()).NotTo(HaveOccurred())
  1196. Expect(bRPop.Val()).To(Equal([]string{"list1", "c"}))
  1197. })
  1198. It("should BRPop blocks", func() {
  1199. started := make(chan bool)
  1200. done := make(chan bool)
  1201. go func() {
  1202. defer GinkgoRecover()
  1203. started <- true
  1204. brpop := client.BRPop(0, "list")
  1205. Expect(brpop.Err()).NotTo(HaveOccurred())
  1206. Expect(brpop.Val()).To(Equal([]string{"list", "a"}))
  1207. done <- true
  1208. }()
  1209. <-started
  1210. select {
  1211. case <-done:
  1212. Fail("BRPop is not blocked")
  1213. case <-time.After(time.Second):
  1214. // ok
  1215. }
  1216. rPush := client.RPush("list", "a")
  1217. Expect(rPush.Err()).NotTo(HaveOccurred())
  1218. select {
  1219. case <-done:
  1220. // ok
  1221. case <-time.After(time.Second):
  1222. Fail("BRPop is still blocked")
  1223. // ok
  1224. }
  1225. })
  1226. It("should BRPopLPush", func() {
  1227. _, err := client.BRPopLPush("list1", "list2", time.Second).Result()
  1228. Expect(err).To(Equal(redis.Nil))
  1229. err = client.RPush("list1", "a", "b", "c").Err()
  1230. Expect(err).NotTo(HaveOccurred())
  1231. v, err := client.BRPopLPush("list1", "list2", 0).Result()
  1232. Expect(err).NotTo(HaveOccurred())
  1233. Expect(v).To(Equal("c"))
  1234. })
  1235. It("should LIndex", func() {
  1236. lPush := client.LPush("list", "World")
  1237. Expect(lPush.Err()).NotTo(HaveOccurred())
  1238. lPush = client.LPush("list", "Hello")
  1239. Expect(lPush.Err()).NotTo(HaveOccurred())
  1240. lIndex := client.LIndex("list", 0)
  1241. Expect(lIndex.Err()).NotTo(HaveOccurred())
  1242. Expect(lIndex.Val()).To(Equal("Hello"))
  1243. lIndex = client.LIndex("list", -1)
  1244. Expect(lIndex.Err()).NotTo(HaveOccurred())
  1245. Expect(lIndex.Val()).To(Equal("World"))
  1246. lIndex = client.LIndex("list", 3)
  1247. Expect(lIndex.Err()).To(Equal(redis.Nil))
  1248. Expect(lIndex.Val()).To(Equal(""))
  1249. })
  1250. It("should LInsert", func() {
  1251. rPush := client.RPush("list", "Hello")
  1252. Expect(rPush.Err()).NotTo(HaveOccurred())
  1253. rPush = client.RPush("list", "World")
  1254. Expect(rPush.Err()).NotTo(HaveOccurred())
  1255. lInsert := client.LInsert("list", "BEFORE", "World", "There")
  1256. Expect(lInsert.Err()).NotTo(HaveOccurred())
  1257. Expect(lInsert.Val()).To(Equal(int64(3)))
  1258. lRange := client.LRange("list", 0, -1)
  1259. Expect(lRange.Err()).NotTo(HaveOccurred())
  1260. Expect(lRange.Val()).To(Equal([]string{"Hello", "There", "World"}))
  1261. })
  1262. It("should LLen", func() {
  1263. lPush := client.LPush("list", "World")
  1264. Expect(lPush.Err()).NotTo(HaveOccurred())
  1265. lPush = client.LPush("list", "Hello")
  1266. Expect(lPush.Err()).NotTo(HaveOccurred())
  1267. lLen := client.LLen("list")
  1268. Expect(lLen.Err()).NotTo(HaveOccurred())
  1269. Expect(lLen.Val()).To(Equal(int64(2)))
  1270. })
  1271. It("should LPop", func() {
  1272. rPush := client.RPush("list", "one")
  1273. Expect(rPush.Err()).NotTo(HaveOccurred())
  1274. rPush = client.RPush("list", "two")
  1275. Expect(rPush.Err()).NotTo(HaveOccurred())
  1276. rPush = client.RPush("list", "three")
  1277. Expect(rPush.Err()).NotTo(HaveOccurred())
  1278. lPop := client.LPop("list")
  1279. Expect(lPop.Err()).NotTo(HaveOccurred())
  1280. Expect(lPop.Val()).To(Equal("one"))
  1281. lRange := client.LRange("list", 0, -1)
  1282. Expect(lRange.Err()).NotTo(HaveOccurred())
  1283. Expect(lRange.Val()).To(Equal([]string{"two", "three"}))
  1284. })
  1285. It("should LPush", func() {
  1286. lPush := client.LPush("list", "World")
  1287. Expect(lPush.Err()).NotTo(HaveOccurred())
  1288. lPush = client.LPush("list", "Hello")
  1289. Expect(lPush.Err()).NotTo(HaveOccurred())
  1290. lRange := client.LRange("list", 0, -1)
  1291. Expect(lRange.Err()).NotTo(HaveOccurred())
  1292. Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
  1293. })
  1294. It("should LPushX", func() {
  1295. lPush := client.LPush("list", "World")
  1296. Expect(lPush.Err()).NotTo(HaveOccurred())
  1297. lPushX := client.LPushX("list", "Hello")
  1298. Expect(lPushX.Err()).NotTo(HaveOccurred())
  1299. Expect(lPushX.Val()).To(Equal(int64(2)))
  1300. lPushX = client.LPushX("list2", "Hello")
  1301. Expect(lPushX.Err()).NotTo(HaveOccurred())
  1302. Expect(lPushX.Val()).To(Equal(int64(0)))
  1303. lRange := client.LRange("list", 0, -1)
  1304. Expect(lRange.Err()).NotTo(HaveOccurred())
  1305. Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
  1306. lRange = client.LRange("list2", 0, -1)
  1307. Expect(lRange.Err()).NotTo(HaveOccurred())
  1308. Expect(lRange.Val()).To(Equal([]string{}))
  1309. })
  1310. It("should LRange", func() {
  1311. rPush := client.RPush("list", "one")
  1312. Expect(rPush.Err()).NotTo(HaveOccurred())
  1313. rPush = client.RPush("list", "two")
  1314. Expect(rPush.Err()).NotTo(HaveOccurred())
  1315. rPush = client.RPush("list", "three")
  1316. Expect(rPush.Err()).NotTo(HaveOccurred())
  1317. lRange := client.LRange("list", 0, 0)
  1318. Expect(lRange.Err()).NotTo(HaveOccurred())
  1319. Expect(lRange.Val()).To(Equal([]string{"one"}))
  1320. lRange = client.LRange("list", -3, 2)
  1321. Expect(lRange.Err()).NotTo(HaveOccurred())
  1322. Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
  1323. lRange = client.LRange("list", -100, 100)
  1324. Expect(lRange.Err()).NotTo(HaveOccurred())
  1325. Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
  1326. lRange = client.LRange("list", 5, 10)
  1327. Expect(lRange.Err()).NotTo(HaveOccurred())
  1328. Expect(lRange.Val()).To(Equal([]string{}))
  1329. })
  1330. It("should LRem", func() {
  1331. rPush := client.RPush("list", "hello")
  1332. Expect(rPush.Err()).NotTo(HaveOccurred())
  1333. rPush = client.RPush("list", "hello")
  1334. Expect(rPush.Err()).NotTo(HaveOccurred())
  1335. rPush = client.RPush("list", "key")
  1336. Expect(rPush.Err()).NotTo(HaveOccurred())
  1337. rPush = client.RPush("list", "hello")
  1338. Expect(rPush.Err()).NotTo(HaveOccurred())
  1339. lRem := client.LRem("list", -2, "hello")
  1340. Expect(lRem.Err()).NotTo(HaveOccurred())
  1341. Expect(lRem.Val()).To(Equal(int64(2)))
  1342. lRange := client.LRange("list", 0, -1)
  1343. Expect(lRange.Err()).NotTo(HaveOccurred())
  1344. Expect(lRange.Val()).To(Equal([]string{"hello", "key"}))
  1345. })
  1346. It("should LSet", func() {
  1347. rPush := client.RPush("list", "one")
  1348. Expect(rPush.Err()).NotTo(HaveOccurred())
  1349. rPush = client.RPush("list", "two")
  1350. Expect(rPush.Err()).NotTo(HaveOccurred())
  1351. rPush = client.RPush("list", "three")
  1352. Expect(rPush.Err()).NotTo(HaveOccurred())
  1353. lSet := client.LSet("list", 0, "four")
  1354. Expect(lSet.Err()).NotTo(HaveOccurred())
  1355. Expect(lSet.Val()).To(Equal("OK"))
  1356. lSet = client.LSet("list", -2, "five")
  1357. Expect(lSet.Err()).NotTo(HaveOccurred())
  1358. Expect(lSet.Val()).To(Equal("OK"))
  1359. lRange := client.LRange("list", 0, -1)
  1360. Expect(lRange.Err()).NotTo(HaveOccurred())
  1361. Expect(lRange.Val()).To(Equal([]string{"four", "five", "three"}))
  1362. })
  1363. It("should LTrim", func() {
  1364. rPush := client.RPush("list", "one")
  1365. Expect(rPush.Err()).NotTo(HaveOccurred())
  1366. rPush = client.RPush("list", "two")
  1367. Expect(rPush.Err()).NotTo(HaveOccurred())
  1368. rPush = client.RPush("list", "three")
  1369. Expect(rPush.Err()).NotTo(HaveOccurred())
  1370. lTrim := client.LTrim("list", 1, -1)
  1371. Expect(lTrim.Err()).NotTo(HaveOccurred())
  1372. Expect(lTrim.Val()).To(Equal("OK"))
  1373. lRange := client.LRange("list", 0, -1)
  1374. Expect(lRange.Err()).NotTo(HaveOccurred())
  1375. Expect(lRange.Val()).To(Equal([]string{"two", "three"}))
  1376. })
  1377. It("should RPop", func() {
  1378. rPush := client.RPush("list", "one")
  1379. Expect(rPush.Err()).NotTo(HaveOccurred())
  1380. rPush = client.RPush("list", "two")
  1381. Expect(rPush.Err()).NotTo(HaveOccurred())
  1382. rPush = client.RPush("list", "three")
  1383. Expect(rPush.Err()).NotTo(HaveOccurred())
  1384. rPop := client.RPop("list")
  1385. Expect(rPop.Err()).NotTo(HaveOccurred())
  1386. Expect(rPop.Val()).To(Equal("three"))
  1387. lRange := client.LRange("list", 0, -1)
  1388. Expect(lRange.Err()).NotTo(HaveOccurred())
  1389. Expect(lRange.Val()).To(Equal([]string{"one", "two"}))
  1390. })
  1391. It("should RPopLPush", func() {
  1392. rPush := client.RPush("list", "one")
  1393. Expect(rPush.Err()).NotTo(HaveOccurred())
  1394. rPush = client.RPush("list", "two")
  1395. Expect(rPush.Err()).NotTo(HaveOccurred())
  1396. rPush = client.RPush("list", "three")
  1397. Expect(rPush.Err()).NotTo(HaveOccurred())
  1398. rPopLPush := client.RPopLPush("list", "list2")
  1399. Expect(rPopLPush.Err()).NotTo(HaveOccurred())
  1400. Expect(rPopLPush.Val()).To(Equal("three"))
  1401. lRange := client.LRange("list", 0, -1)
  1402. Expect(lRange.Err()).NotTo(HaveOccurred())
  1403. Expect(lRange.Val()).To(Equal([]string{"one", "two"}))
  1404. lRange = client.LRange("list2", 0, -1)
  1405. Expect(lRange.Err()).NotTo(HaveOccurred())
  1406. Expect(lRange.Val()).To(Equal([]string{"three"}))
  1407. })
  1408. It("should RPush", func() {
  1409. rPush := client.RPush("list", "Hello")
  1410. Expect(rPush.Err()).NotTo(HaveOccurred())
  1411. Expect(rPush.Val()).To(Equal(int64(1)))
  1412. rPush = client.RPush("list", "World")
  1413. Expect(rPush.Err()).NotTo(HaveOccurred())
  1414. Expect(rPush.Val()).To(Equal(int64(2)))
  1415. lRange := client.LRange("list", 0, -1)
  1416. Expect(lRange.Err()).NotTo(HaveOccurred())
  1417. Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
  1418. })
  1419. It("should RPushX", func() {
  1420. rPush := client.RPush("list", "Hello")
  1421. Expect(rPush.Err()).NotTo(HaveOccurred())
  1422. Expect(rPush.Val()).To(Equal(int64(1)))
  1423. rPushX := client.RPushX("list", "World")
  1424. Expect(rPushX.Err()).NotTo(HaveOccurred())
  1425. Expect(rPushX.Val()).To(Equal(int64(2)))
  1426. rPushX = client.RPushX("list2", "World")
  1427. Expect(rPushX.Err()).NotTo(HaveOccurred())
  1428. Expect(rPushX.Val()).To(Equal(int64(0)))
  1429. lRange := client.LRange("list", 0, -1)
  1430. Expect(lRange.Err()).NotTo(HaveOccurred())
  1431. Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
  1432. lRange = client.LRange("list2", 0, -1)
  1433. Expect(lRange.Err()).NotTo(HaveOccurred())
  1434. Expect(lRange.Val()).To(Equal([]string{}))
  1435. })
  1436. })
  1437. Describe("sets", func() {
  1438. It("should SAdd", func() {
  1439. sAdd := client.SAdd("set", "Hello")
  1440. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1441. Expect(sAdd.Val()).To(Equal(int64(1)))
  1442. sAdd = client.SAdd("set", "World")
  1443. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1444. Expect(sAdd.Val()).To(Equal(int64(1)))
  1445. sAdd = client.SAdd("set", "World")
  1446. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1447. Expect(sAdd.Val()).To(Equal(int64(0)))
  1448. sMembers := client.SMembers("set")
  1449. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1450. Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
  1451. })
  1452. It("should SAdd strings", func() {
  1453. set := []string{"Hello", "World", "World"}
  1454. sAdd := client.SAdd("set", set)
  1455. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1456. Expect(sAdd.Val()).To(Equal(int64(2)))
  1457. sMembers := client.SMembers("set")
  1458. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1459. Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
  1460. })
  1461. It("should SCard", func() {
  1462. sAdd := client.SAdd("set", "Hello")
  1463. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1464. Expect(sAdd.Val()).To(Equal(int64(1)))
  1465. sAdd = client.SAdd("set", "World")
  1466. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1467. Expect(sAdd.Val()).To(Equal(int64(1)))
  1468. sCard := client.SCard("set")
  1469. Expect(sCard.Err()).NotTo(HaveOccurred())
  1470. Expect(sCard.Val()).To(Equal(int64(2)))
  1471. })
  1472. It("should SDiff", func() {
  1473. sAdd := client.SAdd("set1", "a")
  1474. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1475. sAdd = client.SAdd("set1", "b")
  1476. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1477. sAdd = client.SAdd("set1", "c")
  1478. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1479. sAdd = client.SAdd("set2", "c")
  1480. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1481. sAdd = client.SAdd("set2", "d")
  1482. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1483. sAdd = client.SAdd("set2", "e")
  1484. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1485. sDiff := client.SDiff("set1", "set2")
  1486. Expect(sDiff.Err()).NotTo(HaveOccurred())
  1487. Expect(sDiff.Val()).To(ConsistOf([]string{"a", "b"}))
  1488. })
  1489. It("should SDiffStore", func() {
  1490. sAdd := client.SAdd("set1", "a")
  1491. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1492. sAdd = client.SAdd("set1", "b")
  1493. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1494. sAdd = client.SAdd("set1", "c")
  1495. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1496. sAdd = client.SAdd("set2", "c")
  1497. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1498. sAdd = client.SAdd("set2", "d")
  1499. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1500. sAdd = client.SAdd("set2", "e")
  1501. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1502. sDiffStore := client.SDiffStore("set", "set1", "set2")
  1503. Expect(sDiffStore.Err()).NotTo(HaveOccurred())
  1504. Expect(sDiffStore.Val()).To(Equal(int64(2)))
  1505. sMembers := client.SMembers("set")
  1506. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1507. Expect(sMembers.Val()).To(ConsistOf([]string{"a", "b"}))
  1508. })
  1509. It("should SInter", func() {
  1510. sAdd := client.SAdd("set1", "a")
  1511. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1512. sAdd = client.SAdd("set1", "b")
  1513. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1514. sAdd = client.SAdd("set1", "c")
  1515. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1516. sAdd = client.SAdd("set2", "c")
  1517. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1518. sAdd = client.SAdd("set2", "d")
  1519. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1520. sAdd = client.SAdd("set2", "e")
  1521. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1522. sInter := client.SInter("set1", "set2")
  1523. Expect(sInter.Err()).NotTo(HaveOccurred())
  1524. Expect(sInter.Val()).To(Equal([]string{"c"}))
  1525. })
  1526. It("should SInterStore", func() {
  1527. sAdd := client.SAdd("set1", "a")
  1528. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1529. sAdd = client.SAdd("set1", "b")
  1530. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1531. sAdd = client.SAdd("set1", "c")
  1532. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1533. sAdd = client.SAdd("set2", "c")
  1534. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1535. sAdd = client.SAdd("set2", "d")
  1536. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1537. sAdd = client.SAdd("set2", "e")
  1538. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1539. sInterStore := client.SInterStore("set", "set1", "set2")
  1540. Expect(sInterStore.Err()).NotTo(HaveOccurred())
  1541. Expect(sInterStore.Val()).To(Equal(int64(1)))
  1542. sMembers := client.SMembers("set")
  1543. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1544. Expect(sMembers.Val()).To(Equal([]string{"c"}))
  1545. })
  1546. It("should IsMember", func() {
  1547. sAdd := client.SAdd("set", "one")
  1548. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1549. sIsMember := client.SIsMember("set", "one")
  1550. Expect(sIsMember.Err()).NotTo(HaveOccurred())
  1551. Expect(sIsMember.Val()).To(Equal(true))
  1552. sIsMember = client.SIsMember("set", "two")
  1553. Expect(sIsMember.Err()).NotTo(HaveOccurred())
  1554. Expect(sIsMember.Val()).To(Equal(false))
  1555. })
  1556. It("should SMembers", func() {
  1557. sAdd := client.SAdd("set", "Hello")
  1558. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1559. sAdd = client.SAdd("set", "World")
  1560. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1561. sMembers := client.SMembers("set")
  1562. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1563. Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
  1564. })
  1565. It("should SMembersMap", func() {
  1566. sAdd := client.SAdd("set", "Hello")
  1567. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1568. sAdd = client.SAdd("set", "World")
  1569. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1570. sMembersMap := client.SMembersMap("set")
  1571. Expect(sMembersMap.Err()).NotTo(HaveOccurred())
  1572. Expect(sMembersMap.Val()).To(Equal(map[string]struct{}{"Hello": {}, "World": {}}))
  1573. })
  1574. It("should SMove", func() {
  1575. sAdd := client.SAdd("set1", "one")
  1576. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1577. sAdd = client.SAdd("set1", "two")
  1578. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1579. sAdd = client.SAdd("set2", "three")
  1580. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1581. sMove := client.SMove("set1", "set2", "two")
  1582. Expect(sMove.Err()).NotTo(HaveOccurred())
  1583. Expect(sMove.Val()).To(Equal(true))
  1584. sMembers := client.SMembers("set1")
  1585. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1586. Expect(sMembers.Val()).To(Equal([]string{"one"}))
  1587. sMembers = client.SMembers("set2")
  1588. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1589. Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"}))
  1590. })
  1591. It("should SPop", func() {
  1592. sAdd := client.SAdd("set", "one")
  1593. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1594. sAdd = client.SAdd("set", "two")
  1595. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1596. sAdd = client.SAdd("set", "three")
  1597. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1598. sPop := client.SPop("set")
  1599. Expect(sPop.Err()).NotTo(HaveOccurred())
  1600. Expect(sPop.Val()).NotTo(Equal(""))
  1601. sMembers := client.SMembers("set")
  1602. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1603. Expect(sMembers.Val()).To(HaveLen(2))
  1604. })
  1605. It("should SPopN", func() {
  1606. sAdd := client.SAdd("set", "one")
  1607. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1608. sAdd = client.SAdd("set", "two")
  1609. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1610. sAdd = client.SAdd("set", "three")
  1611. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1612. sAdd = client.SAdd("set", "four")
  1613. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1614. sPopN := client.SPopN("set", 1)
  1615. Expect(sPopN.Err()).NotTo(HaveOccurred())
  1616. Expect(sPopN.Val()).NotTo(Equal([]string{""}))
  1617. sMembers := client.SMembers("set")
  1618. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1619. Expect(sMembers.Val()).To(HaveLen(3))
  1620. sPopN = client.SPopN("set", 4)
  1621. Expect(sPopN.Err()).NotTo(HaveOccurred())
  1622. Expect(sPopN.Val()).To(HaveLen(3))
  1623. sMembers = client.SMembers("set")
  1624. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1625. Expect(sMembers.Val()).To(HaveLen(0))
  1626. })
  1627. It("should SRandMember and SRandMemberN", func() {
  1628. err := client.SAdd("set", "one").Err()
  1629. Expect(err).NotTo(HaveOccurred())
  1630. err = client.SAdd("set", "two").Err()
  1631. Expect(err).NotTo(HaveOccurred())
  1632. err = client.SAdd("set", "three").Err()
  1633. Expect(err).NotTo(HaveOccurred())
  1634. members, err := client.SMembers("set").Result()
  1635. Expect(err).NotTo(HaveOccurred())
  1636. Expect(members).To(HaveLen(3))
  1637. member, err := client.SRandMember("set").Result()
  1638. Expect(err).NotTo(HaveOccurred())
  1639. Expect(member).NotTo(Equal(""))
  1640. members, err = client.SRandMemberN("set", 2).Result()
  1641. Expect(err).NotTo(HaveOccurred())
  1642. Expect(members).To(HaveLen(2))
  1643. })
  1644. It("should SRem", func() {
  1645. sAdd := client.SAdd("set", "one")
  1646. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1647. sAdd = client.SAdd("set", "two")
  1648. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1649. sAdd = client.SAdd("set", "three")
  1650. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1651. sRem := client.SRem("set", "one")
  1652. Expect(sRem.Err()).NotTo(HaveOccurred())
  1653. Expect(sRem.Val()).To(Equal(int64(1)))
  1654. sRem = client.SRem("set", "four")
  1655. Expect(sRem.Err()).NotTo(HaveOccurred())
  1656. Expect(sRem.Val()).To(Equal(int64(0)))
  1657. sMembers := client.SMembers("set")
  1658. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1659. Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"}))
  1660. })
  1661. It("should SUnion", func() {
  1662. sAdd := client.SAdd("set1", "a")
  1663. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1664. sAdd = client.SAdd("set1", "b")
  1665. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1666. sAdd = client.SAdd("set1", "c")
  1667. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1668. sAdd = client.SAdd("set2", "c")
  1669. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1670. sAdd = client.SAdd("set2", "d")
  1671. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1672. sAdd = client.SAdd("set2", "e")
  1673. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1674. sUnion := client.SUnion("set1", "set2")
  1675. Expect(sUnion.Err()).NotTo(HaveOccurred())
  1676. Expect(sUnion.Val()).To(HaveLen(5))
  1677. })
  1678. It("should SUnionStore", func() {
  1679. sAdd := client.SAdd("set1", "a")
  1680. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1681. sAdd = client.SAdd("set1", "b")
  1682. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1683. sAdd = client.SAdd("set1", "c")
  1684. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1685. sAdd = client.SAdd("set2", "c")
  1686. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1687. sAdd = client.SAdd("set2", "d")
  1688. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1689. sAdd = client.SAdd("set2", "e")
  1690. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1691. sUnionStore := client.SUnionStore("set", "set1", "set2")
  1692. Expect(sUnionStore.Err()).NotTo(HaveOccurred())
  1693. Expect(sUnionStore.Val()).To(Equal(int64(5)))
  1694. sMembers := client.SMembers("set")
  1695. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1696. Expect(sMembers.Val()).To(HaveLen(5))
  1697. })
  1698. })
  1699. Describe("sorted sets", func() {
  1700. It("should BZPopMax", func() {
  1701. err := client.ZAdd("zset1", redis.Z{
  1702. Score: 1,
  1703. Member: "one",
  1704. }).Err()
  1705. Expect(err).NotTo(HaveOccurred())
  1706. err = client.ZAdd("zset1", redis.Z{
  1707. Score: 2,
  1708. Member: "two",
  1709. }).Err()
  1710. Expect(err).NotTo(HaveOccurred())
  1711. err = client.ZAdd("zset1", redis.Z{
  1712. Score: 3,
  1713. Member: "three",
  1714. }).Err()
  1715. Expect(err).NotTo(HaveOccurred())
  1716. member, err := client.BZPopMax(0, "zset1", "zset2").Result()
  1717. Expect(err).NotTo(HaveOccurred())
  1718. Expect(member).To(Equal(redis.ZWithKey{
  1719. Z: redis.Z{
  1720. Score: 3,
  1721. Member: "three",
  1722. },
  1723. Key: "zset1",
  1724. }))
  1725. })
  1726. It("should BZPopMax blocks", func() {
  1727. started := make(chan bool)
  1728. done := make(chan bool)
  1729. go func() {
  1730. defer GinkgoRecover()
  1731. started <- true
  1732. bZPopMax := client.BZPopMax(0, "zset")
  1733. Expect(bZPopMax.Err()).NotTo(HaveOccurred())
  1734. Expect(bZPopMax.Val()).To(Equal(redis.ZWithKey{
  1735. Z: redis.Z{
  1736. Member: "a",
  1737. Score: 1,
  1738. },
  1739. Key: "zset",
  1740. }))
  1741. done <- true
  1742. }()
  1743. <-started
  1744. select {
  1745. case <-done:
  1746. Fail("BZPopMax is not blocked")
  1747. case <-time.After(time.Second):
  1748. // ok
  1749. }
  1750. zAdd := client.ZAdd("zset", redis.Z{
  1751. Member: "a",
  1752. Score: 1,
  1753. })
  1754. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1755. select {
  1756. case <-done:
  1757. // ok
  1758. case <-time.After(time.Second):
  1759. Fail("BZPopMax is still blocked")
  1760. }
  1761. })
  1762. It("should BZPopMax timeout", func() {
  1763. val, err := client.BZPopMax(time.Second, "zset1").Result()
  1764. Expect(err).To(Equal(redis.Nil))
  1765. Expect(val).To(Equal(redis.ZWithKey{}))
  1766. Expect(client.Ping().Err()).NotTo(HaveOccurred())
  1767. stats := client.PoolStats()
  1768. Expect(stats.Hits).To(Equal(uint32(1)))
  1769. Expect(stats.Misses).To(Equal(uint32(2)))
  1770. Expect(stats.Timeouts).To(Equal(uint32(0)))
  1771. })
  1772. It("should BZPopMin", func() {
  1773. err := client.ZAdd("zset1", redis.Z{
  1774. Score: 1,
  1775. Member: "one",
  1776. }).Err()
  1777. Expect(err).NotTo(HaveOccurred())
  1778. err = client.ZAdd("zset1", redis.Z{
  1779. Score: 2,
  1780. Member: "two",
  1781. }).Err()
  1782. Expect(err).NotTo(HaveOccurred())
  1783. err = client.ZAdd("zset1", redis.Z{
  1784. Score: 3,
  1785. Member: "three",
  1786. }).Err()
  1787. Expect(err).NotTo(HaveOccurred())
  1788. member, err := client.BZPopMin(0, "zset1", "zset2").Result()
  1789. Expect(err).NotTo(HaveOccurred())
  1790. Expect(member).To(Equal(redis.ZWithKey{
  1791. Z: redis.Z{
  1792. Score: 1,
  1793. Member: "one",
  1794. },
  1795. Key: "zset1",
  1796. }))
  1797. })
  1798. It("should BZPopMin blocks", func() {
  1799. started := make(chan bool)
  1800. done := make(chan bool)
  1801. go func() {
  1802. defer GinkgoRecover()
  1803. started <- true
  1804. bZPopMin := client.BZPopMin(0, "zset")
  1805. Expect(bZPopMin.Err()).NotTo(HaveOccurred())
  1806. Expect(bZPopMin.Val()).To(Equal(redis.ZWithKey{
  1807. Z: redis.Z{
  1808. Member: "a",
  1809. Score: 1,
  1810. },
  1811. Key: "zset",
  1812. }))
  1813. done <- true
  1814. }()
  1815. <-started
  1816. select {
  1817. case <-done:
  1818. Fail("BZPopMin is not blocked")
  1819. case <-time.After(time.Second):
  1820. // ok
  1821. }
  1822. zAdd := client.ZAdd("zset", redis.Z{
  1823. Member: "a",
  1824. Score: 1,
  1825. })
  1826. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1827. select {
  1828. case <-done:
  1829. // ok
  1830. case <-time.After(time.Second):
  1831. Fail("BZPopMin is still blocked")
  1832. }
  1833. })
  1834. It("should BZPopMin timeout", func() {
  1835. val, err := client.BZPopMin(time.Second, "zset1").Result()
  1836. Expect(err).To(Equal(redis.Nil))
  1837. Expect(val).To(Equal(redis.ZWithKey{}))
  1838. Expect(client.Ping().Err()).NotTo(HaveOccurred())
  1839. stats := client.PoolStats()
  1840. Expect(stats.Hits).To(Equal(uint32(1)))
  1841. Expect(stats.Misses).To(Equal(uint32(2)))
  1842. Expect(stats.Timeouts).To(Equal(uint32(0)))
  1843. })
  1844. It("should ZAdd", func() {
  1845. added, err := client.ZAdd("zset", redis.Z{
  1846. Score: 1,
  1847. Member: "one",
  1848. }).Result()
  1849. Expect(err).NotTo(HaveOccurred())
  1850. Expect(added).To(Equal(int64(1)))
  1851. added, err = client.ZAdd("zset", redis.Z{
  1852. Score: 1,
  1853. Member: "uno",
  1854. }).Result()
  1855. Expect(err).NotTo(HaveOccurred())
  1856. Expect(added).To(Equal(int64(1)))
  1857. added, err = client.ZAdd("zset", redis.Z{
  1858. Score: 2,
  1859. Member: "two",
  1860. }).Result()
  1861. Expect(err).NotTo(HaveOccurred())
  1862. Expect(added).To(Equal(int64(1)))
  1863. added, err = client.ZAdd("zset", redis.Z{
  1864. Score: 3,
  1865. Member: "two",
  1866. }).Result()
  1867. Expect(err).NotTo(HaveOccurred())
  1868. Expect(added).To(Equal(int64(0)))
  1869. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1870. Expect(err).NotTo(HaveOccurred())
  1871. Expect(vals).To(Equal([]redis.Z{{
  1872. Score: 1,
  1873. Member: "one",
  1874. }, {
  1875. Score: 1,
  1876. Member: "uno",
  1877. }, {
  1878. Score: 3,
  1879. Member: "two",
  1880. }}))
  1881. })
  1882. It("should ZAdd bytes", func() {
  1883. added, err := client.ZAdd("zset", redis.Z{
  1884. Score: 1,
  1885. Member: []byte("one"),
  1886. }).Result()
  1887. Expect(err).NotTo(HaveOccurred())
  1888. Expect(added).To(Equal(int64(1)))
  1889. added, err = client.ZAdd("zset", redis.Z{
  1890. Score: 1,
  1891. Member: []byte("uno"),
  1892. }).Result()
  1893. Expect(err).NotTo(HaveOccurred())
  1894. Expect(added).To(Equal(int64(1)))
  1895. added, err = client.ZAdd("zset", redis.Z{
  1896. Score: 2,
  1897. Member: []byte("two"),
  1898. }).Result()
  1899. Expect(err).NotTo(HaveOccurred())
  1900. Expect(added).To(Equal(int64(1)))
  1901. added, err = client.ZAdd("zset", redis.Z{
  1902. Score: 3,
  1903. Member: []byte("two"),
  1904. }).Result()
  1905. Expect(err).NotTo(HaveOccurred())
  1906. Expect(added).To(Equal(int64(0)))
  1907. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1908. Expect(err).NotTo(HaveOccurred())
  1909. Expect(vals).To(Equal([]redis.Z{{
  1910. Score: 1,
  1911. Member: "one",
  1912. }, {
  1913. Score: 1,
  1914. Member: "uno",
  1915. }, {
  1916. Score: 3,
  1917. Member: "two",
  1918. }}))
  1919. })
  1920. It("should ZAddNX", func() {
  1921. added, err := client.ZAddNX("zset", redis.Z{
  1922. Score: 1,
  1923. Member: "one",
  1924. }).Result()
  1925. Expect(err).NotTo(HaveOccurred())
  1926. Expect(added).To(Equal(int64(1)))
  1927. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1928. Expect(err).NotTo(HaveOccurred())
  1929. Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
  1930. added, err = client.ZAddNX("zset", redis.Z{
  1931. Score: 2,
  1932. Member: "one",
  1933. }).Result()
  1934. Expect(err).NotTo(HaveOccurred())
  1935. Expect(added).To(Equal(int64(0)))
  1936. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1937. Expect(err).NotTo(HaveOccurred())
  1938. Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
  1939. })
  1940. It("should ZAddXX", func() {
  1941. added, err := client.ZAddXX("zset", redis.Z{
  1942. Score: 1,
  1943. Member: "one",
  1944. }).Result()
  1945. Expect(err).NotTo(HaveOccurred())
  1946. Expect(added).To(Equal(int64(0)))
  1947. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1948. Expect(err).NotTo(HaveOccurred())
  1949. Expect(vals).To(BeEmpty())
  1950. added, err = client.ZAdd("zset", redis.Z{
  1951. Score: 1,
  1952. Member: "one",
  1953. }).Result()
  1954. Expect(err).NotTo(HaveOccurred())
  1955. Expect(added).To(Equal(int64(1)))
  1956. added, err = client.ZAddXX("zset", redis.Z{
  1957. Score: 2,
  1958. Member: "one",
  1959. }).Result()
  1960. Expect(err).NotTo(HaveOccurred())
  1961. Expect(added).To(Equal(int64(0)))
  1962. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1963. Expect(err).NotTo(HaveOccurred())
  1964. Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
  1965. })
  1966. It("should ZAddCh", func() {
  1967. changed, err := client.ZAddCh("zset", redis.Z{
  1968. Score: 1,
  1969. Member: "one",
  1970. }).Result()
  1971. Expect(err).NotTo(HaveOccurred())
  1972. Expect(changed).To(Equal(int64(1)))
  1973. changed, err = client.ZAddCh("zset", redis.Z{
  1974. Score: 1,
  1975. Member: "one",
  1976. }).Result()
  1977. Expect(err).NotTo(HaveOccurred())
  1978. Expect(changed).To(Equal(int64(0)))
  1979. })
  1980. It("should ZAddNXCh", func() {
  1981. changed, err := client.ZAddNXCh("zset", redis.Z{
  1982. Score: 1,
  1983. Member: "one",
  1984. }).Result()
  1985. Expect(err).NotTo(HaveOccurred())
  1986. Expect(changed).To(Equal(int64(1)))
  1987. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1988. Expect(err).NotTo(HaveOccurred())
  1989. Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
  1990. changed, err = client.ZAddNXCh("zset", redis.Z{
  1991. Score: 2,
  1992. Member: "one",
  1993. }).Result()
  1994. Expect(err).NotTo(HaveOccurred())
  1995. Expect(changed).To(Equal(int64(0)))
  1996. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1997. Expect(err).NotTo(HaveOccurred())
  1998. Expect(vals).To(Equal([]redis.Z{{
  1999. Score: 1,
  2000. Member: "one",
  2001. }}))
  2002. })
  2003. It("should ZAddXXCh", func() {
  2004. changed, err := client.ZAddXXCh("zset", redis.Z{
  2005. Score: 1,
  2006. Member: "one",
  2007. }).Result()
  2008. Expect(err).NotTo(HaveOccurred())
  2009. Expect(changed).To(Equal(int64(0)))
  2010. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  2011. Expect(err).NotTo(HaveOccurred())
  2012. Expect(vals).To(BeEmpty())
  2013. added, err := client.ZAdd("zset", redis.Z{
  2014. Score: 1,
  2015. Member: "one",
  2016. }).Result()
  2017. Expect(err).NotTo(HaveOccurred())
  2018. Expect(added).To(Equal(int64(1)))
  2019. changed, err = client.ZAddXXCh("zset", redis.Z{
  2020. Score: 2,
  2021. Member: "one",
  2022. }).Result()
  2023. Expect(err).NotTo(HaveOccurred())
  2024. Expect(changed).To(Equal(int64(1)))
  2025. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  2026. Expect(err).NotTo(HaveOccurred())
  2027. Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
  2028. })
  2029. It("should ZIncr", func() {
  2030. score, err := client.ZIncr("zset", redis.Z{
  2031. Score: 1,
  2032. Member: "one",
  2033. }).Result()
  2034. Expect(err).NotTo(HaveOccurred())
  2035. Expect(score).To(Equal(float64(1)))
  2036. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  2037. Expect(err).NotTo(HaveOccurred())
  2038. Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
  2039. score, err = client.ZIncr("zset", redis.Z{Score: 1, Member: "one"}).Result()
  2040. Expect(err).NotTo(HaveOccurred())
  2041. Expect(score).To(Equal(float64(2)))
  2042. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  2043. Expect(err).NotTo(HaveOccurred())
  2044. Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
  2045. })
  2046. It("should ZIncrNX", func() {
  2047. score, err := client.ZIncrNX("zset", redis.Z{
  2048. Score: 1,
  2049. Member: "one",
  2050. }).Result()
  2051. Expect(err).NotTo(HaveOccurred())
  2052. Expect(score).To(Equal(float64(1)))
  2053. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  2054. Expect(err).NotTo(HaveOccurred())
  2055. Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
  2056. score, err = client.ZIncrNX("zset", redis.Z{
  2057. Score: 1,
  2058. Member: "one",
  2059. }).Result()
  2060. Expect(err).To(Equal(redis.Nil))
  2061. Expect(score).To(Equal(float64(0)))
  2062. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  2063. Expect(err).NotTo(HaveOccurred())
  2064. Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
  2065. })
  2066. It("should ZIncrXX", func() {
  2067. score, err := client.ZIncrXX("zset", redis.Z{
  2068. Score: 1,
  2069. Member: "one",
  2070. }).Result()
  2071. Expect(err).To(Equal(redis.Nil))
  2072. Expect(score).To(Equal(float64(0)))
  2073. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  2074. Expect(err).NotTo(HaveOccurred())
  2075. Expect(vals).To(BeEmpty())
  2076. added, err := client.ZAdd("zset", redis.Z{
  2077. Score: 1,
  2078. Member: "one",
  2079. }).Result()
  2080. Expect(err).NotTo(HaveOccurred())
  2081. Expect(added).To(Equal(int64(1)))
  2082. score, err = client.ZIncrXX("zset", redis.Z{
  2083. Score: 1,
  2084. Member: "one",
  2085. }).Result()
  2086. Expect(err).NotTo(HaveOccurred())
  2087. Expect(score).To(Equal(float64(2)))
  2088. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  2089. Expect(err).NotTo(HaveOccurred())
  2090. Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
  2091. })
  2092. It("should ZCard", func() {
  2093. err := client.ZAdd("zset", redis.Z{
  2094. Score: 1,
  2095. Member: "one",
  2096. }).Err()
  2097. Expect(err).NotTo(HaveOccurred())
  2098. err = client.ZAdd("zset", redis.Z{
  2099. Score: 2,
  2100. Member: "two",
  2101. }).Err()
  2102. Expect(err).NotTo(HaveOccurred())
  2103. card, err := client.ZCard("zset").Result()
  2104. Expect(err).NotTo(HaveOccurred())
  2105. Expect(card).To(Equal(int64(2)))
  2106. })
  2107. It("should ZCount", func() {
  2108. err := client.ZAdd("zset", redis.Z{
  2109. Score: 1,
  2110. Member: "one",
  2111. }).Err()
  2112. Expect(err).NotTo(HaveOccurred())
  2113. err = client.ZAdd("zset", redis.Z{
  2114. Score: 2,
  2115. Member: "two",
  2116. }).Err()
  2117. Expect(err).NotTo(HaveOccurred())
  2118. err = client.ZAdd("zset", redis.Z{
  2119. Score: 3,
  2120. Member: "three",
  2121. }).Err()
  2122. Expect(err).NotTo(HaveOccurred())
  2123. count, err := client.ZCount("zset", "-inf", "+inf").Result()
  2124. Expect(err).NotTo(HaveOccurred())
  2125. Expect(count).To(Equal(int64(3)))
  2126. count, err = client.ZCount("zset", "(1", "3").Result()
  2127. Expect(err).NotTo(HaveOccurred())
  2128. Expect(count).To(Equal(int64(2)))
  2129. count, err = client.ZLexCount("zset", "-", "+").Result()
  2130. Expect(err).NotTo(HaveOccurred())
  2131. Expect(count).To(Equal(int64(3)))
  2132. })
  2133. It("should ZIncrBy", func() {
  2134. err := client.ZAdd("zset", redis.Z{
  2135. Score: 1,
  2136. Member: "one",
  2137. }).Err()
  2138. Expect(err).NotTo(HaveOccurred())
  2139. err = client.ZAdd("zset", redis.Z{
  2140. Score: 2,
  2141. Member: "two",
  2142. }).Err()
  2143. Expect(err).NotTo(HaveOccurred())
  2144. n, err := client.ZIncrBy("zset", 2, "one").Result()
  2145. Expect(err).NotTo(HaveOccurred())
  2146. Expect(n).To(Equal(float64(3)))
  2147. val, err := client.ZRangeWithScores("zset", 0, -1).Result()
  2148. Expect(err).NotTo(HaveOccurred())
  2149. Expect(val).To(Equal([]redis.Z{{
  2150. Score: 2,
  2151. Member: "two",
  2152. }, {
  2153. Score: 3,
  2154. Member: "one",
  2155. }}))
  2156. })
  2157. It("should ZInterStore", func() {
  2158. err := client.ZAdd("zset1", redis.Z{
  2159. Score: 1,
  2160. Member: "one",
  2161. }).Err()
  2162. Expect(err).NotTo(HaveOccurred())
  2163. err = client.ZAdd("zset1", redis.Z{
  2164. Score: 2,
  2165. Member: "two",
  2166. }).Err()
  2167. Expect(err).NotTo(HaveOccurred())
  2168. err = client.ZAdd("zset2", redis.Z{Score: 1, Member: "one"}).Err()
  2169. Expect(err).NotTo(HaveOccurred())
  2170. err = client.ZAdd("zset2", redis.Z{Score: 2, Member: "two"}).Err()
  2171. Expect(err).NotTo(HaveOccurred())
  2172. err = client.ZAdd("zset3", redis.Z{Score: 3, Member: "two"}).Err()
  2173. Expect(err).NotTo(HaveOccurred())
  2174. zInterStore := client.ZInterStore(
  2175. "out", redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2")
  2176. Expect(zInterStore.Err()).NotTo(HaveOccurred())
  2177. Expect(zInterStore.Val()).To(Equal(int64(2)))
  2178. vals, err := client.ZRangeWithScores("out", 0, -1).Result()
  2179. Expect(err).NotTo(HaveOccurred())
  2180. Expect(vals).To(Equal([]redis.Z{{
  2181. Score: 5,
  2182. Member: "one",
  2183. }, {
  2184. Score: 10,
  2185. Member: "two",
  2186. }}))
  2187. })
  2188. It("should ZPopMax", func() {
  2189. err := client.ZAdd("zset", redis.Z{
  2190. Score: 1,
  2191. Member: "one",
  2192. }).Err()
  2193. Expect(err).NotTo(HaveOccurred())
  2194. err = client.ZAdd("zset", redis.Z{
  2195. Score: 2,
  2196. Member: "two",
  2197. }).Err()
  2198. Expect(err).NotTo(HaveOccurred())
  2199. err = client.ZAdd("zset", redis.Z{
  2200. Score: 3,
  2201. Member: "three",
  2202. }).Err()
  2203. Expect(err).NotTo(HaveOccurred())
  2204. members, err := client.ZPopMax("zset").Result()
  2205. Expect(err).NotTo(HaveOccurred())
  2206. Expect(members).To(Equal([]redis.Z{{
  2207. Score: 3,
  2208. Member: "three",
  2209. }}))
  2210. // adding back 3
  2211. err = client.ZAdd("zset", redis.Z{
  2212. Score: 3,
  2213. Member: "three",
  2214. }).Err()
  2215. Expect(err).NotTo(HaveOccurred())
  2216. members, err = client.ZPopMax("zset", 2).Result()
  2217. Expect(err).NotTo(HaveOccurred())
  2218. Expect(members).To(Equal([]redis.Z{{
  2219. Score: 3,
  2220. Member: "three",
  2221. }, {
  2222. Score: 2,
  2223. Member: "two",
  2224. }}))
  2225. // adding back 2 & 3
  2226. err = client.ZAdd("zset", redis.Z{
  2227. Score: 3,
  2228. Member: "three",
  2229. }).Err()
  2230. Expect(err).NotTo(HaveOccurred())
  2231. err = client.ZAdd("zset", redis.Z{
  2232. Score: 2,
  2233. Member: "two",
  2234. }).Err()
  2235. Expect(err).NotTo(HaveOccurred())
  2236. members, err = client.ZPopMax("zset", 10).Result()
  2237. Expect(err).NotTo(HaveOccurred())
  2238. Expect(members).To(Equal([]redis.Z{{
  2239. Score: 3,
  2240. Member: "three",
  2241. }, {
  2242. Score: 2,
  2243. Member: "two",
  2244. }, {
  2245. Score: 1,
  2246. Member: "one",
  2247. }}))
  2248. })
  2249. It("should ZPopMin", func() {
  2250. err := client.ZAdd("zset", redis.Z{
  2251. Score: 1,
  2252. Member: "one",
  2253. }).Err()
  2254. Expect(err).NotTo(HaveOccurred())
  2255. err = client.ZAdd("zset", redis.Z{
  2256. Score: 2,
  2257. Member: "two",
  2258. }).Err()
  2259. Expect(err).NotTo(HaveOccurred())
  2260. err = client.ZAdd("zset", redis.Z{
  2261. Score: 3,
  2262. Member: "three",
  2263. }).Err()
  2264. Expect(err).NotTo(HaveOccurred())
  2265. members, err := client.ZPopMin("zset").Result()
  2266. Expect(err).NotTo(HaveOccurred())
  2267. Expect(members).To(Equal([]redis.Z{{
  2268. Score: 1,
  2269. Member: "one",
  2270. }}))
  2271. // adding back 1
  2272. err = client.ZAdd("zset", redis.Z{
  2273. Score: 1,
  2274. Member: "one",
  2275. }).Err()
  2276. Expect(err).NotTo(HaveOccurred())
  2277. members, err = client.ZPopMin("zset", 2).Result()
  2278. Expect(err).NotTo(HaveOccurred())
  2279. Expect(members).To(Equal([]redis.Z{{
  2280. Score: 1,
  2281. Member: "one",
  2282. }, {
  2283. Score: 2,
  2284. Member: "two",
  2285. }}))
  2286. // adding back 1 & 2
  2287. err = client.ZAdd("zset", redis.Z{
  2288. Score: 1,
  2289. Member: "one",
  2290. }).Err()
  2291. Expect(err).NotTo(HaveOccurred())
  2292. err = client.ZAdd("zset", redis.Z{
  2293. Score: 2,
  2294. Member: "two",
  2295. }).Err()
  2296. Expect(err).NotTo(HaveOccurred())
  2297. members, err = client.ZPopMin("zset", 10).Result()
  2298. Expect(err).NotTo(HaveOccurred())
  2299. Expect(members).To(Equal([]redis.Z{{
  2300. Score: 1,
  2301. Member: "one",
  2302. }, {
  2303. Score: 2,
  2304. Member: "two",
  2305. }, {
  2306. Score: 3,
  2307. Member: "three",
  2308. }}))
  2309. })
  2310. It("should ZRange", func() {
  2311. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2312. Expect(err).NotTo(HaveOccurred())
  2313. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2314. Expect(err).NotTo(HaveOccurred())
  2315. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2316. Expect(err).NotTo(HaveOccurred())
  2317. zRange := client.ZRange("zset", 0, -1)
  2318. Expect(zRange.Err()).NotTo(HaveOccurred())
  2319. Expect(zRange.Val()).To(Equal([]string{"one", "two", "three"}))
  2320. zRange = client.ZRange("zset", 2, 3)
  2321. Expect(zRange.Err()).NotTo(HaveOccurred())
  2322. Expect(zRange.Val()).To(Equal([]string{"three"}))
  2323. zRange = client.ZRange("zset", -2, -1)
  2324. Expect(zRange.Err()).NotTo(HaveOccurred())
  2325. Expect(zRange.Val()).To(Equal([]string{"two", "three"}))
  2326. })
  2327. It("should ZRangeWithScores", func() {
  2328. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2329. Expect(err).NotTo(HaveOccurred())
  2330. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2331. Expect(err).NotTo(HaveOccurred())
  2332. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2333. Expect(err).NotTo(HaveOccurred())
  2334. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  2335. Expect(err).NotTo(HaveOccurred())
  2336. Expect(vals).To(Equal([]redis.Z{{
  2337. Score: 1,
  2338. Member: "one",
  2339. }, {
  2340. Score: 2,
  2341. Member: "two",
  2342. }, {
  2343. Score: 3,
  2344. Member: "three",
  2345. }}))
  2346. vals, err = client.ZRangeWithScores("zset", 2, 3).Result()
  2347. Expect(err).NotTo(HaveOccurred())
  2348. Expect(vals).To(Equal([]redis.Z{{Score: 3, Member: "three"}}))
  2349. vals, err = client.ZRangeWithScores("zset", -2, -1).Result()
  2350. Expect(err).NotTo(HaveOccurred())
  2351. Expect(vals).To(Equal([]redis.Z{{
  2352. Score: 2,
  2353. Member: "two",
  2354. }, {
  2355. Score: 3,
  2356. Member: "three",
  2357. }}))
  2358. })
  2359. It("should ZRangeByScore", func() {
  2360. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2361. Expect(err).NotTo(HaveOccurred())
  2362. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2363. Expect(err).NotTo(HaveOccurred())
  2364. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2365. Expect(err).NotTo(HaveOccurred())
  2366. zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeBy{
  2367. Min: "-inf",
  2368. Max: "+inf",
  2369. })
  2370. Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
  2371. Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two", "three"}))
  2372. zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
  2373. Min: "1",
  2374. Max: "2",
  2375. })
  2376. Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
  2377. Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two"}))
  2378. zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
  2379. Min: "(1",
  2380. Max: "2",
  2381. })
  2382. Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
  2383. Expect(zRangeByScore.Val()).To(Equal([]string{"two"}))
  2384. zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
  2385. Min: "(1",
  2386. Max: "(2",
  2387. })
  2388. Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
  2389. Expect(zRangeByScore.Val()).To(Equal([]string{}))
  2390. })
  2391. It("should ZRangeByLex", func() {
  2392. err := client.ZAdd("zset", redis.Z{
  2393. Score: 0,
  2394. Member: "a",
  2395. }).Err()
  2396. Expect(err).NotTo(HaveOccurred())
  2397. err = client.ZAdd("zset", redis.Z{
  2398. Score: 0,
  2399. Member: "b",
  2400. }).Err()
  2401. Expect(err).NotTo(HaveOccurred())
  2402. err = client.ZAdd("zset", redis.Z{
  2403. Score: 0,
  2404. Member: "c",
  2405. }).Err()
  2406. Expect(err).NotTo(HaveOccurred())
  2407. zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeBy{
  2408. Min: "-",
  2409. Max: "+",
  2410. })
  2411. Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
  2412. Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"}))
  2413. zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
  2414. Min: "[a",
  2415. Max: "[b",
  2416. })
  2417. Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
  2418. Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"}))
  2419. zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
  2420. Min: "(a",
  2421. Max: "[b",
  2422. })
  2423. Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
  2424. Expect(zRangeByLex.Val()).To(Equal([]string{"b"}))
  2425. zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
  2426. Min: "(a",
  2427. Max: "(b",
  2428. })
  2429. Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
  2430. Expect(zRangeByLex.Val()).To(Equal([]string{}))
  2431. })
  2432. It("should ZRangeByScoreWithScoresMap", func() {
  2433. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2434. Expect(err).NotTo(HaveOccurred())
  2435. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2436. Expect(err).NotTo(HaveOccurred())
  2437. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2438. Expect(err).NotTo(HaveOccurred())
  2439. vals, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
  2440. Min: "-inf",
  2441. Max: "+inf",
  2442. }).Result()
  2443. Expect(err).NotTo(HaveOccurred())
  2444. Expect(vals).To(Equal([]redis.Z{{
  2445. Score: 1,
  2446. Member: "one",
  2447. }, {
  2448. Score: 2,
  2449. Member: "two",
  2450. }, {
  2451. Score: 3,
  2452. Member: "three",
  2453. }}))
  2454. vals, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
  2455. Min: "1",
  2456. Max: "2",
  2457. }).Result()
  2458. Expect(err).NotTo(HaveOccurred())
  2459. Expect(vals).To(Equal([]redis.Z{{
  2460. Score: 1,
  2461. Member: "one",
  2462. }, {
  2463. Score: 2,
  2464. Member: "two",
  2465. }}))
  2466. vals, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
  2467. Min: "(1",
  2468. Max: "2",
  2469. }).Result()
  2470. Expect(err).NotTo(HaveOccurred())
  2471. Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "two"}}))
  2472. vals, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
  2473. Min: "(1",
  2474. Max: "(2",
  2475. }).Result()
  2476. Expect(err).NotTo(HaveOccurred())
  2477. Expect(vals).To(Equal([]redis.Z{}))
  2478. })
  2479. It("should ZRank", func() {
  2480. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2481. Expect(err).NotTo(HaveOccurred())
  2482. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2483. Expect(err).NotTo(HaveOccurred())
  2484. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2485. Expect(err).NotTo(HaveOccurred())
  2486. zRank := client.ZRank("zset", "three")
  2487. Expect(zRank.Err()).NotTo(HaveOccurred())
  2488. Expect(zRank.Val()).To(Equal(int64(2)))
  2489. zRank = client.ZRank("zset", "four")
  2490. Expect(zRank.Err()).To(Equal(redis.Nil))
  2491. Expect(zRank.Val()).To(Equal(int64(0)))
  2492. })
  2493. It("should ZRem", func() {
  2494. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2495. Expect(err).NotTo(HaveOccurred())
  2496. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2497. Expect(err).NotTo(HaveOccurred())
  2498. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2499. Expect(err).NotTo(HaveOccurred())
  2500. zRem := client.ZRem("zset", "two")
  2501. Expect(zRem.Err()).NotTo(HaveOccurred())
  2502. Expect(zRem.Val()).To(Equal(int64(1)))
  2503. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  2504. Expect(err).NotTo(HaveOccurred())
  2505. Expect(vals).To(Equal([]redis.Z{{
  2506. Score: 1,
  2507. Member: "one",
  2508. }, {
  2509. Score: 3,
  2510. Member: "three",
  2511. }}))
  2512. })
  2513. It("should ZRemRangeByRank", func() {
  2514. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2515. Expect(err).NotTo(HaveOccurred())
  2516. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2517. Expect(err).NotTo(HaveOccurred())
  2518. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2519. Expect(err).NotTo(HaveOccurred())
  2520. zRemRangeByRank := client.ZRemRangeByRank("zset", 0, 1)
  2521. Expect(zRemRangeByRank.Err()).NotTo(HaveOccurred())
  2522. Expect(zRemRangeByRank.Val()).To(Equal(int64(2)))
  2523. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  2524. Expect(err).NotTo(HaveOccurred())
  2525. Expect(vals).To(Equal([]redis.Z{{
  2526. Score: 3,
  2527. Member: "three",
  2528. }}))
  2529. })
  2530. It("should ZRemRangeByScore", func() {
  2531. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2532. Expect(err).NotTo(HaveOccurred())
  2533. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2534. Expect(err).NotTo(HaveOccurred())
  2535. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2536. Expect(err).NotTo(HaveOccurred())
  2537. zRemRangeByScore := client.ZRemRangeByScore("zset", "-inf", "(2")
  2538. Expect(zRemRangeByScore.Err()).NotTo(HaveOccurred())
  2539. Expect(zRemRangeByScore.Val()).To(Equal(int64(1)))
  2540. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  2541. Expect(err).NotTo(HaveOccurred())
  2542. Expect(vals).To(Equal([]redis.Z{{
  2543. Score: 2,
  2544. Member: "two",
  2545. }, {
  2546. Score: 3,
  2547. Member: "three",
  2548. }}))
  2549. })
  2550. It("should ZRemRangeByLex", func() {
  2551. zz := []redis.Z{
  2552. {Score: 0, Member: "aaaa"},
  2553. {Score: 0, Member: "b"},
  2554. {Score: 0, Member: "c"},
  2555. {Score: 0, Member: "d"},
  2556. {Score: 0, Member: "e"},
  2557. {Score: 0, Member: "foo"},
  2558. {Score: 0, Member: "zap"},
  2559. {Score: 0, Member: "zip"},
  2560. {Score: 0, Member: "ALPHA"},
  2561. {Score: 0, Member: "alpha"},
  2562. }
  2563. for _, z := range zz {
  2564. err := client.ZAdd("zset", z).Err()
  2565. Expect(err).NotTo(HaveOccurred())
  2566. }
  2567. n, err := client.ZRemRangeByLex("zset", "[alpha", "[omega").Result()
  2568. Expect(err).NotTo(HaveOccurred())
  2569. Expect(n).To(Equal(int64(6)))
  2570. vals, err := client.ZRange("zset", 0, -1).Result()
  2571. Expect(err).NotTo(HaveOccurred())
  2572. Expect(vals).To(Equal([]string{"ALPHA", "aaaa", "zap", "zip"}))
  2573. })
  2574. It("should ZRevRange", func() {
  2575. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2576. Expect(err).NotTo(HaveOccurred())
  2577. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2578. Expect(err).NotTo(HaveOccurred())
  2579. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2580. Expect(err).NotTo(HaveOccurred())
  2581. zRevRange := client.ZRevRange("zset", 0, -1)
  2582. Expect(zRevRange.Err()).NotTo(HaveOccurred())
  2583. Expect(zRevRange.Val()).To(Equal([]string{"three", "two", "one"}))
  2584. zRevRange = client.ZRevRange("zset", 2, 3)
  2585. Expect(zRevRange.Err()).NotTo(HaveOccurred())
  2586. Expect(zRevRange.Val()).To(Equal([]string{"one"}))
  2587. zRevRange = client.ZRevRange("zset", -2, -1)
  2588. Expect(zRevRange.Err()).NotTo(HaveOccurred())
  2589. Expect(zRevRange.Val()).To(Equal([]string{"two", "one"}))
  2590. })
  2591. It("should ZRevRangeWithScoresMap", func() {
  2592. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2593. Expect(err).NotTo(HaveOccurred())
  2594. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2595. Expect(err).NotTo(HaveOccurred())
  2596. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2597. Expect(err).NotTo(HaveOccurred())
  2598. val, err := client.ZRevRangeWithScores("zset", 0, -1).Result()
  2599. Expect(err).NotTo(HaveOccurred())
  2600. Expect(val).To(Equal([]redis.Z{{
  2601. Score: 3,
  2602. Member: "three",
  2603. }, {
  2604. Score: 2,
  2605. Member: "two",
  2606. }, {
  2607. Score: 1,
  2608. Member: "one",
  2609. }}))
  2610. val, err = client.ZRevRangeWithScores("zset", 2, 3).Result()
  2611. Expect(err).NotTo(HaveOccurred())
  2612. Expect(val).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
  2613. val, err = client.ZRevRangeWithScores("zset", -2, -1).Result()
  2614. Expect(err).NotTo(HaveOccurred())
  2615. Expect(val).To(Equal([]redis.Z{{
  2616. Score: 2,
  2617. Member: "two",
  2618. }, {
  2619. Score: 1,
  2620. Member: "one",
  2621. }}))
  2622. })
  2623. It("should ZRevRangeByScore", func() {
  2624. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2625. Expect(err).NotTo(HaveOccurred())
  2626. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2627. Expect(err).NotTo(HaveOccurred())
  2628. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2629. Expect(err).NotTo(HaveOccurred())
  2630. vals, err := client.ZRevRangeByScore(
  2631. "zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
  2632. Expect(err).NotTo(HaveOccurred())
  2633. Expect(vals).To(Equal([]string{"three", "two", "one"}))
  2634. vals, err = client.ZRevRangeByScore(
  2635. "zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result()
  2636. Expect(err).NotTo(HaveOccurred())
  2637. Expect(vals).To(Equal([]string{"two"}))
  2638. vals, err = client.ZRevRangeByScore(
  2639. "zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result()
  2640. Expect(err).NotTo(HaveOccurred())
  2641. Expect(vals).To(Equal([]string{}))
  2642. })
  2643. It("should ZRevRangeByLex", func() {
  2644. err := client.ZAdd("zset", redis.Z{Score: 0, Member: "a"}).Err()
  2645. Expect(err).NotTo(HaveOccurred())
  2646. err = client.ZAdd("zset", redis.Z{Score: 0, Member: "b"}).Err()
  2647. Expect(err).NotTo(HaveOccurred())
  2648. err = client.ZAdd("zset", redis.Z{Score: 0, Member: "c"}).Err()
  2649. Expect(err).NotTo(HaveOccurred())
  2650. vals, err := client.ZRevRangeByLex(
  2651. "zset", redis.ZRangeBy{Max: "+", Min: "-"}).Result()
  2652. Expect(err).NotTo(HaveOccurred())
  2653. Expect(vals).To(Equal([]string{"c", "b", "a"}))
  2654. vals, err = client.ZRevRangeByLex(
  2655. "zset", redis.ZRangeBy{Max: "[b", Min: "(a"}).Result()
  2656. Expect(err).NotTo(HaveOccurred())
  2657. Expect(vals).To(Equal([]string{"b"}))
  2658. vals, err = client.ZRevRangeByLex(
  2659. "zset", redis.ZRangeBy{Max: "(b", Min: "(a"}).Result()
  2660. Expect(err).NotTo(HaveOccurred())
  2661. Expect(vals).To(Equal([]string{}))
  2662. })
  2663. It("should ZRevRangeByScoreWithScores", func() {
  2664. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2665. Expect(err).NotTo(HaveOccurred())
  2666. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2667. Expect(err).NotTo(HaveOccurred())
  2668. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2669. Expect(err).NotTo(HaveOccurred())
  2670. vals, err := client.ZRevRangeByScoreWithScores(
  2671. "zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
  2672. Expect(err).NotTo(HaveOccurred())
  2673. Expect(vals).To(Equal([]redis.Z{{
  2674. Score: 3,
  2675. Member: "three",
  2676. }, {
  2677. Score: 2,
  2678. Member: "two",
  2679. }, {
  2680. Score: 1,
  2681. Member: "one",
  2682. }}))
  2683. })
  2684. It("should ZRevRangeByScoreWithScoresMap", func() {
  2685. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2686. Expect(err).NotTo(HaveOccurred())
  2687. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2688. Expect(err).NotTo(HaveOccurred())
  2689. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2690. Expect(err).NotTo(HaveOccurred())
  2691. vals, err := client.ZRevRangeByScoreWithScores(
  2692. "zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
  2693. Expect(err).NotTo(HaveOccurred())
  2694. Expect(vals).To(Equal([]redis.Z{{
  2695. Score: 3,
  2696. Member: "three",
  2697. }, {
  2698. Score: 2,
  2699. Member: "two",
  2700. }, {
  2701. Score: 1,
  2702. Member: "one",
  2703. }}))
  2704. vals, err = client.ZRevRangeByScoreWithScores(
  2705. "zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result()
  2706. Expect(err).NotTo(HaveOccurred())
  2707. Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "two"}}))
  2708. vals, err = client.ZRevRangeByScoreWithScores(
  2709. "zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result()
  2710. Expect(err).NotTo(HaveOccurred())
  2711. Expect(vals).To(Equal([]redis.Z{}))
  2712. })
  2713. It("should ZRevRank", func() {
  2714. err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
  2715. Expect(err).NotTo(HaveOccurred())
  2716. err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
  2717. Expect(err).NotTo(HaveOccurred())
  2718. err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
  2719. Expect(err).NotTo(HaveOccurred())
  2720. zRevRank := client.ZRevRank("zset", "one")
  2721. Expect(zRevRank.Err()).NotTo(HaveOccurred())
  2722. Expect(zRevRank.Val()).To(Equal(int64(2)))
  2723. zRevRank = client.ZRevRank("zset", "four")
  2724. Expect(zRevRank.Err()).To(Equal(redis.Nil))
  2725. Expect(zRevRank.Val()).To(Equal(int64(0)))
  2726. })
  2727. It("should ZScore", func() {
  2728. zAdd := client.ZAdd("zset", redis.Z{Score: 1.001, Member: "one"})
  2729. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2730. zScore := client.ZScore("zset", "one")
  2731. Expect(zScore.Err()).NotTo(HaveOccurred())
  2732. Expect(zScore.Val()).To(Equal(float64(1.001)))
  2733. })
  2734. It("should ZUnionStore", func() {
  2735. err := client.ZAdd("zset1", redis.Z{Score: 1, Member: "one"}).Err()
  2736. Expect(err).NotTo(HaveOccurred())
  2737. err = client.ZAdd("zset1", redis.Z{Score: 2, Member: "two"}).Err()
  2738. Expect(err).NotTo(HaveOccurred())
  2739. err = client.ZAdd("zset2", redis.Z{Score: 1, Member: "one"}).Err()
  2740. Expect(err).NotTo(HaveOccurred())
  2741. err = client.ZAdd("zset2", redis.Z{Score: 2, Member: "two"}).Err()
  2742. Expect(err).NotTo(HaveOccurred())
  2743. err = client.ZAdd("zset2", redis.Z{Score: 3, Member: "three"}).Err()
  2744. Expect(err).NotTo(HaveOccurred())
  2745. zUnionStore := client.ZUnionStore(
  2746. "out", redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2")
  2747. Expect(zUnionStore.Err()).NotTo(HaveOccurred())
  2748. Expect(zUnionStore.Val()).To(Equal(int64(3)))
  2749. val, err := client.ZRangeWithScores("out", 0, -1).Result()
  2750. Expect(err).NotTo(HaveOccurred())
  2751. Expect(val).To(Equal([]redis.Z{{
  2752. Score: 5,
  2753. Member: "one",
  2754. }, {
  2755. Score: 9,
  2756. Member: "three",
  2757. }, {
  2758. Score: 10,
  2759. Member: "two",
  2760. }}))
  2761. })
  2762. })
  2763. Describe("streams", func() {
  2764. BeforeEach(func() {
  2765. id, err := client.XAdd(&redis.XAddArgs{
  2766. Stream: "stream",
  2767. ID: "1-0",
  2768. Values: map[string]interface{}{"uno": "un"},
  2769. }).Result()
  2770. Expect(err).NotTo(HaveOccurred())
  2771. Expect(id).To(Equal("1-0"))
  2772. id, err = client.XAdd(&redis.XAddArgs{
  2773. Stream: "stream",
  2774. ID: "2-0",
  2775. Values: map[string]interface{}{"dos": "deux"},
  2776. }).Result()
  2777. Expect(err).NotTo(HaveOccurred())
  2778. Expect(id).To(Equal("2-0"))
  2779. id, err = client.XAdd(&redis.XAddArgs{
  2780. Stream: "stream",
  2781. ID: "3-0",
  2782. Values: map[string]interface{}{"tres": "troix"},
  2783. }).Result()
  2784. Expect(err).NotTo(HaveOccurred())
  2785. Expect(id).To(Equal("3-0"))
  2786. })
  2787. It("should XTrim", func() {
  2788. n, err := client.XTrim("stream", 0).Result()
  2789. Expect(err).NotTo(HaveOccurred())
  2790. Expect(n).To(Equal(int64(3)))
  2791. })
  2792. It("should XTrimApprox", func() {
  2793. n, err := client.XTrimApprox("stream", 0).Result()
  2794. Expect(err).NotTo(HaveOccurred())
  2795. Expect(n).To(Equal(int64(3)))
  2796. })
  2797. It("should XAdd", func() {
  2798. id, err := client.XAdd(&redis.XAddArgs{
  2799. Stream: "stream",
  2800. Values: map[string]interface{}{"quatro": "quatre"},
  2801. }).Result()
  2802. Expect(err).NotTo(HaveOccurred())
  2803. vals, err := client.XRange("stream", "-", "+").Result()
  2804. Expect(err).NotTo(HaveOccurred())
  2805. Expect(vals).To(Equal([]redis.XMessage{
  2806. {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
  2807. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2808. {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
  2809. {ID: id, Values: map[string]interface{}{"quatro": "quatre"}},
  2810. }))
  2811. })
  2812. It("should XAdd with MaxLen", func() {
  2813. id, err := client.XAdd(&redis.XAddArgs{
  2814. Stream: "stream",
  2815. MaxLen: 1,
  2816. Values: map[string]interface{}{"quatro": "quatre"},
  2817. }).Result()
  2818. Expect(err).NotTo(HaveOccurred())
  2819. vals, err := client.XRange("stream", "-", "+").Result()
  2820. Expect(err).NotTo(HaveOccurred())
  2821. Expect(vals).To(Equal([]redis.XMessage{
  2822. {ID: id, Values: map[string]interface{}{"quatro": "quatre"}},
  2823. }))
  2824. })
  2825. It("should XDel", func() {
  2826. n, err := client.XDel("stream", "1-0", "2-0", "3-0").Result()
  2827. Expect(err).NotTo(HaveOccurred())
  2828. Expect(n).To(Equal(int64(3)))
  2829. })
  2830. It("should XLen", func() {
  2831. n, err := client.XLen("stream").Result()
  2832. Expect(err).NotTo(HaveOccurred())
  2833. Expect(n).To(Equal(int64(3)))
  2834. })
  2835. It("should XRange", func() {
  2836. msgs, err := client.XRange("stream", "-", "+").Result()
  2837. Expect(err).NotTo(HaveOccurred())
  2838. Expect(msgs).To(Equal([]redis.XMessage{
  2839. {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
  2840. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2841. {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
  2842. }))
  2843. msgs, err = client.XRange("stream", "2", "+").Result()
  2844. Expect(err).NotTo(HaveOccurred())
  2845. Expect(msgs).To(Equal([]redis.XMessage{
  2846. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2847. {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
  2848. }))
  2849. msgs, err = client.XRange("stream", "-", "2").Result()
  2850. Expect(err).NotTo(HaveOccurred())
  2851. Expect(msgs).To(Equal([]redis.XMessage{
  2852. {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
  2853. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2854. }))
  2855. })
  2856. It("should XRangeN", func() {
  2857. msgs, err := client.XRangeN("stream", "-", "+", 2).Result()
  2858. Expect(err).NotTo(HaveOccurred())
  2859. Expect(msgs).To(Equal([]redis.XMessage{
  2860. {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
  2861. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2862. }))
  2863. msgs, err = client.XRangeN("stream", "2", "+", 1).Result()
  2864. Expect(err).NotTo(HaveOccurred())
  2865. Expect(msgs).To(Equal([]redis.XMessage{
  2866. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2867. }))
  2868. msgs, err = client.XRangeN("stream", "-", "2", 1).Result()
  2869. Expect(err).NotTo(HaveOccurred())
  2870. Expect(msgs).To(Equal([]redis.XMessage{
  2871. {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
  2872. }))
  2873. })
  2874. It("should XRevRange", func() {
  2875. msgs, err := client.XRevRange("stream", "+", "-").Result()
  2876. Expect(err).NotTo(HaveOccurred())
  2877. Expect(msgs).To(Equal([]redis.XMessage{
  2878. {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
  2879. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2880. {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
  2881. }))
  2882. msgs, err = client.XRevRange("stream", "+", "2").Result()
  2883. Expect(err).NotTo(HaveOccurred())
  2884. Expect(msgs).To(Equal([]redis.XMessage{
  2885. {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
  2886. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2887. }))
  2888. })
  2889. It("should XRevRangeN", func() {
  2890. msgs, err := client.XRevRangeN("stream", "+", "-", 2).Result()
  2891. Expect(err).NotTo(HaveOccurred())
  2892. Expect(msgs).To(Equal([]redis.XMessage{
  2893. {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
  2894. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2895. }))
  2896. msgs, err = client.XRevRangeN("stream", "+", "2", 1).Result()
  2897. Expect(err).NotTo(HaveOccurred())
  2898. Expect(msgs).To(Equal([]redis.XMessage{
  2899. {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
  2900. }))
  2901. })
  2902. It("should XRead", func() {
  2903. res, err := client.XReadStreams("stream", "0").Result()
  2904. Expect(err).NotTo(HaveOccurred())
  2905. Expect(res).To(Equal([]redis.XStream{{
  2906. Stream: "stream",
  2907. Messages: []redis.XMessage{
  2908. {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
  2909. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2910. {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
  2911. }},
  2912. }))
  2913. _, err = client.XReadStreams("stream", "3").Result()
  2914. Expect(err).To(Equal(redis.Nil))
  2915. })
  2916. It("should XRead", func() {
  2917. res, err := client.XRead(&redis.XReadArgs{
  2918. Streams: []string{"stream", "0"},
  2919. Count: 2,
  2920. Block: 100 * time.Millisecond,
  2921. }).Result()
  2922. Expect(err).NotTo(HaveOccurred())
  2923. Expect(res).To(Equal([]redis.XStream{{
  2924. Stream: "stream",
  2925. Messages: []redis.XMessage{
  2926. {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
  2927. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2928. }},
  2929. }))
  2930. _, err = client.XRead(&redis.XReadArgs{
  2931. Streams: []string{"stream", "3"},
  2932. Count: 1,
  2933. Block: 100 * time.Millisecond,
  2934. }).Result()
  2935. Expect(err).To(Equal(redis.Nil))
  2936. })
  2937. Describe("group", func() {
  2938. BeforeEach(func() {
  2939. err := client.XGroupCreate("stream", "group", "0").Err()
  2940. Expect(err).NotTo(HaveOccurred())
  2941. res, err := client.XReadGroup(&redis.XReadGroupArgs{
  2942. Group: "group",
  2943. Consumer: "consumer",
  2944. Streams: []string{"stream", ">"},
  2945. }).Result()
  2946. Expect(err).NotTo(HaveOccurred())
  2947. Expect(res).To(Equal([]redis.XStream{{
  2948. Stream: "stream",
  2949. Messages: []redis.XMessage{
  2950. {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
  2951. {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
  2952. {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
  2953. }},
  2954. }))
  2955. })
  2956. AfterEach(func() {
  2957. n, err := client.XGroupDestroy("stream", "group").Result()
  2958. Expect(err).NotTo(HaveOccurred())
  2959. Expect(n).To(Equal(int64(1)))
  2960. })
  2961. It("should XGroupCreateMkStream", func() {
  2962. err := client.XGroupCreateMkStream("stream2", "group", "0").Err()
  2963. Expect(err).NotTo(HaveOccurred())
  2964. err = client.XGroupCreateMkStream("stream2", "group", "0").Err()
  2965. Expect(err).To(Equal(proto.RedisError("BUSYGROUP Consumer Group name already exists")))
  2966. n, err := client.XGroupDestroy("stream2", "group").Result()
  2967. Expect(err).NotTo(HaveOccurred())
  2968. Expect(n).To(Equal(int64(1)))
  2969. n, err = client.Del("stream2").Result()
  2970. Expect(err).NotTo(HaveOccurred())
  2971. Expect(n).To(Equal(int64(1)))
  2972. })
  2973. It("should XPending", func() {
  2974. info, err := client.XPending("stream", "group").Result()
  2975. Expect(err).NotTo(HaveOccurred())
  2976. Expect(info).To(Equal(&redis.XPending{
  2977. Count: 3,
  2978. Lower: "1-0",
  2979. Higher: "3-0",
  2980. Consumers: map[string]int64{"consumer": 3},
  2981. }))
  2982. infoExt, err := client.XPendingExt(&redis.XPendingExtArgs{
  2983. Stream: "stream",
  2984. Group: "group",
  2985. Start: "-",
  2986. End: "+",
  2987. Count: 10,
  2988. Consumer: "consumer",
  2989. }).Result()
  2990. Expect(err).NotTo(HaveOccurred())
  2991. for i := range infoExt {
  2992. infoExt[i].Idle = 0
  2993. }
  2994. Expect(infoExt).To(Equal([]redis.XPendingExt{
  2995. {Id: "1-0", Consumer: "consumer", Idle: 0, RetryCount: 1},
  2996. {Id: "2-0", Consumer: "consumer", Idle: 0, RetryCount: 1},
  2997. {Id: "3-0", Consumer: "consumer", Idle: 0, RetryCount: 1},
  2998. }))
  2999. n, err := client.XGroupDelConsumer("stream", "group", "consumer").Result()
  3000. Expect(err).NotTo(HaveOccurred())
  3001. Expect(n).To(Equal(int64(3)))
  3002. })
  3003. It("should XClaim", func() {
  3004. msgs, err := client.XClaim(&redis.XClaimArgs{
  3005. Stream: "stream",
  3006. Group: "group",
  3007. Consumer: "consumer",
  3008. Messages: []string{"1-0", "2-0", "3-0"},
  3009. }).Result()
  3010. Expect(err).NotTo(HaveOccurred())
  3011. Expect(msgs).To(Equal([]redis.XMessage{{
  3012. ID: "1-0",
  3013. Values: map[string]interface{}{"uno": "un"},
  3014. }, {
  3015. ID: "2-0",
  3016. Values: map[string]interface{}{"dos": "deux"},
  3017. }, {
  3018. ID: "3-0",
  3019. Values: map[string]interface{}{"tres": "troix"},
  3020. }}))
  3021. ids, err := client.XClaimJustID(&redis.XClaimArgs{
  3022. Stream: "stream",
  3023. Group: "group",
  3024. Consumer: "consumer",
  3025. Messages: []string{"1-0", "2-0", "3-0"},
  3026. }).Result()
  3027. Expect(err).NotTo(HaveOccurred())
  3028. Expect(ids).To(Equal([]string{"1-0", "2-0", "3-0"}))
  3029. })
  3030. It("should XAck", func() {
  3031. n, err := client.XAck("stream", "group", "1-0", "2-0", "4-0").Result()
  3032. Expect(err).NotTo(HaveOccurred())
  3033. Expect(n).To(Equal(int64(2)))
  3034. })
  3035. })
  3036. })
  3037. Describe("Geo add and radius search", func() {
  3038. BeforeEach(func() {
  3039. n, err := client.GeoAdd(
  3040. "Sicily",
  3041. &redis.GeoLocation{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"},
  3042. &redis.GeoLocation{Longitude: 15.087269, Latitude: 37.502669, Name: "Catania"},
  3043. ).Result()
  3044. Expect(err).NotTo(HaveOccurred())
  3045. Expect(n).To(Equal(int64(2)))
  3046. })
  3047. It("should not add same geo location", func() {
  3048. geoAdd := client.GeoAdd(
  3049. "Sicily",
  3050. &redis.GeoLocation{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"},
  3051. )
  3052. Expect(geoAdd.Err()).NotTo(HaveOccurred())
  3053. Expect(geoAdd.Val()).To(Equal(int64(0)))
  3054. })
  3055. It("should search geo radius", func() {
  3056. res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{
  3057. Radius: 200,
  3058. }).Result()
  3059. Expect(err).NotTo(HaveOccurred())
  3060. Expect(res).To(HaveLen(2))
  3061. Expect(res[0].Name).To(Equal("Palermo"))
  3062. Expect(res[1].Name).To(Equal("Catania"))
  3063. })
  3064. It("should search geo radius with options", func() {
  3065. res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{
  3066. Radius: 200,
  3067. Unit: "km",
  3068. WithGeoHash: true,
  3069. WithCoord: true,
  3070. WithDist: true,
  3071. Count: 2,
  3072. Sort: "ASC",
  3073. }).Result()
  3074. Expect(err).NotTo(HaveOccurred())
  3075. Expect(res).To(HaveLen(2))
  3076. Expect(res[1].Name).To(Equal("Palermo"))
  3077. Expect(res[1].Dist).To(Equal(190.4424))
  3078. Expect(res[1].GeoHash).To(Equal(int64(3479099956230698)))
  3079. Expect(res[1].Longitude).To(Equal(13.361389338970184))
  3080. Expect(res[1].Latitude).To(Equal(38.115556395496299))
  3081. Expect(res[0].Name).To(Equal("Catania"))
  3082. Expect(res[0].Dist).To(Equal(56.4413))
  3083. Expect(res[0].GeoHash).To(Equal(int64(3479447370796909)))
  3084. Expect(res[0].Longitude).To(Equal(15.087267458438873))
  3085. Expect(res[0].Latitude).To(Equal(37.50266842333162))
  3086. })
  3087. It("should search geo radius with WithDist=false", func() {
  3088. res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{
  3089. Radius: 200,
  3090. Unit: "km",
  3091. WithGeoHash: true,
  3092. WithCoord: true,
  3093. Count: 2,
  3094. Sort: "ASC",
  3095. }).Result()
  3096. Expect(err).NotTo(HaveOccurred())
  3097. Expect(res).To(HaveLen(2))
  3098. Expect(res[1].Name).To(Equal("Palermo"))
  3099. Expect(res[1].Dist).To(Equal(float64(0)))
  3100. Expect(res[1].GeoHash).To(Equal(int64(3479099956230698)))
  3101. Expect(res[1].Longitude).To(Equal(13.361389338970184))
  3102. Expect(res[1].Latitude).To(Equal(38.115556395496299))
  3103. Expect(res[0].Name).To(Equal("Catania"))
  3104. Expect(res[0].Dist).To(Equal(float64(0)))
  3105. Expect(res[0].GeoHash).To(Equal(int64(3479447370796909)))
  3106. Expect(res[0].Longitude).To(Equal(15.087267458438873))
  3107. Expect(res[0].Latitude).To(Equal(37.50266842333162))
  3108. })
  3109. It("should search geo radius by member with options", func() {
  3110. res, err := client.GeoRadiusByMember("Sicily", "Catania", &redis.GeoRadiusQuery{
  3111. Radius: 200,
  3112. Unit: "km",
  3113. WithGeoHash: true,
  3114. WithCoord: true,
  3115. WithDist: true,
  3116. Count: 2,
  3117. Sort: "ASC",
  3118. }).Result()
  3119. Expect(err).NotTo(HaveOccurred())
  3120. Expect(res).To(HaveLen(2))
  3121. Expect(res[0].Name).To(Equal("Catania"))
  3122. Expect(res[0].Dist).To(Equal(0.0))
  3123. Expect(res[0].GeoHash).To(Equal(int64(3479447370796909)))
  3124. Expect(res[0].Longitude).To(Equal(15.087267458438873))
  3125. Expect(res[0].Latitude).To(Equal(37.50266842333162))
  3126. Expect(res[1].Name).To(Equal("Palermo"))
  3127. Expect(res[1].Dist).To(Equal(166.2742))
  3128. Expect(res[1].GeoHash).To(Equal(int64(3479099956230698)))
  3129. Expect(res[1].Longitude).To(Equal(13.361389338970184))
  3130. Expect(res[1].Latitude).To(Equal(38.115556395496299))
  3131. })
  3132. It("should search geo radius with no results", func() {
  3133. res, err := client.GeoRadius("Sicily", 99, 37, &redis.GeoRadiusQuery{
  3134. Radius: 200,
  3135. Unit: "km",
  3136. WithGeoHash: true,
  3137. WithCoord: true,
  3138. WithDist: true,
  3139. }).Result()
  3140. Expect(err).NotTo(HaveOccurred())
  3141. Expect(res).To(HaveLen(0))
  3142. })
  3143. It("should get geo distance with unit options", func() {
  3144. // From Redis CLI, note the difference in rounding in m vs
  3145. // km on Redis itself.
  3146. //
  3147. // GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
  3148. // GEODIST Sicily Palermo Catania m
  3149. // "166274.15156960033"
  3150. // GEODIST Sicily Palermo Catania km
  3151. // "166.27415156960032"
  3152. dist, err := client.GeoDist("Sicily", "Palermo", "Catania", "km").Result()
  3153. Expect(err).NotTo(HaveOccurred())
  3154. Expect(dist).To(BeNumerically("~", 166.27, 0.01))
  3155. dist, err = client.GeoDist("Sicily", "Palermo", "Catania", "m").Result()
  3156. Expect(err).NotTo(HaveOccurred())
  3157. Expect(dist).To(BeNumerically("~", 166274.15, 0.01))
  3158. })
  3159. It("should get geo hash in string representation", func() {
  3160. hashes, err := client.GeoHash("Sicily", "Palermo", "Catania").Result()
  3161. Expect(err).NotTo(HaveOccurred())
  3162. Expect(hashes).To(ConsistOf([]string{"sqc8b49rny0", "sqdtr74hyu0"}))
  3163. })
  3164. It("should return geo position", func() {
  3165. pos, err := client.GeoPos("Sicily", "Palermo", "Catania", "NonExisting").Result()
  3166. Expect(err).NotTo(HaveOccurred())
  3167. Expect(pos).To(ConsistOf([]*redis.GeoPos{
  3168. {
  3169. Longitude: 13.361389338970184,
  3170. Latitude: 38.1155563954963,
  3171. },
  3172. {
  3173. Longitude: 15.087267458438873,
  3174. Latitude: 37.50266842333162,
  3175. },
  3176. nil,
  3177. }))
  3178. })
  3179. })
  3180. Describe("marshaling/unmarshaling", func() {
  3181. type convTest struct {
  3182. value interface{}
  3183. wanted string
  3184. dest interface{}
  3185. }
  3186. convTests := []convTest{
  3187. {nil, "", nil},
  3188. {"hello", "hello", new(string)},
  3189. {[]byte("hello"), "hello", new([]byte)},
  3190. {int(1), "1", new(int)},
  3191. {int8(1), "1", new(int8)},
  3192. {int16(1), "1", new(int16)},
  3193. {int32(1), "1", new(int32)},
  3194. {int64(1), "1", new(int64)},
  3195. {uint(1), "1", new(uint)},
  3196. {uint8(1), "1", new(uint8)},
  3197. {uint16(1), "1", new(uint16)},
  3198. {uint32(1), "1", new(uint32)},
  3199. {uint64(1), "1", new(uint64)},
  3200. {float32(1.0), "1", new(float32)},
  3201. {float64(1.0), "1", new(float64)},
  3202. {true, "1", new(bool)},
  3203. {false, "0", new(bool)},
  3204. }
  3205. It("should convert to string", func() {
  3206. for _, test := range convTests {
  3207. err := client.Set("key", test.value, 0).Err()
  3208. Expect(err).NotTo(HaveOccurred())
  3209. s, err := client.Get("key").Result()
  3210. Expect(err).NotTo(HaveOccurred())
  3211. Expect(s).To(Equal(test.wanted))
  3212. if test.dest == nil {
  3213. continue
  3214. }
  3215. err = client.Get("key").Scan(test.dest)
  3216. Expect(err).NotTo(HaveOccurred())
  3217. Expect(deref(test.dest)).To(Equal(test.value))
  3218. }
  3219. })
  3220. })
  3221. Describe("json marshaling/unmarshaling", func() {
  3222. BeforeEach(func() {
  3223. value := &numberStruct{Number: 42}
  3224. err := client.Set("key", value, 0).Err()
  3225. Expect(err).NotTo(HaveOccurred())
  3226. })
  3227. It("should marshal custom values using json", func() {
  3228. s, err := client.Get("key").Result()
  3229. Expect(err).NotTo(HaveOccurred())
  3230. Expect(s).To(Equal(`{"Number":42}`))
  3231. })
  3232. It("should scan custom values using json", func() {
  3233. value := &numberStruct{}
  3234. err := client.Get("key").Scan(value)
  3235. Expect(err).NotTo(HaveOccurred())
  3236. Expect(value.Number).To(Equal(42))
  3237. })
  3238. })
  3239. Describe("Eval", func() {
  3240. It("returns keys and values", func() {
  3241. vals, err := client.Eval(
  3242. "return {KEYS[1],ARGV[1]}",
  3243. []string{"key"},
  3244. "hello",
  3245. ).Result()
  3246. Expect(err).NotTo(HaveOccurred())
  3247. Expect(vals).To(Equal([]interface{}{"key", "hello"}))
  3248. })
  3249. It("returns all values after an error", func() {
  3250. vals, err := client.Eval(
  3251. `return {12, {err="error"}, "abc"}`,
  3252. nil,
  3253. ).Result()
  3254. Expect(err).NotTo(HaveOccurred())
  3255. Expect(vals).To(Equal([]interface{}{int64(12), proto.RedisError("error"), "abc"}))
  3256. })
  3257. })
  3258. })
  3259. type numberStruct struct {
  3260. Number int
  3261. }
  3262. func (s *numberStruct) MarshalBinary() ([]byte, error) {
  3263. return json.Marshal(s)
  3264. }
  3265. func (s *numberStruct) UnmarshalBinary(b []byte) error {
  3266. return json.Unmarshal(b, s)
  3267. }
  3268. func deref(viface interface{}) interface{} {
  3269. v := reflect.ValueOf(viface)
  3270. for v.Kind() == reflect.Ptr {
  3271. v = v.Elem()
  3272. }
  3273. return v.Interface()
  3274. }