1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
// #Conformance #ComputationExpressions #Async
#if ALL_IN_ONE
module Core_controlStackOverflow
#endif
#light
#nowarn "40" // recursive references
#if NetCore
open System.Threading.Tasks
#endif
let biggerThanTrampoliningLimit = 10000
let failuresFile =
let f = System.Environment.GetEnvironmentVariable("CONTROL_FAILURES_LOG")
match f with
| "" | null -> "failures.log"
| _ -> f
printfn "failures file: %s" failuresFile
let log msg =
printfn "%s" msg
#if Portable
#else
System.IO.File.AppendAllText(failuresFile, sprintf "%A: %s\r\n" System.DateTime.Now msg)
#endif
let mutable failures = []
let syncObj = new obj()
let report_failure s =
stderr.WriteLine " NO";
lock syncObj (fun () ->
failures <- s :: failures;
log (sprintf "FAILURE: %s failed" s)
)
#if Portable
#else
System.AppDomain.CurrentDomain.UnhandledException.AddHandler(
fun _ (args:System.UnhandledExceptionEventArgs) ->
lock syncObj (fun () ->
failures <- (args.ExceptionObject :?> System.Exception).ToString() :: failures
)
)
#endif
let test s b = stderr.Write(s:string); if b then stderr.WriteLine " OK" else report_failure s
let checkQuiet s x1 x2 =
if x1 <> x2 then
(test s false; printfn "expected: %A, got %A" x2 x1)
let check s x1 x2 =
if x1 = x2 then test s true
else (test s false; printfn "expected: %A, got %A" x2 x1)
#if NetCore
#else
let argv = System.Environment.GetCommandLineArgs()
let SetCulture() =
if argv.Length > 2 && argv.[1] = "--culture" then begin
let cultureString = argv.[2] in
let culture = new System.Globalization.CultureInfo(cultureString) in
stdout.WriteLine ("Running under culture "+culture.ToString()+"...");
System.Threading.Thread.CurrentThread.CurrentCulture <- culture
end
do SetCulture()
#endif
(*******************************************************************************)
open Microsoft.FSharp.Control
open Microsoft.FSharp.Control.WebExtensions
module StackDiveTests =
[<Sealed>]
type AsyncResultCell<'T>() =
let mutable result = None
let mutable savedConts : ('T -> unit) list = []
let syncRoot = new obj()
member x.RegisterResult (res:'T) =
let grabbedConts =
lock syncRoot (fun () ->
if result.IsSome then
[]
else
result <- Some res;
List.rev savedConts)
match grabbedConts with
| [] -> ()
| [cont] -> cont res
| otherwise -> failwith "other"
member x.AsyncResult =
Async.FromContinuations(fun (cont,_,_) ->
let grabbedResult =
lock syncRoot (fun () ->
match result with
| Some res ->
result
| None ->
savedConts <- cont::savedConts
None)
match grabbedResult with
| None -> ()
| Some res -> cont res)
let test() =
let Main() =
async {
// Create the channels
let channels = [| for i in 0 .. 100000 -> new AsyncResultCell<int>() |]
// For each channel pair, start a task that takes the result from the right and sends it to the left
channels
|> Seq.pairwise
|> Seq.iter (fun (c1,c2) ->
Async.Start (async { let! res = c2.AsyncResult
c1.RegisterResult(res + 1); }))
// Send the first message to the last on the right
channels.[100000].RegisterResult(0);
// Wait for the result from the first on the left
let! res = channels.[0].AsyncResult
return res
} |> Async.RunSynchronously
check "albatross"
(Main())
100000
let rec so n =
async { if n = 0 then return 1
else
let! res = so (n-1)
return res + 1 }
check "whale-1"
(so 1000000 |> Async.RunSynchronously)
1000001
let rec so2 n =
async { if n = 0 then return 1
else return! so2 (n-1) }
check "whale-2"
(so2 1000000 |> Async.RunSynchronously)
1
let rec so3 n =
async { try
if n = 0 then return 1
else return! so3 (n-1)
with e -> return 3 }
check "whale-3"
(so3 1000000 |> Async.RunSynchronously)
1
let rec so4 n =
async { try
if n = 0 then return 1
else return! so4 (n-1)
finally
() }
check "whale-4"
(so4 1000000 |> Async.RunSynchronously)
1
let justRun x = Async.FromContinuations(fun (c,e,cc) -> c x)
let rec so5 n =
async {
if n = 0 then return 1
else
let! x = justRun n
return! so5 (n-1)
}
check "whale-5"
(so5 1000000 |> Async.RunSynchronously)
1
let contAsync x =
Async.FromContinuations(fun (c,e,cc) ->
Async.StartWithContinuations (async { return x },c,e,cc))
let rec so6 n =
async {
if n = 0 then return 6
else
let! x = contAsync n
return! so6 (n-1)
}
check "whale-6"
(so6 1000000 |> Async.RunSynchronously)
6
let throwingAsync =
Async.FromContinuations(fun (c,e,_) -> e <| System.InvalidOperationException())
let rec so7 n =
async {
if n = 0 then return 7
else
try
do! throwingAsync
with
| :? System.InvalidOperationException ->
let! _ = so7 (n-1)
()
return 85
}
check "whale-7"
(so7 100000 |> Async.RunSynchronously)
85
let throwingAsync' =
Async.FromContinuations(fun (c,e,_) -> raise <| System.InvalidOperationException())
let rec so7' n =
async {
if n = 0 then return "7'"
else
try
do! throwingAsync'
with
| :? System.InvalidOperationException ->
let! _ = so7' (n-1)
()
return "7'"
}
check "whale-7'"
(so7' 100000 |> Async.RunSynchronously)
"7'"
let quwiAsync x =
Async.FromContinuations(fun (c,_,_) ->
#if NetCore
Task.Run(
fun _ ->
async {
do c x
return()
} |> Async.StartImmediate
) |> ignore)
#else
System.Threading.ThreadPool.QueueUserWorkItem(
fun _ ->
async {
do c x
return()
} |> Async.StartImmediate
) |> ignore)
#endif
let rec so8 n =
async {
if n = 0 then return 8
else
let! n = quwiAsync (n-1)
for i in 1..biggerThanTrampoliningLimit do
()
return! so8 n
}
check "whale-8"
(so8 10000 |> Async.RunSynchronously)
8
module ReturnStackoverflow =
let test () =
let rec so n =
async { if n = 0 then return 1
else
try
return! so (n-1)
finally () }
check "so-return"
(so 2000000 |> Async.RunSynchronously)
1
let rec so2 n =
async { if n = 0 then new System.InvalidOperationException() |> raise
else
try
return! so2 (n-1)
finally () }
check "so-return2"
(try
so2 2000000 |> Async.RunSynchronously
"fail"
with
| :? System.InvalidOperationException -> "success")
"success"
let rec so3 n =
async { if n = 0 then return 1
else
try
return! so3 (n-1)
with _ -> return 3 }
check "so-return3"
(so3 2000000 |> Async.RunSynchronously)
1
let rec so4 n =
async { if n = 0 then return (new System.InvalidOperationException() |> raise)
else
try
return! so4 (n-1)
with _ -> return 1 }
check "so-return4"
(so4 2000000 |> Async.RunSynchronously)
1
let rec so5 n =
async { if n = 0 then return 1
else
try
raise (new System.InvalidOperationException())
return 27
with _ ->
let! i = so5 (n-1)
return i
}
check "so-return5"
(so5 1000000 |> Async.RunSynchronously)
1
let rec so6 n =
async { if n = 0 then raise (new System.InvalidOperationException())
else
try
raise (new System.InvalidOperationException())
with _ ->
let! i = so6 (n-1)
return i
}
check "so-return6"
(try
so6 1000000 |> Async.RunSynchronously
1
with
| :? System.InvalidOperationException -> 42)
42
let cts = new System.Threading.CancellationTokenSource() in
let rec so7 n =
Async.TryCancelled(
async {
if n = 0 then
cts.Cancel()
return ()
else
do! so7 (n-1)
},
fun cexn -> ()
) in
check "so-return7"
(try
Async.RunSynchronously(so7 100000,cancellationToken=cts.Token)
"fail"
with
| :? System.OperationCanceledException -> "success")
"success"
let rec so8 n =
async { if n = 0 then return (new System.InvalidOperationException() |> raise)
else
try
return! so8 (n-1)
with _ ->
raise (new System.InvalidOperationException())
return 1 }
check "so-return8"
(try
so8 2000000 |> Async.RunSynchronously
with
| :? System.InvalidOperationException -> 56
)
56
let cts1 = new System.Threading.CancellationTokenSource() in
let rec so9 n =
Async.TryCancelled(
async {
if n = 0 then
cts1.Cancel()
return ()
else
do! so9 (n-1)
},
fun cexn -> raise (new System.InvalidOperationException())
) in
check "so-return9"
(try
Async.RunSynchronously(so9 2000000,cancellationToken=cts1.Token)
"fail"
with
| :? System.OperationCanceledException -> "success")
"success"
(*******************************************************************************)
let RunAll() =
StackDiveTests.test()
ReturnStackoverflow.test()
#if ALL_IN_ONE
let RUN() = RunAll(); failures
#else
RunAll()
let aa =
if not failures.IsEmpty then
stdout.WriteLine "Test Failed"
exit 1
else
stdout.WriteLine "Test Passed"
log "ALL OK, HAPPY HOLIDAYS, MERRY CHRISTMAS!"
System.IO.File.WriteAllText("test.ok","ok")
exit 0
#endif
|