Skip to content

Commit cea2a46

Browse files
authored
Merge pull request #6 from insurello/fromTeeTask
Add fromUnitTask helper
2 parents 2ba6eaa + a246fb8 commit cea2a46

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Insurello.AsyncExtra.Tests/AsyncExtraTests.fs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module AsyncExtraTests
22

3+
open System.Threading
4+
open System.Threading.Tasks
35
open Insurello.AsyncExtra
46
open Expecto
57

68
[<Tests>]
79
let tests =
8-
testList "Sequence tests"
10+
testList
11+
"Sequence tests"
912
[ testAsync "should return values in same order as given tasks" {
1013
let dummyAsync: int -> AsyncResult<int, string> = Ok >> AsyncResult.fromResult
1114

@@ -15,6 +18,7 @@ let tests =
1518
[ (dummyAsync 1)
1619
(dummyAsync 2)
1720
(dummyAsync 3) ]
21+
1822
let! actual = AsyncResult.sequence input
1923
Expect.equal actual expected "should equal"
2024
}
@@ -37,3 +41,57 @@ let tests =
3741
let! _actual = AsyncResult.sequence input
3842
Expect.equal orderRun expectedOkValue "Should be run in same order"
3943
} ]
44+
45+
[<Tests>]
46+
let taskTests =
47+
testList
48+
"Task tests"
49+
[ testList
50+
"fromUnitTask"
51+
[ testAsync "should convert from Task to AsyncResult" {
52+
let source = new CancellationTokenSource()
53+
let input: Task = Task.Delay(0, source.Token)
54+
55+
let expectedValue = Ok()
56+
57+
let! actual = AsyncResult.fromUnitTask input
58+
59+
Expect.equal actual expectedValue "Should be equal"
60+
}
61+
testAsync "failing Task should result in Error" {
62+
let source = new CancellationTokenSource()
63+
let input: Task = Task.Delay(1000, source.Token)
64+
let expectedValue = Error "A task was canceled."
65+
66+
source.Cancel()
67+
68+
let! actual = AsyncResult.fromUnitTask input
69+
70+
Expect.equal actual expectedValue "Should be equal"
71+
} ]
72+
testList
73+
"fromTask"
74+
[ testAsync "should convert from Task<string> to AsyncResult" {
75+
let input =
76+
Async.singleton "Hello" |> Async.StartAsTask
77+
78+
let expectedValue = Ok "Hello"
79+
80+
let! actual = AsyncResult.fromTask input
81+
82+
Expect.equal actual expectedValue "Should be equal"
83+
}
84+
testAsync "fromTask failing Task should result in Error" {
85+
86+
let input =
87+
Async.singleton "Hello"
88+
|> Async.map (fun _ -> failwith "boom")
89+
|> Async.StartAsTask
90+
91+
let expectedValue =
92+
Error "One or more errors occurred. (boom)"
93+
94+
let! actual = AsyncResult.fromTask input
95+
96+
Expect.equal actual expectedValue "Should be equal"
97+
} ] ]

src/Insurello.AsyncExtra/AsyncExtra.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ module AsyncResult =
3030
| Choice1Of2 response -> Ok response
3131
| Choice2Of2 exn -> Error exn.Message)
3232

33+
let fromUnitTask: System.Threading.Tasks.Task -> AsyncResult<unit, string> =
34+
fun task ->
35+
task
36+
|> Async.AwaitTask
37+
|> Async.Catch
38+
|> Async.map (function
39+
| Choice1Of2 response -> Ok response
40+
| Choice2Of2 exn -> Error exn.Message)
41+
3342
let map: ('x -> 'y) -> AsyncResult<'x, 'err> -> AsyncResult<'y, 'err> =
3443
fun f asyncResultX -> Async.map (Result.map f) asyncResultX
3544

0 commit comments

Comments
 (0)