@@ -409,21 +409,33 @@ var _ = Describe("Commands", func() {
409
409
})
410
410
411
411
It ("should ExpireAt" , func () {
412
- set := client .Set (ctx , "key" , "Hello" , 0 )
413
- Expect (set .Err ()).NotTo (HaveOccurred ())
414
- Expect (set .Val ()).To (Equal ("OK" ))
412
+ setCmd := client .Set (ctx , "key" , "Hello" , 0 )
413
+ Expect (setCmd .Err ()).NotTo (HaveOccurred ())
414
+ Expect (setCmd .Val ()).To (Equal ("OK" ))
415
415
416
416
n , err := client .Exists (ctx , "key" ).Result ()
417
417
Expect (err ).NotTo (HaveOccurred ())
418
418
Expect (n ).To (Equal (int64 (1 )))
419
419
420
- expireAt := client .ExpireAt (ctx , "key" , time .Now ().Add (- time .Hour ))
421
- Expect (expireAt .Err ()).NotTo (HaveOccurred ())
422
- Expect (expireAt .Val ()).To (Equal (true ))
420
+ // Check correct expiration time is set in the future
421
+ expireAt := time .Now ().Add (time .Minute )
422
+ expireAtCmd := client .ExpireAt (ctx , "key" , expireAt )
423
+ Expect (expireAtCmd .Err ()).NotTo (HaveOccurred ())
424
+ Expect (expireAtCmd .Val ()).To (Equal (true ))
425
+
426
+ timeCmd := client .ExpireTime (ctx , "key" )
427
+ Expect (timeCmd .Err ()).NotTo (HaveOccurred ())
428
+ Expect (timeCmd .Val ().Seconds ()).To (BeNumerically ("==" , expireAt .Unix ()))
429
+
430
+ // Check correct expiration in the past
431
+ expireAtCmd = client .ExpireAt (ctx , "key" , time .Now ().Add (- time .Hour ))
432
+ Expect (expireAtCmd .Err ()).NotTo (HaveOccurred ())
433
+ Expect (expireAtCmd .Val ()).To (Equal (true ))
423
434
424
435
n , err = client .Exists (ctx , "key" ).Result ()
425
436
Expect (err ).NotTo (HaveOccurred ())
426
437
Expect (n ).To (Equal (int64 (0 )))
438
+
427
439
})
428
440
429
441
It ("should Keys" , func () {
@@ -569,6 +581,28 @@ var _ = Describe("Commands", func() {
569
581
Expect (pttl .Val ()).To (BeNumerically ("~" , expiration , 100 * time .Millisecond ))
570
582
})
571
583
584
+ It ("should PExpireTime" , func () {
585
+
586
+ // The command returns -1 if the key exists but has no associated expiration time.
587
+ // The command returns -2 if the key does not exist.
588
+ pExpireTime := client .PExpireTime (ctx , "key" )
589
+ Expect (pExpireTime .Err ()).NotTo (HaveOccurred ())
590
+ Expect (pExpireTime .Val () < 0 ).To (Equal (true ))
591
+
592
+ set := client .Set (ctx , "key" , "hello" , 0 )
593
+ Expect (set .Err ()).NotTo (HaveOccurred ())
594
+ Expect (set .Val ()).To (Equal ("OK" ))
595
+
596
+ timestamp := time .Now ().Add (time .Minute )
597
+ expireAt := client .PExpireAt (ctx , "key" , timestamp )
598
+ Expect (expireAt .Err ()).NotTo (HaveOccurred ())
599
+ Expect (expireAt .Val ()).To (Equal (true ))
600
+
601
+ pExpireTime = client .PExpireTime (ctx , "key" )
602
+ Expect (pExpireTime .Err ()).NotTo (HaveOccurred ())
603
+ Expect (pExpireTime .Val ().Milliseconds ()).To (BeNumerically ("==" , timestamp .UnixMilli ()))
604
+ })
605
+
572
606
It ("should PTTL" , func () {
573
607
set := client .Set (ctx , "key" , "Hello" , 0 )
574
608
Expect (set .Err ()).NotTo (HaveOccurred ())
@@ -786,7 +820,32 @@ var _ = Describe("Commands", func() {
786
820
Expect (touch .Val ()).To (Equal (int64 (2 )))
787
821
})
788
822
823
+ It ("should ExpireTime" , func () {
824
+
825
+ // The command returns -1 if the key exists but has no associated expiration time.
826
+ // The command returns -2 if the key does not exist.
827
+ expireTimeCmd := client .ExpireTime (ctx , "key" )
828
+ Expect (expireTimeCmd .Err ()).NotTo (HaveOccurred ())
829
+ Expect (expireTimeCmd .Val () < 0 ).To (Equal (true ))
830
+
831
+ set := client .Set (ctx , "key" , "hello" , 0 )
832
+ Expect (set .Err ()).NotTo (HaveOccurred ())
833
+ Expect (set .Val ()).To (Equal ("OK" ))
834
+
835
+ expireAt := time .Now ().Add (time .Minute )
836
+ expireAtCmd := client .ExpireAt (ctx , "key" , expireAt )
837
+ Expect (expireAtCmd .Err ()).NotTo (HaveOccurred ())
838
+ Expect (expireAtCmd .Val ()).To (Equal (true ))
839
+
840
+ expireTimeCmd = client .ExpireTime (ctx , "key" )
841
+ Expect (expireTimeCmd .Err ()).NotTo (HaveOccurred ())
842
+ Expect (expireTimeCmd .Val ().Seconds ()).To (BeNumerically ("==" , expireAt .Unix ()))
843
+ })
844
+
789
845
It ("should TTL" , func () {
846
+
847
+ // The command returns -1 if the key exists but has no associated expire
848
+ // The command returns -2 if the key does not exist.
790
849
ttl := client .TTL (ctx , "key" )
791
850
Expect (ttl .Err ()).NotTo (HaveOccurred ())
792
851
Expect (ttl .Val () < 0 ).To (Equal (true ))
0 commit comments