|
| 1 | +namespace FSharp.Libraries.UnitTests |
| 2 | + |
| 3 | +open System |
| 4 | +open NUnit.Framework |
| 5 | +open FSharp.Compiler.UnitTests |
| 6 | + |
| 7 | +[<TestFixture>] |
| 8 | +module AsyncTests = |
| 9 | + // Regression for FSHARP1.0:5969 |
| 10 | + // Async.StartChild: error when wait async is executed more than once |
| 11 | + [<Test>] |
| 12 | + let ``Execute Async multiple times``() = |
| 13 | + CompilerAssert.CompileExeAndRun |
| 14 | + """ |
| 15 | +module M |
| 16 | +
|
| 17 | +let a = async { |
| 18 | + let! a = Async.StartChild( |
| 19 | + async { |
| 20 | + do! Async.Sleep(1) |
| 21 | + return 27 |
| 22 | + }) |
| 23 | + let! result = Async.Parallel [ a; a; a; a ] |
| 24 | + return result |
| 25 | + } |> Async.RunSynchronously |
| 26 | +
|
| 27 | +exit 0 |
| 28 | + """ |
| 29 | + |
| 30 | + |
| 31 | + // Regression for FSHARP1.0:5970 |
| 32 | + // Async.StartChild: race in implementation of ResultCell in FSharp.Core |
| 33 | + [<Test>] |
| 34 | + let ``Joining StartChild``() = |
| 35 | + CompilerAssert.CompileExeAndRun |
| 36 | + """ |
| 37 | +module M |
| 38 | +
|
| 39 | +let Join (a1: Async<'a>) (a2: Async<'b>) = async { |
| 40 | + let! task1 = a1 |> Async.StartChild |
| 41 | + let! task2 = a2 |> Async.StartChild |
| 42 | + |
| 43 | + let! res1 = task1 |
| 44 | + let! res2 = task2 |
| 45 | + return (res1,res2) } |
| 46 | +
|
| 47 | +let r = |
| 48 | + try |
| 49 | + Async.RunSynchronously (Join (async { do! Async.Sleep(30) |
| 50 | + failwith "fail" |
| 51 | + return 3+3 }) |
| 52 | + (async { do! Async.Sleep(30) |
| 53 | + return 2 + 2 } )) |
| 54 | + with _ -> |
| 55 | + (0,0) |
| 56 | +
|
| 57 | +exit 0 |
| 58 | +
|
| 59 | + """ |
| 60 | + |
| 61 | + // Regression test for FSHARP1.0:6086 |
| 62 | + [<Test>] |
| 63 | + let ``Mailbox Async dot not StackOverflow``() = |
| 64 | + CompilerAssert.CompileExeAndRun |
| 65 | + """ |
| 66 | +open Microsoft.FSharp.Control |
| 67 | +
|
| 68 | +type Color = Blue | Red | Yellow |
| 69 | +let complement = function |
| 70 | + | (Red, Yellow) | (Yellow, Red) -> Blue |
| 71 | + | (Red, Blue) | (Blue, Red) -> Yellow |
| 72 | + | (Yellow, Blue) | (Blue, Yellow) -> Red |
| 73 | + | (Blue, Blue) -> Blue |
| 74 | + | (Red, Red) -> Red |
| 75 | + | (Yellow, Yellow) -> Yellow |
| 76 | +
|
| 77 | +type Message = Color * AsyncReplyChannel<Color option> |
| 78 | +
|
| 79 | +let chameleon (meetingPlace : MailboxProcessor<Message>) initial = |
| 80 | + let rec loop c meets = async { |
| 81 | + let replyMessage = meetingPlace.PostAndReply(fun reply -> c, reply) |
| 82 | + match replyMessage with |
| 83 | + | Some(newColor) -> return! loop newColor (meets + 1) |
| 84 | + | None -> return meets |
| 85 | + } |
| 86 | + loop initial 0 |
| 87 | + |
| 88 | +let meetingPlace chams n = MailboxProcessor.Start(fun (processor : MailboxProcessor<Message>)-> |
| 89 | + let rec fadingLoop total = |
| 90 | + async { |
| 91 | + if total <> 0 then |
| 92 | + let! (_, reply) = processor.Receive() |
| 93 | + reply.Reply None |
| 94 | + return! fadingLoop (total - 1) |
| 95 | + else |
| 96 | + printfn "Done" |
| 97 | + } |
| 98 | + let rec mainLoop curr = |
| 99 | + async { |
| 100 | + if (curr > 0) then |
| 101 | + let! (color1, reply1) = processor.Receive() |
| 102 | + let! (color2, reply2) = processor.Receive() |
| 103 | + let newColor = complement (color1, color2) |
| 104 | + reply1.Reply <| Some(newColor) |
| 105 | + reply2.Reply <| Some(newColor) |
| 106 | + return! mainLoop (curr - 1) |
| 107 | + else |
| 108 | + return! fadingLoop chams |
| 109 | + } |
| 110 | + mainLoop n |
| 111 | + ) |
| 112 | +
|
| 113 | +open System |
| 114 | +open System.Diagnostics |
| 115 | +
|
| 116 | +let meetings = 100000 |
| 117 | +
|
| 118 | +let colors = [Blue; Red; Yellow; Blue] |
| 119 | +let mp = meetingPlace (colors.Length) meetings |
| 120 | +let watch = Stopwatch.StartNew() |
| 121 | +let meets = |
| 122 | + colors |
| 123 | + |> List.map (chameleon mp) |
| 124 | + |> Async.Parallel |
| 125 | + |> Async.RunSynchronously |
| 126 | +watch.Stop() |
| 127 | +for meet in meets do |
| 128 | + printfn "%d" meet |
| 129 | +printfn "Total: %d in %O" (Seq.sum meets) (watch.Elapsed) |
| 130 | +
|
| 131 | +exit 0 |
| 132 | + """ |
| 133 | + |
| 134 | + // Regression for FSHARP1.0:5971 |
| 135 | + [<Test>] |
| 136 | + let ``StartChild do not throw ObjectDisposedException``() = |
| 137 | + CompilerAssert.CompileExeAndRun |
| 138 | + """ |
| 139 | +module M |
| 140 | +
|
| 141 | +let b = async {return 5} |> Async.StartChild |
| 142 | +printfn "%A" (b |> Async.RunSynchronously |> Async.RunSynchronously) |
| 143 | +
|
| 144 | +exit 0 |
| 145 | + """ |
| 146 | + |
| 147 | + |
| 148 | + [<Test>] |
| 149 | + let ``StartChild test Trampoline HijackLimit``() = |
| 150 | + CompilerAssert.CompileExeAndRun |
| 151 | + """ |
| 152 | +module M |
| 153 | +
|
| 154 | +let r = |
| 155 | + async { |
| 156 | + let! a = Async.StartChild( |
| 157 | + async { |
| 158 | + do! Async.Sleep(1) |
| 159 | + return 5 |
| 160 | + } |
| 161 | + ) |
| 162 | + let! _ = a |
| 163 | + for __ in 1..10000 do // 10000 > bindHijackLimit |
| 164 | + () |
| 165 | + } |> Async.RunSynchronously |
| 166 | +
|
| 167 | +exit 0 |
| 168 | + """ |
0 commit comments