@@ -607,18 +607,59 @@ public virtual async Task CanSetMinMaxExpirationAsync()
607607
608608 var expires = DateTime . MaxValue - now . AddDays ( 1 ) ;
609609 Assert . True ( await cache . SetAsync ( "test1" , 1 , expires ) ) ;
610- Assert . False ( await cache . SetAsync ( "test2" , 1 , DateTime . MinValue ) ) ;
611- Assert . True ( await cache . SetAsync ( "test3" , 1 , DateTime . MaxValue ) ) ;
612- Assert . True ( await cache . SetAsync ( "test4" , 1 , DateTime . MaxValue - now . AddDays ( - 1 ) ) ) ;
613-
614610 Assert . Equal ( 1 , ( await cache . GetAsync < int > ( "test1" ) ) . Value ) ;
615- Assert . InRange ( ( await cache . GetExpirationAsync ( "test1" ) ) . Value , expires . Subtract ( TimeSpan . FromSeconds ( 10 ) ) , expires ) ;
611+ var actualExpiration = await cache . GetExpirationAsync ( "test1" ) ;
612+ Assert . NotNull ( actualExpiration ) ;
613+ Assert . InRange ( actualExpiration . Value , expires . Subtract ( TimeSpan . FromSeconds ( 10 ) ) , expires ) ;
616614
615+ // MinValue expires items.
616+ Assert . False ( await cache . SetAsync ( "test2" , 1 , DateTime . MinValue ) ) ;
617617 Assert . False ( await cache . ExistsAsync ( "test2" ) ) ;
618+
619+ // MaxValue never expires.
620+ Assert . True ( await cache . SetAsync ( "test3" , 1 , DateTime . MaxValue ) ) ;
618621 Assert . Equal ( 1 , ( await cache . GetAsync < int > ( "test3" ) ) . Value ) ;
619- Assert . False ( ( await cache . GetExpirationAsync ( "test3" ) ) . HasValue ) ;
622+ actualExpiration = await cache . GetExpirationAsync ( "test3" ) ;
623+ Assert . NotNull ( actualExpiration ) ;
624+
625+ // Really high expiration value.
626+ Assert . True ( await cache . SetAsync ( "test4" , 1 , DateTime . MaxValue - now . AddDays ( - 1 ) ) ) ;
620627 Assert . Equal ( 1 , ( await cache . GetAsync < int > ( "test4" ) ) . Value ) ;
621- Assert . False ( ( await cache . GetExpirationAsync ( "test4" ) ) . HasValue ) ;
628+ actualExpiration = await cache . GetExpirationAsync ( "test4" ) ;
629+ Assert . NotNull ( actualExpiration ) ;
630+
631+ // No Expiration
632+ Assert . True ( await cache . SetAsync ( "test5" , 1 ) ) ;
633+ Assert . Null ( await cache . GetExpirationAsync ( "test5" ) ) ;
634+
635+ // Expire in an hour.
636+ var expiration = now . AddHours ( 1 ) ;
637+ await cache . SetExpirationAsync ( "test5" , expiration ) ;
638+ actualExpiration = await cache . GetExpirationAsync ( "test5" ) ;
639+ Assert . NotNull ( actualExpiration ) ;
640+ Assert . InRange ( actualExpiration . Value , expiration - expiration . Subtract ( TimeSpan . FromSeconds ( 5 ) ) , expiration - now ) ;
641+
642+ // Change expiration to MaxValue.
643+ await cache . SetExpirationAsync ( "test5" , DateTime . MaxValue ) ;
644+ Assert . NotNull ( actualExpiration ) ;
645+
646+ // Change expiration to MinValue.
647+ await cache . SetExpirationAsync ( "test5" , DateTime . MinValue ) ;
648+ Assert . Null ( await cache . GetExpirationAsync ( "test5" ) ) ;
649+ Assert . False ( await cache . ExistsAsync ( "test5" ) ) ;
650+
651+ // Ensure keys are not added as they are already expired
652+ Assert . Equal ( 0 , await cache . SetAllAsync ( new Dictionary < string , object >
653+ {
654+ { "test6" , 1 } ,
655+ { "test7" , 1 } ,
656+ { "test8" , 1 }
657+ } , DateTime . MinValue ) ) ;
658+
659+ // Expire time right now
660+ Assert . False ( await cache . SetAsync ( "test9" , 1 , now ) ) ;
661+ Assert . False ( await cache . ExistsAsync ( "test9" ) ) ;
662+ Assert . Null ( await cache . GetExpirationAsync ( "test9" ) ) ;
622663 }
623664 }
624665
0 commit comments