@@ -42,6 +42,14 @@ public void Constructor_should_initialize_properties()
4242 operationContext . ParentContext . Should ( ) . BeNull ( ) ;
4343 }
4444
45+ [ Fact ]
46+ public void Constructor_throws_on_negative_timeout ( )
47+ {
48+ var exception = Record . Exception ( ( ) => new OperationContext ( TimeSpan . FromSeconds ( - 5 ) , CancellationToken . None ) ) ;
49+
50+ exception . Should ( ) . BeOfType < ArgumentOutOfRangeException > ( ) ;
51+ }
52+
4553 [ Fact ]
4654 public void RemainingTimeout_should_calculate ( )
4755 {
@@ -68,16 +76,12 @@ public void RemainingTimeout_should_return_infinite_for_infinite_timeout()
6876 }
6977
7078 [ Fact ]
71- public void RemainingTimeout_could_be_negative ( )
79+ public void RemainingTimeout_should_return_zero_for_timeout_context ( )
7280 {
73- var timeout = TimeSpan . FromMilliseconds ( 5 ) ;
74- var stopwatch = Stopwatch . StartNew ( ) ;
81+ var operationContext = new OperationContext ( TimeSpan . FromMilliseconds ( 5 ) , CancellationToken . None ) ;
7582 Thread . Sleep ( 10 ) ;
76- stopwatch . Stop ( ) ;
7783
78- var operationContext = new OperationContext ( stopwatch , timeout , CancellationToken . None ) ;
79-
80- operationContext . RemainingTimeout . Should ( ) . Be ( timeout - stopwatch . Elapsed ) ;
84+ operationContext . RemainingTimeout . Should ( ) . Be ( TimeSpan . Zero ) ;
8185 }
8286
8387 [ Theory ]
@@ -276,6 +280,29 @@ public void WithTimeout_should_set_ParentContext()
276280
277281 resultContext. ParentContext . Should ( ) . Be ( operationContext ) ;
278282 }
283+
284+ [ Fact ]
285+ public void WithTimeout_should_create_timed_out_context_on_timed_out_context( )
286+ {
287+ var operationContext = new OperationContext( TimeSpan . FromMilliseconds ( 5 ) , CancellationToken . None ) ;
288+ Thread. Sleep ( 10 ) ;
289+ operationContext. IsTimedOut ( ) . Should ( ) . BeTrue ( ) ;
290+
291+ var resultContext = operationContext. WithTimeout ( TimeSpan . FromSeconds ( 10 ) ) ;
292+
293+ resultContext. IsTimedOut ( ) . Should ( ) . BeTrue ( ) ;
294+ }
295+
296+ [ Fact ]
297+ public void WithTimeout_throws_on_negative_timeout( )
298+ {
299+ var operationContext = new OperationContext( Timeout . InfiniteTimeSpan , CancellationToken . None ) ;
300+
301+ var exception = Record. Exception ( ( ) => operationContext . WithTimeout ( TimeSpan . FromSeconds ( - 5 ) ) ) ;
302+
303+ exception. Should ( ) . BeOfType < ArgumentOutOfRangeException > ( )
304+ . Subject . ParamName . Should ( ) . Be ( "timeout" ) ;
305+ }
279306 }
280307}
281308
0 commit comments