@@ -42,6 +42,14 @@ public void Constructor_should_initialize_properties()
42
42
operationContext . ParentContext . Should ( ) . BeNull ( ) ;
43
43
}
44
44
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
+
45
53
[ Fact ]
46
54
public void RemainingTimeout_should_calculate ( )
47
55
{
@@ -68,16 +76,12 @@ public void RemainingTimeout_should_return_infinite_for_infinite_timeout()
68
76
}
69
77
70
78
[ Fact ]
71
- public void RemainingTimeout_could_be_negative ( )
79
+ public void RemainingTimeout_should_return_zero_for_timeout_context ( )
72
80
{
73
- var timeout = TimeSpan . FromMilliseconds ( 5 ) ;
74
- var stopwatch = Stopwatch . StartNew ( ) ;
81
+ var operationContext = new OperationContext ( TimeSpan . FromMilliseconds ( 5 ) , CancellationToken . None ) ;
75
82
Thread . Sleep ( 10 ) ;
76
- stopwatch . Stop ( ) ;
77
83
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 ) ;
81
85
}
82
86
83
87
[ Theory ]
@@ -276,6 +280,29 @@ public void WithTimeout_should_set_ParentContext()
276
280
277
281
resultContext. ParentContext . Should ( ) . Be ( operationContext ) ;
278
282
}
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
+ }
279
306
}
280
307
}
281
308
0 commit comments