Skip to content

Commit fa5820e

Browse files
committed
Feedback - removed test only function and replaced with shared code + some reflection
1 parent bd6c04d commit fa5820e

File tree

7 files changed

+41
-28
lines changed

7 files changed

+41
-28
lines changed

src/libraries/System.Net.Http/tests/StressTests/HttpStress/HttpStress.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<PackageReference Include="System.Net.Http.WinHttpHandler" Version="4.5.4" />
1818
</ItemGroup>
1919

20+
<!-- Shared production code -->
21+
<ItemGroup>
22+
<Compile Include="..\..\..\..\System.Net.Quic\src\System\Net\Quic\Interop\*.cs" Link="ProductionCode\System\Net\Quic\Interop\*.cs" />
23+
</ItemGroup>
24+
2025
<PropertyGroup>
2126
<!-- These may lead to duplicate generated classes with local (non-docker) Linux builds. -->
2227
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>

src/libraries/System.Net.Http/tests/StressTests/HttpStress/Program.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Net;
1515
using HttpStress;
1616
using System.Net.Quic;
17+
using Microsoft.Quic;
1718

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

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

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

168168
Console.WriteLine(" .NET Core: " + GetAssemblyInfo(typeof(object).Assembly));
169169
Console.WriteLine(" ASP.NET Core: " + GetAssemblyInfo(typeof(WebHost).Assembly));
@@ -185,9 +185,21 @@ private static async Task<ExitCode> Run(Configuration config)
185185
Console.WriteLine("Max Content Size: " + config.MaxContentLength);
186186
Console.WriteLine("Query Parameters: " + config.MaxParameters);
187187
Console.WriteLine();
188-
if (config.HttpVersion == HttpVersion.Version30 && IsQuicSupported && !maxQueueWorkDelaySet)
188+
189+
if (config.HttpVersion == HttpVersion.Version30 && IsQuicSupported)
189190
{
190-
Console.WriteLine($"Unable to set MsQuic MaxWorkerQueueDelayUs.");
191+
unsafe
192+
{
193+
object msQuicApiInstance = msQuicApiType.GetProperty("Api", BindingFlags.NonPublic | BindingFlags.Static).GetGetMethod(true).Invoke(null, Array.Empty<object?>());
194+
QUIC_API_TABLE* apiTable = (QUIC_API_TABLE*)(Pointer.Unbox(msQuicApiType.GetProperty("ApiTable").GetGetMethod().Invoke(msQuicApiInstance, Array.Empty<object?>())));
195+
QUIC_SETTINGS settings = default(QUIC_SETTINGS);
196+
settings.IsSet.MaxWorkerQueueDelayUs = 1;
197+
settings.MaxWorkerQueueDelayUs = 2_500_000u; // 2.5s, 10x the default
198+
if (MsQuic.StatusFailed(apiTable->SetParam(null, MsQuic.QUIC_PARAM_GLOBAL_SETTINGS, (uint)sizeof(QUIC_SETTINGS), (byte*)&settings)))
199+
{
200+
Console.WriteLine($"Unable to set MsQuic MaxWorkerQueueDelayUs.");
201+
}
202+
}
191203
}
192204

193205
StressServer? server = null;

src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ internal sealed unsafe partial class MsQuicApi
3131
// Remove once fixed: https://github.com/mono/linker/issues/1660
3232
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MsQuicSafeHandle))]
3333
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(MsQuicContextSafeHandle))]
34-
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(MsQuicApi))]
3534
private MsQuicApi(QUIC_API_TABLE* apiTable)
3635
{
3736
ApiTable = apiTable;
@@ -228,19 +227,4 @@ private static bool IsTls13Disabled(bool isServer)
228227
#endif
229228
return false;
230229
}
231-
232-
// Do not change the name and signature without looking for textual occurrences!
233-
// This method is invoked via reflection from QUIC functional and HTTP stress tests.
234-
private static (bool, string) SetUpForTests()
235-
{
236-
if (!IsQuicSupported)
237-
{
238-
return (true, MsQuicLibraryVersion);
239-
}
240-
241-
QUIC_SETTINGS settings = default(QUIC_SETTINGS);
242-
settings.IsSet.MaxWorkerQueueDelayUs = 1;
243-
settings.MaxWorkerQueueDelayUs = 2_500_000u; // 2.5s, 10x the default
244-
return (StatusSucceeded(MsQuicApi.Api.ApiTable->SetParam(null, QUIC_PARAM_GLOBAL_SETTINGS, (uint)sizeof(QUIC_SETTINGS), (byte*)&settings)), MsQuicLibraryVersion);
245-
}
246230
}

src/libraries/System.Net.Quic/tests/FunctionalTests/MsQuicTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public async Task ConnectWithServerCertificateCallback()
335335
// TODO: the exception may change if we implement https://github.com/dotnet/runtime/issues/73152 to make server close
336336
// connections with CONNECTION_REFUSED in such cases
337337
var authEx = await Assert.ThrowsAsync<AuthenticationException>(() => clientTask);
338-
Assert.Contains("UserCanceled", authEx.Message);
338+
Assert.Contains(TlsAlertMessage.UserCanceled.ToString(), authEx.Message);
339339
Assert.Equal(clientOptions.ClientAuthenticationOptions.TargetHost, receivedHostName);
340340
await Assert.ThrowsAsync<ArgumentException>(async () => await listener.AcceptConnectionAsync());
341341

src/libraries/System.Net.Quic/tests/FunctionalTests/QuicListenerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public async Task AcceptConnectionAsync_SlowOptionsCallback_TimesOut(bool useCan
119119

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

125125
[Fact]
@@ -160,7 +160,7 @@ public async Task AcceptConnectionAsync_ListenerDisposed_Throws()
160160

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

166166
[Fact]

src/libraries/System.Net.Quic/tests/FunctionalTests/QuicTestBase.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Diagnostics.Tracing;
1515
using System.Net.Sockets;
1616
using System.Reflection;
17+
using Microsoft.Quic;
1718

1819
namespace System.Net.Quic.Tests
1920
{
@@ -43,14 +44,23 @@ public abstract class QuicTestBase : IDisposable
4344

4445
static unsafe QuicTestBase()
4546
{
46-
Type msQuicApi = typeof(QuicConnection).Assembly.GetType("System.Net.Quic.MsQuicApi");
47-
(bool maxQueueWorkDelaySet, string msQuicLibraryVersion) = ((bool, string))msQuicApi.GetMethod("SetUpForTests", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, Array.Empty<object?>());
47+
// 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.
48+
Type msQuicApiType = typeof(QuicConnection).Assembly.GetType("System.Net.Quic.MsQuicApi");
4849

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

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

src/libraries/System.Net.Quic/tests/FunctionalTests/System.Net.Quic.Functional.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs" Link="ProductionCode\Common\System\Net\ArrayBuffer.cs" />
1717
<Compile Include="$(CommonPath)System\Net\MultiArrayBuffer.cs" Link="ProductionCode\Common\System\Net\MultiArrayBuffer.cs" />
1818
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="ProductionCode\Common\System\Net\StreamBuffer.cs" />
19+
<Compile Include="$(CommonPath)System\Net\Security\TlsAlertMessage.cs" Link="Common\System\Net\Security\TlsAlertMessage.cs" />
1920
<Compile Include="$(CommonTestPath)System\IO\ConnectedStreams.cs" Link="Common\System\IO\ConnectedStreams.cs" />
2021
<Compile Include="$(CommonTestPath)System\Net\Capability.Security.cs" Link="Common\System\Net\Capability.Security.cs" />
2122
<Compile Include="$(CommonTestPath)System\Net\Configuration.cs" Link="Common\System\Net\Configuration.cs" />
@@ -32,6 +33,7 @@
3233
<!-- Shared production code -->
3334
<ItemGroup>
3435
<Compile Include="..\..\src\System\Net\Quic\QuicDefaults.cs" Link="ProductionCode\System\Net\Quic\QuicDefaults.cs" />
36+
<Compile Include="..\..\src\System\Net\Quic\Interop\*.cs" Link="ProductionCode\System\Net\Quic\Interop\*.cs" />
3537
</ItemGroup>
3638
<ItemGroup>
3739
<ProjectReference Include="$(CommonTestPath)StreamConformanceTests\StreamConformanceTests.csproj" />

0 commit comments

Comments
 (0)