nunit icon indicating copy to clipboard operation
nunit copied to clipboard

Feature/4021 cancellationtoken

Open manfred-brands opened this issue 3 years ago • 10 comments

Fixes #3859 by setting both message and label. Fixes #4021

It doesn't fix the current behaviour of TimeoutAttribute but allows for a modern alternative.

Two commits:

  1. Add CancellationToken to Test(Execution)Context which will be cancelled on Timeout.
  2. Allow CancellationToken to be injected into Test methods

manfred-brands avatar Dec 27 '21 09:12 manfred-brands

If accepted requires update to nunit.analyzers rule NUnit1027 which checks parameters, to allow for CancellationToken parameter, which must be last.

manfred-brands avatar Dec 27 '21 09:12 manfred-brands

I've tried to make the failing CIs run, but I'm not able to force trigger either. So we probably need to force push a new commit (or force push the latest).

mikkelbu avatar Jan 01 '22 21:01 mikkelbu

The comment change triggered a build.

manfred-brands avatar Jan 01 '22 23:01 manfred-brands

Thanks @mikkelbu You were right. I added a test similar to yours and updated the code to ensure it works.

manfred-brands avatar Jan 10 '22 04:01 manfred-brands

Hi @manfred-brands Just wanted to circle back to this. I've been waiting to get involved in this PR myself until I had a decent amount of time to sink into understanding it. I think this would be a great fix to eventually land into a release as this seems to be a recurring issue for people

I hope to be able to get to it within the coming weeks. Can you help me understand if it's in need of a review or if it's awaiting changes?

stevenaw avatar Jun 11 '22 18:06 stevenaw

I hope to be able to get to it within the coming weeks. Can you help me understand if it's in need of a review or if it's awaiting changes?

Hi @stevenaw this is not a fix for the TimeOutAttribute on non-Framework runtimes. I'm now leaning to introduce a new CancelAfterAttribute instead for this to make it clear it is something completely different.

The current code works, I just rebased it today on v13.3-dev. It is still using the TimeoutAttribute.

The changes are the introduction of a CancellationToken to the TestContext. The tests can either get this from the TestContext or specify an additional test method parameter.

manfred-brands avatar Jun 12 '22 04:06 manfred-brands

Hi @stevenaw this is not a fix for the TimeOutAttribute on non-Framework runtimes. I'm now leaning to introduce a new CancelAfterAttribute instead for this to make it clear it is something completely different.

Sorry, I'd thought this was the fix for non-framework runtimes. Can you help me understand how it's different?

stevenaw avatar Jun 26 '22 15:06 stevenaw

@manfred-brands What is next step for this PR ?

goblinmaks avatar Sep 07 '22 08:09 goblinmaks

@manfred-brands What is next step for this PR ?

Good question. We cannot "fix" timeout attribute for .NET Core as there is no Thread.Abort to stop any runaway test. Personally, I don't like reporting an error and then let the test continue running in the background. There will be too many test that will fail a subsequent test after that. So for new code, I suggest to deprecate TimeOutAttribute and add a CancelAfterAttribute and the user needs to respect the passed in CancellationToken.

If that is acceptable for the @nunit/framework-team, I can update this PR to introduce that feature.

manfred-brands avatar Sep 07 '22 08:09 manfred-brands

We cannot "fix" timeout attribute for .NET Core as there is no Thread.Abort to stop any runaway test. Personally, I don't like reporting an error and then let the test continue running in the background. There will be too many test that will fail a subsequent test after that. So for new code, I suggest to deprecate TimeOutAttribute and add a CancelAfterAttribute and the user needs to respect the passed in CancellationToken.

@manfred-brands Thanks for following up on this. The lack of Thread.Abort() on .NETCore is a real issue. Perhaps it's just lack of familiarity on my part, but would having the Timeout command cancel the CTS not allow for cooperative cancellation if a test opts in to using the newly-exposed token (at least for long-running tests which use operations which support CTs, if the test has opted into use the provided one)? Or are you saying we can't fix it to "just work" without also requiring each test to opt in by handling the token?

I also wouldn't mind hearing what some of the other reviewers on the PR think.

stevenaw avatar Sep 18 '22 00:09 stevenaw

@rprouse @jnm2 @mikkelbu @CharliePoole FYI

goblinmaks avatar Oct 26 '22 21:10 goblinmaks

I think I missed something; why does adding the CancellationToken depend on having a solution for the lack of Thread.Abort in .NET Core+?

NickStrupat avatar Nov 15 '22 19:11 NickStrupat

https://github.com/nunit/nunit/issues/1821

NickStrupat avatar Nov 15 '22 20:11 NickStrupat

I think I missed something; why does adding the CancellationToken depend on having a solution for the lack of Thread.Abort in .NET Core+?

Correct. Hence my suggestion for a CancelAfterAttribute iso using the existing TimeoutAttribute But we have not heard anything from the @nunit/framework-team if this is acceptable. I will update the PR to only add the new attribute and functionality without affecting the existing timeout behaviour. Then end-user can switch to the new cooperative cancellation if they want to.

manfred-brands avatar Nov 17 '22 06:11 manfred-brands

@manfred-brands Just doing a bit of cleanup here. Should this be closed as superseded by https://github.com/nunit/nunit/pull/4386 based on your comment in https://github.com/nunit/nunit/issues/4447#issuecomment-1661516745 ?

stevenaw avatar Aug 02 '23 23:08 stevenaw

Superseded by #4386

manfred-brands avatar Aug 03 '23 01:08 manfred-brands