Skip to content

Commit

Permalink
Feedback - removed test only function and replaced with shared code +…
Browse files Browse the repository at this point in the history
… some reflection
  • Loading branch information
ManickaP committed Apr 20, 2023
1 parent bd6c04d commit fa5820e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<PackageReference Include="System.Net.Http.WinHttpHandler" Version="4.5.4" />
</ItemGroup>

<!-- Shared production code -->
<ItemGroup>
<Compile Include="..\..\..\..\System.Net.Quic\src\System\Net\Quic\Interop\*.cs" Link="ProductionCode\System\Net\Quic\Interop\*.cs" />
</ItemGroup>

<PropertyGroup>
<!-- These may lead to duplicate generated classes with local (non-docker) Linux builds. -->
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Net;
using HttpStress;
using System.Net.Quic;
using Microsoft.Quic;

[assembly:SupportedOSPlatform("windows")]
[assembly:SupportedOSPlatform("linux")]
Expand Down Expand Up @@ -161,9 +162,8 @@ private static async Task<ExitCode> Run(Configuration config)

string GetAssemblyInfo(Assembly assembly) => $"{assembly.Location}, modified {new FileInfo(assembly.Location).LastWriteTime}";


Type msQuicApi = typeof(QuicConnection).Assembly.GetType("System.Net.Quic.MsQuicApi")!;
(bool maxQueueWorkDelaySet, string msQuicLibraryVersion) = ((bool, string))msQuicApi.GetMethod("SetUpForTests", BindingFlags.NonPublic | BindingFlags.Static)!.Invoke(null, Array.Empty<object?>())!;
Type msQuicApiType = typeof(QuicConnection).Assembly.GetType("System.Net.Quic.MsQuicApi");
string msQuicLibraryVersion = (string)msQuicApiType.GetProperty("MsQuicLibraryVersion", BindingFlags.NonPublic | BindingFlags.Static).GetGetMethod(true).Invoke(null, Array.Empty<object?>());

Console.WriteLine(" .NET Core: " + GetAssemblyInfo(typeof(object).Assembly));
Console.WriteLine(" ASP.NET Core: " + GetAssemblyInfo(typeof(WebHost).Assembly));
Expand All @@ -185,9 +185,21 @@ private static async Task<ExitCode> Run(Configuration config)
Console.WriteLine("Max Content Size: " + config.MaxContentLength);
Console.WriteLine("Query Parameters: " + config.MaxParameters);
Console.WriteLine();
if (config.HttpVersion == HttpVersion.Version30 && IsQuicSupported && !maxQueueWorkDelaySet)

if (config.HttpVersion == HttpVersion.Version30 && IsQuicSupported)
{
Console.WriteLine($"Unable to set MsQuic MaxWorkerQueueDelayUs.");
unsafe
{
object msQuicApiInstance = msQuicApiType.GetProperty("Api", BindingFlags.NonPublic | BindingFlags.Static).GetGetMethod(true).Invoke(null, Array.Empty<object?>());
QUIC_API_TABLE* apiTable = (QUIC_API_TABLE*)(Pointer.Unbox(msQuicApiType.GetProperty("ApiTable").GetGetMethod().Invoke(msQuicApiInstance, Array.Empty<object?>())));
QUIC_SETTINGS settings = default(QUIC_SETTINGS);
settings.IsSet.MaxWorkerQueueDelayUs = 1;
settings.MaxWorkerQueueDelayUs = 2_500_000u; // 2.5s, 10x the default
if (MsQuic.StatusFailed(apiTable->SetParam(null, MsQuic.QUIC_PARAM_GLOBAL_SETTINGS, (uint)sizeof(QUIC_SETTINGS), (byte*)&settings)))
{
Console.WriteLine($"Unable to set MsQuic MaxWorkerQueueDelayUs.");
}
}
}

StressServer? server = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ internal sealed unsafe partial class MsQuicApi
// Remove once fixed: https://github.com/mono/linker/issues/1660
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MsQuicSafeHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MsQuicContextSafeHandle))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(MsQuicApi))]
private MsQuicApi(QUIC_API_TABLE* apiTable)
{
ApiTable = apiTable;
Expand Down Expand Up @@ -228,19 +227,4 @@ private static bool IsTls13Disabled(bool isServer)
#endif
return false;
}

// Do not change the name and signature without looking for textual occurrences!
// This method is invoked via reflection from QUIC functional and HTTP stress tests.
private static (bool, string) SetUpForTests()
{
if (!IsQuicSupported)
{
return (true, MsQuicLibraryVersion);
}

QUIC_SETTINGS settings = default(QUIC_SETTINGS);
settings.IsSet.MaxWorkerQueueDelayUs = 1;
settings.MaxWorkerQueueDelayUs = 2_500_000u; // 2.5s, 10x the default
return (StatusSucceeded(MsQuicApi.Api.ApiTable->SetParam(null, QUIC_PARAM_GLOBAL_SETTINGS, (uint)sizeof(QUIC_SETTINGS), (byte*)&settings)), MsQuicLibraryVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public async Task ConnectWithServerCertificateCallback()
// TODO: the exception may change if we implement https://github.com/dotnet/runtime/issues/73152 to make server close
// connections with CONNECTION_REFUSED in such cases
var authEx = await Assert.ThrowsAsync<AuthenticationException>(() => clientTask);
Assert.Contains("UserCanceled", authEx.Message);
Assert.Contains(TlsAlertMessage.UserCanceled.ToString(), authEx.Message);
Assert.Equal(clientOptions.ClientAuthenticationOptions.TargetHost, receivedHostName);
await Assert.ThrowsAsync<ArgumentException>(async () => await listener.AcceptConnectionAsync());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public async Task AcceptConnectionAsync_SlowOptionsCallback_TimesOut(bool useCan

// Connect attempt should be stopped with "UserCanceled".
var connectException = await Assert.ThrowsAsync<AuthenticationException>(async () => await connectTask);
Assert.Contains("UserCanceled", connectException.Message);
Assert.Contains(TlsAlertMessage.UserCanceled.ToString(), connectException.Message);
}

[Fact]
Expand Down Expand Up @@ -160,7 +160,7 @@ public async Task AcceptConnectionAsync_ListenerDisposed_Throws()

// Connect attempt should be stopped with "UserCanceled".
var connectException = await Assert.ThrowsAsync<AuthenticationException>(async () => await connectTask);
Assert.Contains("UserCanceled", connectException.Message);
Assert.Contains(TlsAlertMessage.UserCanceled.ToString(), connectException.Message);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Diagnostics.Tracing;
using System.Net.Sockets;
using System.Reflection;
using Microsoft.Quic;

namespace System.Net.Quic.Tests
{
Expand Down Expand Up @@ -43,14 +44,23 @@ public abstract class QuicTestBase : IDisposable

static unsafe QuicTestBase()
{
Type msQuicApi = typeof(QuicConnection).Assembly.GetType("System.Net.Quic.MsQuicApi");
(bool maxQueueWorkDelaySet, string msQuicLibraryVersion) = ((bool, string))msQuicApi.GetMethod("SetUpForTests", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, Array.Empty<object?>());
// If any of the reflection bellow breaks due to changes in "System.Net.Quic.MsQuicApi", also check and fix HttpStress project as it uses the same hack.
Type msQuicApiType = typeof(QuicConnection).Assembly.GetType("System.Net.Quic.MsQuicApi");

string msQuicLibraryVersion = (string)msQuicApiType.GetProperty("MsQuicLibraryVersion", BindingFlags.NonPublic | BindingFlags.Static).GetGetMethod(true).Invoke(null, Array.Empty<object?>());
Console.WriteLine($"MsQuic {(IsSupported ? "supported" : "not supported")} and using '{msQuicLibraryVersion}'.");

if (IsSupported && !maxQueueWorkDelaySet)
if (IsSupported)
{
Console.WriteLine($"Unable to set MsQuic MaxWorkerQueueDelayUs.");
object msQuicApiInstance = msQuicApiType.GetProperty("Api", BindingFlags.NonPublic | BindingFlags.Static).GetGetMethod(true).Invoke(null, Array.Empty<object?>());
QUIC_API_TABLE* apiTable = (QUIC_API_TABLE*)(Pointer.Unbox(msQuicApiType.GetProperty("ApiTable").GetGetMethod().Invoke(msQuicApiInstance, Array.Empty<object?>())));
QUIC_SETTINGS settings = default(QUIC_SETTINGS);
settings.IsSet.MaxWorkerQueueDelayUs = 1;
settings.MaxWorkerQueueDelayUs = 2_500_000u; // 2.5s, 10x the default
if (MsQuic.StatusFailed(apiTable->SetParam(null, MsQuic.QUIC_PARAM_GLOBAL_SETTINGS, (uint)sizeof(QUIC_SETTINGS), (byte*)&settings)))
{
Console.WriteLine($"Unable to set MsQuic MaxWorkerQueueDelayUs.");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs" Link="ProductionCode\Common\System\Net\ArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
<Compile Include="$(CommonPath)System\Net\Security\TlsAlertMessage.cs" Link="Common\System\Net\Security\TlsAlertMessage.cs" />
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="Common\System\IO\ConnectedStreams.cs" />
<Compile Include="$(CommonTestPath)System\Net\Capability.Security.cs" Link="Common\System\Net\Capability.Security.cs" />
<Compile Include="$(CommonTestPath)System\Net\Configuration.cs" Link="Common\System\Net\Configuration.cs" />
Expand All @@ -32,6 +33,7 @@
<!-- Shared production code -->
<ItemGroup>
<Compile Include="..\..\src\System\Net\Quic\QuicDefaults.cs" Link="ProductionCode\System\Net\Quic\QuicDefaults.cs" />
<Compile Include="..\..\src\System\Net\Quic\Interop\*.cs" Link="ProductionCode\System\Net\Quic\Interop\*.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />
Expand Down

0 comments on commit fa5820e

Please sign in to comment.