mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-27 13:19:31 -10:00
This test wasn't actually running because it was incorrectly annotated as a [Theory] but without any data. When changed to be a [Fact], it failed because the test depends on the Socket's listening backlog being filled so that a subsequent connect will time out, but Listen(1) doesn't actually force the OS to use a queue of max size of 1, its just advisory. The fix is to flood the server with requests that don't get accepted, filling the listen queue. Commit migrated from https://github.com/dotnet/corefx/commit/883a32630a21c83bb91735cebf1d87c76691bd7d
115 lines
4.4 KiB
C#
115 lines
4.4 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Net.Sockets;
|
|
using System.Net.Test.Common;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Xunit;
|
|
|
|
namespace System.Net.Http.Functional.Tests
|
|
{
|
|
public class HttpClientHandler_Timeouts_Test
|
|
{
|
|
[Fact]
|
|
public void ConnectTimeout_Default()
|
|
{
|
|
using (var handler = new HttpClientHandler())
|
|
{
|
|
Assert.Equal(Timeout.InfiniteTimeSpan, handler.ConnectTimeout);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(-2)]
|
|
[InlineData(int.MaxValue + 1L)]
|
|
public void ConnectTimeout_InvalidValues(long ms)
|
|
{
|
|
using (var handler = new HttpClientHandler())
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => handler.ConnectTimeout = TimeSpan.FromMilliseconds(ms));
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(-1)]
|
|
[InlineData(1)]
|
|
[InlineData(int.MaxValue - 1)]
|
|
[InlineData(int.MaxValue)]
|
|
public void ConnectTimeout_ValidValues_Roundtrip(long ms)
|
|
{
|
|
using (var handler = new HttpClientHandler())
|
|
{
|
|
handler.ConnectTimeout = TimeSpan.FromMilliseconds(ms);
|
|
Assert.Equal(TimeSpan.FromMilliseconds(ms), handler.ConnectTimeout);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConnectTimeout_SetAfterUse_Throws()
|
|
{
|
|
using (var handler = new HttpClientHandler())
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
handler.ConnectTimeout = TimeSpan.FromMilliseconds(int.MaxValue);
|
|
await client.GetAsync(HttpTestServers.RemoteEchoServer);
|
|
Assert.Equal(TimeSpan.FromMilliseconds(int.MaxValue), handler.ConnectTimeout);
|
|
Assert.Throws<InvalidOperationException>(() => handler.ConnectTimeout = TimeSpan.FromMilliseconds(1));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConnectTimeout_TimesOut_Throws()
|
|
{
|
|
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
|
{
|
|
s.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
|
s.Listen(1);
|
|
|
|
IPEndPoint ep = (IPEndPoint)s.LocalEndPoint;
|
|
Uri uri = new Uri($"http://{ep.Address}:{ep.Port}");
|
|
|
|
using (var handler = new HttpClientHandler())
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
const int NumBacklogSockets = 16; // must be larger than OS' queue length when using Listen(1).
|
|
var socketBacklog = new List<Socket>(NumBacklogSockets);
|
|
try
|
|
{
|
|
handler.ConnectTimeout = TimeSpan.FromMilliseconds(10);
|
|
|
|
// Listen's backlog is only advisory; the OS may actually allow a larger backlog than that.
|
|
// As such, create a bunch of clients to connect to the endpoint so that our actual request
|
|
// will timeout while trying to connect.
|
|
for (int i = 0; i < NumBacklogSockets; i++)
|
|
{
|
|
var tmpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
var ignored = tmpClient.ConnectAsync((IPEndPoint)s.LocalEndPoint);
|
|
socketBacklog.Add(tmpClient);
|
|
}
|
|
|
|
// Make the actual connection. It should timeout in connect.
|
|
var sw = Stopwatch.StartNew();
|
|
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => client.GetAsync(uri));
|
|
sw.Stop();
|
|
|
|
Assert.InRange(sw.ElapsedMilliseconds, 1, 10 * 1000); // allow a very wide range
|
|
}
|
|
finally
|
|
{
|
|
foreach (Socket c in socketBacklog)
|
|
{
|
|
c.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|