Skip to content

Commit 4f638f8

Browse files
authored
Cleanup ClientCancellationAbortsRequest Internal/2771 (#12248)
1 parent 45d3e69 commit 4f638f8

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/Hosting/TestHost/test/TestClientTests.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public async Task WebSocketTinyReceiveGeneratesEndOfMessage()
366366
public async Task ClientDisposalAbortsRequest()
367367
{
368368
// Arrange
369-
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
369+
var tcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
370370
RequestDelegate appDelegate = async ctx =>
371371
{
372372
// Write Headers
@@ -399,30 +399,26 @@ public async Task ClientDisposalAbortsRequest()
399399
[Fact]
400400
public async Task ClientCancellationAbortsRequest()
401401
{
402-
// Arrange
403-
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
404-
RequestDelegate appDelegate = async ctx =>
402+
var tcs = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
403+
var builder = new WebHostBuilder().Configure(app => app.Run(async ctx =>
405404
{
406-
var sem = new SemaphoreSlim(0);
407405
try
408406
{
409-
await sem.WaitAsync(ctx.RequestAborted);
407+
await Task.Delay(TimeSpan.FromSeconds(30), ctx.RequestAborted);
408+
tcs.SetResult(0);
410409
}
411410
catch (Exception e)
412411
{
413412
tcs.SetException(e);
413+
return;
414414
}
415-
};
416-
417-
// Act
418-
var builder = new WebHostBuilder().Configure(app => app.Run(appDelegate));
419-
var server = new TestServer(builder);
420-
var client = server.CreateClient();
421-
var cts = new CancellationTokenSource();
422-
cts.CancelAfter(500);
415+
throw new InvalidOperationException("The request was not aborted");
416+
}));
417+
using var server = new TestServer(builder);
418+
using var client = server.CreateClient();
419+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
423420
var response = await Assert.ThrowsAnyAsync<OperationCanceledException>(() => client.GetAsync("http://localhost:12345", cts.Token));
424421

425-
// Assert
426422
var exception = await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await tcs.Task);
427423
}
428424

0 commit comments

Comments
 (0)