@@ -89,9 +89,15 @@ type AsyncType() =
8989 Async.RunSynchronously( computation, timeout = 1000 ) |> ignore
9090
9191 [<Test>]
92- member this.AsyncSleepCancellation1 () =
92+ [<TestCase( " int32" ) >]
93+ [<TestCase( " timespan" ) >]
94+ member this.AsyncSleepCancellation1 ( sleepType ) =
9395 ignoreSynchCtx ( fun () ->
94- let computation = Async.Sleep( 10000000 )
96+ let computation =
97+ match sleepType with
98+ | " int32" -> Async.Sleep( 10000000 )
99+ | " timespan" -> Async.Sleep( 10000000.0 |> TimeSpan.FromMilliseconds)
100+ | unknown -> raise ( NotImplementedException( unknown))
95101 let result = ref " "
96102 use cts = new CancellationTokenSource()
97103 Async.StartWithContinuations( computation,
@@ -105,9 +111,15 @@ type AsyncType() =
105111 )
106112
107113 [<Test>]
108- member this.AsyncSleepCancellation2 () =
114+ [<TestCase( " int32" ) >]
115+ [<TestCase( " timespan" ) >]
116+ member this.AsyncSleepCancellation2 ( sleepType ) =
109117 ignoreSynchCtx ( fun () ->
110- let computation = Async.Sleep( 10 )
118+ let computation =
119+ match sleepType with
120+ | " int32" -> Async.Sleep( 10 )
121+ | " timespan" -> Async.Sleep( 10.0 |> TimeSpan.FromMilliseconds)
122+ | unknown -> raise ( NotImplementedException( unknown))
111123 for i in 1 .. 100 do
112124 let result = ref " "
113125 use completedEvent = new ManualResetEvent( false )
@@ -123,6 +135,36 @@ type AsyncType() =
123135 Assert.IsTrue(! result = " Cancel" || ! result = " Ok" )
124136 )
125137
138+ [<Test>]
139+ [<TestCase( " int32" ) >]
140+ [<TestCase( " timespan" ) >]
141+ member this.AsyncSleepThrowsOnNegativeDueTimes ( sleepType ) =
142+ async {
143+ try
144+ do ! match sleepType with
145+ | " int32" -> Async.Sleep(- 100 )
146+ | " timespan" -> Async.Sleep(- 100.0 |> TimeSpan.FromMilliseconds)
147+ | unknown -> raise ( NotImplementedException( unknown))
148+ failwith " Expected ArgumentOutOfRangeException"
149+ with
150+ | : ? ArgumentOutOfRangeException -> ()
151+ } |> Async.RunSynchronously
152+
153+ [<Test>]
154+ member this.AsyncSleepInfinitely () =
155+ ignoreSynchCtx ( fun () ->
156+ let computation = Async.Sleep( System.Threading.Timeout.Infinite)
157+ let result = TaskCompletionSource()
158+ use cts = new CancellationTokenSource( TimeSpan.FromSeconds( 1.0 )) // there's a long way from 1 sec to infinity, but it'll have to do.
159+ Async.StartWithContinuations( computation,
160+ ( fun _ -> result.TrySetResult( " Ok" ) |> ignore),
161+ ( fun _ -> result.TrySetResult( " Exception" ) |> ignore),
162+ ( fun _ -> result.TrySetResult( " Cancel" ) |> ignore),
163+ cts.Token)
164+ let result = result.Task |> Async.AwaitTask |> Async.RunSynchronously
165+ Assert.AreEqual( " Cancel" , result)
166+ )
167+
126168 member private this.WaitASec ( t : Task ) =
127169 let result = t.Wait( TimeSpan( hours= 0 , minutes= 0 , seconds= 1 ))
128170 Assert.IsTrue( result, " Task did not finish after waiting for a second." )
0 commit comments