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

Clean up async timeout extension methods #31671

Merged
merged 7 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/Hosting/TestHost/test/ClientHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
Expand Down Expand Up @@ -392,7 +393,7 @@ public async Task ClientDisposalCloses()
Task<int> readTask = responseStream.ReadAsync(new byte[100], 0, 100);
Assert.False(readTask.IsCompleted);
responseStream.Dispose();
await Assert.ThrowsAsync<OperationCanceledException>(() => readTask.WithTimeout());
await Assert.ThrowsAsync<OperationCanceledException>(() => readTask.DefaultTimeout());
block.SetResult(0);
}

Expand All @@ -415,7 +416,7 @@ public async Task ClientCancellationAborts()
Task<int> readTask = responseStream.ReadAsync(new byte[100], 0, 100, cts.Token);
Assert.False(readTask.IsCompleted, "Not Completed");
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(() => readTask.WithTimeout());
await Assert.ThrowsAsync<OperationCanceledException>(() => readTask.DefaultTimeout());
block.SetResult(0);
}

Expand Down
9 changes: 5 additions & 4 deletions src/Hosting/TestHost/test/HttpContextBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
Expand Down Expand Up @@ -212,7 +213,7 @@ public async Task ClientDisposalCloses()
Task<int> readTask = responseStream.ReadAsync(new byte[100], 0, 100);
Assert.False(readTask.IsCompleted);
responseStream.Dispose();
await Assert.ThrowsAsync<OperationCanceledException>(() => readTask.WithTimeout());
await Assert.ThrowsAsync<OperationCanceledException>(() => readTask.DefaultTimeout());
block.SetResult(0);
}

Expand All @@ -236,7 +237,7 @@ public async Task ClientCancellationAborts()
await block.Task;
cts.Cancel();

await Assert.ThrowsAsync<OperationCanceledException>(() => contextTask.WithTimeout());
await Assert.ThrowsAsync<OperationCanceledException>(() => contextTask.DefaultTimeout());
}

[Fact]
Expand All @@ -262,7 +263,7 @@ public async Task ClientCancellationAbortsReadAsync()
var readTask = responseStream.ReadAsync(new byte[100], 0, 100, cts.Token);
Assert.False(readTask.IsCompleted);
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(() => readTask.WithTimeout());
await Assert.ThrowsAsync<OperationCanceledException>(() => readTask.DefaultTimeout());
block.SetResult(0);
}

Expand Down Expand Up @@ -345,7 +346,7 @@ public async Task CallingAbortInsideHandlerShouldSetRequestAborted()

var ex = await Assert.ThrowsAsync<Exception>(() => server.SendAsync(c => { }));
Assert.Equal("The application aborted the request.", ex.Message);
await requestAborted.Task.WithTimeout();
await requestAborted.Task.DefaultTimeout();
}

private class VerifierLogger : ILogger<IWebHost>
Expand Down
16 changes: 8 additions & 8 deletions src/Hosting/TestHost/test/RequestLifetimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Hosting;
using Xunit;

Expand All @@ -24,13 +24,13 @@ public async Task LifetimeFeature_Abort_TriggersRequestAbortedToken()
httpContext.RequestAborted.Register(() => requestAborted.SetResult(0));
httpContext.Abort();

await requestAborted.Task.WithTimeout();
await requestAborted.Task.DefaultTimeout();
});

var client = host.GetTestServer().CreateClient();
var ex = await Assert.ThrowsAsync<Exception>(() => client.GetAsync("/", HttpCompletionOption.ResponseHeadersRead));
Assert.Equal("The application aborted the request.", ex.Message);
await requestAborted.Task.WithTimeout();
await requestAborted.Task.DefaultTimeout();
}

[Fact]
Expand All @@ -40,7 +40,7 @@ public async Task LifetimeFeature_AbortBeforeHeadersSent_ClientThrows()
using var host = await CreateHost(async httpContext =>
{
httpContext.Abort();
await abortReceived.Task.WithTimeout();
await abortReceived.Task.DefaultTimeout();
});

var client = host.GetTestServer().CreateClient();
Expand All @@ -57,9 +57,9 @@ public async Task LifetimeFeature_AbortAfterHeadersSent_ClientBodyThrows()
using var host = await CreateHost(async httpContext =>
{
await httpContext.Response.Body.FlushAsync();
await responseReceived.Task.WithTimeout();
await responseReceived.Task.DefaultTimeout();
httpContext.Abort();
await abortReceived.Task.WithTimeout();
await abortReceived.Task.DefaultTimeout();
});

var client = host.GetTestServer().CreateClient();
Expand All @@ -80,9 +80,9 @@ public async Task LifetimeFeature_AbortAfterSomeDataSent_ClientBodyThrows()
using var host = await CreateHost(async httpContext =>
{
await httpContext.Response.WriteAsync("Hello World");
await responseReceived.Task.WithTimeout();
await responseReceived.Task.DefaultTimeout();
httpContext.Abort();
await abortReceived.Task.WithTimeout();
await abortReceived.Task.DefaultTimeout();
});

var client = host.GetTestServer().CreateClient();
Expand Down
15 changes: 8 additions & 7 deletions src/Hosting/TestHost/test/ResponseResetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Hosting;
using Xunit;

Expand Down Expand Up @@ -59,15 +60,15 @@ public async Task ResetFeature_Reset_TriggersRequestAbortedToken()

var feature = httpContext.Features.Get<IHttpResetFeature>();
feature.Reset(12345);
await requestAborted.Task.WithTimeout();
await requestAborted.Task.DefaultTimeout();
});

var client = host.GetTestServer().CreateClient();
client.DefaultRequestVersion = HttpVersion.Version20;
var rex = await Assert.ThrowsAsync<HttpResetTestException>(() => client.GetAsync("/"));
Assert.Equal("The application reset the request with error code 12345.", rex.Message);
Assert.Equal(12345, rex.ErrorCode);
await requestAborted.Task.WithTimeout();
await requestAborted.Task.DefaultTimeout();
}

[Fact]
Expand All @@ -78,7 +79,7 @@ public async Task ResetFeature_ResetBeforeHeadersSent_ClientThrows()
{
var feature = httpContext.Features.Get<IHttpResetFeature>();
feature.Reset(12345);
await resetReceived.Task.WithTimeout();
await resetReceived.Task.DefaultTimeout();
});

var client = host.GetTestServer().CreateClient();
Expand All @@ -97,10 +98,10 @@ public async Task ResetFeature_ResetAfterHeadersSent_ClientBodyThrows()
using var host = await CreateHost(async httpContext =>
{
await httpContext.Response.Body.FlushAsync();
await responseReceived.Task.WithTimeout();
await responseReceived.Task.DefaultTimeout();
var feature = httpContext.Features.Get<IHttpResetFeature>();
feature.Reset(12345);
await resetReceived.Task.WithTimeout();
await resetReceived.Task.DefaultTimeout();
});

var client = host.GetTestServer().CreateClient();
Expand All @@ -123,10 +124,10 @@ public async Task ResetFeature_ResetAfterSomeDataSent_ClientBodyThrows()
using var host = await CreateHost(async httpContext =>
{
await httpContext.Response.WriteAsync("Hello World");
await responseReceived.Task.WithTimeout();
await responseReceived.Task.DefaultTimeout();
var feature = httpContext.Features.Get<IHttpResetFeature>();
feature.Reset(12345);
await resetReceived.Task.WithTimeout();
await resetReceived.Task.DefaultTimeout();
});

var client = host.GetTestServer().CreateClient();
Expand Down
68 changes: 34 additions & 34 deletions src/Hosting/TestHost/test/TestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public async Task PutAsyncWorks()

// Act
var content = new StringContent("Hello world");
var response = await client.PutAsync("http://localhost:12345", content).WithTimeout();
var response = await client.PutAsync("http://localhost:12345", content).DefaultTimeout();

// Assert
Assert.Equal("Hello world PUT Response", await response.Content.ReadAsStringAsync().WithTimeout());
Assert.Equal("Hello world PUT Response", await response.Content.ReadAsStringAsync().DefaultTimeout());
}

[Fact]
Expand All @@ -119,10 +119,10 @@ public async Task PostAsyncWorks()

// Act
var content = new StringContent("Hello world");
var response = await client.PostAsync("http://localhost:12345", content).WithTimeout();
var response = await client.PostAsync("http://localhost:12345", content).DefaultTimeout();

// Assert
Assert.Equal("Hello world POST Response", await response.Content.ReadAsStringAsync().WithTimeout());
Assert.Equal("Hello world POST Response", await response.Content.ReadAsStringAsync().DefaultTimeout());
}

[Fact]
Expand Down Expand Up @@ -207,37 +207,37 @@ public async Task ClientStreamingWorks()
});

// Act
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).WithTimeout();
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout();

await responseStartedSyncPoint.WaitForSyncPoint().WithTimeout();
await responseStartedSyncPoint.WaitForSyncPoint().DefaultTimeout();
responseStartedSyncPoint.Continue();

var responseContent = await response.Content.ReadAsStreamAsync().WithTimeout();
var responseContent = await response.Content.ReadAsStreamAsync().DefaultTimeout();

// Assert

// Ensure request stream has started
await requestStreamSyncPoint.WaitForSyncPoint();

byte[] buffer = new byte[1024];
var length = await responseContent.ReadAsync(buffer).AsTask().WithTimeout();
var length = await responseContent.ReadAsync(buffer).AsTask().DefaultTimeout();
Assert.Equal("STARTED", Encoding.UTF8.GetString(buffer, 0, length));

// Send content and finish request body
await requestStream.WriteAsync(Encoding.UTF8.GetBytes("Hello world")).AsTask().WithTimeout();
await requestStream.FlushAsync().WithTimeout();
await requestStream.WriteAsync(Encoding.UTF8.GetBytes("Hello world")).AsTask().DefaultTimeout();
await requestStream.FlushAsync().DefaultTimeout();
requestStreamSyncPoint.Continue();

// Ensure content is received while request is in progress
length = await responseContent.ReadAsync(buffer).AsTask().WithTimeout();
length = await responseContent.ReadAsync(buffer).AsTask().DefaultTimeout();
Assert.Equal("Hello world POST Response", Encoding.UTF8.GetString(buffer, 0, length));

// Request is ending
await requestEndingSyncPoint.WaitForSyncPoint().WithTimeout();
await requestEndingSyncPoint.WaitForSyncPoint().DefaultTimeout();
requestEndingSyncPoint.Continue();

// No more response content
length = await responseContent.ReadAsync(buffer).AsTask().WithTimeout();
length = await responseContent.ReadAsync(buffer).AsTask().DefaultTimeout();
Assert.Equal(0, length);
}

Expand Down Expand Up @@ -293,28 +293,28 @@ public async Task ClientStreaming_Cancellation()
});

// Act
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).WithTimeout();
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout();

await responseStartedSyncPoint.WaitForSyncPoint().WithTimeout();
await responseStartedSyncPoint.WaitForSyncPoint().DefaultTimeout();
responseStartedSyncPoint.Continue();

var responseContent = await response.Content.ReadAsStreamAsync().WithTimeout();
var responseContent = await response.Content.ReadAsStreamAsync().DefaultTimeout();

// Assert

// Ensure request stream has started
await requestStreamSyncPoint.WaitForSyncPoint();

// Write to request
await requestStream.WriteAsync(Encoding.UTF8.GetBytes("SENT")).AsTask().WithTimeout();
await requestStream.FlushAsync().WithTimeout();
await responseReadSyncPoint.WaitForSyncPoint().WithTimeout();
await requestStream.WriteAsync(Encoding.UTF8.GetBytes("SENT")).AsTask().DefaultTimeout();
await requestStream.FlushAsync().DefaultTimeout();
await responseReadSyncPoint.WaitForSyncPoint().DefaultTimeout();

// Cancel request. Disposing response must be used because SendAsync has finished.
response.Dispose();
responseReadSyncPoint.Continue();

await responseEndingSyncPoint.WaitForSyncPoint().WithTimeout();
await responseEndingSyncPoint.WaitForSyncPoint().DefaultTimeout();
responseEndingSyncPoint.Continue();

Assert.True(readCanceled);
Expand Down Expand Up @@ -350,23 +350,23 @@ public async Task ClientStreaming_ResponseCompletesWithoutReadingRequest()
});

// Act
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).WithTimeout();
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout();

var responseContent = await response.Content.ReadAsStreamAsync().WithTimeout();
var responseContent = await response.Content.ReadAsStreamAsync().DefaultTimeout();

// Assert

// Read response
byte[] buffer = new byte[1024];
var length = await responseContent.ReadAsync(buffer).AsTask().WithTimeout();
var length = await responseContent.ReadAsync(buffer).AsTask().DefaultTimeout();
Assert.Equal("POST Response", Encoding.UTF8.GetString(buffer, 0, length));

// Send large content and block on back pressure
var writeTask = Task.Run(async () =>
{
try
{
await requestStream.WriteAsync(Encoding.UTF8.GetBytes(new string('!', 1024 * 1024 * 50))).AsTask().WithTimeout();
await requestStream.WriteAsync(Encoding.UTF8.GetBytes(new string('!', 1024 * 1024 * 50))).AsTask().DefaultTimeout();
requestStreamTcs.SetResult(null);
}
catch (Exception ex)
Expand All @@ -378,7 +378,7 @@ public async Task ClientStreaming_ResponseCompletesWithoutReadingRequest()
responseEndingSyncPoint.Continue();

// No more response content
length = await responseContent.ReadAsync(buffer).AsTask().WithTimeout();
length = await responseContent.ReadAsync(buffer).AsTask().DefaultTimeout();
Assert.Equal(0, length);

await writeTask;
Expand Down Expand Up @@ -412,9 +412,9 @@ public async Task ClientStreaming_ResponseCompletesWithPendingRead_ThrowError()
});

// Act
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).WithTimeout();
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout();

var responseContent = await response.Content.ReadAsStreamAsync().WithTimeout();
var responseContent = await response.Content.ReadAsStreamAsync().DefaultTimeout();

// Assert
response.EnsureSuccessStatusCode();
Expand All @@ -424,7 +424,7 @@ public async Task ClientStreaming_ResponseCompletesWithPendingRead_ThrowError()
var ex = await Assert.ThrowsAsync<IOException>(async () =>
{
byte[] buffer = new byte[1024];
var length = await responseContent.ReadAsync(buffer).AsTask().WithTimeout();
var length = await responseContent.ReadAsync(buffer).AsTask().DefaultTimeout();
});
Assert.Equal("An error occurred when completing the request. Request delegate may have finished while there is a pending read of the request body.", ex.InnerException.Message);

Expand Down Expand Up @@ -459,17 +459,17 @@ public async Task ClientStreaming_ResponseCompletesWithoutResponseBodyWrite()
});

// Act
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).WithTimeout();
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout();

var responseContent = await response.Content.ReadAsStreamAsync().WithTimeout();
var responseContent = await response.Content.ReadAsStreamAsync().DefaultTimeout();

// Assert
response.EnsureSuccessStatusCode();
Assert.Equal("true", response.Headers.GetValues("test-header").Single());

// Read response
byte[] buffer = new byte[1024];
var length = await responseContent.ReadAsync(buffer).AsTask().WithTimeout();
var length = await responseContent.ReadAsync(buffer).AsTask().DefaultTimeout();
Assert.Equal(0, length);

// Writing to request stream will fail because server is complete
Expand Down Expand Up @@ -510,9 +510,9 @@ public async Task ClientStreaming_ServerAbort()
});

// Act
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).WithTimeout();
var response = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout();

var responseContent = await response.Content.ReadAsStreamAsync().WithTimeout();
var responseContent = await response.Content.ReadAsStreamAsync().DefaultTimeout();

// Assert

Expand All @@ -525,7 +525,7 @@ public async Task ClientStreaming_ServerAbort()
// Send content and finish request body
await ExceptionAssert.ThrowsAsync<OperationCanceledException>(
() => requestStream.WriteAsync(Encoding.UTF8.GetBytes("Hello world")).AsTask(),
"Flush was canceled on underlying PipeWriter.").WithTimeout();
"Flush was canceled on underlying PipeWriter.").DefaultTimeout();

responseEndingSyncPoint.Continue();
requestStreamSyncPoint.Continue();
Expand Down
Loading