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

Fix typo and links in CancelAfter documentation #1011

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/articles/nunit/writing-tests/attributes/cancelafter.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
---
uid: cancelafterattribute
---

# CancelAfter

Normally, NUnit simply runs tests and waits for them to terminate -- the test is allowed to run indefinitely. For
certain kinds of tests, however, it may be desirable to specify a timeout value.

For .NET Core and later,
[`Thread.Abort`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.abort?view=net-7.0) as used by the
[`TimeoutAttribue`](./timeout.md) can no longer be used, and there is therefore no way to interrupt an endless loop.
[`Thread.Abort`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.abort?view=net-8.0) as used by the
[`TimeoutAttribute`](./timeout.md) can no longer be used, and there is therefore no way to interrupt an endless loop.

For all tests, one could use the `--blame-hang(-timeout)` options of [`dotnet
test`](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-test#options). However, this will stop any further
execution of the remaining tests.

To still be able to cancel tests, one has to move to cooperative cancellation. See [Cancellation in Managed
Threads](https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads) using a
[`CancellationToken``](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=net-7.0).
[`CancellationToken``](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=net-8.0).

The `CancelAfterAttribute` is used to specify a timeout value in milliseconds for a test case. If the test case runs
longer than the time specified, the supplied `CancellationToken` is set to canceled. It is however up to the test code
Expand Down Expand Up @@ -60,3 +64,6 @@ public async Task PotentiallyLongRunningTest(string uri, CancellationToken token
> When debugging a unit test, i.e. when a debugger is attached to the process, the timeout is not enforced.

## See Also

* [Timeout Attribute](./timeout.md)
* [MaxTime Attribute](./maxtime.md)
Loading