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

Change cancellation test to not depend on how AwaitTask works #7467

Merged
merged 2 commits into from
Sep 27, 2019

Conversation

Frassle
Copy link
Contributor

@Frassle Frassle commented Aug 29, 2019

This test is to check that when an Async is started as a Task (via
StartAsTask) that when cancelled the Task isn't immediately marked as
cancelled but instead waits for the underlying Async to finish (normally
via cancellation)

Prompted from comments on #7357.

@Frassle
Copy link
Contributor Author

Frassle commented Aug 29, 2019

@matthid I think this test is still testing what you intended it to.

@matthid
Copy link
Contributor

matthid commented Aug 29, 2019

@matthid I think this test is still testing what you intended it to.

No, the test was exactly how I intended it ;)

This test is to check that when an Async is started as a Task (via
StartAsTask) that when cancelled the Task isn't immediately marked as
cancelled but instead waits for the underlying Async to finish (normally
via cancellation)
@Frassle
Copy link
Contributor Author

Frassle commented Aug 29, 2019

Bit sad that it tested both the behaviour of StartAsTask and AwaitTask in the same test. How about now, split into two tests, does that still test what you intended?

@matthid
Copy link
Contributor

matthid commented Aug 29, 2019

Yes I think they do, though testing those two primitives together is probably fine as they are often used together as well.

In any case increasing test coverage is a good thing.

let innerTcs = new TaskCompletionSource<unit>()
let a = innerTcs.Task |> Async.AwaitTask

Async.StartWithContinuations(a, tcs.SetResult, tcs.SetException, ignore >> tcs.SetCanceled, cts.Token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit bogus to me you await the task here and use setresult as continuation. But if the task finished setresult is already called and the continuation will throw

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you have two tcs so this is basically just StartAsTask or very similar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tcs vs innerTcs, this is setting the tcs result, while the task that we AwaitTask on is from innerTcs.

@KevinRansom
Copy link
Member

@Frassle, I'm not sure I understand what benefits, this test change achieves. Are you sure you wish to perservere with this PR?

@Frassle
Copy link
Contributor Author

Frassle commented Sep 1, 2019

Prompted by comments on #7357 (which is a PR to fix #2127).

It was pointed out that StartAsTaskCancellation was added as part of #3256 which changed how cancellation worked for asyncs started via StartAsTask. Pre-3256 the task would cancel as soon as the cancellation token was triggered, while post-3256 the task only cancels once the underlying async has finished. StartAsTaskCancellation tested this by using the fact that Async.AwaitTask ignores async cancellation to create an async where it was easy to control when it cancelled.

That use of AwaitTask is in conflict with #2127/#7357 and it isn't actually necessary for the test of StartAsTask. There's also currently no test to show that AwaitTask can't be cancelled. This PR hopefully makes the current state clearer. There's one test to show that AwaitTask can't be cancelled, and another test to show that StartAsTask waits for the underlying async (with less dependence on how other async functions work because we're now blocking via a spin loop).

Copy link
Member

@KevinRansom KevinRansom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution.

Kevin

@KevinRansom KevinRansom reopened this Sep 24, 2019
@KevinRansom KevinRansom reopened this Sep 27, 2019
@KevinRansom KevinRansom merged commit b2c8d66 into dotnet:master Sep 27, 2019
nosami pushed a commit to xamarin/visualfsharp that referenced this pull request Feb 23, 2021
…#7467)

* Change cancellation test to not depend on how AwaitTask works

This test is to check that when an Async is started as a Task (via
StartAsTask) that when cancelled the Task isn't immediately marked as
cancelled but instead waits for the underlying Async to finish (normally
via cancellation)

* Add test that AwaitTask ignore async cancellation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants