12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941 |
- package redis_test
- import (
- "encoding/json"
- "fmt"
- "reflect"
- "time"
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- "github.com/go-redis/redis"
- "github.com/go-redis/redis/internal/proto"
- )
- var _ = Describe("Commands", func() {
- var client *redis.Client
- BeforeEach(func() {
- client = redis.NewClient(redisOptions())
- Expect(client.FlushDB().Err()).NotTo(HaveOccurred())
- })
- AfterEach(func() {
- Expect(client.Close()).NotTo(HaveOccurred())
- })
- Describe("server", func() {
- It("should Auth", func() {
- cmds, err := client.Pipelined(func(pipe redis.Pipeliner) error {
- pipe.Auth("password")
- pipe.Auth("")
- return nil
- })
- Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set"))
- Expect(cmds[0].Err()).To(MatchError("ERR Client sent AUTH, but no password is set"))
- Expect(cmds[1].Err()).To(MatchError("ERR Client sent AUTH, but no password is set"))
- stats := client.PoolStats()
- Expect(stats.Hits).To(Equal(uint32(1)))
- Expect(stats.Misses).To(Equal(uint32(1)))
- Expect(stats.Timeouts).To(Equal(uint32(0)))
- Expect(stats.TotalConns).To(Equal(uint32(1)))
- Expect(stats.IdleConns).To(Equal(uint32(1)))
- })
- It("should Echo", func() {
- pipe := client.Pipeline()
- echo := pipe.Echo("hello")
- _, err := pipe.Exec()
- Expect(err).NotTo(HaveOccurred())
- Expect(echo.Err()).NotTo(HaveOccurred())
- Expect(echo.Val()).To(Equal("hello"))
- })
- It("should Ping", func() {
- ping := client.Ping()
- Expect(ping.Err()).NotTo(HaveOccurred())
- Expect(ping.Val()).To(Equal("PONG"))
- })
- It("should Wait", func() {
- const wait = 3 * time.Second
- // assume testing on single redis instance
- start := time.Now()
- val, err := client.Wait(1, wait).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal(int64(0)))
- Expect(time.Now()).To(BeTemporally("~", start.Add(wait), 3*time.Second))
- })
- It("should Select", func() {
- pipe := client.Pipeline()
- sel := pipe.Select(1)
- _, err := pipe.Exec()
- Expect(err).NotTo(HaveOccurred())
- Expect(sel.Err()).NotTo(HaveOccurred())
- Expect(sel.Val()).To(Equal("OK"))
- })
- It("should SwapDB", func() {
- pipe := client.Pipeline()
- sel := pipe.SwapDB(1, 2)
- _, err := pipe.Exec()
- Expect(err).NotTo(HaveOccurred())
- Expect(sel.Err()).NotTo(HaveOccurred())
- Expect(sel.Val()).To(Equal("OK"))
- })
- It("should BgRewriteAOF", func() {
- Skip("flaky test")
- val, err := client.BgRewriteAOF().Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(ContainSubstring("Background append only file rewriting"))
- })
- It("should BgSave", func() {
- Skip("flaky test")
- // workaround for "ERR Can't BGSAVE while AOF log rewriting is in progress"
- Eventually(func() string {
- return client.BgSave().Val()
- }, "30s").Should(Equal("Background saving started"))
- })
- It("should ClientKill", func() {
- r := client.ClientKill("1.1.1.1:1111")
- Expect(r.Err()).To(MatchError("ERR No such client"))
- Expect(r.Val()).To(Equal(""))
- })
- It("should ClientKillByFilter", func() {
- r := client.ClientKillByFilter("TYPE", "test")
- Expect(r.Err()).To(MatchError("ERR Unknown client type 'test'"))
- Expect(r.Val()).To(Equal(int64(0)))
- })
- It("should ClientID", func() {
- err := client.ClientID().Err()
- Expect(err).NotTo(HaveOccurred())
- Expect(client.ClientID().Val()).To(BeNumerically(">=", 0))
- })
- It("should ClientUnblock", func() {
- id := client.ClientID().Val()
- r, err := client.ClientUnblock(id).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(r).To(Equal(int64(0)))
- })
- It("should ClientUnblockWithError", func() {
- id := client.ClientID().Val()
- r, err := client.ClientUnblockWithError(id).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(r).To(Equal(int64(0)))
- })
- It("should ClientPause", func() {
- err := client.ClientPause(time.Second).Err()
- Expect(err).NotTo(HaveOccurred())
- start := time.Now()
- err = client.Ping().Err()
- Expect(err).NotTo(HaveOccurred())
- Expect(time.Now()).To(BeTemporally("~", start.Add(time.Second), 800*time.Millisecond))
- })
- It("should ClientSetName and ClientGetName", func() {
- pipe := client.Pipeline()
- set := pipe.ClientSetName("theclientname")
- get := pipe.ClientGetName()
- _, err := pipe.Exec()
- Expect(err).NotTo(HaveOccurred())
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(BeTrue())
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("theclientname"))
- })
- It("should ConfigGet", func() {
- val, err := client.ConfigGet("*").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).NotTo(BeEmpty())
- })
- It("should ConfigResetStat", func() {
- r := client.ConfigResetStat()
- Expect(r.Err()).NotTo(HaveOccurred())
- Expect(r.Val()).To(Equal("OK"))
- })
- It("should ConfigSet", func() {
- configGet := client.ConfigGet("maxmemory")
- Expect(configGet.Err()).NotTo(HaveOccurred())
- Expect(configGet.Val()).To(HaveLen(2))
- Expect(configGet.Val()[0]).To(Equal("maxmemory"))
- configSet := client.ConfigSet("maxmemory", configGet.Val()[1].(string))
- Expect(configSet.Err()).NotTo(HaveOccurred())
- Expect(configSet.Val()).To(Equal("OK"))
- })
- It("should ConfigRewrite", func() {
- configRewrite := client.ConfigRewrite()
- Expect(configRewrite.Err()).NotTo(HaveOccurred())
- Expect(configRewrite.Val()).To(Equal("OK"))
- })
- It("should DBSize", func() {
- size, err := client.DBSize().Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(0)))
- })
- It("should Info", func() {
- info := client.Info()
- Expect(info.Err()).NotTo(HaveOccurred())
- Expect(info.Val()).NotTo(Equal(""))
- })
- It("should Info cpu", func() {
- info := client.Info("cpu")
- Expect(info.Err()).NotTo(HaveOccurred())
- Expect(info.Val()).NotTo(Equal(""))
- Expect(info.Val()).To(ContainSubstring(`used_cpu_sys`))
- })
- It("should LastSave", func() {
- lastSave := client.LastSave()
- Expect(lastSave.Err()).NotTo(HaveOccurred())
- Expect(lastSave.Val()).NotTo(Equal(0))
- })
- It("should Save", func() {
- // workaround for "ERR Background save already in progress"
- Eventually(func() string {
- return client.Save().Val()
- }, "10s").Should(Equal("OK"))
- })
- It("should SlaveOf", func() {
- slaveOf := client.SlaveOf("localhost", "8888")
- Expect(slaveOf.Err()).NotTo(HaveOccurred())
- Expect(slaveOf.Val()).To(Equal("OK"))
- slaveOf = client.SlaveOf("NO", "ONE")
- Expect(slaveOf.Err()).NotTo(HaveOccurred())
- Expect(slaveOf.Val()).To(Equal("OK"))
- })
- It("should Time", func() {
- tm, err := client.Time().Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(tm).To(BeTemporally("~", time.Now(), 3*time.Second))
- })
- It("should Command", func() {
- cmds, err := client.Command().Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(len(cmds)).To(BeNumerically("~", 200, 20))
- cmd := cmds["mget"]
- Expect(cmd.Name).To(Equal("mget"))
- Expect(cmd.Arity).To(Equal(int8(-2)))
- Expect(cmd.Flags).To(ContainElement("readonly"))
- Expect(cmd.FirstKeyPos).To(Equal(int8(1)))
- Expect(cmd.LastKeyPos).To(Equal(int8(-1)))
- Expect(cmd.StepCount).To(Equal(int8(1)))
- cmd = cmds["ping"]
- Expect(cmd.Name).To(Equal("ping"))
- Expect(cmd.Arity).To(Equal(int8(-1)))
- Expect(cmd.Flags).To(ContainElement("stale"))
- Expect(cmd.Flags).To(ContainElement("fast"))
- Expect(cmd.FirstKeyPos).To(Equal(int8(0)))
- Expect(cmd.LastKeyPos).To(Equal(int8(0)))
- Expect(cmd.StepCount).To(Equal(int8(0)))
- })
- })
- Describe("debugging", func() {
- It("should DebugObject", func() {
- err := client.DebugObject("foo").Err()
- Expect(err).To(MatchError("ERR no such key"))
- err = client.Set("foo", "bar", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- s, err := client.DebugObject("foo").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(s).To(ContainSubstring("serializedlength:4"))
- })
- It("should MemoryUsage", func() {
- err := client.MemoryUsage("foo").Err()
- Expect(err).To(Equal(redis.Nil))
- err = client.Set("foo", "bar", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- n, err := client.MemoryUsage("foo").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(50)))
- n, err = client.MemoryUsage("foo", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(50)))
- })
- })
- Describe("keys", func() {
- It("should Del", func() {
- err := client.Set("key1", "Hello", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.Set("key2", "World", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- n, err := client.Del("key1", "key2", "key3").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(2)))
- })
- It("should Unlink", func() {
- err := client.Set("key1", "Hello", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.Set("key2", "World", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- n, err := client.Unlink("key1", "key2", "key3").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(2)))
- })
- It("should Dump", func() {
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- dump := client.Dump("key")
- Expect(dump.Err()).NotTo(HaveOccurred())
- Expect(dump.Val()).NotTo(BeEmpty())
- })
- It("should Exists", func() {
- set := client.Set("key1", "Hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- n, err := client.Exists("key1").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(1)))
- n, err = client.Exists("key2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(0)))
- n, err = client.Exists("key1", "key2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(1)))
- n, err = client.Exists("key1", "key1").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(2)))
- })
- It("should Expire", func() {
- set := client.Set("key", "Hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- expire := client.Expire("key", 10*time.Second)
- Expect(expire.Err()).NotTo(HaveOccurred())
- Expect(expire.Val()).To(Equal(true))
- ttl := client.TTL("key")
- Expect(ttl.Err()).NotTo(HaveOccurred())
- Expect(ttl.Val()).To(Equal(10 * time.Second))
- set = client.Set("key", "Hello World", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- ttl = client.TTL("key")
- Expect(ttl.Err()).NotTo(HaveOccurred())
- Expect(ttl.Val() < 0).To(Equal(true))
- })
- It("should ExpireAt", func() {
- set := client.Set("key", "Hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- n, err := client.Exists("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(1)))
- expireAt := client.ExpireAt("key", time.Now().Add(-time.Hour))
- Expect(expireAt.Err()).NotTo(HaveOccurred())
- Expect(expireAt.Val()).To(Equal(true))
- n, err = client.Exists("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(0)))
- })
- It("should Keys", func() {
- mset := client.MSet("one", "1", "two", "2", "three", "3", "four", "4")
- Expect(mset.Err()).NotTo(HaveOccurred())
- Expect(mset.Val()).To(Equal("OK"))
- keys := client.Keys("*o*")
- Expect(keys.Err()).NotTo(HaveOccurred())
- Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "two"}))
- keys = client.Keys("t??")
- Expect(keys.Err()).NotTo(HaveOccurred())
- Expect(keys.Val()).To(Equal([]string{"two"}))
- keys = client.Keys("*")
- Expect(keys.Err()).NotTo(HaveOccurred())
- Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "three", "two"}))
- })
- It("should Migrate", func() {
- migrate := client.Migrate("localhost", redisSecondaryPort, "key", 0, 0)
- Expect(migrate.Err()).NotTo(HaveOccurred())
- Expect(migrate.Val()).To(Equal("NOKEY"))
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- migrate = client.Migrate("localhost", redisSecondaryPort, "key", 0, 0)
- Expect(migrate.Err()).To(MatchError("IOERR error or timeout writing to target instance"))
- Expect(migrate.Val()).To(Equal(""))
- })
- It("should Move", func() {
- move := client.Move("key", 2)
- Expect(move.Err()).NotTo(HaveOccurred())
- Expect(move.Val()).To(Equal(false))
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- move = client.Move("key", 2)
- Expect(move.Err()).NotTo(HaveOccurred())
- Expect(move.Val()).To(Equal(true))
- get := client.Get("key")
- Expect(get.Err()).To(Equal(redis.Nil))
- Expect(get.Val()).To(Equal(""))
- pipe := client.Pipeline()
- pipe.Select(2)
- get = pipe.Get("key")
- pipe.FlushDB()
- _, err := pipe.Exec()
- Expect(err).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("hello"))
- })
- It("should Object", func() {
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- refCount := client.ObjectRefCount("key")
- Expect(refCount.Err()).NotTo(HaveOccurred())
- Expect(refCount.Val()).To(Equal(int64(1)))
- err := client.ObjectEncoding("key").Err()
- Expect(err).NotTo(HaveOccurred())
- idleTime := client.ObjectIdleTime("key")
- Expect(idleTime.Err()).NotTo(HaveOccurred())
- Expect(idleTime.Val()).To(Equal(time.Duration(0)))
- })
- It("should Persist", func() {
- set := client.Set("key", "Hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- expire := client.Expire("key", 10*time.Second)
- Expect(expire.Err()).NotTo(HaveOccurred())
- Expect(expire.Val()).To(Equal(true))
- ttl := client.TTL("key")
- Expect(ttl.Err()).NotTo(HaveOccurred())
- Expect(ttl.Val()).To(Equal(10 * time.Second))
- persist := client.Persist("key")
- Expect(persist.Err()).NotTo(HaveOccurred())
- Expect(persist.Val()).To(Equal(true))
- ttl = client.TTL("key")
- Expect(ttl.Err()).NotTo(HaveOccurred())
- Expect(ttl.Val() < 0).To(Equal(true))
- })
- It("should PExpire", func() {
- set := client.Set("key", "Hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- expiration := 900 * time.Millisecond
- pexpire := client.PExpire("key", expiration)
- Expect(pexpire.Err()).NotTo(HaveOccurred())
- Expect(pexpire.Val()).To(Equal(true))
- ttl := client.TTL("key")
- Expect(ttl.Err()).NotTo(HaveOccurred())
- Expect(ttl.Val()).To(Equal(time.Second))
- pttl := client.PTTL("key")
- Expect(pttl.Err()).NotTo(HaveOccurred())
- Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
- })
- It("should PExpireAt", func() {
- set := client.Set("key", "Hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- expiration := 900 * time.Millisecond
- pexpireat := client.PExpireAt("key", time.Now().Add(expiration))
- Expect(pexpireat.Err()).NotTo(HaveOccurred())
- Expect(pexpireat.Val()).To(Equal(true))
- ttl := client.TTL("key")
- Expect(ttl.Err()).NotTo(HaveOccurred())
- Expect(ttl.Val()).To(Equal(time.Second))
- pttl := client.PTTL("key")
- Expect(pttl.Err()).NotTo(HaveOccurred())
- Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
- })
- It("should PTTL", func() {
- set := client.Set("key", "Hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- expiration := time.Second
- expire := client.Expire("key", expiration)
- Expect(expire.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- pttl := client.PTTL("key")
- Expect(pttl.Err()).NotTo(HaveOccurred())
- Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
- })
- It("should RandomKey", func() {
- randomKey := client.RandomKey()
- Expect(randomKey.Err()).To(Equal(redis.Nil))
- Expect(randomKey.Val()).To(Equal(""))
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- randomKey = client.RandomKey()
- Expect(randomKey.Err()).NotTo(HaveOccurred())
- Expect(randomKey.Val()).To(Equal("key"))
- })
- It("should Rename", func() {
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- status := client.Rename("key", "key1")
- Expect(status.Err()).NotTo(HaveOccurred())
- Expect(status.Val()).To(Equal("OK"))
- get := client.Get("key1")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("hello"))
- })
- It("should RenameNX", func() {
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- renameNX := client.RenameNX("key", "key1")
- Expect(renameNX.Err()).NotTo(HaveOccurred())
- Expect(renameNX.Val()).To(Equal(true))
- get := client.Get("key1")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("hello"))
- })
- It("should Restore", func() {
- err := client.Set("key", "hello", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- dump := client.Dump("key")
- Expect(dump.Err()).NotTo(HaveOccurred())
- err = client.Del("key").Err()
- Expect(err).NotTo(HaveOccurred())
- restore, err := client.Restore("key", 0, dump.Val()).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(restore).To(Equal("OK"))
- type_, err := client.Type("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(type_).To(Equal("string"))
- val, err := client.Get("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal("hello"))
- })
- It("should RestoreReplace", func() {
- err := client.Set("key", "hello", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- dump := client.Dump("key")
- Expect(dump.Err()).NotTo(HaveOccurred())
- restore, err := client.RestoreReplace("key", 0, dump.Val()).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(restore).To(Equal("OK"))
- type_, err := client.Type("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(type_).To(Equal("string"))
- val, err := client.Get("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal("hello"))
- })
- It("should Sort", func() {
- size, err := client.LPush("list", "1").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(1)))
- size, err = client.LPush("list", "3").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(2)))
- size, err = client.LPush("list", "2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(3)))
- els, err := client.Sort("list", &redis.Sort{
- Offset: 0,
- Count: 2,
- Order: "ASC",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(els).To(Equal([]string{"1", "2"}))
- })
- It("should Sort and Get", func() {
- size, err := client.LPush("list", "1").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(1)))
- size, err = client.LPush("list", "3").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(2)))
- size, err = client.LPush("list", "2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(3)))
- err = client.Set("object_2", "value2", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- {
- els, err := client.Sort("list", &redis.Sort{
- Get: []string{"object_*"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(els).To(Equal([]string{"", "value2", ""}))
- }
- {
- els, err := client.SortInterfaces("list", &redis.Sort{
- Get: []string{"object_*"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(els).To(Equal([]interface{}{nil, "value2", nil}))
- }
- })
- It("should Sort and Store", func() {
- size, err := client.LPush("list", "1").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(1)))
- size, err = client.LPush("list", "3").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(2)))
- size, err = client.LPush("list", "2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(size).To(Equal(int64(3)))
- n, err := client.SortStore("list", "list2", &redis.Sort{
- Offset: 0,
- Count: 2,
- Order: "ASC",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(2)))
- els, err := client.LRange("list2", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(els).To(Equal([]string{"1", "2"}))
- })
- It("should Touch", func() {
- set1 := client.Set("touch1", "hello", 0)
- Expect(set1.Err()).NotTo(HaveOccurred())
- Expect(set1.Val()).To(Equal("OK"))
- set2 := client.Set("touch2", "hello", 0)
- Expect(set2.Err()).NotTo(HaveOccurred())
- Expect(set2.Val()).To(Equal("OK"))
- touch := client.Touch("touch1", "touch2", "touch3")
- Expect(touch.Err()).NotTo(HaveOccurred())
- Expect(touch.Val()).To(Equal(int64(2)))
- })
- It("should TTL", func() {
- ttl := client.TTL("key")
- Expect(ttl.Err()).NotTo(HaveOccurred())
- Expect(ttl.Val() < 0).To(Equal(true))
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- expire := client.Expire("key", 60*time.Second)
- Expect(expire.Err()).NotTo(HaveOccurred())
- Expect(expire.Val()).To(Equal(true))
- ttl = client.TTL("key")
- Expect(ttl.Err()).NotTo(HaveOccurred())
- Expect(ttl.Val()).To(Equal(60 * time.Second))
- })
- It("should Type", func() {
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- type_ := client.Type("key")
- Expect(type_.Err()).NotTo(HaveOccurred())
- Expect(type_.Val()).To(Equal("string"))
- })
- })
- Describe("scanning", func() {
- It("should Scan", func() {
- for i := 0; i < 1000; i++ {
- set := client.Set(fmt.Sprintf("key%d", i), "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- }
- keys, cursor, err := client.Scan(0, "", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(keys).NotTo(BeEmpty())
- Expect(cursor).NotTo(BeZero())
- })
- It("should SScan", func() {
- for i := 0; i < 1000; i++ {
- sadd := client.SAdd("myset", fmt.Sprintf("member%d", i))
- Expect(sadd.Err()).NotTo(HaveOccurred())
- }
- keys, cursor, err := client.SScan("myset", 0, "", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(keys).NotTo(BeEmpty())
- Expect(cursor).NotTo(BeZero())
- })
- It("should HScan", func() {
- for i := 0; i < 1000; i++ {
- sadd := client.HSet("myhash", fmt.Sprintf("key%d", i), "hello")
- Expect(sadd.Err()).NotTo(HaveOccurred())
- }
- keys, cursor, err := client.HScan("myhash", 0, "", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(keys).NotTo(BeEmpty())
- Expect(cursor).NotTo(BeZero())
- })
- It("should ZScan", func() {
- for i := 0; i < 1000; i++ {
- err := client.ZAdd("myset", redis.Z{
- Score: float64(i),
- Member: fmt.Sprintf("member%d", i),
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- }
- keys, cursor, err := client.ZScan("myset", 0, "", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(keys).NotTo(BeEmpty())
- Expect(cursor).NotTo(BeZero())
- })
- })
- Describe("strings", func() {
- It("should Append", func() {
- n, err := client.Exists("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(0)))
- append := client.Append("key", "Hello")
- Expect(append.Err()).NotTo(HaveOccurred())
- Expect(append.Val()).To(Equal(int64(5)))
- append = client.Append("key", " World")
- Expect(append.Err()).NotTo(HaveOccurred())
- Expect(append.Val()).To(Equal(int64(11)))
- get := client.Get("key")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("Hello World"))
- })
- It("should BitCount", func() {
- set := client.Set("key", "foobar", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- bitCount := client.BitCount("key", nil)
- Expect(bitCount.Err()).NotTo(HaveOccurred())
- Expect(bitCount.Val()).To(Equal(int64(26)))
- bitCount = client.BitCount("key", &redis.BitCount{
- Start: 0,
- End: 0,
- })
- Expect(bitCount.Err()).NotTo(HaveOccurred())
- Expect(bitCount.Val()).To(Equal(int64(4)))
- bitCount = client.BitCount("key", &redis.BitCount{
- Start: 1,
- End: 1,
- })
- Expect(bitCount.Err()).NotTo(HaveOccurred())
- Expect(bitCount.Val()).To(Equal(int64(6)))
- })
- It("should BitOpAnd", func() {
- set := client.Set("key1", "1", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- set = client.Set("key2", "0", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- bitOpAnd := client.BitOpAnd("dest", "key1", "key2")
- Expect(bitOpAnd.Err()).NotTo(HaveOccurred())
- Expect(bitOpAnd.Val()).To(Equal(int64(1)))
- get := client.Get("dest")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("0"))
- })
- It("should BitOpOr", func() {
- set := client.Set("key1", "1", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- set = client.Set("key2", "0", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- bitOpOr := client.BitOpOr("dest", "key1", "key2")
- Expect(bitOpOr.Err()).NotTo(HaveOccurred())
- Expect(bitOpOr.Val()).To(Equal(int64(1)))
- get := client.Get("dest")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("1"))
- })
- It("should BitOpXor", func() {
- set := client.Set("key1", "\xff", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- set = client.Set("key2", "\x0f", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- bitOpXor := client.BitOpXor("dest", "key1", "key2")
- Expect(bitOpXor.Err()).NotTo(HaveOccurred())
- Expect(bitOpXor.Val()).To(Equal(int64(1)))
- get := client.Get("dest")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("\xf0"))
- })
- It("should BitOpNot", func() {
- set := client.Set("key1", "\x00", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- bitOpNot := client.BitOpNot("dest", "key1")
- Expect(bitOpNot.Err()).NotTo(HaveOccurred())
- Expect(bitOpNot.Val()).To(Equal(int64(1)))
- get := client.Get("dest")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("\xff"))
- })
- It("should BitPos", func() {
- err := client.Set("mykey", "\xff\xf0\x00", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- pos, err := client.BitPos("mykey", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(Equal(int64(12)))
- pos, err = client.BitPos("mykey", 1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(Equal(int64(0)))
- pos, err = client.BitPos("mykey", 0, 2).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(Equal(int64(16)))
- pos, err = client.BitPos("mykey", 1, 2).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(Equal(int64(-1)))
- pos, err = client.BitPos("mykey", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(Equal(int64(16)))
- pos, err = client.BitPos("mykey", 1, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(Equal(int64(-1)))
- pos, err = client.BitPos("mykey", 0, 2, 1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(Equal(int64(-1)))
- pos, err = client.BitPos("mykey", 0, 0, -3).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(Equal(int64(-1)))
- pos, err = client.BitPos("mykey", 0, 0, 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(Equal(int64(-1)))
- })
- It("should Decr", func() {
- set := client.Set("key", "10", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- decr := client.Decr("key")
- Expect(decr.Err()).NotTo(HaveOccurred())
- Expect(decr.Val()).To(Equal(int64(9)))
- set = client.Set("key", "234293482390480948029348230948", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- decr = client.Decr("key")
- Expect(decr.Err()).To(MatchError("ERR value is not an integer or out of range"))
- Expect(decr.Val()).To(Equal(int64(0)))
- })
- It("should DecrBy", func() {
- set := client.Set("key", "10", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- decrBy := client.DecrBy("key", 5)
- Expect(decrBy.Err()).NotTo(HaveOccurred())
- Expect(decrBy.Val()).To(Equal(int64(5)))
- })
- It("should Get", func() {
- get := client.Get("_")
- Expect(get.Err()).To(Equal(redis.Nil))
- Expect(get.Val()).To(Equal(""))
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- get = client.Get("key")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("hello"))
- })
- It("should GetBit", func() {
- setBit := client.SetBit("key", 7, 1)
- Expect(setBit.Err()).NotTo(HaveOccurred())
- Expect(setBit.Val()).To(Equal(int64(0)))
- getBit := client.GetBit("key", 0)
- Expect(getBit.Err()).NotTo(HaveOccurred())
- Expect(getBit.Val()).To(Equal(int64(0)))
- getBit = client.GetBit("key", 7)
- Expect(getBit.Err()).NotTo(HaveOccurred())
- Expect(getBit.Val()).To(Equal(int64(1)))
- getBit = client.GetBit("key", 100)
- Expect(getBit.Err()).NotTo(HaveOccurred())
- Expect(getBit.Val()).To(Equal(int64(0)))
- })
- It("should GetRange", func() {
- set := client.Set("key", "This is a string", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- getRange := client.GetRange("key", 0, 3)
- Expect(getRange.Err()).NotTo(HaveOccurred())
- Expect(getRange.Val()).To(Equal("This"))
- getRange = client.GetRange("key", -3, -1)
- Expect(getRange.Err()).NotTo(HaveOccurred())
- Expect(getRange.Val()).To(Equal("ing"))
- getRange = client.GetRange("key", 0, -1)
- Expect(getRange.Err()).NotTo(HaveOccurred())
- Expect(getRange.Val()).To(Equal("This is a string"))
- getRange = client.GetRange("key", 10, 100)
- Expect(getRange.Err()).NotTo(HaveOccurred())
- Expect(getRange.Val()).To(Equal("string"))
- })
- It("should GetSet", func() {
- incr := client.Incr("key")
- Expect(incr.Err()).NotTo(HaveOccurred())
- Expect(incr.Val()).To(Equal(int64(1)))
- getSet := client.GetSet("key", "0")
- Expect(getSet.Err()).NotTo(HaveOccurred())
- Expect(getSet.Val()).To(Equal("1"))
- get := client.Get("key")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("0"))
- })
- It("should Incr", func() {
- set := client.Set("key", "10", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- incr := client.Incr("key")
- Expect(incr.Err()).NotTo(HaveOccurred())
- Expect(incr.Val()).To(Equal(int64(11)))
- get := client.Get("key")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("11"))
- })
- It("should IncrBy", func() {
- set := client.Set("key", "10", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- incrBy := client.IncrBy("key", 5)
- Expect(incrBy.Err()).NotTo(HaveOccurred())
- Expect(incrBy.Val()).To(Equal(int64(15)))
- })
- It("should IncrByFloat", func() {
- set := client.Set("key", "10.50", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- incrByFloat := client.IncrByFloat("key", 0.1)
- Expect(incrByFloat.Err()).NotTo(HaveOccurred())
- Expect(incrByFloat.Val()).To(Equal(10.6))
- set = client.Set("key", "5.0e3", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- incrByFloat = client.IncrByFloat("key", 2.0e2)
- Expect(incrByFloat.Err()).NotTo(HaveOccurred())
- Expect(incrByFloat.Val()).To(Equal(float64(5200)))
- })
- It("should IncrByFloatOverflow", func() {
- incrByFloat := client.IncrByFloat("key", 996945661)
- Expect(incrByFloat.Err()).NotTo(HaveOccurred())
- Expect(incrByFloat.Val()).To(Equal(float64(996945661)))
- })
- It("should MSetMGet", func() {
- mSet := client.MSet("key1", "hello1", "key2", "hello2")
- Expect(mSet.Err()).NotTo(HaveOccurred())
- Expect(mSet.Val()).To(Equal("OK"))
- mGet := client.MGet("key1", "key2", "_")
- Expect(mGet.Err()).NotTo(HaveOccurred())
- Expect(mGet.Val()).To(Equal([]interface{}{"hello1", "hello2", nil}))
- })
- It("should MSetNX", func() {
- mSetNX := client.MSetNX("key1", "hello1", "key2", "hello2")
- Expect(mSetNX.Err()).NotTo(HaveOccurred())
- Expect(mSetNX.Val()).To(Equal(true))
- mSetNX = client.MSetNX("key2", "hello1", "key3", "hello2")
- Expect(mSetNX.Err()).NotTo(HaveOccurred())
- Expect(mSetNX.Val()).To(Equal(false))
- })
- It("should Set with expiration", func() {
- err := client.Set("key", "hello", 100*time.Millisecond).Err()
- Expect(err).NotTo(HaveOccurred())
- val, err := client.Get("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal("hello"))
- Eventually(func() error {
- return client.Get("foo").Err()
- }, "1s", "100ms").Should(Equal(redis.Nil))
- })
- It("should SetGet", func() {
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- get := client.Get("key")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("hello"))
- })
- It("should SetNX", func() {
- setNX := client.SetNX("key", "hello", 0)
- Expect(setNX.Err()).NotTo(HaveOccurred())
- Expect(setNX.Val()).To(Equal(true))
- setNX = client.SetNX("key", "hello2", 0)
- Expect(setNX.Err()).NotTo(HaveOccurred())
- Expect(setNX.Val()).To(Equal(false))
- get := client.Get("key")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("hello"))
- })
- It("should SetNX with expiration", func() {
- isSet, err := client.SetNX("key", "hello", time.Second).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(isSet).To(Equal(true))
- isSet, err = client.SetNX("key", "hello2", time.Second).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(isSet).To(Equal(false))
- val, err := client.Get("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal("hello"))
- })
- It("should SetXX", func() {
- isSet, err := client.SetXX("key", "hello2", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(isSet).To(Equal(false))
- err = client.Set("key", "hello", 0).Err()
- Expect(err).NotTo(HaveOccurred())
- isSet, err = client.SetXX("key", "hello2", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(isSet).To(Equal(true))
- val, err := client.Get("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal("hello2"))
- })
- It("should SetXX with expiration", func() {
- isSet, err := client.SetXX("key", "hello2", time.Second).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(isSet).To(Equal(false))
- err = client.Set("key", "hello", time.Second).Err()
- Expect(err).NotTo(HaveOccurred())
- isSet, err = client.SetXX("key", "hello2", time.Second).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(isSet).To(Equal(true))
- val, err := client.Get("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal("hello2"))
- })
- It("should SetRange", func() {
- set := client.Set("key", "Hello World", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- range_ := client.SetRange("key", 6, "Redis")
- Expect(range_.Err()).NotTo(HaveOccurred())
- Expect(range_.Val()).To(Equal(int64(11)))
- get := client.Get("key")
- Expect(get.Err()).NotTo(HaveOccurred())
- Expect(get.Val()).To(Equal("Hello Redis"))
- })
- It("should StrLen", func() {
- set := client.Set("key", "hello", 0)
- Expect(set.Err()).NotTo(HaveOccurred())
- Expect(set.Val()).To(Equal("OK"))
- strLen := client.StrLen("key")
- Expect(strLen.Err()).NotTo(HaveOccurred())
- Expect(strLen.Val()).To(Equal(int64(5)))
- strLen = client.StrLen("_")
- Expect(strLen.Err()).NotTo(HaveOccurred())
- Expect(strLen.Val()).To(Equal(int64(0)))
- })
- })
- Describe("hashes", func() {
- It("should HDel", func() {
- hSet := client.HSet("hash", "key", "hello")
- Expect(hSet.Err()).NotTo(HaveOccurred())
- hDel := client.HDel("hash", "key")
- Expect(hDel.Err()).NotTo(HaveOccurred())
- Expect(hDel.Val()).To(Equal(int64(1)))
- hDel = client.HDel("hash", "key")
- Expect(hDel.Err()).NotTo(HaveOccurred())
- Expect(hDel.Val()).To(Equal(int64(0)))
- })
- It("should HExists", func() {
- hSet := client.HSet("hash", "key", "hello")
- Expect(hSet.Err()).NotTo(HaveOccurred())
- hExists := client.HExists("hash", "key")
- Expect(hExists.Err()).NotTo(HaveOccurred())
- Expect(hExists.Val()).To(Equal(true))
- hExists = client.HExists("hash", "key1")
- Expect(hExists.Err()).NotTo(HaveOccurred())
- Expect(hExists.Val()).To(Equal(false))
- })
- It("should HGet", func() {
- hSet := client.HSet("hash", "key", "hello")
- Expect(hSet.Err()).NotTo(HaveOccurred())
- hGet := client.HGet("hash", "key")
- Expect(hGet.Err()).NotTo(HaveOccurred())
- Expect(hGet.Val()).To(Equal("hello"))
- hGet = client.HGet("hash", "key1")
- Expect(hGet.Err()).To(Equal(redis.Nil))
- Expect(hGet.Val()).To(Equal(""))
- })
- It("should HGetAll", func() {
- err := client.HSet("hash", "key1", "hello1").Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.HSet("hash", "key2", "hello2").Err()
- Expect(err).NotTo(HaveOccurred())
- m, err := client.HGetAll("hash").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(m).To(Equal(map[string]string{"key1": "hello1", "key2": "hello2"}))
- })
- It("should HIncrBy", func() {
- hSet := client.HSet("hash", "key", "5")
- Expect(hSet.Err()).NotTo(HaveOccurred())
- hIncrBy := client.HIncrBy("hash", "key", 1)
- Expect(hIncrBy.Err()).NotTo(HaveOccurred())
- Expect(hIncrBy.Val()).To(Equal(int64(6)))
- hIncrBy = client.HIncrBy("hash", "key", -1)
- Expect(hIncrBy.Err()).NotTo(HaveOccurred())
- Expect(hIncrBy.Val()).To(Equal(int64(5)))
- hIncrBy = client.HIncrBy("hash", "key", -10)
- Expect(hIncrBy.Err()).NotTo(HaveOccurred())
- Expect(hIncrBy.Val()).To(Equal(int64(-5)))
- })
- It("should HIncrByFloat", func() {
- hSet := client.HSet("hash", "field", "10.50")
- Expect(hSet.Err()).NotTo(HaveOccurred())
- Expect(hSet.Val()).To(Equal(true))
- hIncrByFloat := client.HIncrByFloat("hash", "field", 0.1)
- Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
- Expect(hIncrByFloat.Val()).To(Equal(10.6))
- hSet = client.HSet("hash", "field", "5.0e3")
- Expect(hSet.Err()).NotTo(HaveOccurred())
- Expect(hSet.Val()).To(Equal(false))
- hIncrByFloat = client.HIncrByFloat("hash", "field", 2.0e2)
- Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
- Expect(hIncrByFloat.Val()).To(Equal(float64(5200)))
- })
- It("should HKeys", func() {
- hkeys := client.HKeys("hash")
- Expect(hkeys.Err()).NotTo(HaveOccurred())
- Expect(hkeys.Val()).To(Equal([]string{}))
- hset := client.HSet("hash", "key1", "hello1")
- Expect(hset.Err()).NotTo(HaveOccurred())
- hset = client.HSet("hash", "key2", "hello2")
- Expect(hset.Err()).NotTo(HaveOccurred())
- hkeys = client.HKeys("hash")
- Expect(hkeys.Err()).NotTo(HaveOccurred())
- Expect(hkeys.Val()).To(Equal([]string{"key1", "key2"}))
- })
- It("should HLen", func() {
- hSet := client.HSet("hash", "key1", "hello1")
- Expect(hSet.Err()).NotTo(HaveOccurred())
- hSet = client.HSet("hash", "key2", "hello2")
- Expect(hSet.Err()).NotTo(HaveOccurred())
- hLen := client.HLen("hash")
- Expect(hLen.Err()).NotTo(HaveOccurred())
- Expect(hLen.Val()).To(Equal(int64(2)))
- })
- It("should HMGet", func() {
- err := client.HSet("hash", "key1", "hello1").Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.HSet("hash", "key2", "hello2").Err()
- Expect(err).NotTo(HaveOccurred())
- vals, err := client.HMGet("hash", "key1", "key2", "_").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]interface{}{"hello1", "hello2", nil}))
- })
- It("should HMSet", func() {
- ok, err := client.HMSet("hash", map[string]interface{}{
- "key1": "hello1",
- "key2": "hello2",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(ok).To(Equal("OK"))
- v, err := client.HGet("hash", "key1").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(v).To(Equal("hello1"))
- v, err = client.HGet("hash", "key2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(v).To(Equal("hello2"))
- })
- It("should HSet", func() {
- hSet := client.HSet("hash", "key", "hello")
- Expect(hSet.Err()).NotTo(HaveOccurred())
- Expect(hSet.Val()).To(Equal(true))
- hGet := client.HGet("hash", "key")
- Expect(hGet.Err()).NotTo(HaveOccurred())
- Expect(hGet.Val()).To(Equal("hello"))
- })
- It("should HSetNX", func() {
- hSetNX := client.HSetNX("hash", "key", "hello")
- Expect(hSetNX.Err()).NotTo(HaveOccurred())
- Expect(hSetNX.Val()).To(Equal(true))
- hSetNX = client.HSetNX("hash", "key", "hello")
- Expect(hSetNX.Err()).NotTo(HaveOccurred())
- Expect(hSetNX.Val()).To(Equal(false))
- hGet := client.HGet("hash", "key")
- Expect(hGet.Err()).NotTo(HaveOccurred())
- Expect(hGet.Val()).To(Equal("hello"))
- })
- It("should HVals", func() {
- err := client.HSet("hash", "key1", "hello1").Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.HSet("hash", "key2", "hello2").Err()
- Expect(err).NotTo(HaveOccurred())
- v, err := client.HVals("hash").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(v).To(Equal([]string{"hello1", "hello2"}))
- var slice []string
- err = client.HVals("hash").ScanSlice(&slice)
- Expect(err).NotTo(HaveOccurred())
- Expect(slice).To(Equal([]string{"hello1", "hello2"}))
- })
- })
- Describe("hyperloglog", func() {
- It("should PFMerge", func() {
- pfAdd := client.PFAdd("hll1", "1", "2", "3", "4", "5")
- Expect(pfAdd.Err()).NotTo(HaveOccurred())
- pfCount := client.PFCount("hll1")
- Expect(pfCount.Err()).NotTo(HaveOccurred())
- Expect(pfCount.Val()).To(Equal(int64(5)))
- pfAdd = client.PFAdd("hll2", "a", "b", "c", "d", "e")
- Expect(pfAdd.Err()).NotTo(HaveOccurred())
- pfMerge := client.PFMerge("hllMerged", "hll1", "hll2")
- Expect(pfMerge.Err()).NotTo(HaveOccurred())
- pfCount = client.PFCount("hllMerged")
- Expect(pfCount.Err()).NotTo(HaveOccurred())
- Expect(pfCount.Val()).To(Equal(int64(10)))
- pfCount = client.PFCount("hll1", "hll2")
- Expect(pfCount.Err()).NotTo(HaveOccurred())
- Expect(pfCount.Val()).To(Equal(int64(10)))
- })
- })
- Describe("lists", func() {
- It("should BLPop", func() {
- rPush := client.RPush("list1", "a", "b", "c")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- bLPop := client.BLPop(0, "list1", "list2")
- Expect(bLPop.Err()).NotTo(HaveOccurred())
- Expect(bLPop.Val()).To(Equal([]string{"list1", "a"}))
- })
- It("should BLPopBlocks", func() {
- started := make(chan bool)
- done := make(chan bool)
- go func() {
- defer GinkgoRecover()
- started <- true
- bLPop := client.BLPop(0, "list")
- Expect(bLPop.Err()).NotTo(HaveOccurred())
- Expect(bLPop.Val()).To(Equal([]string{"list", "a"}))
- done <- true
- }()
- <-started
- select {
- case <-done:
- Fail("BLPop is not blocked")
- case <-time.After(time.Second):
- // ok
- }
- rPush := client.RPush("list", "a")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- select {
- case <-done:
- // ok
- case <-time.After(time.Second):
- Fail("BLPop is still blocked")
- }
- })
- It("should BLPop timeout", func() {
- val, err := client.BLPop(time.Second, "list1").Result()
- Expect(err).To(Equal(redis.Nil))
- Expect(val).To(BeNil())
- Expect(client.Ping().Err()).NotTo(HaveOccurred())
- stats := client.PoolStats()
- Expect(stats.Hits).To(Equal(uint32(1)))
- Expect(stats.Misses).To(Equal(uint32(2)))
- Expect(stats.Timeouts).To(Equal(uint32(0)))
- })
- It("should BRPop", func() {
- rPush := client.RPush("list1", "a", "b", "c")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- bRPop := client.BRPop(0, "list1", "list2")
- Expect(bRPop.Err()).NotTo(HaveOccurred())
- Expect(bRPop.Val()).To(Equal([]string{"list1", "c"}))
- })
- It("should BRPop blocks", func() {
- started := make(chan bool)
- done := make(chan bool)
- go func() {
- defer GinkgoRecover()
- started <- true
- brpop := client.BRPop(0, "list")
- Expect(brpop.Err()).NotTo(HaveOccurred())
- Expect(brpop.Val()).To(Equal([]string{"list", "a"}))
- done <- true
- }()
- <-started
- select {
- case <-done:
- Fail("BRPop is not blocked")
- case <-time.After(time.Second):
- // ok
- }
- rPush := client.RPush("list", "a")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- select {
- case <-done:
- // ok
- case <-time.After(time.Second):
- Fail("BRPop is still blocked")
- // ok
- }
- })
- It("should BRPopLPush", func() {
- _, err := client.BRPopLPush("list1", "list2", time.Second).Result()
- Expect(err).To(Equal(redis.Nil))
- err = client.RPush("list1", "a", "b", "c").Err()
- Expect(err).NotTo(HaveOccurred())
- v, err := client.BRPopLPush("list1", "list2", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(v).To(Equal("c"))
- })
- It("should LIndex", func() {
- lPush := client.LPush("list", "World")
- Expect(lPush.Err()).NotTo(HaveOccurred())
- lPush = client.LPush("list", "Hello")
- Expect(lPush.Err()).NotTo(HaveOccurred())
- lIndex := client.LIndex("list", 0)
- Expect(lIndex.Err()).NotTo(HaveOccurred())
- Expect(lIndex.Val()).To(Equal("Hello"))
- lIndex = client.LIndex("list", -1)
- Expect(lIndex.Err()).NotTo(HaveOccurred())
- Expect(lIndex.Val()).To(Equal("World"))
- lIndex = client.LIndex("list", 3)
- Expect(lIndex.Err()).To(Equal(redis.Nil))
- Expect(lIndex.Val()).To(Equal(""))
- })
- It("should LInsert", func() {
- rPush := client.RPush("list", "Hello")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "World")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- lInsert := client.LInsert("list", "BEFORE", "World", "There")
- Expect(lInsert.Err()).NotTo(HaveOccurred())
- Expect(lInsert.Val()).To(Equal(int64(3)))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"Hello", "There", "World"}))
- })
- It("should LLen", func() {
- lPush := client.LPush("list", "World")
- Expect(lPush.Err()).NotTo(HaveOccurred())
- lPush = client.LPush("list", "Hello")
- Expect(lPush.Err()).NotTo(HaveOccurred())
- lLen := client.LLen("list")
- Expect(lLen.Err()).NotTo(HaveOccurred())
- Expect(lLen.Val()).To(Equal(int64(2)))
- })
- It("should LPop", func() {
- rPush := client.RPush("list", "one")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "two")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "three")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- lPop := client.LPop("list")
- Expect(lPop.Err()).NotTo(HaveOccurred())
- Expect(lPop.Val()).To(Equal("one"))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"two", "three"}))
- })
- It("should LPush", func() {
- lPush := client.LPush("list", "World")
- Expect(lPush.Err()).NotTo(HaveOccurred())
- lPush = client.LPush("list", "Hello")
- Expect(lPush.Err()).NotTo(HaveOccurred())
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
- })
- It("should LPushX", func() {
- lPush := client.LPush("list", "World")
- Expect(lPush.Err()).NotTo(HaveOccurred())
- lPushX := client.LPushX("list", "Hello")
- Expect(lPushX.Err()).NotTo(HaveOccurred())
- Expect(lPushX.Val()).To(Equal(int64(2)))
- lPushX = client.LPushX("list2", "Hello")
- Expect(lPushX.Err()).NotTo(HaveOccurred())
- Expect(lPushX.Val()).To(Equal(int64(0)))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
- lRange = client.LRange("list2", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{}))
- })
- It("should LRange", func() {
- rPush := client.RPush("list", "one")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "two")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "three")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- lRange := client.LRange("list", 0, 0)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"one"}))
- lRange = client.LRange("list", -3, 2)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
- lRange = client.LRange("list", -100, 100)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
- lRange = client.LRange("list", 5, 10)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{}))
- })
- It("should LRem", func() {
- rPush := client.RPush("list", "hello")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "hello")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "key")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "hello")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- lRem := client.LRem("list", -2, "hello")
- Expect(lRem.Err()).NotTo(HaveOccurred())
- Expect(lRem.Val()).To(Equal(int64(2)))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"hello", "key"}))
- })
- It("should LSet", func() {
- rPush := client.RPush("list", "one")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "two")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "three")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- lSet := client.LSet("list", 0, "four")
- Expect(lSet.Err()).NotTo(HaveOccurred())
- Expect(lSet.Val()).To(Equal("OK"))
- lSet = client.LSet("list", -2, "five")
- Expect(lSet.Err()).NotTo(HaveOccurred())
- Expect(lSet.Val()).To(Equal("OK"))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"four", "five", "three"}))
- })
- It("should LTrim", func() {
- rPush := client.RPush("list", "one")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "two")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "three")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- lTrim := client.LTrim("list", 1, -1)
- Expect(lTrim.Err()).NotTo(HaveOccurred())
- Expect(lTrim.Val()).To(Equal("OK"))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"two", "three"}))
- })
- It("should RPop", func() {
- rPush := client.RPush("list", "one")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "two")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "three")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPop := client.RPop("list")
- Expect(rPop.Err()).NotTo(HaveOccurred())
- Expect(rPop.Val()).To(Equal("three"))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"one", "two"}))
- })
- It("should RPopLPush", func() {
- rPush := client.RPush("list", "one")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "two")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPush = client.RPush("list", "three")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- rPopLPush := client.RPopLPush("list", "list2")
- Expect(rPopLPush.Err()).NotTo(HaveOccurred())
- Expect(rPopLPush.Val()).To(Equal("three"))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"one", "two"}))
- lRange = client.LRange("list2", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"three"}))
- })
- It("should RPush", func() {
- rPush := client.RPush("list", "Hello")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- Expect(rPush.Val()).To(Equal(int64(1)))
- rPush = client.RPush("list", "World")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- Expect(rPush.Val()).To(Equal(int64(2)))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
- })
- It("should RPushX", func() {
- rPush := client.RPush("list", "Hello")
- Expect(rPush.Err()).NotTo(HaveOccurred())
- Expect(rPush.Val()).To(Equal(int64(1)))
- rPushX := client.RPushX("list", "World")
- Expect(rPushX.Err()).NotTo(HaveOccurred())
- Expect(rPushX.Val()).To(Equal(int64(2)))
- rPushX = client.RPushX("list2", "World")
- Expect(rPushX.Err()).NotTo(HaveOccurred())
- Expect(rPushX.Val()).To(Equal(int64(0)))
- lRange := client.LRange("list", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
- lRange = client.LRange("list2", 0, -1)
- Expect(lRange.Err()).NotTo(HaveOccurred())
- Expect(lRange.Val()).To(Equal([]string{}))
- })
- })
- Describe("sets", func() {
- It("should SAdd", func() {
- sAdd := client.SAdd("set", "Hello")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- Expect(sAdd.Val()).To(Equal(int64(1)))
- sAdd = client.SAdd("set", "World")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- Expect(sAdd.Val()).To(Equal(int64(1)))
- sAdd = client.SAdd("set", "World")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- Expect(sAdd.Val()).To(Equal(int64(0)))
- sMembers := client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
- })
- It("should SAdd strings", func() {
- set := []string{"Hello", "World", "World"}
- sAdd := client.SAdd("set", set)
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- Expect(sAdd.Val()).To(Equal(int64(2)))
- sMembers := client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
- })
- It("should SCard", func() {
- sAdd := client.SAdd("set", "Hello")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- Expect(sAdd.Val()).To(Equal(int64(1)))
- sAdd = client.SAdd("set", "World")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- Expect(sAdd.Val()).To(Equal(int64(1)))
- sCard := client.SCard("set")
- Expect(sCard.Err()).NotTo(HaveOccurred())
- Expect(sCard.Val()).To(Equal(int64(2)))
- })
- It("should SDiff", func() {
- sAdd := client.SAdd("set1", "a")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "b")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "d")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "e")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sDiff := client.SDiff("set1", "set2")
- Expect(sDiff.Err()).NotTo(HaveOccurred())
- Expect(sDiff.Val()).To(ConsistOf([]string{"a", "b"}))
- })
- It("should SDiffStore", func() {
- sAdd := client.SAdd("set1", "a")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "b")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "d")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "e")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sDiffStore := client.SDiffStore("set", "set1", "set2")
- Expect(sDiffStore.Err()).NotTo(HaveOccurred())
- Expect(sDiffStore.Val()).To(Equal(int64(2)))
- sMembers := client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(ConsistOf([]string{"a", "b"}))
- })
- It("should SInter", func() {
- sAdd := client.SAdd("set1", "a")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "b")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "d")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "e")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sInter := client.SInter("set1", "set2")
- Expect(sInter.Err()).NotTo(HaveOccurred())
- Expect(sInter.Val()).To(Equal([]string{"c"}))
- })
- It("should SInterStore", func() {
- sAdd := client.SAdd("set1", "a")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "b")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "d")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "e")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sInterStore := client.SInterStore("set", "set1", "set2")
- Expect(sInterStore.Err()).NotTo(HaveOccurred())
- Expect(sInterStore.Val()).To(Equal(int64(1)))
- sMembers := client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(Equal([]string{"c"}))
- })
- It("should IsMember", func() {
- sAdd := client.SAdd("set", "one")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sIsMember := client.SIsMember("set", "one")
- Expect(sIsMember.Err()).NotTo(HaveOccurred())
- Expect(sIsMember.Val()).To(Equal(true))
- sIsMember = client.SIsMember("set", "two")
- Expect(sIsMember.Err()).NotTo(HaveOccurred())
- Expect(sIsMember.Val()).To(Equal(false))
- })
- It("should SMembers", func() {
- sAdd := client.SAdd("set", "Hello")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set", "World")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sMembers := client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
- })
- It("should SMembersMap", func() {
- sAdd := client.SAdd("set", "Hello")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set", "World")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sMembersMap := client.SMembersMap("set")
- Expect(sMembersMap.Err()).NotTo(HaveOccurred())
- Expect(sMembersMap.Val()).To(Equal(map[string]struct{}{"Hello": {}, "World": {}}))
- })
- It("should SMove", func() {
- sAdd := client.SAdd("set1", "one")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "two")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "three")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sMove := client.SMove("set1", "set2", "two")
- Expect(sMove.Err()).NotTo(HaveOccurred())
- Expect(sMove.Val()).To(Equal(true))
- sMembers := client.SMembers("set1")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(Equal([]string{"one"}))
- sMembers = client.SMembers("set2")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"}))
- })
- It("should SPop", func() {
- sAdd := client.SAdd("set", "one")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set", "two")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set", "three")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sPop := client.SPop("set")
- Expect(sPop.Err()).NotTo(HaveOccurred())
- Expect(sPop.Val()).NotTo(Equal(""))
- sMembers := client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(HaveLen(2))
- })
- It("should SPopN", func() {
- sAdd := client.SAdd("set", "one")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set", "two")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set", "three")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set", "four")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sPopN := client.SPopN("set", 1)
- Expect(sPopN.Err()).NotTo(HaveOccurred())
- Expect(sPopN.Val()).NotTo(Equal([]string{""}))
- sMembers := client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(HaveLen(3))
- sPopN = client.SPopN("set", 4)
- Expect(sPopN.Err()).NotTo(HaveOccurred())
- Expect(sPopN.Val()).To(HaveLen(3))
- sMembers = client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(HaveLen(0))
- })
- It("should SRandMember and SRandMemberN", func() {
- err := client.SAdd("set", "one").Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.SAdd("set", "two").Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.SAdd("set", "three").Err()
- Expect(err).NotTo(HaveOccurred())
- members, err := client.SMembers("set").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(members).To(HaveLen(3))
- member, err := client.SRandMember("set").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(member).NotTo(Equal(""))
- members, err = client.SRandMemberN("set", 2).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(members).To(HaveLen(2))
- })
- It("should SRem", func() {
- sAdd := client.SAdd("set", "one")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set", "two")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set", "three")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sRem := client.SRem("set", "one")
- Expect(sRem.Err()).NotTo(HaveOccurred())
- Expect(sRem.Val()).To(Equal(int64(1)))
- sRem = client.SRem("set", "four")
- Expect(sRem.Err()).NotTo(HaveOccurred())
- Expect(sRem.Val()).To(Equal(int64(0)))
- sMembers := client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"}))
- })
- It("should SUnion", func() {
- sAdd := client.SAdd("set1", "a")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "b")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "d")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "e")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sUnion := client.SUnion("set1", "set2")
- Expect(sUnion.Err()).NotTo(HaveOccurred())
- Expect(sUnion.Val()).To(HaveLen(5))
- })
- It("should SUnionStore", func() {
- sAdd := client.SAdd("set1", "a")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "b")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set1", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "c")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "d")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sAdd = client.SAdd("set2", "e")
- Expect(sAdd.Err()).NotTo(HaveOccurred())
- sUnionStore := client.SUnionStore("set", "set1", "set2")
- Expect(sUnionStore.Err()).NotTo(HaveOccurred())
- Expect(sUnionStore.Val()).To(Equal(int64(5)))
- sMembers := client.SMembers("set")
- Expect(sMembers.Err()).NotTo(HaveOccurred())
- Expect(sMembers.Val()).To(HaveLen(5))
- })
- })
- Describe("sorted sets", func() {
- It("should BZPopMax", func() {
- err := client.ZAdd("zset1", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset1", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset1", redis.Z{
- Score: 3,
- Member: "three",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- member, err := client.BZPopMax(0, "zset1", "zset2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(member).To(Equal(redis.ZWithKey{
- Z: redis.Z{
- Score: 3,
- Member: "three",
- },
- Key: "zset1",
- }))
- })
- It("should BZPopMax blocks", func() {
- started := make(chan bool)
- done := make(chan bool)
- go func() {
- defer GinkgoRecover()
- started <- true
- bZPopMax := client.BZPopMax(0, "zset")
- Expect(bZPopMax.Err()).NotTo(HaveOccurred())
- Expect(bZPopMax.Val()).To(Equal(redis.ZWithKey{
- Z: redis.Z{
- Member: "a",
- Score: 1,
- },
- Key: "zset",
- }))
- done <- true
- }()
- <-started
- select {
- case <-done:
- Fail("BZPopMax is not blocked")
- case <-time.After(time.Second):
- // ok
- }
- zAdd := client.ZAdd("zset", redis.Z{
- Member: "a",
- Score: 1,
- })
- Expect(zAdd.Err()).NotTo(HaveOccurred())
- select {
- case <-done:
- // ok
- case <-time.After(time.Second):
- Fail("BZPopMax is still blocked")
- }
- })
- It("should BZPopMax timeout", func() {
- val, err := client.BZPopMax(time.Second, "zset1").Result()
- Expect(err).To(Equal(redis.Nil))
- Expect(val).To(Equal(redis.ZWithKey{}))
- Expect(client.Ping().Err()).NotTo(HaveOccurred())
- stats := client.PoolStats()
- Expect(stats.Hits).To(Equal(uint32(1)))
- Expect(stats.Misses).To(Equal(uint32(2)))
- Expect(stats.Timeouts).To(Equal(uint32(0)))
- })
- It("should BZPopMin", func() {
- err := client.ZAdd("zset1", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset1", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset1", redis.Z{
- Score: 3,
- Member: "three",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- member, err := client.BZPopMin(0, "zset1", "zset2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(member).To(Equal(redis.ZWithKey{
- Z: redis.Z{
- Score: 1,
- Member: "one",
- },
- Key: "zset1",
- }))
- })
- It("should BZPopMin blocks", func() {
- started := make(chan bool)
- done := make(chan bool)
- go func() {
- defer GinkgoRecover()
- started <- true
- bZPopMin := client.BZPopMin(0, "zset")
- Expect(bZPopMin.Err()).NotTo(HaveOccurred())
- Expect(bZPopMin.Val()).To(Equal(redis.ZWithKey{
- Z: redis.Z{
- Member: "a",
- Score: 1,
- },
- Key: "zset",
- }))
- done <- true
- }()
- <-started
- select {
- case <-done:
- Fail("BZPopMin is not blocked")
- case <-time.After(time.Second):
- // ok
- }
- zAdd := client.ZAdd("zset", redis.Z{
- Member: "a",
- Score: 1,
- })
- Expect(zAdd.Err()).NotTo(HaveOccurred())
- select {
- case <-done:
- // ok
- case <-time.After(time.Second):
- Fail("BZPopMin is still blocked")
- }
- })
- It("should BZPopMin timeout", func() {
- val, err := client.BZPopMin(time.Second, "zset1").Result()
- Expect(err).To(Equal(redis.Nil))
- Expect(val).To(Equal(redis.ZWithKey{}))
- Expect(client.Ping().Err()).NotTo(HaveOccurred())
- stats := client.PoolStats()
- Expect(stats.Hits).To(Equal(uint32(1)))
- Expect(stats.Misses).To(Equal(uint32(2)))
- Expect(stats.Timeouts).To(Equal(uint32(0)))
- })
- It("should ZAdd", func() {
- added, err := client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- added, err = client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "uno",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- added, err = client.ZAdd("zset", redis.Z{
- Score: 2,
- Member: "two",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- added, err = client.ZAdd("zset", redis.Z{
- Score: 3,
- Member: "two",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(0)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }, {
- Score: 1,
- Member: "uno",
- }, {
- Score: 3,
- Member: "two",
- }}))
- })
- It("should ZAdd bytes", func() {
- added, err := client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: []byte("one"),
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- added, err = client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: []byte("uno"),
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- added, err = client.ZAdd("zset", redis.Z{
- Score: 2,
- Member: []byte("two"),
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- added, err = client.ZAdd("zset", redis.Z{
- Score: 3,
- Member: []byte("two"),
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(0)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }, {
- Score: 1,
- Member: "uno",
- }, {
- Score: 3,
- Member: "two",
- }}))
- })
- It("should ZAddNX", func() {
- added, err := client.ZAddNX("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
- added, err = client.ZAddNX("zset", redis.Z{
- Score: 2,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(0)))
- vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
- })
- It("should ZAddXX", func() {
- added, err := client.ZAddXX("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(0)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(BeEmpty())
- added, err = client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- added, err = client.ZAddXX("zset", redis.Z{
- Score: 2,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(0)))
- vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
- })
- It("should ZAddCh", func() {
- changed, err := client.ZAddCh("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(changed).To(Equal(int64(1)))
- changed, err = client.ZAddCh("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(changed).To(Equal(int64(0)))
- })
- It("should ZAddNXCh", func() {
- changed, err := client.ZAddNXCh("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(changed).To(Equal(int64(1)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
- changed, err = client.ZAddNXCh("zset", redis.Z{
- Score: 2,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(changed).To(Equal(int64(0)))
- vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }}))
- })
- It("should ZAddXXCh", func() {
- changed, err := client.ZAddXXCh("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(changed).To(Equal(int64(0)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(BeEmpty())
- added, err := client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- changed, err = client.ZAddXXCh("zset", redis.Z{
- Score: 2,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(changed).To(Equal(int64(1)))
- vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
- })
- It("should ZIncr", func() {
- score, err := client.ZIncr("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(score).To(Equal(float64(1)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
- score, err = client.ZIncr("zset", redis.Z{Score: 1, Member: "one"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(score).To(Equal(float64(2)))
- vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
- })
- It("should ZIncrNX", func() {
- score, err := client.ZIncrNX("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(score).To(Equal(float64(1)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
- score, err = client.ZIncrNX("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).To(Equal(redis.Nil))
- Expect(score).To(Equal(float64(0)))
- vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
- })
- It("should ZIncrXX", func() {
- score, err := client.ZIncrXX("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).To(Equal(redis.Nil))
- Expect(score).To(Equal(float64(0)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(BeEmpty())
- added, err := client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(added).To(Equal(int64(1)))
- score, err = client.ZIncrXX("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(score).To(Equal(float64(2)))
- vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
- })
- It("should ZCard", func() {
- err := client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- card, err := client.ZCard("zset").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(card).To(Equal(int64(2)))
- })
- It("should ZCount", func() {
- err := client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 3,
- Member: "three",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- count, err := client.ZCount("zset", "-inf", "+inf").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(count).To(Equal(int64(3)))
- count, err = client.ZCount("zset", "(1", "3").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(count).To(Equal(int64(2)))
- count, err = client.ZLexCount("zset", "-", "+").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(count).To(Equal(int64(3)))
- })
- It("should ZIncrBy", func() {
- err := client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- n, err := client.ZIncrBy("zset", 2, "one").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(float64(3)))
- val, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal([]redis.Z{{
- Score: 2,
- Member: "two",
- }, {
- Score: 3,
- Member: "one",
- }}))
- })
- It("should ZInterStore", func() {
- err := client.ZAdd("zset1", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset1", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset2", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset2", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset3", redis.Z{Score: 3, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zInterStore := client.ZInterStore(
- "out", redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2")
- Expect(zInterStore.Err()).NotTo(HaveOccurred())
- Expect(zInterStore.Val()).To(Equal(int64(2)))
- vals, err := client.ZRangeWithScores("out", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 5,
- Member: "one",
- }, {
- Score: 10,
- Member: "two",
- }}))
- })
- It("should ZPopMax", func() {
- err := client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 3,
- Member: "three",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- members, err := client.ZPopMax("zset").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(members).To(Equal([]redis.Z{{
- Score: 3,
- Member: "three",
- }}))
- // adding back 3
- err = client.ZAdd("zset", redis.Z{
- Score: 3,
- Member: "three",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- members, err = client.ZPopMax("zset", 2).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(members).To(Equal([]redis.Z{{
- Score: 3,
- Member: "three",
- }, {
- Score: 2,
- Member: "two",
- }}))
- // adding back 2 & 3
- err = client.ZAdd("zset", redis.Z{
- Score: 3,
- Member: "three",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- members, err = client.ZPopMax("zset", 10).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(members).To(Equal([]redis.Z{{
- Score: 3,
- Member: "three",
- }, {
- Score: 2,
- Member: "two",
- }, {
- Score: 1,
- Member: "one",
- }}))
- })
- It("should ZPopMin", func() {
- err := client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 3,
- Member: "three",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- members, err := client.ZPopMin("zset").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(members).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }}))
- // adding back 1
- err = client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- members, err = client.ZPopMin("zset", 2).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(members).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }, {
- Score: 2,
- Member: "two",
- }}))
- // adding back 1 & 2
- err = client.ZAdd("zset", redis.Z{
- Score: 1,
- Member: "one",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 2,
- Member: "two",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- members, err = client.ZPopMin("zset", 10).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(members).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }, {
- Score: 2,
- Member: "two",
- }, {
- Score: 3,
- Member: "three",
- }}))
- })
- It("should ZRange", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zRange := client.ZRange("zset", 0, -1)
- Expect(zRange.Err()).NotTo(HaveOccurred())
- Expect(zRange.Val()).To(Equal([]string{"one", "two", "three"}))
- zRange = client.ZRange("zset", 2, 3)
- Expect(zRange.Err()).NotTo(HaveOccurred())
- Expect(zRange.Val()).To(Equal([]string{"three"}))
- zRange = client.ZRange("zset", -2, -1)
- Expect(zRange.Err()).NotTo(HaveOccurred())
- Expect(zRange.Val()).To(Equal([]string{"two", "three"}))
- })
- It("should ZRangeWithScores", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }, {
- Score: 2,
- Member: "two",
- }, {
- Score: 3,
- Member: "three",
- }}))
- vals, err = client.ZRangeWithScores("zset", 2, 3).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 3, Member: "three"}}))
- vals, err = client.ZRangeWithScores("zset", -2, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 2,
- Member: "two",
- }, {
- Score: 3,
- Member: "three",
- }}))
- })
- It("should ZRangeByScore", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeBy{
- Min: "-inf",
- Max: "+inf",
- })
- Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
- Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two", "three"}))
- zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
- Min: "1",
- Max: "2",
- })
- Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
- Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two"}))
- zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
- Min: "(1",
- Max: "2",
- })
- Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
- Expect(zRangeByScore.Val()).To(Equal([]string{"two"}))
- zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
- Min: "(1",
- Max: "(2",
- })
- Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
- Expect(zRangeByScore.Val()).To(Equal([]string{}))
- })
- It("should ZRangeByLex", func() {
- err := client.ZAdd("zset", redis.Z{
- Score: 0,
- Member: "a",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 0,
- Member: "b",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{
- Score: 0,
- Member: "c",
- }).Err()
- Expect(err).NotTo(HaveOccurred())
- zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeBy{
- Min: "-",
- Max: "+",
- })
- Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
- Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"}))
- zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
- Min: "[a",
- Max: "[b",
- })
- Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
- Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"}))
- zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
- Min: "(a",
- Max: "[b",
- })
- Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
- Expect(zRangeByLex.Val()).To(Equal([]string{"b"}))
- zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
- Min: "(a",
- Max: "(b",
- })
- Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
- Expect(zRangeByLex.Val()).To(Equal([]string{}))
- })
- It("should ZRangeByScoreWithScoresMap", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- vals, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
- Min: "-inf",
- Max: "+inf",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }, {
- Score: 2,
- Member: "two",
- }, {
- Score: 3,
- Member: "three",
- }}))
- vals, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
- Min: "1",
- Max: "2",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }, {
- Score: 2,
- Member: "two",
- }}))
- vals, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
- Min: "(1",
- Max: "2",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "two"}}))
- vals, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
- Min: "(1",
- Max: "(2",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{}))
- })
- It("should ZRank", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zRank := client.ZRank("zset", "three")
- Expect(zRank.Err()).NotTo(HaveOccurred())
- Expect(zRank.Val()).To(Equal(int64(2)))
- zRank = client.ZRank("zset", "four")
- Expect(zRank.Err()).To(Equal(redis.Nil))
- Expect(zRank.Val()).To(Equal(int64(0)))
- })
- It("should ZRem", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zRem := client.ZRem("zset", "two")
- Expect(zRem.Err()).NotTo(HaveOccurred())
- Expect(zRem.Val()).To(Equal(int64(1)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 1,
- Member: "one",
- }, {
- Score: 3,
- Member: "three",
- }}))
- })
- It("should ZRemRangeByRank", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zRemRangeByRank := client.ZRemRangeByRank("zset", 0, 1)
- Expect(zRemRangeByRank.Err()).NotTo(HaveOccurred())
- Expect(zRemRangeByRank.Val()).To(Equal(int64(2)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 3,
- Member: "three",
- }}))
- })
- It("should ZRemRangeByScore", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zRemRangeByScore := client.ZRemRangeByScore("zset", "-inf", "(2")
- Expect(zRemRangeByScore.Err()).NotTo(HaveOccurred())
- Expect(zRemRangeByScore.Val()).To(Equal(int64(1)))
- vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 2,
- Member: "two",
- }, {
- Score: 3,
- Member: "three",
- }}))
- })
- It("should ZRemRangeByLex", func() {
- zz := []redis.Z{
- {Score: 0, Member: "aaaa"},
- {Score: 0, Member: "b"},
- {Score: 0, Member: "c"},
- {Score: 0, Member: "d"},
- {Score: 0, Member: "e"},
- {Score: 0, Member: "foo"},
- {Score: 0, Member: "zap"},
- {Score: 0, Member: "zip"},
- {Score: 0, Member: "ALPHA"},
- {Score: 0, Member: "alpha"},
- }
- for _, z := range zz {
- err := client.ZAdd("zset", z).Err()
- Expect(err).NotTo(HaveOccurred())
- }
- n, err := client.ZRemRangeByLex("zset", "[alpha", "[omega").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(6)))
- vals, err := client.ZRange("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]string{"ALPHA", "aaaa", "zap", "zip"}))
- })
- It("should ZRevRange", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zRevRange := client.ZRevRange("zset", 0, -1)
- Expect(zRevRange.Err()).NotTo(HaveOccurred())
- Expect(zRevRange.Val()).To(Equal([]string{"three", "two", "one"}))
- zRevRange = client.ZRevRange("zset", 2, 3)
- Expect(zRevRange.Err()).NotTo(HaveOccurred())
- Expect(zRevRange.Val()).To(Equal([]string{"one"}))
- zRevRange = client.ZRevRange("zset", -2, -1)
- Expect(zRevRange.Err()).NotTo(HaveOccurred())
- Expect(zRevRange.Val()).To(Equal([]string{"two", "one"}))
- })
- It("should ZRevRangeWithScoresMap", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- val, err := client.ZRevRangeWithScores("zset", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal([]redis.Z{{
- Score: 3,
- Member: "three",
- }, {
- Score: 2,
- Member: "two",
- }, {
- Score: 1,
- Member: "one",
- }}))
- val, err = client.ZRevRangeWithScores("zset", 2, 3).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
- val, err = client.ZRevRangeWithScores("zset", -2, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal([]redis.Z{{
- Score: 2,
- Member: "two",
- }, {
- Score: 1,
- Member: "one",
- }}))
- })
- It("should ZRevRangeByScore", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- vals, err := client.ZRevRangeByScore(
- "zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]string{"three", "two", "one"}))
- vals, err = client.ZRevRangeByScore(
- "zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]string{"two"}))
- vals, err = client.ZRevRangeByScore(
- "zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]string{}))
- })
- It("should ZRevRangeByLex", func() {
- err := client.ZAdd("zset", redis.Z{Score: 0, Member: "a"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 0, Member: "b"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 0, Member: "c"}).Err()
- Expect(err).NotTo(HaveOccurred())
- vals, err := client.ZRevRangeByLex(
- "zset", redis.ZRangeBy{Max: "+", Min: "-"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]string{"c", "b", "a"}))
- vals, err = client.ZRevRangeByLex(
- "zset", redis.ZRangeBy{Max: "[b", Min: "(a"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]string{"b"}))
- vals, err = client.ZRevRangeByLex(
- "zset", redis.ZRangeBy{Max: "(b", Min: "(a"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]string{}))
- })
- It("should ZRevRangeByScoreWithScores", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- vals, err := client.ZRevRangeByScoreWithScores(
- "zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 3,
- Member: "three",
- }, {
- Score: 2,
- Member: "two",
- }, {
- Score: 1,
- Member: "one",
- }}))
- })
- It("should ZRevRangeByScoreWithScoresMap", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- vals, err := client.ZRevRangeByScoreWithScores(
- "zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{
- Score: 3,
- Member: "three",
- }, {
- Score: 2,
- Member: "two",
- }, {
- Score: 1,
- Member: "one",
- }}))
- vals, err = client.ZRevRangeByScoreWithScores(
- "zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "two"}}))
- vals, err = client.ZRevRangeByScoreWithScores(
- "zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.Z{}))
- })
- It("should ZRevRank", func() {
- err := client.ZAdd("zset", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zRevRank := client.ZRevRank("zset", "one")
- Expect(zRevRank.Err()).NotTo(HaveOccurred())
- Expect(zRevRank.Val()).To(Equal(int64(2)))
- zRevRank = client.ZRevRank("zset", "four")
- Expect(zRevRank.Err()).To(Equal(redis.Nil))
- Expect(zRevRank.Val()).To(Equal(int64(0)))
- })
- It("should ZScore", func() {
- zAdd := client.ZAdd("zset", redis.Z{Score: 1.001, Member: "one"})
- Expect(zAdd.Err()).NotTo(HaveOccurred())
- zScore := client.ZScore("zset", "one")
- Expect(zScore.Err()).NotTo(HaveOccurred())
- Expect(zScore.Val()).To(Equal(float64(1.001)))
- })
- It("should ZUnionStore", func() {
- err := client.ZAdd("zset1", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset1", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset2", redis.Z{Score: 1, Member: "one"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset2", redis.Z{Score: 2, Member: "two"}).Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.ZAdd("zset2", redis.Z{Score: 3, Member: "three"}).Err()
- Expect(err).NotTo(HaveOccurred())
- zUnionStore := client.ZUnionStore(
- "out", redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2")
- Expect(zUnionStore.Err()).NotTo(HaveOccurred())
- Expect(zUnionStore.Val()).To(Equal(int64(3)))
- val, err := client.ZRangeWithScores("out", 0, -1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(val).To(Equal([]redis.Z{{
- Score: 5,
- Member: "one",
- }, {
- Score: 9,
- Member: "three",
- }, {
- Score: 10,
- Member: "two",
- }}))
- })
- })
- Describe("streams", func() {
- BeforeEach(func() {
- id, err := client.XAdd(&redis.XAddArgs{
- Stream: "stream",
- ID: "1-0",
- Values: map[string]interface{}{"uno": "un"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(id).To(Equal("1-0"))
- id, err = client.XAdd(&redis.XAddArgs{
- Stream: "stream",
- ID: "2-0",
- Values: map[string]interface{}{"dos": "deux"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(id).To(Equal("2-0"))
- id, err = client.XAdd(&redis.XAddArgs{
- Stream: "stream",
- ID: "3-0",
- Values: map[string]interface{}{"tres": "troix"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(id).To(Equal("3-0"))
- })
- It("should XTrim", func() {
- n, err := client.XTrim("stream", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(3)))
- })
- It("should XTrimApprox", func() {
- n, err := client.XTrimApprox("stream", 0).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(3)))
- })
- It("should XAdd", func() {
- id, err := client.XAdd(&redis.XAddArgs{
- Stream: "stream",
- Values: map[string]interface{}{"quatro": "quatre"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- vals, err := client.XRange("stream", "-", "+").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.XMessage{
- {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
- {ID: id, Values: map[string]interface{}{"quatro": "quatre"}},
- }))
- })
- It("should XAdd with MaxLen", func() {
- id, err := client.XAdd(&redis.XAddArgs{
- Stream: "stream",
- MaxLen: 1,
- Values: map[string]interface{}{"quatro": "quatre"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- vals, err := client.XRange("stream", "-", "+").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]redis.XMessage{
- {ID: id, Values: map[string]interface{}{"quatro": "quatre"}},
- }))
- })
- It("should XDel", func() {
- n, err := client.XDel("stream", "1-0", "2-0", "3-0").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(3)))
- })
- It("should XLen", func() {
- n, err := client.XLen("stream").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(3)))
- })
- It("should XRange", func() {
- msgs, err := client.XRange("stream", "-", "+").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
- }))
- msgs, err = client.XRange("stream", "2", "+").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
- }))
- msgs, err = client.XRange("stream", "-", "2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- }))
- })
- It("should XRangeN", func() {
- msgs, err := client.XRangeN("stream", "-", "+", 2).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- }))
- msgs, err = client.XRangeN("stream", "2", "+", 1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- }))
- msgs, err = client.XRangeN("stream", "-", "2", 1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
- }))
- })
- It("should XRevRange", func() {
- msgs, err := client.XRevRange("stream", "+", "-").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
- }))
- msgs, err = client.XRevRange("stream", "+", "2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- }))
- })
- It("should XRevRangeN", func() {
- msgs, err := client.XRevRangeN("stream", "+", "-", 2).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- }))
- msgs, err = client.XRevRangeN("stream", "+", "2", 1).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{
- {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
- }))
- })
- It("should XRead", func() {
- res, err := client.XReadStreams("stream", "0").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(res).To(Equal([]redis.XStream{{
- Stream: "stream",
- Messages: []redis.XMessage{
- {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
- }},
- }))
- _, err = client.XReadStreams("stream", "3").Result()
- Expect(err).To(Equal(redis.Nil))
- })
- It("should XRead", func() {
- res, err := client.XRead(&redis.XReadArgs{
- Streams: []string{"stream", "0"},
- Count: 2,
- Block: 100 * time.Millisecond,
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(res).To(Equal([]redis.XStream{{
- Stream: "stream",
- Messages: []redis.XMessage{
- {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- }},
- }))
- _, err = client.XRead(&redis.XReadArgs{
- Streams: []string{"stream", "3"},
- Count: 1,
- Block: 100 * time.Millisecond,
- }).Result()
- Expect(err).To(Equal(redis.Nil))
- })
- Describe("group", func() {
- BeforeEach(func() {
- err := client.XGroupCreate("stream", "group", "0").Err()
- Expect(err).NotTo(HaveOccurred())
- res, err := client.XReadGroup(&redis.XReadGroupArgs{
- Group: "group",
- Consumer: "consumer",
- Streams: []string{"stream", ">"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(res).To(Equal([]redis.XStream{{
- Stream: "stream",
- Messages: []redis.XMessage{
- {ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
- {ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
- {ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
- }},
- }))
- })
- AfterEach(func() {
- n, err := client.XGroupDestroy("stream", "group").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(1)))
- })
- It("should XGroupCreateMkStream", func() {
- err := client.XGroupCreateMkStream("stream2", "group", "0").Err()
- Expect(err).NotTo(HaveOccurred())
- err = client.XGroupCreateMkStream("stream2", "group", "0").Err()
- Expect(err).To(Equal(proto.RedisError("BUSYGROUP Consumer Group name already exists")))
- n, err := client.XGroupDestroy("stream2", "group").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(1)))
- n, err = client.Del("stream2").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(1)))
- })
- It("should XPending", func() {
- info, err := client.XPending("stream", "group").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(info).To(Equal(&redis.XPending{
- Count: 3,
- Lower: "1-0",
- Higher: "3-0",
- Consumers: map[string]int64{"consumer": 3},
- }))
- infoExt, err := client.XPendingExt(&redis.XPendingExtArgs{
- Stream: "stream",
- Group: "group",
- Start: "-",
- End: "+",
- Count: 10,
- Consumer: "consumer",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- for i := range infoExt {
- infoExt[i].Idle = 0
- }
- Expect(infoExt).To(Equal([]redis.XPendingExt{
- {Id: "1-0", Consumer: "consumer", Idle: 0, RetryCount: 1},
- {Id: "2-0", Consumer: "consumer", Idle: 0, RetryCount: 1},
- {Id: "3-0", Consumer: "consumer", Idle: 0, RetryCount: 1},
- }))
- n, err := client.XGroupDelConsumer("stream", "group", "consumer").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(3)))
- })
- It("should XClaim", func() {
- msgs, err := client.XClaim(&redis.XClaimArgs{
- Stream: "stream",
- Group: "group",
- Consumer: "consumer",
- Messages: []string{"1-0", "2-0", "3-0"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(msgs).To(Equal([]redis.XMessage{{
- ID: "1-0",
- Values: map[string]interface{}{"uno": "un"},
- }, {
- ID: "2-0",
- Values: map[string]interface{}{"dos": "deux"},
- }, {
- ID: "3-0",
- Values: map[string]interface{}{"tres": "troix"},
- }}))
- ids, err := client.XClaimJustID(&redis.XClaimArgs{
- Stream: "stream",
- Group: "group",
- Consumer: "consumer",
- Messages: []string{"1-0", "2-0", "3-0"},
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(ids).To(Equal([]string{"1-0", "2-0", "3-0"}))
- })
- It("should XAck", func() {
- n, err := client.XAck("stream", "group", "1-0", "2-0", "4-0").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(2)))
- })
- })
- })
- Describe("Geo add and radius search", func() {
- BeforeEach(func() {
- n, err := client.GeoAdd(
- "Sicily",
- &redis.GeoLocation{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"},
- &redis.GeoLocation{Longitude: 15.087269, Latitude: 37.502669, Name: "Catania"},
- ).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(n).To(Equal(int64(2)))
- })
- It("should not add same geo location", func() {
- geoAdd := client.GeoAdd(
- "Sicily",
- &redis.GeoLocation{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"},
- )
- Expect(geoAdd.Err()).NotTo(HaveOccurred())
- Expect(geoAdd.Val()).To(Equal(int64(0)))
- })
- It("should search geo radius", func() {
- res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{
- Radius: 200,
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(res).To(HaveLen(2))
- Expect(res[0].Name).To(Equal("Palermo"))
- Expect(res[1].Name).To(Equal("Catania"))
- })
- It("should search geo radius with options", func() {
- res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{
- Radius: 200,
- Unit: "km",
- WithGeoHash: true,
- WithCoord: true,
- WithDist: true,
- Count: 2,
- Sort: "ASC",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(res).To(HaveLen(2))
- Expect(res[1].Name).To(Equal("Palermo"))
- Expect(res[1].Dist).To(Equal(190.4424))
- Expect(res[1].GeoHash).To(Equal(int64(3479099956230698)))
- Expect(res[1].Longitude).To(Equal(13.361389338970184))
- Expect(res[1].Latitude).To(Equal(38.115556395496299))
- Expect(res[0].Name).To(Equal("Catania"))
- Expect(res[0].Dist).To(Equal(56.4413))
- Expect(res[0].GeoHash).To(Equal(int64(3479447370796909)))
- Expect(res[0].Longitude).To(Equal(15.087267458438873))
- Expect(res[0].Latitude).To(Equal(37.50266842333162))
- })
- It("should search geo radius with WithDist=false", func() {
- res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{
- Radius: 200,
- Unit: "km",
- WithGeoHash: true,
- WithCoord: true,
- Count: 2,
- Sort: "ASC",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(res).To(HaveLen(2))
- Expect(res[1].Name).To(Equal("Palermo"))
- Expect(res[1].Dist).To(Equal(float64(0)))
- Expect(res[1].GeoHash).To(Equal(int64(3479099956230698)))
- Expect(res[1].Longitude).To(Equal(13.361389338970184))
- Expect(res[1].Latitude).To(Equal(38.115556395496299))
- Expect(res[0].Name).To(Equal("Catania"))
- Expect(res[0].Dist).To(Equal(float64(0)))
- Expect(res[0].GeoHash).To(Equal(int64(3479447370796909)))
- Expect(res[0].Longitude).To(Equal(15.087267458438873))
- Expect(res[0].Latitude).To(Equal(37.50266842333162))
- })
- It("should search geo radius by member with options", func() {
- res, err := client.GeoRadiusByMember("Sicily", "Catania", &redis.GeoRadiusQuery{
- Radius: 200,
- Unit: "km",
- WithGeoHash: true,
- WithCoord: true,
- WithDist: true,
- Count: 2,
- Sort: "ASC",
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(res).To(HaveLen(2))
- Expect(res[0].Name).To(Equal("Catania"))
- Expect(res[0].Dist).To(Equal(0.0))
- Expect(res[0].GeoHash).To(Equal(int64(3479447370796909)))
- Expect(res[0].Longitude).To(Equal(15.087267458438873))
- Expect(res[0].Latitude).To(Equal(37.50266842333162))
- Expect(res[1].Name).To(Equal("Palermo"))
- Expect(res[1].Dist).To(Equal(166.2742))
- Expect(res[1].GeoHash).To(Equal(int64(3479099956230698)))
- Expect(res[1].Longitude).To(Equal(13.361389338970184))
- Expect(res[1].Latitude).To(Equal(38.115556395496299))
- })
- It("should search geo radius with no results", func() {
- res, err := client.GeoRadius("Sicily", 99, 37, &redis.GeoRadiusQuery{
- Radius: 200,
- Unit: "km",
- WithGeoHash: true,
- WithCoord: true,
- WithDist: true,
- }).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(res).To(HaveLen(0))
- })
- It("should get geo distance with unit options", func() {
- // From Redis CLI, note the difference in rounding in m vs
- // km on Redis itself.
- //
- // GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
- // GEODIST Sicily Palermo Catania m
- // "166274.15156960033"
- // GEODIST Sicily Palermo Catania km
- // "166.27415156960032"
- dist, err := client.GeoDist("Sicily", "Palermo", "Catania", "km").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(dist).To(BeNumerically("~", 166.27, 0.01))
- dist, err = client.GeoDist("Sicily", "Palermo", "Catania", "m").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(dist).To(BeNumerically("~", 166274.15, 0.01))
- })
- It("should get geo hash in string representation", func() {
- hashes, err := client.GeoHash("Sicily", "Palermo", "Catania").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(hashes).To(ConsistOf([]string{"sqc8b49rny0", "sqdtr74hyu0"}))
- })
- It("should return geo position", func() {
- pos, err := client.GeoPos("Sicily", "Palermo", "Catania", "NonExisting").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(pos).To(ConsistOf([]*redis.GeoPos{
- {
- Longitude: 13.361389338970184,
- Latitude: 38.1155563954963,
- },
- {
- Longitude: 15.087267458438873,
- Latitude: 37.50266842333162,
- },
- nil,
- }))
- })
- })
- Describe("marshaling/unmarshaling", func() {
- type convTest struct {
- value interface{}
- wanted string
- dest interface{}
- }
- convTests := []convTest{
- {nil, "", nil},
- {"hello", "hello", new(string)},
- {[]byte("hello"), "hello", new([]byte)},
- {int(1), "1", new(int)},
- {int8(1), "1", new(int8)},
- {int16(1), "1", new(int16)},
- {int32(1), "1", new(int32)},
- {int64(1), "1", new(int64)},
- {uint(1), "1", new(uint)},
- {uint8(1), "1", new(uint8)},
- {uint16(1), "1", new(uint16)},
- {uint32(1), "1", new(uint32)},
- {uint64(1), "1", new(uint64)},
- {float32(1.0), "1", new(float32)},
- {float64(1.0), "1", new(float64)},
- {true, "1", new(bool)},
- {false, "0", new(bool)},
- }
- It("should convert to string", func() {
- for _, test := range convTests {
- err := client.Set("key", test.value, 0).Err()
- Expect(err).NotTo(HaveOccurred())
- s, err := client.Get("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(s).To(Equal(test.wanted))
- if test.dest == nil {
- continue
- }
- err = client.Get("key").Scan(test.dest)
- Expect(err).NotTo(HaveOccurred())
- Expect(deref(test.dest)).To(Equal(test.value))
- }
- })
- })
- Describe("json marshaling/unmarshaling", func() {
- BeforeEach(func() {
- value := &numberStruct{Number: 42}
- err := client.Set("key", value, 0).Err()
- Expect(err).NotTo(HaveOccurred())
- })
- It("should marshal custom values using json", func() {
- s, err := client.Get("key").Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(s).To(Equal(`{"Number":42}`))
- })
- It("should scan custom values using json", func() {
- value := &numberStruct{}
- err := client.Get("key").Scan(value)
- Expect(err).NotTo(HaveOccurred())
- Expect(value.Number).To(Equal(42))
- })
- })
- Describe("Eval", func() {
- It("returns keys and values", func() {
- vals, err := client.Eval(
- "return {KEYS[1],ARGV[1]}",
- []string{"key"},
- "hello",
- ).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]interface{}{"key", "hello"}))
- })
- It("returns all values after an error", func() {
- vals, err := client.Eval(
- `return {12, {err="error"}, "abc"}`,
- nil,
- ).Result()
- Expect(err).NotTo(HaveOccurred())
- Expect(vals).To(Equal([]interface{}{int64(12), proto.RedisError("error"), "abc"}))
- })
- })
- })
- type numberStruct struct {
- Number int
- }
- func (s *numberStruct) MarshalBinary() ([]byte, error) {
- return json.Marshal(s)
- }
- func (s *numberStruct) UnmarshalBinary(b []byte) error {
- return json.Unmarshal(b, s)
- }
- func deref(viface interface{}) interface{} {
- v := reflect.ValueOf(viface)
- for v.Kind() == reflect.Ptr {
- v = v.Elem()
- }
- return v.Interface()
- }
|