Skip to content
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

Improve StartWithContinuations test in AsyncType tests #17767

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

majocha
Copy link
Contributor

@majocha majocha commented Sep 20, 2024

Use a local cancellation token instead of Async.CancelDefaultToken() to cancel just one computation.

When running tests in parallel I've seen OperationCancelledException raised in unrelated tests of the same assembly.
It seems despite the fact this test collection has DisableParallelization = true, xUnit sometimes can't perfectly isolate the test cases.

@majocha majocha requested a review from a team as a code owner September 20, 2024 06:59
Copy link
Contributor

✅ No release notes required

@majocha
Copy link
Contributor Author

majocha commented Sep 20, 2024

New version goes like this:

module AsyncType =

    type ExpectedContinuation = Success | Exception | Cancellation

    [<Fact>]
    let startWithContinuations() =

        let cont actual expected _ =
            if expected <> actual then
                failwith $"expected {expected} continuation, but ran {actual}"

        let onSuccess = cont Success
        let onExeption = cont Exception
        let onCancellation = cont Cancellation

        let expect expected cancellationToken computation =
            Async.StartWithContinuations(computation, onSuccess expected, onExeption expected, onCancellation expected, ?cancellationToken = cancellationToken)

        let cancelledToken =
            let cts = new CancellationTokenSource()
            cts.Cancel()
            Some cts.Token

        async { return () } |> expect Cancellation cancelledToken

        async { failwith "computation failed" } |> expect Exception None

        async { return () } |> expect Success None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New
Development

Successfully merging this pull request may close these issues.

1 participant