Files
dotnet-runtime/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTestBase.cs
T
Stephen Toub 4c75f5d1cd Add response drain max time to SocketsHttpHandler (dotnet/corefx#27708)
* Add response drain max time to SocketsHttpHandler

Adds support for a max time limit on how long we'll try to drain a connection before returning it to the pool, and adds a (currently non-public) knob for controlling that limit.

This also fixes a race condition with regards to cancellation and chunked responses.  If cancellation was requested after returning the connection to the pool but before we disposed of the cancellation registration, someone else could get the pooled connection and then cancellation of one request could end up canceling the other request.

* Address PR feedback


Commit migrated from https://github.com/dotnet/corefx/commit/4ca662cf7ed1bff8e423839936083838d705008d
2018-03-06 07:38:06 -05:00

55 lines
2.7 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.Diagnostics;
using System.Reflection;
namespace System.Net.Http.Functional.Tests
{
public abstract class HttpClientTestBase : RemoteExecutorTestBase
{
protected virtual bool UseSocketsHttpHandler => false;
protected bool IsWinHttpHandler => !UseSocketsHttpHandler && PlatformDetection.IsWindows && !PlatformDetection.IsUap && !PlatformDetection.IsFullFramework;
protected bool IsCurlHandler => !UseSocketsHttpHandler && !PlatformDetection.IsWindows;
protected bool IsNetfxHandler => !UseSocketsHttpHandler && PlatformDetection.IsWindows && PlatformDetection.IsFullFramework;
protected bool IsUapHandler => !UseSocketsHttpHandler && PlatformDetection.IsWindows && PlatformDetection.IsUap;
protected HttpClient CreateHttpClient() => new HttpClient(CreateHttpClientHandler());
protected HttpClientHandler CreateHttpClientHandler() => CreateHttpClientHandler(UseSocketsHttpHandler);
protected static HttpClient CreateHttpClient(string useSocketsHttpHandlerBoolString) =>
new HttpClient(CreateHttpClientHandler(useSocketsHttpHandlerBoolString));
protected static HttpClientHandler CreateHttpClientHandler(string useSocketsHttpHandlerBoolString) =>
CreateHttpClientHandler(bool.Parse(useSocketsHttpHandlerBoolString));
protected static HttpClientHandler CreateHttpClientHandler(bool useSocketsHttpHandler)
{
if (!PlatformDetection.IsNetCore) // SocketsHttpHandler only exists on .NET Core
{
return new HttpClientHandler();
}
ConstructorInfo ctor = typeof(HttpClientHandler).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(bool) }, null);
Debug.Assert(ctor != null, "Couldn't find test constructor on HttpClientHandler");
HttpClientHandler handler = (HttpClientHandler)ctor.Invoke(new object[] { useSocketsHttpHandler });
Debug.Assert(useSocketsHttpHandler == IsSocketsHttpHandler(handler), "Unexpected handler.");
return handler;
}
protected static bool IsSocketsHttpHandler(HttpClientHandler handler) =>
GetUnderlyingSocketsHttpHandler(handler) != null;
protected static object GetUnderlyingSocketsHttpHandler(HttpClientHandler handler)
{
FieldInfo field = typeof(HttpClientHandler).GetField("_socketsHttpHandler", BindingFlags.Instance | BindingFlags.NonPublic);
return field?.GetValue(handler);
}
}
}