Skip to content

Commit

Permalink
incorporate feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Dec 17, 2015
1 parent 4f44983 commit e758c51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ open FSharp.Core.Unittests.LibraryTestFx
open NUnit.Framework
#if !(FSHARP_CORE_PORTABLE || FSHARP_CORE_NETCORE_PORTABLE)
open FsCheck
#endif

#if !(FSHARP_CORE_PORTABLE || FSHARP_CORE_NETCORE_PORTABLE)
[<AutoOpen>]
module ChoiceUtils =

// FsCheck driven Async.Choice tests
// FsCheck driven Async.Choice specification test

exception ChoiceExn of index:int

Expand All @@ -24,7 +26,7 @@ module ChoiceUtils =
| NoneResultAfter of timeout:int
| SomeResultAfter of timeout:int
| ExceptionAfter of timeout:int
with

member c.Timeout =
match c with
| NoneResultAfter t -> t
Expand Down Expand Up @@ -61,7 +63,7 @@ module ChoiceUtils =
let mkOp (index : int) = function
| NoneResultAfter t -> returnAfter t (fun () -> None)
| SomeResultAfter t -> returnAfter t (fun () -> Some index)
| ExceptionAfter t -> returnAfter t (fun () -> raise <| ChoiceExn index)
| ExceptionAfter t -> returnAfter t (fun () -> raise (ChoiceExn index))

let choiceWorkflow = ops |> List.mapi mkOp |> Async.Choice

Expand Down Expand Up @@ -92,7 +94,7 @@ module ChoiceUtils =
} |> Seq.min

let verifyIndex index =
if index < 0 || index >= ops.Length then
if index < 0 || index >= ops.Length then
Assert.Fail "Returned choice index is out of bounds."

// Step 3a. check that output is up to spec
Expand Down Expand Up @@ -413,8 +415,8 @@ type AsyncModule() =
testErrorAndCancelRace (Async.Sleep (-5))

#if !(FSHARP_CORE_PORTABLE || FSHARP_CORE_NETCORE_PORTABLE)
[<Test>]
member this.``Async.Choice correctness and cancellation``() =
[<Test; Category("CHOICE")>]
member this.``Async.Choice specification test``() =
Check.QuickThrowOnFailure (normalize >> runChoice)
#endif

Expand Down
20 changes: 12 additions & 8 deletions src/fsharp/FSharp.Core/control.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1465,13 +1465,12 @@ namespace Microsoft.FSharp.Control
static member Choice(computations : Async<'T option> seq) : Async<'T option> =
unprotectedPrimitive(fun args ->
let result =
try Choice1Of2 <| Seq.toArray computations
with exn -> Choice2Of2 <| ExceptionDispatchInfo.RestoreOrCapture(exn)
try Seq.toArray computations |> Choice1Of2
with exn -> ExceptionDispatchInfo.RestoreOrCapture exn |> Choice2Of2

match result with
| Choice2Of2 edi -> args.aux.econt edi
| Choice1Of2 [||] -> args.cont None
| Choice1Of2 [|P body|] -> body args
| Choice1Of2 computations ->
protectedPrimitiveCore args (fun args ->
let ({ aux = aux } as args) = delimitSyncContext args
Expand All @@ -1482,12 +1481,17 @@ namespace Microsoft.FSharp.Control

let scont (result : 'T option) =
match result with
| Some _ when Interlocked.Increment exnCount = 1 ->
innerCts.Cancel(); trampolineHolder.Protect(fun () -> args.cont result)
| None when Interlocked.Increment noneCount = computations.Length ->
innerCts.Cancel(); trampolineHolder.Protect(fun () -> args.cont None)
| Some _ ->
if Interlocked.Increment exnCount = 1 then
innerCts.Cancel(); trampolineHolder.Protect(fun () -> args.cont result)
else
FakeUnit

| _ -> FakeUnit
| None ->
if Interlocked.Increment noneCount = computations.Length then
innerCts.Cancel(); trampolineHolder.Protect(fun () -> args.cont None)
else
FakeUnit

let econt (exn : ExceptionDispatchInfo) =
if Interlocked.Increment exnCount = 1 then
Expand Down

0 comments on commit e758c51

Please sign in to comment.