Skip to content

Commit

Permalink
Fix typo and links in cancelafter.md (#1011) fa0286d
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanKilleen committed Jan 17, 2025
1 parent bc88254 commit 53bd927
Show file tree
Hide file tree
Showing 4 changed files with 719 additions and 712 deletions.
12 changes: 8 additions & 4 deletions articles/nunit/writing-tests/attributes/cancelafter.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@
</div>
<div class="article row grid-right">
<div class="col-md-10">
<article class="content wrap" id="_content" data-uid="">
<article class="content wrap" id="_content" data-uid="cancelafterattribute">
<h1 id="cancelafter">CancelAfter</h1>

<p>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.</p>
<p>For .NET Core and later,
<a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.abort?view=net-7.0"><code>Thread.Abort</code></a> as used by the
<a href="timeout.html"><code>TimeoutAttribue</code></a> can no longer be used, and there is therefore no way to interrupt an endless loop.</p>
<a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.abort?view=net-8.0"><code>Thread.Abort</code></a> as used by the
<a href="timeout.html"><code>TimeoutAttribute</code></a> can no longer be used, and there is therefore no way to interrupt an endless loop.</p>
<p>For all tests, one could use the <code>--blame-hang(-timeout)</code> options of <a href="https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-test#options"><code>dotnet test</code></a>. However, this will stop any further
execution of the remaining tests.</p>
<p>To still be able to cancel tests, one has to move to cooperative cancellation. See <a href="https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads">Cancellation in Managed
Threads</a> using a
<a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=net-7.0">`CancellationToken``</a>.</p>
<a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=net-8.0">`CancellationToken``</a>.</p>
<p>The <code>CancelAfterAttribute</code> 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 <code>CancellationToken</code> is set to canceled. It is however up to the test code
to check this token, either directly or indirectly.</p>
Expand Down Expand Up @@ -135,6 +135,10 @@ <h5>Note</h5>
<p>When debugging a unit test, i.e. when a debugger is attached to the process, the timeout is not enforced.</p>
</div>
<h2 id="see-also">See Also</h2>
<ul>
<li><a href="timeout.html">Timeout Attribute</a></li>
<li><a href="maxtime.html">MaxTime Attribute</a></li>
</ul>

</article>
</div>
Expand Down
2 changes: 1 addition & 1 deletion index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2922,7 +2922,7 @@
"articles/nunit/writing-tests/attributes/cancelafter.html": {
"href": "articles/nunit/writing-tests/attributes/cancelafter.html",
"title": "CancelAfter | NUnit Docs",
"keywords": "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 as used by the TimeoutAttribue 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. 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 using a `CancellationToken``. 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 to check this token, either directly or indirectly. The specified timeout value covers the test setup and teardown as well as the test method itself. Before and after actions may also be included, depending on where they were specified. Since the timeout may occur during any of these execution phases, no guarantees can be made as to what will be run and any of these phases of execution may be incomplete. If only used on a test, once a test has timed out, its teardown methods are executed. The attribute may also be specified on a fixture, in which case it indicates the default timeout for any subordinate test cases. When using the console runner, it is also possible to specify a default timeout on the command-line. When used on test methods, NUnit automatically adds an extra argument to your method containing the NUnit CancellationToken. If you want to check for cancellation in SetUp methods, you can use TestContext.CurrentContext.CancellationToken Example [Test, CancelAfter(2000)] public void RunningTestUntilCanceled(CancellationToken token) { while (!token.IsCancellationRequested) { /* */ } } [CancelAfter(2000)] [TestCase(\"http://server1\")] [TestCase(\"http://server2\")] public async Task PotentiallyLongRunningTest(string uri, CancellationToken token) { HttpClient client = _httpClientFactory.CreateClient(); HttpContent content = CreateContent(); await client.PostAync(uri, content, token); HttpResponseMessage response = await client.GetAsync(uri, token); /* */ } Note When debugging a unit test, i.e. when a debugger is attached to the process, the timeout is not enforced. See Also"
"keywords": "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 as used by the TimeoutAttribute 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. 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 using a `CancellationToken``. 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 to check this token, either directly or indirectly. The specified timeout value covers the test setup and teardown as well as the test method itself. Before and after actions may also be included, depending on where they were specified. Since the timeout may occur during any of these execution phases, no guarantees can be made as to what will be run and any of these phases of execution may be incomplete. If only used on a test, once a test has timed out, its teardown methods are executed. The attribute may also be specified on a fixture, in which case it indicates the default timeout for any subordinate test cases. When using the console runner, it is also possible to specify a default timeout on the command-line. When used on test methods, NUnit automatically adds an extra argument to your method containing the NUnit CancellationToken. If you want to check for cancellation in SetUp methods, you can use TestContext.CurrentContext.CancellationToken Example [Test, CancelAfter(2000)] public void RunningTestUntilCanceled(CancellationToken token) { while (!token.IsCancellationRequested) { /* */ } } [CancelAfter(2000)] [TestCase(\"http://server1\")] [TestCase(\"http://server2\")] public async Task PotentiallyLongRunningTest(string uri, CancellationToken token) { HttpClient client = _httpClientFactory.CreateClient(); HttpContent content = CreateContent(); await client.PostAync(uri, content, token); HttpResponseMessage response = await client.GetAsync(uri, token); /* */ } Note When debugging a unit test, i.e. when a debugger is attached to the process, the timeout is not enforced. See Also Timeout Attribute MaxTime Attribute"
},
"articles/nunit/writing-tests/attributes/category.html": {
"href": "articles/nunit/writing-tests/attributes/category.html",
Expand Down
Loading

0 comments on commit 53bd927

Please sign in to comment.