Skip to content

Commit

Permalink
Use data keys in order to track exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
wallymathieu committed Feb 13, 2024
1 parent cc6ca3d commit cf4d4b5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
14 changes: 8 additions & 6 deletions tests/FSharpPlus.Tests/Asyncs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ module Async =
if not isFailed && delay = 0 then async.Return value
else
async {
if delay = 0 then do! Async.raise (TestException (sprintf "Ouch, can't create: %A" value ))
let excn = TestException (sprintf "Ouch, can't create: %A" value)
excn.Data.Add("key", value)
if delay = 0 then do! Async.raise excn
do! Async.Sleep delay
if isFailed then do! Async.raise (TestException (sprintf "Ouch, can't create: %A" value ))
if isFailed then do! Async.raise excn
return value }


Expand All @@ -54,15 +56,15 @@ module Async =
let t12123 = Async.zip3 t12t12 t33 t4
let ac1 =
try
Async.AsTaskAndWait(t12123).Exception.InnerExceptions |> Seq.map (fun x -> int (Char.GetNumericValue x.Message.[35]))
Async.AsTaskAndWait(t12123).Exception.InnerExceptions |> Seq.map (fun x -> x.Data["key"] :?> int)
with e ->
failwithf "Failure in testAsyncZip. Async status is %A . Exception is %A" (Async.AsTaskAndWait t12123).Status e

CollectionAssert.AreEquivalent ([1; 2; 1; 2; 3], ac1, "Async.zip(3) should add only non already existing exceptions.")

let t13 = Async.zip3 (Async.zip t1 t3) t4 (Async.zip t5 t6)
Assert.AreEqual (true, Async.AsTaskAndWait(t13).IsFaulted, "Async.zip(3) between a value, an exception and a cancellation -> exception wins.")
let ac2 = Async.AsTaskAndWait(t13).Exception.InnerExceptions |> Seq.map (fun x -> int (Char.GetNumericValue x.Message.[35]))
let ac2 = Async.AsTaskAndWait(t13).Exception.InnerExceptions |> Seq.map (fun x -> x.Data["key"] :?> int)
CollectionAssert.AreEquivalent ([1; 3], ac2, "Async.zip between 2 exceptions => both exceptions returned, even after combining with cancellation and values.")

[<Test>]
Expand All @@ -84,15 +86,15 @@ module Async =
let t12123 = Async.zip3 t12t12 t33 t4
let ac1 =
try
Async.AsTaskAndWait(t12123).Exception.InnerExceptions |> Seq.map (fun x -> int (Char.GetNumericValue x.Message.[35]))
Async.AsTaskAndWait(t12123).Exception.InnerExceptions |> Seq.map (fun x -> x.Data["key"] :?> int)
with e ->
failwithf "Failure in testAsyncZipAsync. Async status is %A . Exception is %A" (Async.AsTaskAndWait t12123).Status e

CollectionAssert.AreEquivalent ([1; 2; 1; 2; 3], ac1, "Async.zip(3)Async should add only non already existing exceptions.")

let t13 = Async.zip3 (Async.zip t1 t3) t4 (Async.zip t5 t6)
Assert.AreEqual (true, Async.AsTaskAndWait(t13).IsFaulted, "Async.zip(3)Async between a value, an exception and a cancellation -> exception wins.")
let ac2 = Async.AsTaskAndWait(t13).Exception.InnerExceptions |> Seq.map (fun x -> int (Char.GetNumericValue x.Message.[35]))
let ac2 = Async.AsTaskAndWait(t13).Exception.InnerExceptions |> Seq.map (fun x -> x.Data["key"] :?> int)
CollectionAssert.AreEquivalent ([1; 3], ac2, "Async.zipAsync between 2 exceptions => both exceptions returned, even after combining with cancellation and values.")

[<Test>]
Expand Down
6 changes: 4 additions & 2 deletions tests/FSharpPlus.Tests/Task.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ module Task =
if not isFailed && delay = 0 then Task.FromResult value
else
let tcs = TaskCompletionSource<_> ()
if delay = 0 then tcs.SetException (TestException (sprintf "Ouch, can't create: %A" value ))
let excn = TestException (sprintf "Ouch, can't create: %A" value)
excn.Data.Add("key", value)
if delay = 0 then tcs.SetException (excn)
else (Task.Delay delay).ContinueWith (fun _ ->
if isFailed then tcs.SetException (TestException (sprintf "Ouch, can't create: %A" value )) else tcs.SetResult value) |> ignore
if isFailed then tcs.SetException (excn) else tcs.SetResult value) |> ignore
tcs.Task

let (|AggregateException|_|) (x: exn) =
Expand Down
7 changes: 5 additions & 2 deletions tests/FSharpPlus.Tests/ValueTask.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ module ValueTask =
if not isFailed && delay = 0 then ValueTask.result value
else
let tcs = TaskCompletionSource<_> ()
if delay = 0 then tcs.SetException (TestException (sprintf "Ouch, can't create: %A" value ))
let excn = TestException (sprintf "Ouch, can't create: %A" value)
excn.Data.Add("key", value)

if delay = 0 then tcs.SetException (excn)
else (Task.Delay delay).ContinueWith (fun _ ->
if isFailed then tcs.SetException (TestException (sprintf "Ouch, can't create: %A" value )) else tcs.SetResult value) |> ignore
if isFailed then tcs.SetException (excn) else tcs.SetResult value) |> ignore
tcs.Task |> ValueTask<'T>

let (|AggregateException|_|) (x: exn) =
Expand Down

0 comments on commit cf4d4b5

Please sign in to comment.