@@ -672,3 +672,79 @@ func TestTTL(t *testing.T) {
672
672
673
673
d .Close ()
674
674
}
675
+
676
+ func TestExpirations (t * testing.T ) {
677
+ var err error
678
+
679
+ d , done := newDS (t )
680
+ defer done ()
681
+
682
+ txn := d .NewTransaction (false )
683
+ ttltxn := txn .(ds.TTLDatastore )
684
+ defer txn .Discard ()
685
+
686
+ key := ds .NewKey ("/abc/def" )
687
+ val := make ([]byte , 32 )
688
+ if n , err := rand .Read (val ); n != 32 || err != nil {
689
+ t .Fatal ("source of randomness failed" )
690
+ }
691
+
692
+ ttl := time .Hour
693
+ now := time .Now ()
694
+ tgt := now .Add (ttl )
695
+
696
+ if err = ttltxn .PutWithTTL (key , val , ttl ); err != nil {
697
+ t .Fatalf ("adding with ttl failed: %v" , err )
698
+ }
699
+
700
+ if err = txn .Commit (); err != nil {
701
+ t .Fatalf ("commiting transaction failed: %v" , err )
702
+ }
703
+
704
+ // Second transaction to retrieve expirations.
705
+ txn = d .NewTransaction (true )
706
+ ttltxn = txn .(ds.TTLDatastore )
707
+ defer txn .Discard ()
708
+
709
+ // GetExpiration returns expected value.
710
+ var dsExp time.Time
711
+ if dsExp , err = ttltxn .GetExpiration (key ); err != nil {
712
+ t .Fatalf ("getting expiration failed: %v" , err )
713
+ } else if tgt .Sub (dsExp ) >= 5 * time .Second {
714
+ t .Fatal ("expiration returned by datastore not within the expected range (tolerance: 5 seconds)" )
715
+ } else if tgt .Sub (dsExp ) < 0 {
716
+ t .Fatal ("expiration returned by datastore was earlier than expected" )
717
+ }
718
+
719
+ // Iterator returns expected value.
720
+ q := dsq.Query {
721
+ ReturnExpirations : true ,
722
+ KeysOnly : true ,
723
+ }
724
+ var ress dsq.Results
725
+ if ress , err = txn .Query (q ); err != nil {
726
+ t .Fatalf ("querying datastore failed: %v" , err )
727
+ }
728
+
729
+ defer ress .Close ()
730
+ if res , ok := ress .NextSync (); ! ok {
731
+ t .Fatal ("expected 1 result in iterator" )
732
+ } else if res .Expiration != dsExp {
733
+ t .Fatalf ("expiration returned from iterator differs from GetExpiration, expected: %v, actual: %v" , dsExp , res .Expiration )
734
+ }
735
+
736
+ if _ , ok := ress .NextSync (); ok {
737
+ t .Fatal ("expected no more results in iterator" )
738
+ }
739
+
740
+ // Datastore->GetExpiration()
741
+ if exp , err := d .GetExpiration (key ); err != nil {
742
+ t .Fatalf ("querying datastore failed: %v" , err )
743
+ } else if exp != dsExp {
744
+ t .Fatalf ("expiration returned from DB differs from that returned by txn, expected: %v, actual: %v" , dsExp , exp )
745
+ }
746
+
747
+ if _ , err := d .GetExpiration (ds .NewKey ("/foo/bar" )); err != ds .ErrNotFound {
748
+ t .Fatalf ("wrong error type: %v" , err )
749
+ }
750
+ }
0 commit comments