-
-
Notifications
You must be signed in to change notification settings - Fork 107
Improve support for F# Async return types #3986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| namespace TUnit.TestProject.FSharp | ||
|
|
||
| open System.Threading.Tasks | ||
| open TUnit.Assertions | ||
| open TUnit.Assertions.Extensions | ||
| open TUnit.Assertions.FSharp.Operations | ||
| open TUnit.Core | ||
|
|
||
| /// Tests to verify F# Async<'T> return types are properly executed | ||
| type AsyncTests() = | ||
|
|
||
| /// Static tracker to verify tests actually execute | ||
| static member val private ExecutionCount = 0 with get, set | ||
|
|
||
| [<Test>] | ||
| member _.FSharpAsync_BasicExecution() : Async<unit> = async { | ||
| AsyncTests.ExecutionCount <- 1 | ||
| do! Async.Sleep 10 | ||
| } | ||
|
|
||
| [<Test>] | ||
| [<DependsOn("FSharpAsync_BasicExecution")>] | ||
| member _.VerifyFSharpAsyncExecuted() = | ||
| // This test depends on the previous one and verifies it actually ran | ||
| if AsyncTests.ExecutionCount = 0 then | ||
| failwith "F# Async test did not execute!" | ||
| Task.CompletedTask | ||
|
|
||
| [<Test>] | ||
| member _.FSharpAsync_WithReturnValue() : Async<int> = async { | ||
| do! Async.Sleep 10 | ||
| return 42 | ||
| } | ||
|
Comment on lines
+30
to
+33
|
||
|
|
||
| [<Test>] | ||
| member _.FSharpAsync_WithAsyncSleep() : Async<unit> = async { | ||
| // Verify async operations work correctly | ||
| do! Async.Sleep 50 | ||
| } | ||
|
|
||
| [<Test>] | ||
| member _.FSharpAsync_CallingTask() : Async<unit> = async { | ||
| // F# Async calling Task-based API | ||
| do! Task.Delay(10) |> Async.AwaitTask | ||
| } | ||
|
|
||
| [<Test>] | ||
| member _.FSharpAsync_WithAssertion() : Async<unit> = async { | ||
| let result = 1 + 1 | ||
| do! check (Assert.That(result).IsEqualTo(2)) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unconditional use of
AsyncConvert.ConvertObject(result).AsTask()introduces a performance regression for void methods. The old code returnedTask.CompletedTask(a cached singleton), while the new code creates an async state machine allocation even for null results.Consider checking the return type or result before calling AsyncConvert:
This preserves the original performance for the common case of void methods while adding F# Async support.