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 Send_TimeoutResponseContent_Throws #44356

Merged
merged 1 commit into from
Nov 6, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -1076,42 +1076,33 @@ await server.AcceptConnectionAsync(async connection =>

[Fact]
[OuterLoop]
public async Task Send_TimeoutResponseContent_Throws()
public void Send_TimeoutResponseContent_Throws()
{
string content = "Test content";
const string Content = "Test content";

await LoopbackServer.CreateClientAndServerAsync(
async uri =>
{
var sendTask = Task.Run(() => {
using HttpClient httpClient = CreateHttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(0.5);
HttpResponseMessage response = httpClient.Send(new HttpRequestMessage(HttpMethod.Get, uri));
});
using var server = new LoopbackServer();

TaskCanceledException ex = await Assert.ThrowsAsync<TaskCanceledException>(() => sendTask);
Assert.IsType<TimeoutException>(ex.InnerException);
},
async server =>
// Ignore all failures from the server. This includes being disposed of before ever accepting a connection,
// which is possible if the client times out so quickly that it hasn't initiated a connection yet.
_ = server.AcceptConnectionAsync(async connection =>
{
await connection.ReadRequestDataAsync();
await connection.SendResponseAsync(headers: new[] { new HttpHeaderData("Content-Length", (Content.Length * 100).ToString()) });
for (int i = 0; i < 100; ++i)
{
await server.AcceptConnectionAsync(async connection =>
{
try
{
await connection.ReadRequestDataAsync();
await connection.SendResponseAsync(headers: new List<HttpHeaderData>() {
new HttpHeaderData("Content-Length", (content.Length * 100).ToString())
});
for (int i = 0; i < 100; ++i)
{
await connection.Writer.WriteLineAsync(content);
await connection.Writer.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(0.1));
}
}
catch { }
});
});
await connection.Writer.WriteLineAsync(Content);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize this pre-existed, and it doesn't seem to actually cause problems in practice, but...

Technically this seems wrong. We calculate the Content-Length as Content.Length * 100 above, but then we add a newline each time we write it.

await connection.Writer.FlushAsync();
await Task.Delay(TimeSpan.FromSeconds(0.1));
}
});

TaskCanceledException ex = Assert.Throws<TaskCanceledException>(() =>
{
using HttpClient httpClient = CreateHttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(0.5);
HttpResponseMessage response = httpClient.Send(new HttpRequestMessage(HttpMethod.Get, server.Address));
});
Assert.IsType<TimeoutException>(ex.InnerException);
}

public static IEnumerable<object[]> VersionSelectionMemberData()
Expand Down