GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix hosted-service cancellation test scheduling (#7506) (#7546) · thomhurst/TUnit@6317963

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • tests/TUnit.AspNetCore.Tests
    • HostedServiceStopOnceTests.cs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎tests/TUnit.AspNetCore.Tests/HostedServiceStopOnceTests.cs‎

Copy file name to clipboardExpand all lines: tests/TUnit.AspNetCore.Tests/HostedServiceStopOnceTests.cs
+25-5Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,44 @@ public async Task Duplicate_Stop_Caller_Does_Not_Wait_For_Synchronous_Startup()
109109
using var inner = new SynchronouslyBlockingStopHostedService();
110110
var wrapper = new FlowSuppressingHostedService(inner);
111111

112-
var first = Task.Run(() => wrapper.StopAsync(CancellationToken.None));
113-
inner.WaitUntilEntered();
112+
// The inner stop blocks synchronously. Dedicated threads keep both callers
113+
// independent of thread-pool starvation under parallel CI load.
114+
var first = Task.Factory.StartNew(
115+
() => wrapper.StopAsync(CancellationToken.None),
116+
CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap();
117+
Task? second = null;
114118

115119
using var cancellationTokenSource = new CancellationTokenSource();
116120
cancellationTokenSource.Cancel();
117121

118122
try
119123
{
120-
var second = Task.Run(() => wrapper.StopAsync(cancellationTokenSource.Token));
121-
await Assert.That(async () => await second.WaitAsync(TimeSpan.FromSeconds(1)))
124+
inner.WaitUntilEntered();
125+
second = Task.Factory.StartNew(
126+
() => wrapper.StopAsync(cancellationTokenSource.Token),
127+
CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap();
128+
await Assert.That(async () => await second.WaitAsync(TimeSpan.FromSeconds(5)))
122129
.Throws<OperationCanceledException>();
123130
}
124131
finally
125132
{
126133
inner.Release();
134+
// Drain callers before disposing the synchronization gates, even if
135+
// the assertion or startup handshake fails.
136+
await first;
137+
if (second is not null)
138+
{
139+
try
140+
{
141+
await second;
142+
}
143+
catch (OperationCanceledException) when (cancellationTokenSource.IsCancellationRequested)
144+
{
145+
// Cancellation is asserted above while the inner stop is blocked.
146+
}
147+
}
127148
}
128149

129-
await first;
130150
await Assert.That(inner.StopCalls).IsEqualTo(1);
131151
}
132152

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.