Skip to content

Commit b6168c7

Browse files
committed
Use data keys in order to track exceptions
1 parent cc6ca3d commit b6168c7

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

tests/FSharpPlus.Tests/Asyncs.fs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ module Async =
2929
if not isFailed && delay = 0 then async.Return value
3030
else
3131
async {
32-
if delay = 0 then do! Async.raise (TestException (sprintf "Ouch, can't create: %A" value ))
32+
let excn = TestException (sprintf "Ouch, can't create: %A" value)
33+
excn.Data.Add("key", value)
34+
if delay = 0 then do! Async.raise excn
3335
do! Async.Sleep delay
34-
if isFailed then do! Async.raise (TestException (sprintf "Ouch, can't create: %A" value ))
36+
if isFailed then do! Async.raise excn
3537
return value }
3638

3739

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

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

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

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

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

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

98100
[<Test>]

tests/FSharpPlus.Tests/Task.fs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ module Task =
1818
if not isFailed && delay = 0 then Task.FromResult value
1919
else
2020
let tcs = TaskCompletionSource<_> ()
21-
if delay = 0 then tcs.SetException (TestException (sprintf "Ouch, can't create: %A" value ))
21+
let excn = TestException (sprintf "Ouch, can't create: %A" value)
22+
excn.Data.Add("key", value)
23+
if delay = 0 then tcs.SetException (excn)
2224
else (Task.Delay delay).ContinueWith (fun _ ->
23-
if isFailed then tcs.SetException (TestException (sprintf "Ouch, can't create: %A" value )) else tcs.SetResult value) |> ignore
25+
if isFailed then tcs.SetException (excn) else tcs.SetResult value) |> ignore
2426
tcs.Task
2527

2628
let (|AggregateException|_|) (x: exn) =

tests/FSharpPlus.Tests/ValueTask.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ module ValueTask =
4040
if not isFailed && delay = 0 then ValueTask.result value
4141
else
4242
let tcs = TaskCompletionSource<_> ()
43-
if delay = 0 then tcs.SetException (TestException (sprintf "Ouch, can't create: %A" value ))
43+
let excn = TestException (sprintf "Ouch, can't create: %A" value)
44+
excn.Data.Add("key", value)
45+
46+
if delay = 0 then tcs.SetException (excn)
4447
else (Task.Delay delay).ContinueWith (fun _ ->
45-
if isFailed then tcs.SetException (TestException (sprintf "Ouch, can't create: %A" value )) else tcs.SetResult value) |> ignore
48+
if isFailed then tcs.SetException (excn) else tcs.SetResult value) |> ignore
4649
tcs.Task |> ValueTask<'T>
4750

4851
let (|AggregateException|_|) (x: exn) =

0 commit comments

Comments
 (0)