This commit is contained in:
Chris Ross (ASP.NET) 2018-10-30 12:35:26 -07:00
Родитель 7ef22b12e4
Коммит 7974467dc9
2 изменённых файлов: 42 добавлений и 60 удалений

Просмотреть файл

@ -300,22 +300,15 @@ namespace Microsoft.AspNetCore.Server.HttpSys
} }
[ConditionalFact] [ConditionalFact]
public async Task Server_SetHttp503VebosittHittingThrottle_Success() public async Task Server_SetHttp503VebosityHittingThrottle_Success()
{ {
// This is just to get a dynamic port using (Utilities.CreateDynamicHost(out var address, options =>
string address; {
using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { } Assert.Null(options.MaxConnections);
options.MaxConnections = 3;
var server = Utilities.CreatePump(); options.Http503Verbosity = Http503VerbosityLevel.Limited;
server.Listener.Options.UrlPrefixes.Add(UrlPrefix.Create(address)); }, httpContext => Task.FromResult(0)))
Assert.Null(server.Listener.Options.MaxConnections);
server.Listener.Options.MaxConnections = 3;
server.Listener.Options.Http503Verbosity = Http503VerbosityLevel.Limited;
using (server)
{ {
await server.StartAsync(new DummyApplication(), CancellationToken.None);
using (var client1 = await SendHungRequestAsync("GET", address)) using (var client1 = await SendHungRequestAsync("GET", address))
using (var client2 = await SendHungRequestAsync("GET", address)) using (var client2 = await SendHungRequestAsync("GET", address))
{ {
@ -340,31 +333,25 @@ namespace Microsoft.AspNetCore.Server.HttpSys
[ConditionalFact] [ConditionalFact]
public void Server_SetConnectionLimitArgumentValidation_Success() public void Server_SetConnectionLimitArgumentValidation_Success()
{ {
var server = Utilities.CreatePump(); using (var server = Utilities.CreatePump())
{
Assert.Null(server.Listener.Options.MaxConnections); Assert.Null(server.Listener.Options.MaxConnections);
Assert.Throws<ArgumentOutOfRangeException>(() => server.Listener.Options.MaxConnections = -2); Assert.Throws<ArgumentOutOfRangeException>(() => server.Listener.Options.MaxConnections = -2);
Assert.Null(server.Listener.Options.MaxConnections); Assert.Null(server.Listener.Options.MaxConnections);
server.Listener.Options.MaxConnections = null; server.Listener.Options.MaxConnections = null;
server.Listener.Options.MaxConnections = 3; server.Listener.Options.MaxConnections = 3;
}
} }
[ConditionalFact] [ConditionalFact]
public async Task Server_SetConnectionLimit_Success() public async Task Server_SetConnectionLimit_Success()
{ {
// This is just to get a dynamic port using (Utilities.CreateDynamicHost(out var address, options =>
string address; {
using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { } Assert.Null(options.MaxConnections);
options.MaxConnections = 3;
var server = Utilities.CreatePump(); }, httpContext => Task.FromResult(0)))
server.Listener.Options.UrlPrefixes.Add(UrlPrefix.Create(address));
Assert.Null(server.Listener.Options.MaxConnections);
server.Listener.Options.MaxConnections = 3;
using (server)
{ {
await server.StartAsync(new DummyApplication(), CancellationToken.None);
using (var client1 = await SendHungRequestAsync("GET", address)) using (var client1 = await SendHungRequestAsync("GET", address))
using (var client2 = await SendHungRequestAsync("GET", address)) using (var client2 = await SendHungRequestAsync("GET", address))
{ {
@ -384,19 +371,14 @@ namespace Microsoft.AspNetCore.Server.HttpSys
[ConditionalFact] [ConditionalFact]
public async Task Server_SetConnectionLimitChangeAfterStarted_Success() public async Task Server_SetConnectionLimitChangeAfterStarted_Success()
{ {
// This is just to get a dynamic port HttpSysOptions options = null;
string address; using (Utilities.CreateDynamicHost(out var address, opt =>
using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { } {
options = opt;
var server = Utilities.CreatePump(); Assert.Null(options.MaxConnections);
server.Listener.Options.UrlPrefixes.Add(UrlPrefix.Create(address)); options.MaxConnections = 3;
Assert.Null(server.Listener.Options.MaxConnections); }, httpContext => Task.FromResult(0)))
server.Listener.Options.MaxConnections = 3;
using (server)
{ {
await server.StartAsync(new DummyApplication(), CancellationToken.None);
using (var client1 = await SendHungRequestAsync("GET", address)) using (var client1 = await SendHungRequestAsync("GET", address))
using (var client2 = await SendHungRequestAsync("GET", address)) using (var client2 = await SendHungRequestAsync("GET", address))
using (var client3 = await SendHungRequestAsync("GET", address)) using (var client3 = await SendHungRequestAsync("GET", address))
@ -404,12 +386,12 @@ namespace Microsoft.AspNetCore.Server.HttpSys
// Maxed out, refuses connection and throws // Maxed out, refuses connection and throws
await Assert.ThrowsAsync<HttpRequestException>(() => SendRequestAsync(address)); await Assert.ThrowsAsync<HttpRequestException>(() => SendRequestAsync(address));
server.Listener.Options.MaxConnections = 4; options.MaxConnections = 4;
string responseText = await SendRequestAsync(address); string responseText = await SendRequestAsync(address);
Assert.Equal(string.Empty, responseText); Assert.Equal(string.Empty, responseText);
server.Listener.Options.MaxConnections = 2; options.MaxConnections = 2;
// Maxed out, refuses connection and throws // Maxed out, refuses connection and throws
await Assert.ThrowsAsync<HttpRequestException>(() => SendRequestAsync(address)); await Assert.ThrowsAsync<HttpRequestException>(() => SendRequestAsync(address));
@ -420,18 +402,12 @@ namespace Microsoft.AspNetCore.Server.HttpSys
[ConditionalFact] [ConditionalFact]
public async Task Server_SetConnectionLimitInfinite_Success() public async Task Server_SetConnectionLimitInfinite_Success()
{ {
// This is just to get a dynamic port using (Utilities.CreateDynamicHost(out var address, options =>
string address; {
using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { } Assert.Null(options.MaxConnections);
options.MaxConnections = -1; // infinite
var server = Utilities.CreatePump(); }, httpContext => Task.FromResult(0)))
server.Listener.Options.UrlPrefixes.Add(UrlPrefix.Create(address));
server.Listener.Options.MaxConnections = -1; // infinite
using (server)
{ {
await server.StartAsync(new DummyApplication(), CancellationToken.None);
using (var client1 = await SendHungRequestAsync("GET", address)) using (var client1 = await SendHungRequestAsync("GET", address))
using (var client2 = await SendHungRequestAsync("GET", address)) using (var client2 = await SendHungRequestAsync("GET", address))
using (var client3 = await SendHungRequestAsync("GET", address)) using (var client3 = await SendHungRequestAsync("GET", address))
@ -633,7 +609,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
private async Task<string> SendRequestAsync(string uri) private async Task<string> SendRequestAsync(string uri)
{ {
using (HttpClient client = new HttpClient()) using (HttpClient client = new HttpClient() { Timeout = Utilities.DefaultTimeout } )
{ {
return await client.GetStringAsync(uri); return await client.GetStringAsync(uri);
} }
@ -641,7 +617,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
private async Task<string> SendRequestAsync(string uri, string upload) private async Task<string> SendRequestAsync(string uri, string upload)
{ {
using (HttpClient client = new HttpClient()) using (HttpClient client = new HttpClient() { Timeout = Utilities.DefaultTimeout })
{ {
HttpResponseMessage response = await client.PostAsync(uri, new StringContent(upload)); HttpResponseMessage response = await client.PostAsync(uri, new StringContent(upload));
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();

Просмотреть файл

@ -23,6 +23,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
private const int MaxPort = 8000; private const int MaxPort = 8000;
private static int NextPort = BasePort; private static int NextPort = BasePort;
private static object PortLock = new object(); private static object PortLock = new object();
internal static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(15);
internal static IServer CreateHttpServer(out string baseAddress, RequestDelegate app) internal static IServer CreateHttpServer(out string baseAddress, RequestDelegate app)
{ {
@ -61,6 +62,11 @@ namespace Microsoft.AspNetCore.Server.HttpSys
}, app); }, app);
} }
internal static IWebHost CreateDynamicHost(out string baseAddress, Action<HttpSysOptions> configureOptions, RequestDelegate app)
{
return CreateDynamicHost(string.Empty, out var root, out baseAddress, configureOptions, app);
}
internal static IWebHost CreateDynamicHost(string basePath, out string root, out string baseAddress, Action<HttpSysOptions> configureOptions, RequestDelegate app) internal static IWebHost CreateDynamicHost(string basePath, out string root, out string baseAddress, Action<HttpSysOptions> configureOptions, RequestDelegate app)
{ {
lock (PortLock) lock (PortLock)