1 Star 0 Fork 0

GU/redis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
commands_test.go 164.21 KB
一键复制 编辑 原始数据 按行查看 历史
Vladimir Mihailenco 提交于 2021-09-23 16:31 . chore: update dependencies
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496
package redis_test
import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/go-redis/redis/v8"
"github.com/go-redis/redis/v8/internal/proto"
)
var _ = Describe("Commands", func() {
ctx := context.TODO()
var client *redis.Client
BeforeEach(func() {
client = redis.NewClient(redisOptions())
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
Describe("server", func() {
It("should Auth", func() {
cmds, err := client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Auth(ctx, "password")
pipe.Auth(ctx, "")
return nil
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("ERR AUTH"))
Expect(cmds[0].Err().Error()).To(ContainSubstring("ERR AUTH"))
Expect(cmds[1].Err().Error()).To(ContainSubstring("ERR AUTH"))
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(ctx, "hello")
_, err := pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(echo.Err()).NotTo(HaveOccurred())
Expect(echo.Val()).To(Equal("hello"))
})
It("should Ping", func() {
ping := client.Ping(ctx)
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(ctx, 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(ctx, 1)
_, err := pipe.Exec(ctx)
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(ctx, 1, 2)
_, err := pipe.Exec(ctx)
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(ctx).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(ctx).Val()
}, "30s").Should(Equal("Background saving started"))
})
It("should ClientKill", func() {
r := client.ClientKill(ctx, "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(ctx, "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(ctx).Err()
Expect(err).NotTo(HaveOccurred())
Expect(client.ClientID(ctx).Val()).To(BeNumerically(">=", 0))
})
It("should ClientUnblock", func() {
id := client.ClientID(ctx).Val()
r, err := client.ClientUnblock(ctx, id).Result()
Expect(err).NotTo(HaveOccurred())
Expect(r).To(Equal(int64(0)))
})
It("should ClientUnblockWithError", func() {
id := client.ClientID(ctx).Val()
r, err := client.ClientUnblockWithError(ctx, id).Result()
Expect(err).NotTo(HaveOccurred())
Expect(r).To(Equal(int64(0)))
})
It("should ClientPause", func() {
err := client.ClientPause(ctx, time.Second).Err()
Expect(err).NotTo(HaveOccurred())
start := time.Now()
err = client.Ping(ctx).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(ctx, "theclientname")
get := pipe.ClientGetName(ctx)
_, err := pipe.Exec(ctx)
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(ctx, "*").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).NotTo(BeEmpty())
})
It("should ConfigResetStat", func() {
r := client.ConfigResetStat(ctx)
Expect(r.Err()).NotTo(HaveOccurred())
Expect(r.Val()).To(Equal("OK"))
})
It("should ConfigSet", func() {
configGet := client.ConfigGet(ctx, "maxmemory")
Expect(configGet.Err()).NotTo(HaveOccurred())
Expect(configGet.Val()).To(HaveLen(2))
Expect(configGet.Val()[0]).To(Equal("maxmemory"))
configSet := client.ConfigSet(ctx, "maxmemory", configGet.Val()[1].(string))
Expect(configSet.Err()).NotTo(HaveOccurred())
Expect(configSet.Val()).To(Equal("OK"))
})
It("should ConfigRewrite", func() {
configRewrite := client.ConfigRewrite(ctx)
Expect(configRewrite.Err()).NotTo(HaveOccurred())
Expect(configRewrite.Val()).To(Equal("OK"))
})
It("should DBSize", func() {
size, err := client.DBSize(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(0)))
})
It("should Info", func() {
info := client.Info(ctx)
Expect(info.Err()).NotTo(HaveOccurred())
Expect(info.Val()).NotTo(Equal(""))
})
It("should Info cpu", func() {
info := client.Info(ctx, "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(ctx)
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(ctx).Val()
}, "10s").Should(Equal("OK"))
})
It("should SlaveOf", func() {
slaveOf := client.SlaveOf(ctx, "localhost", "8888")
Expect(slaveOf.Err()).NotTo(HaveOccurred())
Expect(slaveOf.Val()).To(Equal("OK"))
slaveOf = client.SlaveOf(ctx, "NO", "ONE")
Expect(slaveOf.Err()).NotTo(HaveOccurred())
Expect(slaveOf.Val()).To(Equal("OK"))
})
It("should Time", func() {
tm, err := client.Time(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(tm).To(BeTemporally("~", time.Now(), 3*time.Second))
})
It("should Command", func() {
cmds, err := client.Command(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(len(cmds)).To(BeNumerically("~", 200, 25))
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(ctx, "foo").Err()
Expect(err).To(MatchError("ERR no such key"))
err = client.Set(ctx, "foo", "bar", 0).Err()
Expect(err).NotTo(HaveOccurred())
s, err := client.DebugObject(ctx, "foo").Result()
Expect(err).NotTo(HaveOccurred())
Expect(s).To(ContainSubstring("serializedlength:4"))
})
It("should MemoryUsage", func() {
err := client.MemoryUsage(ctx, "foo").Err()
Expect(err).To(Equal(redis.Nil))
err = client.Set(ctx, "foo", "bar", 0).Err()
Expect(err).NotTo(HaveOccurred())
n, err := client.MemoryUsage(ctx, "foo").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).NotTo(BeZero())
n, err = client.MemoryUsage(ctx, "foo", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).NotTo(BeZero())
})
})
Describe("keys", func() {
It("should Del", func() {
err := client.Set(ctx, "key1", "Hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
err = client.Set(ctx, "key2", "World", 0).Err()
Expect(err).NotTo(HaveOccurred())
n, err := client.Del(ctx, "key1", "key2", "key3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
})
It("should Unlink", func() {
err := client.Set(ctx, "key1", "Hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
err = client.Set(ctx, "key2", "World", 0).Err()
Expect(err).NotTo(HaveOccurred())
n, err := client.Unlink(ctx, "key1", "key2", "key3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
})
It("should Dump", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
dump := client.Dump(ctx, "key")
Expect(dump.Err()).NotTo(HaveOccurred())
Expect(dump.Val()).NotTo(BeEmpty())
})
It("should Exists", func() {
set := client.Set(ctx, "key1", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
n, err := client.Exists(ctx, "key1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
n, err = client.Exists(ctx, "key2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0)))
n, err = client.Exists(ctx, "key1", "key2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
n, err = client.Exists(ctx, "key1", "key1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
})
It("should Expire", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expire := client.Expire(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(10 * time.Second))
set = client.Set(ctx, "key", "Hello World", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-1)))
ttl = client.TTL(ctx, "nonexistent_key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-2)))
})
It("should ExpireAt", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
n, err := client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
expireAt := client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
Expect(expireAt.Err()).NotTo(HaveOccurred())
Expect(expireAt.Val()).To(Equal(true))
n, err = client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0)))
})
It("should Keys", func() {
mset := client.MSet(ctx, "one", "1", "two", "2", "three", "3", "four", "4")
Expect(mset.Err()).NotTo(HaveOccurred())
Expect(mset.Val()).To(Equal("OK"))
keys := client.Keys(ctx, "*o*")
Expect(keys.Err()).NotTo(HaveOccurred())
Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "two"}))
keys = client.Keys(ctx, "t??")
Expect(keys.Err()).NotTo(HaveOccurred())
Expect(keys.Val()).To(Equal([]string{"two"}))
keys = client.Keys(ctx, "*")
Expect(keys.Err()).NotTo(HaveOccurred())
Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "three", "two"}))
})
It("should Migrate", func() {
migrate := client.Migrate(ctx, "localhost", redisSecondaryPort, "key", 0, 0)
Expect(migrate.Err()).NotTo(HaveOccurred())
Expect(migrate.Val()).To(Equal("NOKEY"))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
migrate = client.Migrate(ctx, "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(ctx, "key", 2)
Expect(move.Err()).NotTo(HaveOccurred())
Expect(move.Val()).To(Equal(false))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
move = client.Move(ctx, "key", 2)
Expect(move.Err()).NotTo(HaveOccurred())
Expect(move.Val()).To(Equal(true))
get := client.Get(ctx, "key")
Expect(get.Err()).To(Equal(redis.Nil))
Expect(get.Val()).To(Equal(""))
pipe := client.Pipeline()
pipe.Select(ctx, 2)
get = pipe.Get(ctx, "key")
pipe.FlushDB(ctx)
_, err := pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
})
It("should Object", func() {
start := time.Now()
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
refCount := client.ObjectRefCount(ctx, "key")
Expect(refCount.Err()).NotTo(HaveOccurred())
Expect(refCount.Val()).To(Equal(int64(1)))
err := client.ObjectEncoding(ctx, "key").Err()
Expect(err).NotTo(HaveOccurred())
idleTime := client.ObjectIdleTime(ctx, "key")
Expect(idleTime.Err()).NotTo(HaveOccurred())
// Redis returned milliseconds/1000, which may cause ObjectIdleTime to be at a critical value,
// should be +1s to deal with the critical value problem.
// if too much time (>1s) is used during command execution, it may also cause the test to fail.
// so the ObjectIdleTime result should be <=now-start+1s
// link: https://github.com/redis/redis/blob/5b48d900498c85bbf4772c1d466c214439888115/src/object.c#L1265-L1272
Expect(idleTime.Val()).To(BeNumerically("<=", time.Now().Sub(start)+time.Second))
})
It("should Persist", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expire := client.Expire(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(10 * time.Second))
persist := client.Persist(ctx, "key")
Expect(persist.Err()).NotTo(HaveOccurred())
Expect(persist.Val()).To(Equal(true))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val() < 0).To(Equal(true))
})
It("should PExpire", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 900 * time.Millisecond
pexpire := client.PExpire(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Second))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should PExpireAt", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 900 * time.Millisecond
pexpireat := client.PExpireAt(ctx, "key", time.Now().Add(expiration))
Expect(pexpireat.Err()).NotTo(HaveOccurred())
Expect(pexpireat.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Second))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should PTTL", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := time.Second
expire := client.Expire(ctx, "key", expiration)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should RandomKey", func() {
randomKey := client.RandomKey(ctx)
Expect(randomKey.Err()).To(Equal(redis.Nil))
Expect(randomKey.Val()).To(Equal(""))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
randomKey = client.RandomKey(ctx)
Expect(randomKey.Err()).NotTo(HaveOccurred())
Expect(randomKey.Val()).To(Equal("key"))
})
It("should Rename", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
status := client.Rename(ctx, "key", "key1")
Expect(status.Err()).NotTo(HaveOccurred())
Expect(status.Val()).To(Equal("OK"))
get := client.Get(ctx, "key1")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
})
It("should RenameNX", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
renameNX := client.RenameNX(ctx, "key", "key1")
Expect(renameNX.Err()).NotTo(HaveOccurred())
Expect(renameNX.Val()).To(Equal(true))
get := client.Get(ctx, "key1")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
})
It("should Restore", func() {
err := client.Set(ctx, "key", "hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
dump := client.Dump(ctx, "key")
Expect(dump.Err()).NotTo(HaveOccurred())
err = client.Del(ctx, "key").Err()
Expect(err).NotTo(HaveOccurred())
restore, err := client.Restore(ctx, "key", 0, dump.Val()).Result()
Expect(err).NotTo(HaveOccurred())
Expect(restore).To(Equal("OK"))
type_, err := client.Type(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(type_).To(Equal("string"))
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
})
It("should RestoreReplace", func() {
err := client.Set(ctx, "key", "hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
dump := client.Dump(ctx, "key")
Expect(dump.Err()).NotTo(HaveOccurred())
restore, err := client.RestoreReplace(ctx, "key", 0, dump.Val()).Result()
Expect(err).NotTo(HaveOccurred())
Expect(restore).To(Equal("OK"))
type_, err := client.Type(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(type_).To(Equal("string"))
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
})
It("should Sort", func() {
size, err := client.LPush(ctx, "list", "1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(1)))
size, err = client.LPush(ctx, "list", "3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(2)))
size, err = client.LPush(ctx, "list", "2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
els, err := client.Sort(ctx, "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(ctx, "list", "1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(1)))
size, err = client.LPush(ctx, "list", "3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(2)))
size, err = client.LPush(ctx, "list", "2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
err = client.Set(ctx, "object_2", "value2", 0).Err()
Expect(err).NotTo(HaveOccurred())
{
els, err := client.Sort(ctx, "list", &redis.Sort{
Get: []string{"object_*"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]string{"", "value2", ""}))
}
{
els, err := client.SortInterfaces(ctx, "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(ctx, "list", "1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(1)))
size, err = client.LPush(ctx, "list", "3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(2)))
size, err = client.LPush(ctx, "list", "2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(size).To(Equal(int64(3)))
n, err := client.SortStore(ctx, "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(ctx, "list2", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(els).To(Equal([]string{"1", "2"}))
})
It("should Touch", func() {
set1 := client.Set(ctx, "touch1", "hello", 0)
Expect(set1.Err()).NotTo(HaveOccurred())
Expect(set1.Val()).To(Equal("OK"))
set2 := client.Set(ctx, "touch2", "hello", 0)
Expect(set2.Err()).NotTo(HaveOccurred())
Expect(set2.Val()).To(Equal("OK"))
touch := client.Touch(ctx, "touch1", "touch2", "touch3")
Expect(touch.Err()).NotTo(HaveOccurred())
Expect(touch.Val()).To(Equal(int64(2)))
})
It("should TTL", func() {
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val() < 0).To(Equal(true))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expire := client.Expire(ctx, "key", 60*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(60 * time.Second))
})
It("should Type", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
type_ := client.Type(ctx, "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(ctx, fmt.Sprintf("key%d", i), "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
}
keys, cursor, err := client.Scan(ctx, 0, "", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).NotTo(BeEmpty())
Expect(cursor).NotTo(BeZero())
})
It("should ScanType", func() {
for i := 0; i < 1000; i++ {
set := client.Set(ctx, fmt.Sprintf("key%d", i), "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
}
keys, cursor, err := client.ScanType(ctx, 0, "", 0, "string").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(ctx, "myset", fmt.Sprintf("member%d", i))
Expect(sadd.Err()).NotTo(HaveOccurred())
}
keys, cursor, err := client.SScan(ctx, "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(ctx, "myhash", fmt.Sprintf("key%d", i), "hello")
Expect(sadd.Err()).NotTo(HaveOccurred())
}
keys, cursor, err := client.HScan(ctx, "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(ctx, "myset", &redis.Z{
Score: float64(i),
Member: fmt.Sprintf("member%d", i),
}).Err()
Expect(err).NotTo(HaveOccurred())
}
keys, cursor, err := client.ZScan(ctx, "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(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0)))
append := client.Append(ctx, "key", "Hello")
Expect(append.Err()).NotTo(HaveOccurred())
Expect(append.Val()).To(Equal(int64(5)))
append = client.Append(ctx, "key", " World")
Expect(append.Err()).NotTo(HaveOccurred())
Expect(append.Val()).To(Equal(int64(11)))
get := client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("Hello World"))
})
It("should BitCount", func() {
set := client.Set(ctx, "key", "foobar", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
bitCount := client.BitCount(ctx, "key", nil)
Expect(bitCount.Err()).NotTo(HaveOccurred())
Expect(bitCount.Val()).To(Equal(int64(26)))
bitCount = client.BitCount(ctx, "key", &redis.BitCount{
Start: 0,
End: 0,
})
Expect(bitCount.Err()).NotTo(HaveOccurred())
Expect(bitCount.Val()).To(Equal(int64(4)))
bitCount = client.BitCount(ctx, "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(ctx, "key1", "1", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
set = client.Set(ctx, "key2", "0", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
bitOpAnd := client.BitOpAnd(ctx, "dest", "key1", "key2")
Expect(bitOpAnd.Err()).NotTo(HaveOccurred())
Expect(bitOpAnd.Val()).To(Equal(int64(1)))
get := client.Get(ctx, "dest")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("0"))
})
It("should BitOpOr", func() {
set := client.Set(ctx, "key1", "1", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
set = client.Set(ctx, "key2", "0", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
bitOpOr := client.BitOpOr(ctx, "dest", "key1", "key2")
Expect(bitOpOr.Err()).NotTo(HaveOccurred())
Expect(bitOpOr.Val()).To(Equal(int64(1)))
get := client.Get(ctx, "dest")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("1"))
})
It("should BitOpXor", func() {
set := client.Set(ctx, "key1", "\xff", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
set = client.Set(ctx, "key2", "\x0f", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
bitOpXor := client.BitOpXor(ctx, "dest", "key1", "key2")
Expect(bitOpXor.Err()).NotTo(HaveOccurred())
Expect(bitOpXor.Val()).To(Equal(int64(1)))
get := client.Get(ctx, "dest")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("\xf0"))
})
It("should BitOpNot", func() {
set := client.Set(ctx, "key1", "\x00", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
bitOpNot := client.BitOpNot(ctx, "dest", "key1")
Expect(bitOpNot.Err()).NotTo(HaveOccurred())
Expect(bitOpNot.Val()).To(Equal(int64(1)))
get := client.Get(ctx, "dest")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("\xff"))
})
It("should BitPos", func() {
err := client.Set(ctx, "mykey", "\xff\xf0\x00", 0).Err()
Expect(err).NotTo(HaveOccurred())
pos, err := client.BitPos(ctx, "mykey", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(12)))
pos, err = client.BitPos(ctx, "mykey", 1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(0)))
pos, err = client.BitPos(ctx, "mykey", 0, 2).Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(16)))
pos, err = client.BitPos(ctx, "mykey", 1, 2).Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(-1)))
pos, err = client.BitPos(ctx, "mykey", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(16)))
pos, err = client.BitPos(ctx, "mykey", 1, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(-1)))
pos, err = client.BitPos(ctx, "mykey", 0, 2, 1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(-1)))
pos, err = client.BitPos(ctx, "mykey", 0, 0, -3).Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(-1)))
pos, err = client.BitPos(ctx, "mykey", 0, 0, 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(pos).To(Equal(int64(-1)))
})
It("should BitField", func() {
nn, err := client.BitField(ctx, "mykey", "INCRBY", "i5", 100, 1, "GET", "u4", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(nn).To(Equal([]int64{1, 0}))
})
It("should Decr", func() {
set := client.Set(ctx, "key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
decr := client.Decr(ctx, "key")
Expect(decr.Err()).NotTo(HaveOccurred())
Expect(decr.Val()).To(Equal(int64(9)))
set = client.Set(ctx, "key", "234293482390480948029348230948", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
decr = client.Decr(ctx, "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(ctx, "key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
decrBy := client.DecrBy(ctx, "key", 5)
Expect(decrBy.Err()).NotTo(HaveOccurred())
Expect(decrBy.Val()).To(Equal(int64(5)))
})
It("should Get", func() {
get := client.Get(ctx, "_")
Expect(get.Err()).To(Equal(redis.Nil))
Expect(get.Val()).To(Equal(""))
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
get = client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
})
It("should GetBit", func() {
setBit := client.SetBit(ctx, "key", 7, 1)
Expect(setBit.Err()).NotTo(HaveOccurred())
Expect(setBit.Val()).To(Equal(int64(0)))
getBit := client.GetBit(ctx, "key", 0)
Expect(getBit.Err()).NotTo(HaveOccurred())
Expect(getBit.Val()).To(Equal(int64(0)))
getBit = client.GetBit(ctx, "key", 7)
Expect(getBit.Err()).NotTo(HaveOccurred())
Expect(getBit.Val()).To(Equal(int64(1)))
getBit = client.GetBit(ctx, "key", 100)
Expect(getBit.Err()).NotTo(HaveOccurred())
Expect(getBit.Val()).To(Equal(int64(0)))
})
It("should GetRange", func() {
set := client.Set(ctx, "key", "This is a string", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
getRange := client.GetRange(ctx, "key", 0, 3)
Expect(getRange.Err()).NotTo(HaveOccurred())
Expect(getRange.Val()).To(Equal("This"))
getRange = client.GetRange(ctx, "key", -3, -1)
Expect(getRange.Err()).NotTo(HaveOccurred())
Expect(getRange.Val()).To(Equal("ing"))
getRange = client.GetRange(ctx, "key", 0, -1)
Expect(getRange.Err()).NotTo(HaveOccurred())
Expect(getRange.Val()).To(Equal("This is a string"))
getRange = client.GetRange(ctx, "key", 10, 100)
Expect(getRange.Err()).NotTo(HaveOccurred())
Expect(getRange.Val()).To(Equal("string"))
})
It("should GetSet", func() {
incr := client.Incr(ctx, "key")
Expect(incr.Err()).NotTo(HaveOccurred())
Expect(incr.Val()).To(Equal(int64(1)))
getSet := client.GetSet(ctx, "key", "0")
Expect(getSet.Err()).NotTo(HaveOccurred())
Expect(getSet.Val()).To(Equal("1"))
get := client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("0"))
})
It("should GetEX", func() {
set := client.Set(ctx, "key", "value", 100*time.Second)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(BeNumerically("~", 100*time.Second, 3*time.Second))
getEX := client.GetEx(ctx, "key", 200*time.Second)
Expect(getEX.Err()).NotTo(HaveOccurred())
Expect(getEX.Val()).To(Equal("value"))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(BeNumerically("~", 200*time.Second, 3*time.Second))
})
It("should GetDel", func() {
set := client.Set(ctx, "key", "value", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
getDel := client.GetDel(ctx, "key")
Expect(getDel.Err()).NotTo(HaveOccurred())
Expect(getDel.Val()).To(Equal("value"))
get := client.Get(ctx, "key")
Expect(get.Err()).To(Equal(redis.Nil))
})
It("should Incr", func() {
set := client.Set(ctx, "key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
incr := client.Incr(ctx, "key")
Expect(incr.Err()).NotTo(HaveOccurred())
Expect(incr.Val()).To(Equal(int64(11)))
get := client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("11"))
})
It("should IncrBy", func() {
set := client.Set(ctx, "key", "10", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
incrBy := client.IncrBy(ctx, "key", 5)
Expect(incrBy.Err()).NotTo(HaveOccurred())
Expect(incrBy.Val()).To(Equal(int64(15)))
})
It("should IncrByFloat", func() {
set := client.Set(ctx, "key", "10.50", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
incrByFloat := client.IncrByFloat(ctx, "key", 0.1)
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
Expect(incrByFloat.Val()).To(Equal(10.6))
set = client.Set(ctx, "key", "5.0e3", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
incrByFloat = client.IncrByFloat(ctx, "key", 2.0e2)
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
Expect(incrByFloat.Val()).To(Equal(float64(5200)))
})
It("should IncrByFloatOverflow", func() {
incrByFloat := client.IncrByFloat(ctx, "key", 996945661)
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
Expect(incrByFloat.Val()).To(Equal(float64(996945661)))
})
It("should MSetMGet", func() {
mSet := client.MSet(ctx, "key1", "hello1", "key2", "hello2")
Expect(mSet.Err()).NotTo(HaveOccurred())
Expect(mSet.Val()).To(Equal("OK"))
mGet := client.MGet(ctx, "key1", "key2", "_")
Expect(mGet.Err()).NotTo(HaveOccurred())
Expect(mGet.Val()).To(Equal([]interface{}{"hello1", "hello2", nil}))
})
It("should scan Mget", func() {
err := client.MSet(ctx, "key1", "hello1", "key2", 123).Err()
Expect(err).NotTo(HaveOccurred())
res := client.MGet(ctx, "key1", "key2", "_")
Expect(res.Err()).NotTo(HaveOccurred())
type data struct {
Key1 string `redis:"key1"`
Key2 int `redis:"key2"`
}
var d data
Expect(res.Scan(&d)).NotTo(HaveOccurred())
Expect(d).To(Equal(data{Key1: "hello1", Key2: 123}))
})
It("should MSetNX", func() {
mSetNX := client.MSetNX(ctx, "key1", "hello1", "key2", "hello2")
Expect(mSetNX.Err()).NotTo(HaveOccurred())
Expect(mSetNX.Val()).To(Equal(true))
mSetNX = client.MSetNX(ctx, "key2", "hello1", "key3", "hello2")
Expect(mSetNX.Err()).NotTo(HaveOccurred())
Expect(mSetNX.Val()).To(Equal(false))
})
It("should SetWithArgs with TTL", func() {
args := redis.SetArgs{
TTL: 500 * time.Millisecond,
}
err := client.SetArgs(ctx, "key", "hello", args).Err()
Expect(err).NotTo(HaveOccurred())
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
Eventually(func() error {
return client.Get(ctx, "key").Err()
}, "2s", "100ms").Should(Equal(redis.Nil))
})
It("should SetWithArgs with expiration date", func() {
expireAt := time.Now().AddDate(1, 1, 1)
args := redis.SetArgs{
ExpireAt: expireAt,
}
err := client.SetArgs(ctx, "key", "hello", args).Err()
Expect(err).NotTo(HaveOccurred())
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
// check the key has an expiration date
// (so a TTL value different of -1)
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).ToNot(Equal(-1))
})
It("should SetWithArgs with negative expiration date", func() {
args := redis.SetArgs{
ExpireAt: time.Now().AddDate(-3, 1, 1),
}
// redis accepts a timestamp less than the current date
// but returns nil when trying to get the key
err := client.SetArgs(ctx, "key", "hello", args).Err()
Expect(err).NotTo(HaveOccurred())
val, err := client.Get(ctx, "key").Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(Equal(""))
})
It("should SetWithArgs with keepttl", func() {
// Set with ttl
argsWithTTL := redis.SetArgs{
TTL: 5 * time.Second,
}
set := client.SetArgs(ctx, "key", "hello", argsWithTTL)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Result()).To(Equal("OK"))
// Set with keepttl
argsWithKeepTTL := redis.SetArgs{
KeepTTL: true,
}
set = client.SetArgs(ctx, "key", "hello", argsWithKeepTTL)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Result()).To(Equal("OK"))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
// set keepttl will Retain the ttl associated with the key
Expect(ttl.Val().Nanoseconds()).NotTo(Equal(-1))
})
It("should SetWithArgs with NX mode and key exists", func() {
err := client.Set(ctx, "key", "hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
args := redis.SetArgs{
Mode: "nx",
}
val, err := client.SetArgs(ctx, "key", "hello", args).Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(Equal(""))
})
It("should SetWithArgs with NX mode and key does not exist", func() {
args := redis.SetArgs{
Mode: "nx",
}
val, err := client.SetArgs(ctx, "key", "hello", args).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("OK"))
})
It("should SetWithArgs with NX mode and GET option", func() {
args := redis.SetArgs{
Mode: "nx",
Get: true,
}
val, err := client.SetArgs(ctx, "key", "hello", args).Result()
Expect(err).To(Equal(proto.RedisError("ERR syntax error")))
Expect(val).To(Equal(""))
})
It("should SetWithArgs with expiration, NX mode, and key does not exist", func() {
args := redis.SetArgs{
TTL: 500 * time.Millisecond,
Mode: "nx",
}
val, err := client.SetArgs(ctx, "key", "hello", args).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("OK"))
Eventually(func() error {
return client.Get(ctx, "key").Err()
}, "1s", "100ms").Should(Equal(redis.Nil))
})
It("should SetWithArgs with expiration, NX mode, and key exists", func() {
e := client.Set(ctx, "key", "hello", 0)
Expect(e.Err()).NotTo(HaveOccurred())
args := redis.SetArgs{
TTL: 500 * time.Millisecond,
Mode: "nx",
}
val, err := client.SetArgs(ctx, "key", "world", args).Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(Equal(""))
})
It("should SetWithArgs with expiration, NX mode, and GET option", func() {
args := redis.SetArgs{
TTL: 500 * time.Millisecond,
Mode: "nx",
Get: true,
}
val, err := client.SetArgs(ctx, "key", "hello", args).Result()
Expect(err).To(Equal(proto.RedisError("ERR syntax error")))
Expect(val).To(Equal(""))
})
It("should SetWithArgs with XX mode and key does not exist", func() {
args := redis.SetArgs{
Mode: "xx",
}
val, err := client.SetArgs(ctx, "key", "world", args).Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(Equal(""))
})
It("should SetWithArgs with XX mode and key exists", func() {
e := client.Set(ctx, "key", "hello", 0).Err()
Expect(e).NotTo(HaveOccurred())
args := redis.SetArgs{
Mode: "xx",
}
val, err := client.SetArgs(ctx, "key", "world", args).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("OK"))
})
It("should SetWithArgs with XX mode and GET option, and key exists", func() {
e := client.Set(ctx, "key", "hello", 0).Err()
Expect(e).NotTo(HaveOccurred())
args := redis.SetArgs{
Mode: "xx",
Get: true,
}
val, err := client.SetArgs(ctx, "key", "world", args).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
})
It("should SetWithArgs with XX mode and GET option, and key does not exist", func() {
args := redis.SetArgs{
Mode: "xx",
Get: true,
}
val, err := client.SetArgs(ctx, "key", "world", args).Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(Equal(""))
})
It("should SetWithArgs with expiration, XX mode, GET option, and key does not exist", func() {
args := redis.SetArgs{
TTL: 500 * time.Millisecond,
Mode: "xx",
Get: true,
}
val, err := client.SetArgs(ctx, "key", "world", args).Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(Equal(""))
})
It("should SetWithArgs with expiration, XX mode, GET option, and key exists", func() {
e := client.Set(ctx, "key", "hello", 0)
Expect(e.Err()).NotTo(HaveOccurred())
args := redis.SetArgs{
TTL: 500 * time.Millisecond,
Mode: "xx",
Get: true,
}
val, err := client.SetArgs(ctx, "key", "world", args).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
Eventually(func() error {
return client.Get(ctx, "key").Err()
}, "1s", "100ms").Should(Equal(redis.Nil))
})
It("should SetWithArgs with Get and key does not exist yet", func() {
args := redis.SetArgs{
Get: true,
}
val, err := client.SetArgs(ctx, "key", "hello", args).Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(Equal(""))
})
It("should SetWithArgs with Get and key exists", func() {
e := client.Set(ctx, "key", "hello", 0)
Expect(e.Err()).NotTo(HaveOccurred())
args := redis.SetArgs{
Get: true,
}
val, err := client.SetArgs(ctx, "key", "world", args).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
})
It("should Pipelined SetArgs with Get and key exists", func() {
e := client.Set(ctx, "key", "hello", 0)
Expect(e.Err()).NotTo(HaveOccurred())
args := redis.SetArgs{
Get: true,
}
pipe := client.Pipeline()
setArgs := pipe.SetArgs(ctx, "key", "world", args)
_, err := pipe.Exec(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(setArgs.Err()).NotTo(HaveOccurred())
Expect(setArgs.Val()).To(Equal("hello"))
})
It("should Set with expiration", func() {
err := client.Set(ctx, "key", "hello", 100*time.Millisecond).Err()
Expect(err).NotTo(HaveOccurred())
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
Eventually(func() error {
return client.Get(ctx, "key").Err()
}, "1s", "100ms").Should(Equal(redis.Nil))
})
It("should Set with keepttl", func() {
// set with ttl
set := client.Set(ctx, "key", "hello", 5*time.Second)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
// set with keepttl
set = client.Set(ctx, "key", "hello1", redis.KeepTTL)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
// set keepttl will Retain the ttl associated with the key
Expect(ttl.Val().Nanoseconds()).NotTo(Equal(-1))
})
It("should SetGet", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
get := client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
})
It("should SetEX", func() {
err := client.SetEX(ctx, "key", "hello", 1*time.Second).Err()
Expect(err).NotTo(HaveOccurred())
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
Eventually(func() error {
return client.Get(ctx, "foo").Err()
}, "2s", "100ms").Should(Equal(redis.Nil))
})
It("should SetNX", func() {
setNX := client.SetNX(ctx, "key", "hello", 0)
Expect(setNX.Err()).NotTo(HaveOccurred())
Expect(setNX.Val()).To(Equal(true))
setNX = client.SetNX(ctx, "key", "hello2", 0)
Expect(setNX.Err()).NotTo(HaveOccurred())
Expect(setNX.Val()).To(Equal(false))
get := client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello"))
})
It("should SetNX with expiration", func() {
isSet, err := client.SetNX(ctx, "key", "hello", time.Second).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(true))
isSet, err = client.SetNX(ctx, "key", "hello2", time.Second).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(false))
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello"))
})
It("should SetNX with keepttl", func() {
isSet, err := client.SetNX(ctx, "key", "hello1", redis.KeepTTL).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val().Nanoseconds()).To(Equal(int64(-1)))
})
It("should SetXX", func() {
isSet, err := client.SetXX(ctx, "key", "hello2", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(false))
err = client.Set(ctx, "key", "hello", 0).Err()
Expect(err).NotTo(HaveOccurred())
isSet, err = client.SetXX(ctx, "key", "hello2", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(true))
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello2"))
})
It("should SetXX with expiration", func() {
isSet, err := client.SetXX(ctx, "key", "hello2", time.Second).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(false))
err = client.Set(ctx, "key", "hello", time.Second).Err()
Expect(err).NotTo(HaveOccurred())
isSet, err = client.SetXX(ctx, "key", "hello2", time.Second).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(true))
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello2"))
})
It("should SetXX with keepttl", func() {
isSet, err := client.SetXX(ctx, "key", "hello2", time.Second).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(false))
err = client.Set(ctx, "key", "hello", time.Second).Err()
Expect(err).NotTo(HaveOccurred())
isSet, err = client.SetXX(ctx, "key", "hello2", 5*time.Second).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(true))
isSet, err = client.SetXX(ctx, "key", "hello3", redis.KeepTTL).Result()
Expect(err).NotTo(HaveOccurred())
Expect(isSet).To(Equal(true))
val, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("hello3"))
// set keepttl will Retain the ttl associated with the key
ttl, err := client.TTL(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(ttl).NotTo(Equal(-1))
})
It("should SetRange", func() {
set := client.Set(ctx, "key", "Hello World", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
range_ := client.SetRange(ctx, "key", 6, "Redis")
Expect(range_.Err()).NotTo(HaveOccurred())
Expect(range_.Val()).To(Equal(int64(11)))
get := client.Get(ctx, "key")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("Hello Redis"))
})
It("should StrLen", func() {
set := client.Set(ctx, "key", "hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
strLen := client.StrLen(ctx, "key")
Expect(strLen.Err()).NotTo(HaveOccurred())
Expect(strLen.Val()).To(Equal(int64(5)))
strLen = client.StrLen(ctx, "_")
Expect(strLen.Err()).NotTo(HaveOccurred())
Expect(strLen.Val()).To(Equal(int64(0)))
})
})
Describe("hashes", func() {
It("should HDel", func() {
hSet := client.HSet(ctx, "hash", "key", "hello")
Expect(hSet.Err()).NotTo(HaveOccurred())
hDel := client.HDel(ctx, "hash", "key")
Expect(hDel.Err()).NotTo(HaveOccurred())
Expect(hDel.Val()).To(Equal(int64(1)))
hDel = client.HDel(ctx, "hash", "key")
Expect(hDel.Err()).NotTo(HaveOccurred())
Expect(hDel.Val()).To(Equal(int64(0)))
})
It("should HExists", func() {
hSet := client.HSet(ctx, "hash", "key", "hello")
Expect(hSet.Err()).NotTo(HaveOccurred())
hExists := client.HExists(ctx, "hash", "key")
Expect(hExists.Err()).NotTo(HaveOccurred())
Expect(hExists.Val()).To(Equal(true))
hExists = client.HExists(ctx, "hash", "key1")
Expect(hExists.Err()).NotTo(HaveOccurred())
Expect(hExists.Val()).To(Equal(false))
})
It("should HGet", func() {
hSet := client.HSet(ctx, "hash", "key", "hello")
Expect(hSet.Err()).NotTo(HaveOccurred())
hGet := client.HGet(ctx, "hash", "key")
Expect(hGet.Err()).NotTo(HaveOccurred())
Expect(hGet.Val()).To(Equal("hello"))
hGet = client.HGet(ctx, "hash", "key1")
Expect(hGet.Err()).To(Equal(redis.Nil))
Expect(hGet.Val()).To(Equal(""))
})
It("should HGetAll", func() {
err := client.HSet(ctx, "hash", "key1", "hello1").Err()
Expect(err).NotTo(HaveOccurred())
err = client.HSet(ctx, "hash", "key2", "hello2").Err()
Expect(err).NotTo(HaveOccurred())
m, err := client.HGetAll(ctx, "hash").Result()
Expect(err).NotTo(HaveOccurred())
Expect(m).To(Equal(map[string]string{"key1": "hello1", "key2": "hello2"}))
})
It("should scan", func() {
err := client.HMSet(ctx, "hash", "key1", "hello1", "key2", 123).Err()
Expect(err).NotTo(HaveOccurred())
res := client.HGetAll(ctx, "hash")
Expect(res.Err()).NotTo(HaveOccurred())
type data struct {
Key1 string `redis:"key1"`
Key2 int `redis:"key2"`
}
var d data
Expect(res.Scan(&d)).NotTo(HaveOccurred())
Expect(d).To(Equal(data{Key1: "hello1", Key2: 123}))
})
It("should HIncrBy", func() {
hSet := client.HSet(ctx, "hash", "key", "5")
Expect(hSet.Err()).NotTo(HaveOccurred())
hIncrBy := client.HIncrBy(ctx, "hash", "key", 1)
Expect(hIncrBy.Err()).NotTo(HaveOccurred())
Expect(hIncrBy.Val()).To(Equal(int64(6)))
hIncrBy = client.HIncrBy(ctx, "hash", "key", -1)
Expect(hIncrBy.Err()).NotTo(HaveOccurred())
Expect(hIncrBy.Val()).To(Equal(int64(5)))
hIncrBy = client.HIncrBy(ctx, "hash", "key", -10)
Expect(hIncrBy.Err()).NotTo(HaveOccurred())
Expect(hIncrBy.Val()).To(Equal(int64(-5)))
})
It("should HIncrByFloat", func() {
hSet := client.HSet(ctx, "hash", "field", "10.50")
Expect(hSet.Err()).NotTo(HaveOccurred())
Expect(hSet.Val()).To(Equal(int64(1)))
hIncrByFloat := client.HIncrByFloat(ctx, "hash", "field", 0.1)
Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
Expect(hIncrByFloat.Val()).To(Equal(10.6))
hSet = client.HSet(ctx, "hash", "field", "5.0e3")
Expect(hSet.Err()).NotTo(HaveOccurred())
Expect(hSet.Val()).To(Equal(int64(0)))
hIncrByFloat = client.HIncrByFloat(ctx, "hash", "field", 2.0e2)
Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
Expect(hIncrByFloat.Val()).To(Equal(float64(5200)))
})
It("should HKeys", func() {
hkeys := client.HKeys(ctx, "hash")
Expect(hkeys.Err()).NotTo(HaveOccurred())
Expect(hkeys.Val()).To(Equal([]string{}))
hset := client.HSet(ctx, "hash", "key1", "hello1")
Expect(hset.Err()).NotTo(HaveOccurred())
hset = client.HSet(ctx, "hash", "key2", "hello2")
Expect(hset.Err()).NotTo(HaveOccurred())
hkeys = client.HKeys(ctx, "hash")
Expect(hkeys.Err()).NotTo(HaveOccurred())
Expect(hkeys.Val()).To(Equal([]string{"key1", "key2"}))
})
It("should HLen", func() {
hSet := client.HSet(ctx, "hash", "key1", "hello1")
Expect(hSet.Err()).NotTo(HaveOccurred())
hSet = client.HSet(ctx, "hash", "key2", "hello2")
Expect(hSet.Err()).NotTo(HaveOccurred())
hLen := client.HLen(ctx, "hash")
Expect(hLen.Err()).NotTo(HaveOccurred())
Expect(hLen.Val()).To(Equal(int64(2)))
})
It("should HMGet", func() {
err := client.HSet(ctx, "hash", "key1", "hello1", "key2", "hello2").Err()
Expect(err).NotTo(HaveOccurred())
vals, err := client.HMGet(ctx, "hash", "key1", "key2", "_").Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]interface{}{"hello1", "hello2", nil}))
})
It("should HSet", func() {
ok, err := client.HSet(ctx, "hash", map[string]interface{}{
"key1": "hello1",
"key2": "hello2",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(Equal(int64(2)))
v, err := client.HGet(ctx, "hash", "key1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal("hello1"))
v, err = client.HGet(ctx, "hash", "key2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal("hello2"))
keys, err := client.HKeys(ctx, "hash").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).To(ConsistOf([]string{"key1", "key2"}))
})
It("should HSet", func() {
hSet := client.HSet(ctx, "hash", "key", "hello")
Expect(hSet.Err()).NotTo(HaveOccurred())
Expect(hSet.Val()).To(Equal(int64(1)))
hGet := client.HGet(ctx, "hash", "key")
Expect(hGet.Err()).NotTo(HaveOccurred())
Expect(hGet.Val()).To(Equal("hello"))
})
It("should HSetNX", func() {
hSetNX := client.HSetNX(ctx, "hash", "key", "hello")
Expect(hSetNX.Err()).NotTo(HaveOccurred())
Expect(hSetNX.Val()).To(Equal(true))
hSetNX = client.HSetNX(ctx, "hash", "key", "hello")
Expect(hSetNX.Err()).NotTo(HaveOccurred())
Expect(hSetNX.Val()).To(Equal(false))
hGet := client.HGet(ctx, "hash", "key")
Expect(hGet.Err()).NotTo(HaveOccurred())
Expect(hGet.Val()).To(Equal("hello"))
})
It("should HVals", func() {
err := client.HSet(ctx, "hash", "key1", "hello1").Err()
Expect(err).NotTo(HaveOccurred())
err = client.HSet(ctx, "hash", "key2", "hello2").Err()
Expect(err).NotTo(HaveOccurred())
v, err := client.HVals(ctx, "hash").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal([]string{"hello1", "hello2"}))
var slice []string
err = client.HVals(ctx, "hash").ScanSlice(&slice)
Expect(err).NotTo(HaveOccurred())
Expect(slice).To(Equal([]string{"hello1", "hello2"}))
})
It("should HRandField", func() {
err := client.HSet(ctx, "hash", "key1", "hello1").Err()
Expect(err).NotTo(HaveOccurred())
err = client.HSet(ctx, "hash", "key2", "hello2").Err()
Expect(err).NotTo(HaveOccurred())
v := client.HRandField(ctx, "hash", 1, false)
Expect(v.Err()).NotTo(HaveOccurred())
Expect(v.Val()).To(Or(Equal([]string{"key1"}), Equal([]string{"key2"})))
v = client.HRandField(ctx, "hash", 0, false)
Expect(v.Err()).NotTo(HaveOccurred())
Expect(v.Val()).To(HaveLen(0))
var slice []string
err = client.HRandField(ctx, "hash", 1, true).ScanSlice(&slice)
Expect(err).NotTo(HaveOccurred())
Expect(slice).To(Or(Equal([]string{"key1", "hello1"}), Equal([]string{"key2", "hello2"})))
})
})
Describe("hyperloglog", func() {
It("should PFMerge", func() {
pfAdd := client.PFAdd(ctx, "hll1", "1", "2", "3", "4", "5")
Expect(pfAdd.Err()).NotTo(HaveOccurred())
pfCount := client.PFCount(ctx, "hll1")
Expect(pfCount.Err()).NotTo(HaveOccurred())
Expect(pfCount.Val()).To(Equal(int64(5)))
pfAdd = client.PFAdd(ctx, "hll2", "a", "b", "c", "d", "e")
Expect(pfAdd.Err()).NotTo(HaveOccurred())
pfMerge := client.PFMerge(ctx, "hllMerged", "hll1", "hll2")
Expect(pfMerge.Err()).NotTo(HaveOccurred())
pfCount = client.PFCount(ctx, "hllMerged")
Expect(pfCount.Err()).NotTo(HaveOccurred())
Expect(pfCount.Val()).To(Equal(int64(10)))
pfCount = client.PFCount(ctx, "hll1", "hll2")
Expect(pfCount.Err()).NotTo(HaveOccurred())
Expect(pfCount.Val()).To(Equal(int64(10)))
})
})
Describe("lists", func() {
It("should BLPop", func() {
rPush := client.RPush(ctx, "list1", "a", "b", "c")
Expect(rPush.Err()).NotTo(HaveOccurred())
bLPop := client.BLPop(ctx, 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(ctx, 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(ctx, "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(ctx, time.Second, "list1").Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(BeNil())
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
stats := client.PoolStats()
Expect(stats.Hits).To(Equal(uint32(2)))
Expect(stats.Misses).To(Equal(uint32(1)))
Expect(stats.Timeouts).To(Equal(uint32(0)))
})
It("should BRPop", func() {
rPush := client.RPush(ctx, "list1", "a", "b", "c")
Expect(rPush.Err()).NotTo(HaveOccurred())
bRPop := client.BRPop(ctx, 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(ctx, 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(ctx, "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(ctx, "list1", "list2", time.Second).Result()
Expect(err).To(Equal(redis.Nil))
err = client.RPush(ctx, "list1", "a", "b", "c").Err()
Expect(err).NotTo(HaveOccurred())
v, err := client.BRPopLPush(ctx, "list1", "list2", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal("c"))
})
It("should LIndex", func() {
lPush := client.LPush(ctx, "list", "World")
Expect(lPush.Err()).NotTo(HaveOccurred())
lPush = client.LPush(ctx, "list", "Hello")
Expect(lPush.Err()).NotTo(HaveOccurred())
lIndex := client.LIndex(ctx, "list", 0)
Expect(lIndex.Err()).NotTo(HaveOccurred())
Expect(lIndex.Val()).To(Equal("Hello"))
lIndex = client.LIndex(ctx, "list", -1)
Expect(lIndex.Err()).NotTo(HaveOccurred())
Expect(lIndex.Val()).To(Equal("World"))
lIndex = client.LIndex(ctx, "list", 3)
Expect(lIndex.Err()).To(Equal(redis.Nil))
Expect(lIndex.Val()).To(Equal(""))
})
It("should LInsert", func() {
rPush := client.RPush(ctx, "list", "Hello")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "World")
Expect(rPush.Err()).NotTo(HaveOccurred())
lInsert := client.LInsert(ctx, "list", "BEFORE", "World", "There")
Expect(lInsert.Err()).NotTo(HaveOccurred())
Expect(lInsert.Val()).To(Equal(int64(3)))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"Hello", "There", "World"}))
})
It("should LLen", func() {
lPush := client.LPush(ctx, "list", "World")
Expect(lPush.Err()).NotTo(HaveOccurred())
lPush = client.LPush(ctx, "list", "Hello")
Expect(lPush.Err()).NotTo(HaveOccurred())
lLen := client.LLen(ctx, "list")
Expect(lLen.Err()).NotTo(HaveOccurred())
Expect(lLen.Val()).To(Equal(int64(2)))
})
It("should LPop", func() {
rPush := client.RPush(ctx, "list", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "two")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "three")
Expect(rPush.Err()).NotTo(HaveOccurred())
lPop := client.LPop(ctx, "list")
Expect(lPop.Err()).NotTo(HaveOccurred())
Expect(lPop.Val()).To(Equal("one"))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"two", "three"}))
})
It("should LPopCount", func() {
rPush := client.RPush(ctx, "list", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "two")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "three")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "four")
Expect(rPush.Err()).NotTo(HaveOccurred())
lPopCount := client.LPopCount(ctx, "list", 2)
Expect(lPopCount.Err()).NotTo(HaveOccurred())
Expect(lPopCount.Val()).To(Equal([]string{"one", "two"}))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"three", "four"}))
})
It("should LPos", func() {
rPush := client.RPush(ctx, "list", "a")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "b")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "c")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "b")
Expect(rPush.Err()).NotTo(HaveOccurred())
lPos := client.LPos(ctx, "list", "b", redis.LPosArgs{})
Expect(lPos.Err()).NotTo(HaveOccurred())
Expect(lPos.Val()).To(Equal(int64(1)))
lPos = client.LPos(ctx, "list", "b", redis.LPosArgs{Rank: 2})
Expect(lPos.Err()).NotTo(HaveOccurred())
Expect(lPos.Val()).To(Equal(int64(3)))
lPos = client.LPos(ctx, "list", "b", redis.LPosArgs{Rank: -2})
Expect(lPos.Err()).NotTo(HaveOccurred())
Expect(lPos.Val()).To(Equal(int64(1)))
lPos = client.LPos(ctx, "list", "b", redis.LPosArgs{Rank: 2, MaxLen: 1})
Expect(lPos.Err()).To(Equal(redis.Nil))
lPos = client.LPos(ctx, "list", "z", redis.LPosArgs{})
Expect(lPos.Err()).To(Equal(redis.Nil))
})
It("should LPosCount", func() {
rPush := client.RPush(ctx, "list", "a")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "b")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "c")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "b")
Expect(rPush.Err()).NotTo(HaveOccurred())
lPos := client.LPosCount(ctx, "list", "b", 2, redis.LPosArgs{})
Expect(lPos.Err()).NotTo(HaveOccurred())
Expect(lPos.Val()).To(Equal([]int64{1, 3}))
lPos = client.LPosCount(ctx, "list", "b", 2, redis.LPosArgs{Rank: 2})
Expect(lPos.Err()).NotTo(HaveOccurred())
Expect(lPos.Val()).To(Equal([]int64{3}))
lPos = client.LPosCount(ctx, "list", "b", 1, redis.LPosArgs{Rank: 1, MaxLen: 1})
Expect(lPos.Err()).NotTo(HaveOccurred())
Expect(lPos.Val()).To(Equal([]int64{}))
lPos = client.LPosCount(ctx, "list", "b", 1, redis.LPosArgs{Rank: 1, MaxLen: 0})
Expect(lPos.Err()).NotTo(HaveOccurred())
Expect(lPos.Val()).To(Equal([]int64{1}))
})
It("should LPush", func() {
lPush := client.LPush(ctx, "list", "World")
Expect(lPush.Err()).NotTo(HaveOccurred())
lPush = client.LPush(ctx, "list", "Hello")
Expect(lPush.Err()).NotTo(HaveOccurred())
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
})
It("should LPushX", func() {
lPush := client.LPush(ctx, "list", "World")
Expect(lPush.Err()).NotTo(HaveOccurred())
lPushX := client.LPushX(ctx, "list", "Hello")
Expect(lPushX.Err()).NotTo(HaveOccurred())
Expect(lPushX.Val()).To(Equal(int64(2)))
lPush = client.LPush(ctx, "list1", "three")
Expect(lPush.Err()).NotTo(HaveOccurred())
Expect(lPush.Val()).To(Equal(int64(1)))
lPushX = client.LPushX(ctx, "list1", "two", "one")
Expect(lPushX.Err()).NotTo(HaveOccurred())
Expect(lPushX.Val()).To(Equal(int64(3)))
lPushX = client.LPushX(ctx, "list2", "Hello")
Expect(lPushX.Err()).NotTo(HaveOccurred())
Expect(lPushX.Val()).To(Equal(int64(0)))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
lRange = client.LRange(ctx, "list1", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
lRange = client.LRange(ctx, "list2", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{}))
})
It("should LRange", func() {
rPush := client.RPush(ctx, "list", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "two")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "three")
Expect(rPush.Err()).NotTo(HaveOccurred())
lRange := client.LRange(ctx, "list", 0, 0)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"one"}))
lRange = client.LRange(ctx, "list", -3, 2)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
lRange = client.LRange(ctx, "list", -100, 100)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
lRange = client.LRange(ctx, "list", 5, 10)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{}))
})
It("should LRem", func() {
rPush := client.RPush(ctx, "list", "hello")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "hello")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "key")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "hello")
Expect(rPush.Err()).NotTo(HaveOccurred())
lRem := client.LRem(ctx, "list", -2, "hello")
Expect(lRem.Err()).NotTo(HaveOccurred())
Expect(lRem.Val()).To(Equal(int64(2)))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"hello", "key"}))
})
It("should LSet", func() {
rPush := client.RPush(ctx, "list", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "two")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "three")
Expect(rPush.Err()).NotTo(HaveOccurred())
lSet := client.LSet(ctx, "list", 0, "four")
Expect(lSet.Err()).NotTo(HaveOccurred())
Expect(lSet.Val()).To(Equal("OK"))
lSet = client.LSet(ctx, "list", -2, "five")
Expect(lSet.Err()).NotTo(HaveOccurred())
Expect(lSet.Val()).To(Equal("OK"))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"four", "five", "three"}))
})
It("should LTrim", func() {
rPush := client.RPush(ctx, "list", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "two")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "three")
Expect(rPush.Err()).NotTo(HaveOccurred())
lTrim := client.LTrim(ctx, "list", 1, -1)
Expect(lTrim.Err()).NotTo(HaveOccurred())
Expect(lTrim.Val()).To(Equal("OK"))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"two", "three"}))
})
It("should RPop", func() {
rPush := client.RPush(ctx, "list", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "two")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "three")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPop := client.RPop(ctx, "list")
Expect(rPop.Err()).NotTo(HaveOccurred())
Expect(rPop.Val()).To(Equal("three"))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"one", "two"}))
})
It("should RPopCount", func() {
rPush := client.RPush(ctx, "list", "one", "two", "three", "four")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(4)))
rPopCount := client.RPopCount(ctx, "list", 2)
Expect(rPopCount.Err()).NotTo(HaveOccurred())
Expect(rPopCount.Val()).To(Equal([]string{"four", "three"}))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"one", "two"}))
})
It("should RPopLPush", func() {
rPush := client.RPush(ctx, "list", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "two")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPush = client.RPush(ctx, "list", "three")
Expect(rPush.Err()).NotTo(HaveOccurred())
rPopLPush := client.RPopLPush(ctx, "list", "list2")
Expect(rPopLPush.Err()).NotTo(HaveOccurred())
Expect(rPopLPush.Val()).To(Equal("three"))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"one", "two"}))
lRange = client.LRange(ctx, "list2", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"three"}))
})
It("should RPush", func() {
rPush := client.RPush(ctx, "list", "Hello")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(1)))
rPush = client.RPush(ctx, "list", "World")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(2)))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
})
It("should RPushX", func() {
rPush := client.RPush(ctx, "list", "Hello")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(1)))
rPushX := client.RPushX(ctx, "list", "World")
Expect(rPushX.Err()).NotTo(HaveOccurred())
Expect(rPushX.Val()).To(Equal(int64(2)))
rPush = client.RPush(ctx, "list1", "one")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(1)))
rPushX = client.RPushX(ctx, "list1", "two", "three")
Expect(rPushX.Err()).NotTo(HaveOccurred())
Expect(rPushX.Val()).To(Equal(int64(3)))
rPushX = client.RPushX(ctx, "list2", "World")
Expect(rPushX.Err()).NotTo(HaveOccurred())
Expect(rPushX.Val()).To(Equal(int64(0)))
lRange := client.LRange(ctx, "list", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
lRange = client.LRange(ctx, "list1", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
lRange = client.LRange(ctx, "list2", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{}))
})
It("should LMove", func() {
rPush := client.RPush(ctx, "lmove1", "ichi")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(1)))
rPush = client.RPush(ctx, "lmove1", "ni")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(2)))
rPush = client.RPush(ctx, "lmove1", "san")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(3)))
lMove := client.LMove(ctx, "lmove1", "lmove2", "RIGHT", "LEFT")
Expect(lMove.Err()).NotTo(HaveOccurred())
Expect(lMove.Val()).To(Equal("san"))
lRange := client.LRange(ctx, "lmove2", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"san"}))
})
It("should BLMove", func() {
rPush := client.RPush(ctx, "blmove1", "ichi")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(1)))
rPush = client.RPush(ctx, "blmove1", "ni")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(2)))
rPush = client.RPush(ctx, "blmove1", "san")
Expect(rPush.Err()).NotTo(HaveOccurred())
Expect(rPush.Val()).To(Equal(int64(3)))
blMove := client.BLMove(ctx, "blmove1", "blmove2", "RIGHT", "LEFT", time.Second)
Expect(blMove.Err()).NotTo(HaveOccurred())
Expect(blMove.Val()).To(Equal("san"))
lRange := client.LRange(ctx, "blmove2", 0, -1)
Expect(lRange.Err()).NotTo(HaveOccurred())
Expect(lRange.Val()).To(Equal([]string{"san"}))
})
})
Describe("sets", func() {
It("should SAdd", func() {
sAdd := client.SAdd(ctx, "set", "Hello")
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expect(sAdd.Val()).To(Equal(int64(1)))
sAdd = client.SAdd(ctx, "set", "World")
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expect(sAdd.Val()).To(Equal(int64(1)))
sAdd = client.SAdd(ctx, "set", "World")
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expect(sAdd.Val()).To(Equal(int64(0)))
sMembers := client.SMembers(ctx, "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(ctx, "set", set)
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expect(sAdd.Val()).To(Equal(int64(2)))
sMembers := client.SMembers(ctx, "set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
})
It("should SCard", func() {
sAdd := client.SAdd(ctx, "set", "Hello")
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expect(sAdd.Val()).To(Equal(int64(1)))
sAdd = client.SAdd(ctx, "set", "World")
Expect(sAdd.Err()).NotTo(HaveOccurred())
Expect(sAdd.Val()).To(Equal(int64(1)))
sCard := client.SCard(ctx, "set")
Expect(sCard.Err()).NotTo(HaveOccurred())
Expect(sCard.Val()).To(Equal(int64(2)))
})
It("should SDiff", func() {
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sDiff := client.SDiff(ctx, "set1", "set2")
Expect(sDiff.Err()).NotTo(HaveOccurred())
Expect(sDiff.Val()).To(ConsistOf([]string{"a", "b"}))
})
It("should SDiffStore", func() {
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sDiffStore := client.SDiffStore(ctx, "set", "set1", "set2")
Expect(sDiffStore.Err()).NotTo(HaveOccurred())
Expect(sDiffStore.Val()).To(Equal(int64(2)))
sMembers := client.SMembers(ctx, "set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(ConsistOf([]string{"a", "b"}))
})
It("should SInter", func() {
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sInter := client.SInter(ctx, "set1", "set2")
Expect(sInter.Err()).NotTo(HaveOccurred())
Expect(sInter.Val()).To(Equal([]string{"c"}))
})
It("should SInterStore", func() {
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sInterStore := client.SInterStore(ctx, "set", "set1", "set2")
Expect(sInterStore.Err()).NotTo(HaveOccurred())
Expect(sInterStore.Val()).To(Equal(int64(1)))
sMembers := client.SMembers(ctx, "set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(Equal([]string{"c"}))
})
It("should IsMember", func() {
sAdd := client.SAdd(ctx, "set", "one")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sIsMember := client.SIsMember(ctx, "set", "one")
Expect(sIsMember.Err()).NotTo(HaveOccurred())
Expect(sIsMember.Val()).To(Equal(true))
sIsMember = client.SIsMember(ctx, "set", "two")
Expect(sIsMember.Err()).NotTo(HaveOccurred())
Expect(sIsMember.Val()).To(Equal(false))
})
It("should SMIsMember", func() {
sAdd := client.SAdd(ctx, "set", "one")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sMIsMember := client.SMIsMember(ctx, "set", "one", "two")
Expect(sMIsMember.Err()).NotTo(HaveOccurred())
Expect(sMIsMember.Val()).To(Equal([]bool{true, false}))
})
It("should SMembers", func() {
sAdd := client.SAdd(ctx, "set", "Hello")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set", "World")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sMembers := client.SMembers(ctx, "set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
})
It("should SMembersMap", func() {
sAdd := client.SAdd(ctx, "set", "Hello")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set", "World")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sMembersMap := client.SMembersMap(ctx, "set")
Expect(sMembersMap.Err()).NotTo(HaveOccurred())
Expect(sMembersMap.Val()).To(Equal(map[string]struct{}{"Hello": {}, "World": {}}))
})
It("should SMove", func() {
sAdd := client.SAdd(ctx, "set1", "one")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "two")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "three")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sMove := client.SMove(ctx, "set1", "set2", "two")
Expect(sMove.Err()).NotTo(HaveOccurred())
Expect(sMove.Val()).To(Equal(true))
sMembers := client.SMembers(ctx, "set1")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(Equal([]string{"one"}))
sMembers = client.SMembers(ctx, "set2")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"}))
})
It("should SPop", func() {
sAdd := client.SAdd(ctx, "set", "one")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set", "two")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set", "three")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sPop := client.SPop(ctx, "set")
Expect(sPop.Err()).NotTo(HaveOccurred())
Expect(sPop.Val()).NotTo(Equal(""))
sMembers := client.SMembers(ctx, "set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(HaveLen(2))
})
It("should SPopN", func() {
sAdd := client.SAdd(ctx, "set", "one")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set", "two")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set", "three")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set", "four")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sPopN := client.SPopN(ctx, "set", 1)
Expect(sPopN.Err()).NotTo(HaveOccurred())
Expect(sPopN.Val()).NotTo(Equal([]string{""}))
sMembers := client.SMembers(ctx, "set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(HaveLen(3))
sPopN = client.SPopN(ctx, "set", 4)
Expect(sPopN.Err()).NotTo(HaveOccurred())
Expect(sPopN.Val()).To(HaveLen(3))
sMembers = client.SMembers(ctx, "set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(HaveLen(0))
})
It("should SRandMember and SRandMemberN", func() {
err := client.SAdd(ctx, "set", "one").Err()
Expect(err).NotTo(HaveOccurred())
err = client.SAdd(ctx, "set", "two").Err()
Expect(err).NotTo(HaveOccurred())
err = client.SAdd(ctx, "set", "three").Err()
Expect(err).NotTo(HaveOccurred())
members, err := client.SMembers(ctx, "set").Result()
Expect(err).NotTo(HaveOccurred())
Expect(members).To(HaveLen(3))
member, err := client.SRandMember(ctx, "set").Result()
Expect(err).NotTo(HaveOccurred())
Expect(member).NotTo(Equal(""))
members, err = client.SRandMemberN(ctx, "set", 2).Result()
Expect(err).NotTo(HaveOccurred())
Expect(members).To(HaveLen(2))
})
It("should SRem", func() {
sAdd := client.SAdd(ctx, "set", "one")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set", "two")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set", "three")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sRem := client.SRem(ctx, "set", "one")
Expect(sRem.Err()).NotTo(HaveOccurred())
Expect(sRem.Val()).To(Equal(int64(1)))
sRem = client.SRem(ctx, "set", "four")
Expect(sRem.Err()).NotTo(HaveOccurred())
Expect(sRem.Val()).To(Equal(int64(0)))
sMembers := client.SMembers(ctx, "set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"}))
})
It("should SUnion", func() {
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sUnion := client.SUnion(ctx, "set1", "set2")
Expect(sUnion.Err()).NotTo(HaveOccurred())
Expect(sUnion.Val()).To(HaveLen(5))
})
It("should SUnionStore", func() {
sAdd := client.SAdd(ctx, "set1", "a")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "b")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set1", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "c")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "d")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sAdd = client.SAdd(ctx, "set2", "e")
Expect(sAdd.Err()).NotTo(HaveOccurred())
sUnionStore := client.SUnionStore(ctx, "set", "set1", "set2")
Expect(sUnionStore.Err()).NotTo(HaveOccurred())
Expect(sUnionStore.Val()).To(Equal(int64(5)))
sMembers := client.SMembers(ctx, "set")
Expect(sMembers.Err()).NotTo(HaveOccurred())
Expect(sMembers.Val()).To(HaveLen(5))
})
})
Describe("sorted sets", func() {
It("should BZPopMax", func() {
err := client.ZAdd(ctx, "zset1", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{
Score: 3,
Member: "three",
}).Err()
Expect(err).NotTo(HaveOccurred())
member, err := client.BZPopMax(ctx, 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(ctx, 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(ctx, "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(ctx, time.Second, "zset1").Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(BeNil())
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
stats := client.PoolStats()
Expect(stats.Hits).To(Equal(uint32(2)))
Expect(stats.Misses).To(Equal(uint32(1)))
Expect(stats.Timeouts).To(Equal(uint32(0)))
})
It("should BZPopMin", func() {
err := client.ZAdd(ctx, "zset1", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{
Score: 3,
Member: "three",
}).Err()
Expect(err).NotTo(HaveOccurred())
member, err := client.BZPopMin(ctx, 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(ctx, 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(ctx, "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(ctx, time.Second, "zset1").Result()
Expect(err).To(Equal(redis.Nil))
Expect(val).To(BeNil())
Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred())
stats := client.PoolStats()
Expect(stats.Hits).To(Equal(uint32(2)))
Expect(stats.Misses).To(Equal(uint32(1)))
Expect(stats.Timeouts).To(Equal(uint32(0)))
})
It("should ZAdd", func() {
added, err := client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
added, err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: "uno",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
added, err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 2,
Member: "two",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
added, err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 3,
Member: "two",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(0)))
vals, err := client.ZRangeWithScores(ctx, "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(ctx, "zset", &redis.Z{
Score: 1,
Member: []byte("one"),
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
added, err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: []byte("uno"),
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
added, err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 2,
Member: []byte("two"),
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
added, err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 3,
Member: []byte("two"),
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(0)))
vals, err := client.ZRangeWithScores(ctx, "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 ZAddArgs", func() {
// Test only the GT+LT options.
added, err := client.ZAddArgs(ctx, "zset", redis.ZAddArgs{
GT: true,
Members: []redis.Z{{Score: 1, Member: "one"}},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
added, err = client.ZAddArgs(ctx, "zset", redis.ZAddArgs{
GT: true,
Members: []redis.Z{{Score: 2, Member: "one"}},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(0)))
vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
added, err = client.ZAddArgs(ctx, "zset", redis.ZAddArgs{
LT: true,
Members: []redis.Z{{Score: 1, Member: "one"}},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(0)))
vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
})
It("should ZAddNX", func() {
added, err := client.ZAddNX(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
added, err = client.ZAddNX(ctx, "zset", &redis.Z{
Score: 2,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(0)))
vals, err = client.ZRangeWithScores(ctx, "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(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(0)))
vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(BeEmpty())
added, err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
added, err = client.ZAddXX(ctx, "zset", &redis.Z{
Score: 2,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(0)))
vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
})
// TODO: remove in v9.
It("should ZAddCh", func() {
changed, err := client.ZAddCh(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(Equal(int64(1)))
changed, err = client.ZAddCh(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(Equal(int64(0)))
})
// TODO: remove in v9.
It("should ZAddNXCh", func() {
changed, err := client.ZAddNXCh(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(Equal(int64(1)))
vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
changed, err = client.ZAddNXCh(ctx, "zset", &redis.Z{
Score: 2,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(Equal(int64(0)))
vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{
Score: 1,
Member: "one",
}}))
})
// TODO: remove in v9.
It("should ZAddXXCh", func() {
changed, err := client.ZAddXXCh(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(Equal(int64(0)))
vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(BeEmpty())
added, err := client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
changed, err = client.ZAddXXCh(ctx, "zset", &redis.Z{
Score: 2,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(changed).To(Equal(int64(1)))
vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
})
// TODO: remove in v9.
It("should ZIncr", func() {
score, err := client.ZIncr(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(score).To(Equal(float64(1)))
vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
score, err = client.ZIncr(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(score).To(Equal(float64(2)))
vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 2, Member: "one"}}))
})
// TODO: remove in v9.
It("should ZIncrNX", func() {
score, err := client.ZIncrNX(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(score).To(Equal(float64(1)))
vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
score, err = client.ZIncrNX(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).To(Equal(redis.Nil))
Expect(score).To(Equal(float64(0)))
vals, err = client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
})
// TODO: remove in v9.
It("should ZIncrXX", func() {
score, err := client.ZIncrXX(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).To(Equal(redis.Nil))
Expect(score).To(Equal(float64(0)))
vals, err := client.ZRangeWithScores(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(BeEmpty())
added, err := client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(1)))
score, err = client.ZIncrXX(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(score).To(Equal(float64(2)))
vals, err = client.ZRangeWithScores(ctx, "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(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
card, err := client.ZCard(ctx, "zset").Result()
Expect(err).NotTo(HaveOccurred())
Expect(card).To(Equal(int64(2)))
})
It("should ZCount", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 3,
Member: "three",
}).Err()
Expect(err).NotTo(HaveOccurred())
count, err := client.ZCount(ctx, "zset", "-inf", "+inf").Result()
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(int64(3)))
count, err = client.ZCount(ctx, "zset", "(1", "3").Result()
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(int64(2)))
count, err = client.ZLexCount(ctx, "zset", "-", "+").Result()
Expect(err).NotTo(HaveOccurred())
Expect(count).To(Equal(int64(3)))
})
It("should ZIncrBy", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
n, err := client.ZIncrBy(ctx, "zset", 2, "one").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(float64(3)))
val, err := client.ZRangeWithScores(ctx, "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(ctx, "zset1", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset3", &redis.Z{Score: 3, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
n, err := client.ZInterStore(ctx, "out", &redis.ZStore{
Keys: []string{"zset1", "zset2"},
Weights: []float64{2, 3},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
vals, err := client.ZRangeWithScores(ctx, "out", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{
Score: 5,
Member: "one",
}, {
Score: 10,
Member: "two",
}}))
})
It("should ZMScore", func() {
zmScore := client.ZMScore(ctx, "zset", "one", "three")
Expect(zmScore.Err()).NotTo(HaveOccurred())
Expect(zmScore.Val()).To(HaveLen(2))
Expect(zmScore.Val()[0]).To(Equal(float64(0)))
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
zmScore = client.ZMScore(ctx, "zset", "one", "three")
Expect(zmScore.Err()).NotTo(HaveOccurred())
Expect(zmScore.Val()).To(HaveLen(2))
Expect(zmScore.Val()[0]).To(Equal(float64(1)))
zmScore = client.ZMScore(ctx, "zset", "four")
Expect(zmScore.Err()).NotTo(HaveOccurred())
Expect(zmScore.Val()).To(HaveLen(1))
zmScore = client.ZMScore(ctx, "zset", "four", "one")
Expect(zmScore.Err()).NotTo(HaveOccurred())
Expect(zmScore.Val()).To(HaveLen(2))
})
It("should ZPopMax", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 3,
Member: "three",
}).Err()
Expect(err).NotTo(HaveOccurred())
members, err := client.ZPopMax(ctx, "zset").Result()
Expect(err).NotTo(HaveOccurred())
Expect(members).To(Equal([]redis.Z{{
Score: 3,
Member: "three",
}}))
// adding back 3
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 3,
Member: "three",
}).Err()
Expect(err).NotTo(HaveOccurred())
members, err = client.ZPopMax(ctx, "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(ctx, "zset", &redis.Z{
Score: 3,
Member: "three",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
members, err = client.ZPopMax(ctx, "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(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 3,
Member: "three",
}).Err()
Expect(err).NotTo(HaveOccurred())
members, err := client.ZPopMin(ctx, "zset").Result()
Expect(err).NotTo(HaveOccurred())
Expect(members).To(Equal([]redis.Z{{
Score: 1,
Member: "one",
}}))
// adding back 1
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
members, err = client.ZPopMin(ctx, "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(ctx, "zset", &redis.Z{
Score: 1,
Member: "one",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 2,
Member: "two",
}).Err()
Expect(err).NotTo(HaveOccurred())
members, err = client.ZPopMin(ctx, "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(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
zRange := client.ZRange(ctx, "zset", 0, -1)
Expect(zRange.Err()).NotTo(HaveOccurred())
Expect(zRange.Val()).To(Equal([]string{"one", "two", "three"}))
zRange = client.ZRange(ctx, "zset", 2, 3)
Expect(zRange.Err()).NotTo(HaveOccurred())
Expect(zRange.Val()).To(Equal([]string{"three"}))
zRange = client.ZRange(ctx, "zset", -2, -1)
Expect(zRange.Err()).NotTo(HaveOccurred())
Expect(zRange.Val()).To(Equal([]string{"two", "three"}))
})
It("should ZRangeWithScores", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
vals, err := client.ZRangeWithScores(ctx, "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(ctx, "zset", 2, 3).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{Score: 3, Member: "three"}}))
vals, err = client.ZRangeWithScores(ctx, "zset", -2, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{
Score: 2,
Member: "two",
}, {
Score: 3,
Member: "three",
}}))
})
It("should ZRangeArgs", func() {
added, err := client.ZAddArgs(ctx, "zset", redis.ZAddArgs{
Members: []redis.Z{
{Score: 1, Member: "one"},
{Score: 2, Member: "two"},
{Score: 3, Member: "three"},
{Score: 4, Member: "four"},
},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(4)))
zRange, err := client.ZRangeArgs(ctx, redis.ZRangeArgs{
Key: "zset",
Start: 1,
Stop: 4,
ByScore: true,
Rev: true,
Offset: 1,
Count: 2,
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(zRange).To(Equal([]string{"three", "two"}))
zRange, err = client.ZRangeArgs(ctx, redis.ZRangeArgs{
Key: "zset",
Start: "-",
Stop: "+",
ByLex: true,
Rev: true,
Offset: 2,
Count: 2,
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(zRange).To(Equal([]string{"two", "one"}))
zRange, err = client.ZRangeArgs(ctx, redis.ZRangeArgs{
Key: "zset",
Start: "(1",
Stop: "(4",
ByScore: true,
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(zRange).To(Equal([]string{"two", "three"}))
// withScores.
zSlice, err := client.ZRangeArgsWithScores(ctx, redis.ZRangeArgs{
Key: "zset",
Start: 1,
Stop: 4,
ByScore: true,
Rev: true,
Offset: 1,
Count: 2,
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(zSlice).To(Equal([]redis.Z{
{Score: 3, Member: "three"},
{Score: 2, Member: "two"},
}))
})
It("should ZRangeByScore", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
zRangeByScore := client.ZRangeByScore(ctx, "zset", &redis.ZRangeBy{
Min: "-inf",
Max: "+inf",
})
Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two", "three"}))
zRangeByScore = client.ZRangeByScore(ctx, "zset", &redis.ZRangeBy{
Min: "1",
Max: "2",
})
Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two"}))
zRangeByScore = client.ZRangeByScore(ctx, "zset", &redis.ZRangeBy{
Min: "(1",
Max: "2",
})
Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRangeByScore.Val()).To(Equal([]string{"two"}))
zRangeByScore = client.ZRangeByScore(ctx, "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(ctx, "zset", &redis.Z{
Score: 0,
Member: "a",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 0,
Member: "b",
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{
Score: 0,
Member: "c",
}).Err()
Expect(err).NotTo(HaveOccurred())
zRangeByLex := client.ZRangeByLex(ctx, "zset", &redis.ZRangeBy{
Min: "-",
Max: "+",
})
Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"}))
zRangeByLex = client.ZRangeByLex(ctx, "zset", &redis.ZRangeBy{
Min: "[a",
Max: "[b",
})
Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"}))
zRangeByLex = client.ZRangeByLex(ctx, "zset", &redis.ZRangeBy{
Min: "(a",
Max: "[b",
})
Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
Expect(zRangeByLex.Val()).To(Equal([]string{"b"}))
zRangeByLex = client.ZRangeByLex(ctx, "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(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
vals, err := client.ZRangeByScoreWithScores(ctx, "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(ctx, "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(ctx, "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(ctx, "zset", &redis.ZRangeBy{
Min: "(1",
Max: "(2",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{}))
})
It("should ZRangeStore", func() {
added, err := client.ZAddArgs(ctx, "zset", redis.ZAddArgs{
Members: []redis.Z{
{Score: 1, Member: "one"},
{Score: 2, Member: "two"},
{Score: 3, Member: "three"},
{Score: 4, Member: "four"},
},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(added).To(Equal(int64(4)))
rangeStore, err := client.ZRangeStore(ctx, "new-zset", redis.ZRangeArgs{
Key: "zset",
Start: 1,
Stop: 4,
ByScore: true,
Rev: true,
Offset: 1,
Count: 2,
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(rangeStore).To(Equal(int64(2)))
zRange, err := client.ZRange(ctx, "new-zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(zRange).To(Equal([]string{"two", "three"}))
})
It("should ZRank", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
zRank := client.ZRank(ctx, "zset", "three")
Expect(zRank.Err()).NotTo(HaveOccurred())
Expect(zRank.Val()).To(Equal(int64(2)))
zRank = client.ZRank(ctx, "zset", "four")
Expect(zRank.Err()).To(Equal(redis.Nil))
Expect(zRank.Val()).To(Equal(int64(0)))
})
It("should ZRem", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
zRem := client.ZRem(ctx, "zset", "two")
Expect(zRem.Err()).NotTo(HaveOccurred())
Expect(zRem.Val()).To(Equal(int64(1)))
vals, err := client.ZRangeWithScores(ctx, "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(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
zRemRangeByRank := client.ZRemRangeByRank(ctx, "zset", 0, 1)
Expect(zRemRangeByRank.Err()).NotTo(HaveOccurred())
Expect(zRemRangeByRank.Val()).To(Equal(int64(2)))
vals, err := client.ZRangeWithScores(ctx, "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(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
zRemRangeByScore := client.ZRemRangeByScore(ctx, "zset", "-inf", "(2")
Expect(zRemRangeByScore.Err()).NotTo(HaveOccurred())
Expect(zRemRangeByScore.Val()).To(Equal(int64(1)))
vals, err := client.ZRangeWithScores(ctx, "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(ctx, "zset", z).Err()
Expect(err).NotTo(HaveOccurred())
}
n, err := client.ZRemRangeByLex(ctx, "zset", "[alpha", "[omega").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(6)))
vals, err := client.ZRange(ctx, "zset", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"ALPHA", "aaaa", "zap", "zip"}))
})
It("should ZRevRange", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
zRevRange := client.ZRevRange(ctx, "zset", 0, -1)
Expect(zRevRange.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"three", "two", "one"}))
zRevRange = client.ZRevRange(ctx, "zset", 2, 3)
Expect(zRevRange.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"one"}))
zRevRange = client.ZRevRange(ctx, "zset", -2, -1)
Expect(zRevRange.Err()).NotTo(HaveOccurred())
Expect(zRevRange.Val()).To(Equal([]string{"two", "one"}))
})
It("should ZRevRangeWithScoresMap", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
val, err := client.ZRevRangeWithScores(ctx, "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(ctx, "zset", 2, 3).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.Z{{Score: 1, Member: "one"}}))
val, err = client.ZRevRangeWithScores(ctx, "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(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
vals, err := client.ZRevRangeByScore(
ctx, "zset", &redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"three", "two", "one"}))
vals, err = client.ZRevRangeByScore(
ctx, "zset", &redis.ZRangeBy{Max: "2", Min: "(1"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"two"}))
vals, err = client.ZRevRangeByScore(
ctx, "zset", &redis.ZRangeBy{Max: "(2", Min: "(1"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{}))
})
It("should ZRevRangeByLex", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 0, Member: "a"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 0, Member: "b"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 0, Member: "c"}).Err()
Expect(err).NotTo(HaveOccurred())
vals, err := client.ZRevRangeByLex(
ctx, "zset", &redis.ZRangeBy{Max: "+", Min: "-"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"c", "b", "a"}))
vals, err = client.ZRevRangeByLex(
ctx, "zset", &redis.ZRangeBy{Max: "[b", Min: "(a"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{"b"}))
vals, err = client.ZRevRangeByLex(
ctx, "zset", &redis.ZRangeBy{Max: "(b", Min: "(a"}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]string{}))
})
It("should ZRevRangeByScoreWithScores", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
vals, err := client.ZRevRangeByScoreWithScores(
ctx, "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(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
vals, err := client.ZRevRangeByScoreWithScores(
ctx, "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(
ctx, "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(
ctx, "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(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
zRevRank := client.ZRevRank(ctx, "zset", "one")
Expect(zRevRank.Err()).NotTo(HaveOccurred())
Expect(zRevRank.Val()).To(Equal(int64(2)))
zRevRank = client.ZRevRank(ctx, "zset", "four")
Expect(zRevRank.Err()).To(Equal(redis.Nil))
Expect(zRevRank.Val()).To(Equal(int64(0)))
})
It("should ZScore", func() {
zAdd := client.ZAdd(ctx, "zset", &redis.Z{Score: 1.001, Member: "one"})
Expect(zAdd.Err()).NotTo(HaveOccurred())
zScore := client.ZScore(ctx, "zset", "one")
Expect(zScore.Err()).NotTo(HaveOccurred())
Expect(zScore.Val()).To(Equal(float64(1.001)))
})
It("should ZUnion", func() {
err := client.ZAddArgs(ctx, "zset1", redis.ZAddArgs{
Members: []redis.Z{
{Score: 1, Member: "one"},
{Score: 2, Member: "two"},
},
}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAddArgs(ctx, "zset2", redis.ZAddArgs{
Members: []redis.Z{
{Score: 1, Member: "one"},
{Score: 2, Member: "two"},
{Score: 3, Member: "three"},
},
}).Err()
Expect(err).NotTo(HaveOccurred())
union, err := client.ZUnion(ctx, redis.ZStore{
Keys: []string{"zset1", "zset2"},
Weights: []float64{2, 3},
Aggregate: "sum",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(union).To(Equal([]string{"one", "three", "two"}))
unionScores, err := client.ZUnionWithScores(ctx, redis.ZStore{
Keys: []string{"zset1", "zset2"},
Weights: []float64{2, 3},
Aggregate: "sum",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(unionScores).To(Equal([]redis.Z{
{Score: 5, Member: "one"},
{Score: 9, Member: "three"},
{Score: 10, Member: "two"},
}))
})
It("should ZUnionStore", func() {
err := client.ZAdd(ctx, "zset1", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
n, err := client.ZUnionStore(ctx, "out", &redis.ZStore{
Keys: []string{"zset1", "zset2"},
Weights: []float64{2, 3},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
val, err := client.ZRangeWithScores(ctx, "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",
}}))
})
It("should ZRandMember", func() {
err := client.ZAdd(ctx, "zset", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
v := client.ZRandMember(ctx, "zset", 1, false)
Expect(v.Err()).NotTo(HaveOccurred())
Expect(v.Val()).To(Or(Equal([]string{"one"}), Equal([]string{"two"})))
v = client.ZRandMember(ctx, "zset", 0, false)
Expect(v.Err()).NotTo(HaveOccurred())
Expect(v.Val()).To(HaveLen(0))
var slice []string
err = client.ZRandMember(ctx, "zset", 1, true).ScanSlice(&slice)
Expect(err).NotTo(HaveOccurred())
Expect(slice).To(Or(Equal([]string{"one", "1"}), Equal([]string{"two", "2"})))
})
It("should ZDiff", func() {
err := client.ZAdd(ctx, "zset1", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
v, err := client.ZDiff(ctx, "zset1", "zset2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal([]string{"two", "three"}))
})
It("should ZDiffWithScores", func() {
err := client.ZAdd(ctx, "zset1", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
v, err := client.ZDiffWithScores(ctx, "zset1", "zset2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal([]redis.Z{
{
Member: "two",
Score: 2,
},
{
Member: "three",
Score: 3,
},
}))
})
It("should ZInter", func() {
err := client.ZAdd(ctx, "zset1", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
v, err := client.ZInter(ctx, &redis.ZStore{
Keys: []string{"zset1", "zset2"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal([]string{"one", "two"}))
})
It("should ZInterWithScores", func() {
err := client.ZAdd(ctx, "zset1", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
v, err := client.ZInterWithScores(ctx, &redis.ZStore{
Keys: []string{"zset1", "zset2"},
Weights: []float64{2, 3},
Aggregate: "Max",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal([]redis.Z{
{
Member: "one",
Score: 3,
},
{
Member: "two",
Score: 6,
},
}))
})
It("should ZDiffStore", func() {
err := client.ZAdd(ctx, "zset1", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset1", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 1, Member: "one"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 2, Member: "two"}).Err()
Expect(err).NotTo(HaveOccurred())
err = client.ZAdd(ctx, "zset2", &redis.Z{Score: 3, Member: "three"}).Err()
Expect(err).NotTo(HaveOccurred())
v, err := client.ZDiffStore(ctx, "out1", "zset1", "zset2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal(int64(0)))
v, err = client.ZDiffStore(ctx, "out1", "zset2", "zset1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal(int64(1)))
vals, err := client.ZRangeWithScores(ctx, "out1", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.Z{{
Score: 3,
Member: "three",
}}))
})
})
Describe("streams", func() {
BeforeEach(func() {
id, err := client.XAdd(ctx, &redis.XAddArgs{
Stream: "stream",
ID: "1-0",
Values: map[string]interface{}{"uno": "un"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(id).To(Equal("1-0"))
// Values supports []interface{}.
id, err = client.XAdd(ctx, &redis.XAddArgs{
Stream: "stream",
ID: "2-0",
Values: []interface{}{"dos", "deux"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(id).To(Equal("2-0"))
// Value supports []string.
id, err = client.XAdd(ctx, &redis.XAddArgs{
Stream: "stream",
ID: "3-0",
Values: []string{"tres", "troix"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(id).To(Equal("3-0"))
})
// TODO remove in v9.
It("should XTrim", func() {
n, err := client.XTrim(ctx, "stream", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
})
// TODO remove in v9.
It("should XTrimApprox", func() {
n, err := client.XTrimApprox(ctx, "stream", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
})
// TODO XTrimMaxLenApprox/XTrimMinIDApprox There is a bug in the limit parameter.
// TODO Don't test it for now.
// TODO link: https://github.com/redis/redis/issues/9046
It("should XTrimMaxLen", func() {
n, err := client.XTrimMaxLen(ctx, "stream", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
})
It("should XTrimMaxLenApprox", func() {
n, err := client.XTrimMaxLenApprox(ctx, "stream", 0, 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
})
It("should XTrimMinID", func() {
n, err := client.XTrimMinID(ctx, "stream", "4-0").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
})
It("should XTrimMinIDApprox", func() {
n, err := client.XTrimMinIDApprox(ctx, "stream", "4-0", 0).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
})
It("should XAdd", func() {
id, err := client.XAdd(ctx, &redis.XAddArgs{
Stream: "stream",
Values: map[string]interface{}{"quatro": "quatre"},
}).Result()
Expect(err).NotTo(HaveOccurred())
vals, err := client.XRange(ctx, "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"}},
}))
})
// TODO XAdd There is a bug in the limit parameter.
// TODO Don't test it for now.
// TODO link: https://github.com/redis/redis/issues/9046
It("should XAdd with MaxLen", func() {
id, err := client.XAdd(ctx, &redis.XAddArgs{
Stream: "stream",
MaxLen: 1,
Values: map[string]interface{}{"quatro": "quatre"},
}).Result()
Expect(err).NotTo(HaveOccurred())
vals, err := client.XRange(ctx, "stream", "-", "+").Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]redis.XMessage{
{ID: id, Values: map[string]interface{}{"quatro": "quatre"}},
}))
})
It("should XAdd with MinID", func() {
id, err := client.XAdd(ctx, &redis.XAddArgs{
Stream: "stream",
MinID: "5-0",
ID: "4-0",
Values: map[string]interface{}{"quatro": "quatre"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(id).To(Equal("4-0"))
vals, err := client.XRange(ctx, "stream", "-", "+").Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(HaveLen(0))
})
It("should XDel", func() {
n, err := client.XDel(ctx, "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(ctx, "stream").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
})
It("should XRange", func() {
msgs, err := client.XRange(ctx, "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(ctx, "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(ctx, "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(ctx, "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(ctx, "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(ctx, "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(ctx, "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(ctx, "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(ctx, "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(ctx, "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(ctx, "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(ctx, "stream", "3").Result()
Expect(err).To(Equal(redis.Nil))
})
It("should XRead", func() {
res, err := client.XRead(ctx, &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(ctx, &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(ctx, "stream", "group", "0").Err()
Expect(err).NotTo(HaveOccurred())
res, err := client.XReadGroup(ctx, &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(ctx, "stream", "group").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
})
It("should XReadGroup skip empty", func() {
n, err := client.XDel(ctx, "stream", "2-0").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
res, err := client.XReadGroup(ctx, &redis.XReadGroupArgs{
Group: "group",
Consumer: "consumer",
Streams: []string{"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: nil},
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
},
},
}))
})
It("should XGroupCreateMkStream", func() {
err := client.XGroupCreateMkStream(ctx, "stream2", "group", "0").Err()
Expect(err).NotTo(HaveOccurred())
err = client.XGroupCreateMkStream(ctx, "stream2", "group", "0").Err()
Expect(err).To(Equal(proto.RedisError("BUSYGROUP Consumer Group name already exists")))
n, err := client.XGroupDestroy(ctx, "stream2", "group").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
n, err = client.Del(ctx, "stream2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
})
It("should XPending", func() {
info, err := client.XPending(ctx, "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},
}))
args := &redis.XPendingExtArgs{
Stream: "stream",
Group: "group",
Start: "-",
End: "+",
Count: 10,
Consumer: "consumer",
}
infoExt, err := client.XPendingExt(ctx, args).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},
}))
args.Idle = 72 * time.Hour
infoExt, err = client.XPendingExt(ctx, args).Result()
Expect(err).NotTo(HaveOccurred())
Expect(infoExt).To(HaveLen(0))
})
It("should XGroup Create Delete Consumer", func() {
n, err := client.XGroupCreateConsumer(ctx, "stream", "group", "c1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
n, err = client.XGroupDelConsumer(ctx, "stream", "group", "consumer").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
})
It("should XAutoClaim", func() {
xca := &redis.XAutoClaimArgs{
Stream: "stream",
Group: "group",
Consumer: "consumer",
Start: "-",
Count: 2,
}
msgs, start, err := client.XAutoClaim(ctx, xca).Result()
Expect(err).NotTo(HaveOccurred())
Expect(start).To(Equal("3-0"))
Expect(msgs).To(Equal([]redis.XMessage{{
ID: "1-0",
Values: map[string]interface{}{"uno": "un"},
}, {
ID: "2-0",
Values: map[string]interface{}{"dos": "deux"},
}}))
xca.Start = start
msgs, start, err = client.XAutoClaim(ctx, xca).Result()
Expect(err).NotTo(HaveOccurred())
Expect(start).To(Equal("0-0"))
Expect(msgs).To(Equal([]redis.XMessage{{
ID: "3-0",
Values: map[string]interface{}{"tres": "troix"},
}}))
ids, start, err := client.XAutoClaimJustID(ctx, xca).Result()
Expect(err).NotTo(HaveOccurred())
Expect(start).To(Equal("0-0"))
Expect(ids).To(Equal([]string{"3-0"}))
})
It("should XClaim", func() {
msgs, err := client.XClaim(ctx, &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(ctx, &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(ctx, "stream", "group", "1-0", "2-0", "4-0").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
})
})
Describe("xinfo", func() {
BeforeEach(func() {
err := client.XGroupCreate(ctx, "stream", "group1", "0").Err()
Expect(err).NotTo(HaveOccurred())
res, err := client.XReadGroup(ctx, &redis.XReadGroupArgs{
Group: "group1",
Consumer: "consumer1",
Streams: []string{"stream", ">"},
Count: 2,
}).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"}},
},
},
}))
res, err = client.XReadGroup(ctx, &redis.XReadGroupArgs{
Group: "group1",
Consumer: "consumer2",
Streams: []string{"stream", ">"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal([]redis.XStream{
{
Stream: "stream",
Messages: []redis.XMessage{
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
},
},
}))
err = client.XGroupCreate(ctx, "stream", "group2", "1-0").Err()
Expect(err).NotTo(HaveOccurred())
res, err = client.XReadGroup(ctx, &redis.XReadGroupArgs{
Group: "group2",
Consumer: "consumer1",
Streams: []string{"stream", ">"},
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal([]redis.XStream{
{
Stream: "stream",
Messages: []redis.XMessage{
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
},
},
}))
})
AfterEach(func() {
n, err := client.XGroupDestroy(ctx, "stream", "group1").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
n, err = client.XGroupDestroy(ctx, "stream", "group2").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
})
It("should XINFO STREAM", func() {
res, err := client.XInfoStream(ctx, "stream").Result()
Expect(err).NotTo(HaveOccurred())
res.RadixTreeKeys = 0
res.RadixTreeNodes = 0
Expect(res).To(Equal(&redis.XInfoStream{
Length: 3,
RadixTreeKeys: 0,
RadixTreeNodes: 0,
Groups: 2,
LastGeneratedID: "3-0",
FirstEntry: redis.XMessage{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
LastEntry: redis.XMessage{ID: "3-0", Values: map[string]interface{}{"tres": "troix"}},
}))
// stream is empty
n, err := client.XDel(ctx, "stream", "1-0", "2-0", "3-0").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(3)))
res, err = client.XInfoStream(ctx, "stream").Result()
Expect(err).NotTo(HaveOccurred())
res.RadixTreeKeys = 0
res.RadixTreeNodes = 0
Expect(res).To(Equal(&redis.XInfoStream{
Length: 0,
RadixTreeKeys: 0,
RadixTreeNodes: 0,
Groups: 2,
LastGeneratedID: "3-0",
FirstEntry: redis.XMessage{},
LastEntry: redis.XMessage{},
}))
})
It("should XINFO STREAM FULL", func() {
res, err := client.XInfoStreamFull(ctx, "stream", 2).Result()
Expect(err).NotTo(HaveOccurred())
res.RadixTreeKeys = 0
res.RadixTreeNodes = 0
// Verify DeliveryTime
now := time.Now()
maxElapsed := 10 * time.Minute
for k, g := range res.Groups {
for k2, p := range g.Pending {
Expect(now.Sub(p.DeliveryTime)).To(BeNumerically("<=", maxElapsed))
res.Groups[k].Pending[k2].DeliveryTime = time.Time{}
}
for k3, c := range g.Consumers {
Expect(now.Sub(c.SeenTime)).To(BeNumerically("<=", maxElapsed))
res.Groups[k].Consumers[k3].SeenTime = time.Time{}
for k4, p := range c.Pending {
Expect(now.Sub(p.DeliveryTime)).To(BeNumerically("<=", maxElapsed))
res.Groups[k].Consumers[k3].Pending[k4].DeliveryTime = time.Time{}
}
}
}
Expect(res).To(Equal(&redis.XInfoStreamFull{
Length: 3,
RadixTreeKeys: 0,
RadixTreeNodes: 0,
LastGeneratedID: "3-0",
Entries: []redis.XMessage{
{ID: "1-0", Values: map[string]interface{}{"uno": "un"}},
{ID: "2-0", Values: map[string]interface{}{"dos": "deux"}},
},
Groups: []redis.XInfoStreamGroup{
{
Name: "group1",
LastDeliveredID: "3-0",
PelCount: 3,
Pending: []redis.XInfoStreamGroupPending{
{
ID: "1-0",
Consumer: "consumer1",
DeliveryTime: time.Time{},
DeliveryCount: 1,
},
{
ID: "2-0",
Consumer: "consumer1",
DeliveryTime: time.Time{},
DeliveryCount: 1,
},
},
Consumers: []redis.XInfoStreamConsumer{
{
Name: "consumer1",
SeenTime: time.Time{},
PelCount: 2,
Pending: []redis.XInfoStreamConsumerPending{
{
ID: "1-0",
DeliveryTime: time.Time{},
DeliveryCount: 1,
},
{
ID: "2-0",
DeliveryTime: time.Time{},
DeliveryCount: 1,
},
},
},
{
Name: "consumer2",
SeenTime: time.Time{},
PelCount: 1,
Pending: []redis.XInfoStreamConsumerPending{
{
ID: "3-0",
DeliveryTime: time.Time{},
DeliveryCount: 1,
},
},
},
},
},
{
Name: "group2",
LastDeliveredID: "3-0",
PelCount: 2,
Pending: []redis.XInfoStreamGroupPending{
{
ID: "2-0",
Consumer: "consumer1",
DeliveryTime: time.Time{},
DeliveryCount: 1,
},
{
ID: "3-0",
Consumer: "consumer1",
DeliveryTime: time.Time{},
DeliveryCount: 1,
},
},
Consumers: []redis.XInfoStreamConsumer{
{
Name: "consumer1",
SeenTime: time.Time{},
PelCount: 2,
Pending: []redis.XInfoStreamConsumerPending{
{
ID: "2-0",
DeliveryTime: time.Time{},
DeliveryCount: 1,
},
{
ID: "3-0",
DeliveryTime: time.Time{},
DeliveryCount: 1,
},
},
},
},
},
},
}))
})
It("should XINFO GROUPS", func() {
res, err := client.XInfoGroups(ctx, "stream").Result()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(Equal([]redis.XInfoGroup{
{Name: "group1", Consumers: 2, Pending: 3, LastDeliveredID: "3-0"},
{Name: "group2", Consumers: 1, Pending: 2, LastDeliveredID: "3-0"},
}))
})
It("should XINFO CONSUMERS", func() {
res, err := client.XInfoConsumers(ctx, "stream", "group1").Result()
Expect(err).NotTo(HaveOccurred())
for i := range res {
res[i].Idle = 0
}
Expect(res).To(Equal([]redis.XInfoConsumer{
{Name: "consumer1", Pending: 2, Idle: 0},
{Name: "consumer2", Pending: 1, Idle: 0},
}))
})
})
})
Describe("Geo add and radius search", func() {
BeforeEach(func() {
n, err := client.GeoAdd(
ctx,
"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(
ctx,
"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(ctx, "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 geo radius and store the result", func() {
n, err := client.GeoRadiusStore(ctx, "Sicily", 15, 37, &redis.GeoRadiusQuery{
Radius: 200,
Store: "result",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
res, err := client.ZRangeWithScores(ctx, "result", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(ContainElement(redis.Z{
Score: 3.479099956230698e+15,
Member: "Palermo",
}))
Expect(res).To(ContainElement(redis.Z{
Score: 3.479447370796909e+15,
Member: "Catania",
}))
})
It("should geo radius and store dist", func() {
n, err := client.GeoRadiusStore(ctx, "Sicily", 15, 37, &redis.GeoRadiusQuery{
Radius: 200,
StoreDist: "result",
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(2)))
res, err := client.ZRangeWithScores(ctx, "result", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(res).To(ContainElement(redis.Z{
Score: 190.44242984775784,
Member: "Palermo",
}))
Expect(res).To(ContainElement(redis.Z{
Score: 56.4412578701582,
Member: "Catania",
}))
})
It("should search geo radius with options", func() {
res, err := client.GeoRadius(ctx, "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(ctx, "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(ctx, "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(ctx, "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(ctx, "Sicily", "Palermo", "Catania", "km").Result()
Expect(err).NotTo(HaveOccurred())
Expect(dist).To(BeNumerically("~", 166.27, 0.01))
dist, err = client.GeoDist(ctx, "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(ctx, "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(ctx, "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,
}))
})
It("should geo search", func() {
q := &redis.GeoSearchQuery{
Member: "Catania",
BoxWidth: 400,
BoxHeight: 100,
BoxUnit: "km",
Sort: "asc",
}
val, err := client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania"}))
q.BoxHeight = 400
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania", "Palermo"}))
q.Count = 1
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania"}))
q.CountAny = true
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Palermo"}))
q = &redis.GeoSearchQuery{
Member: "Catania",
Radius: 100,
RadiusUnit: "km",
Sort: "asc",
}
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania"}))
q.Radius = 400
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania", "Palermo"}))
q.Count = 1
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania"}))
q.CountAny = true
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Palermo"}))
q = &redis.GeoSearchQuery{
Longitude: 15,
Latitude: 37,
BoxWidth: 200,
BoxHeight: 200,
BoxUnit: "km",
Sort: "asc",
}
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania"}))
q.BoxWidth, q.BoxHeight = 400, 400
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania", "Palermo"}))
q.Count = 1
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania"}))
q.CountAny = true
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Palermo"}))
q = &redis.GeoSearchQuery{
Longitude: 15,
Latitude: 37,
Radius: 100,
RadiusUnit: "km",
Sort: "asc",
}
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania"}))
q.Radius = 200
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania", "Palermo"}))
q.Count = 1
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Catania"}))
q.CountAny = true
val, err = client.GeoSearch(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]string{"Palermo"}))
})
It("should geo search with options", func() {
q := &redis.GeoSearchLocationQuery{
GeoSearchQuery: redis.GeoSearchQuery{
Longitude: 15,
Latitude: 37,
Radius: 200,
RadiusUnit: "km",
Sort: "asc",
},
WithHash: true,
WithDist: true,
WithCoord: true,
}
val, err := client.GeoSearchLocation(ctx, "Sicily", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal([]redis.GeoLocation{
{
Name: "Catania",
Longitude: 15.08726745843887329,
Latitude: 37.50266842333162032,
Dist: 56.4413,
GeoHash: 3479447370796909,
},
{
Name: "Palermo",
Longitude: 13.36138933897018433,
Latitude: 38.11555639549629859,
Dist: 190.4424,
GeoHash: 3479099956230698,
},
}))
})
It("should geo search store", func() {
q := &redis.GeoSearchStoreQuery{
GeoSearchQuery: redis.GeoSearchQuery{
Longitude: 15,
Latitude: 37,
Radius: 200,
RadiusUnit: "km",
Sort: "asc",
},
StoreDist: false,
}
val, err := client.GeoSearchStore(ctx, "Sicily", "key1", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(int64(2)))
q.StoreDist = true
val, err = client.GeoSearchStore(ctx, "Sicily", "key2", q).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal(int64(2)))
loc, err := client.GeoSearchLocation(ctx, "key1", &redis.GeoSearchLocationQuery{
GeoSearchQuery: q.GeoSearchQuery,
WithCoord: true,
WithDist: true,
WithHash: true,
}).Result()
Expect(err).NotTo(HaveOccurred())
Expect(loc).To(Equal([]redis.GeoLocation{
{
Name: "Catania",
Longitude: 15.08726745843887329,
Latitude: 37.50266842333162032,
Dist: 56.4413,
GeoHash: 3479447370796909,
},
{
Name: "Palermo",
Longitude: 13.36138933897018433,
Latitude: 38.11555639549629859,
Dist: 190.4424,
GeoHash: 3479099956230698,
},
}))
v, err := client.ZRangeWithScores(ctx, "key2", 0, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal([]redis.Z{
{
Score: 56.441257870158204,
Member: "Catania",
},
{
Score: 190.44242984775784,
Member: "Palermo",
},
}))
})
})
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(ctx, "key", test.value, 0).Err()
Expect(err).NotTo(HaveOccurred())
s, err := client.Get(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(s).To(Equal(test.wanted))
if test.dest == nil {
continue
}
err = client.Get(ctx, "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(ctx, "key", value, 0).Err()
Expect(err).NotTo(HaveOccurred())
})
It("should marshal custom values using json", func() {
s, err := client.Get(ctx, "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(ctx, "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(
ctx,
"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(
ctx,
`return {12, {err="error"}, "abc"}`,
nil,
).Result()
Expect(err).NotTo(HaveOccurred())
Expect(vals).To(Equal([]interface{}{int64(12), proto.RedisError("error"), "abc"}))
})
})
Describe("SlowLogGet", func() {
It("returns slow query result", func() {
const key = "slowlog-log-slower-than"
old := client.ConfigGet(ctx, key).Val()
client.ConfigSet(ctx, key, "0")
defer client.ConfigSet(ctx, key, old[1].(string))
err := client.Do(ctx, "slowlog", "reset").Err()
Expect(err).NotTo(HaveOccurred())
client.Set(ctx, "test", "true", 0)
result, err := client.SlowLogGet(ctx, -1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(len(result)).NotTo(BeZero())
})
})
})
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()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/q7w/redis.git
git@gitee.com:q7w/redis.git
q7w
redis
redis
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385