Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using System.Reflection;
#if NET
using System.Runtime.InteropServices;
Comment thread
paulmedynski marked this conversation as resolved.
#endif

namespace Microsoft.Data.SqlClient.Tests.Common;

Expand Down Expand Up @@ -56,9 +59,12 @@ public sealed class LocalAppContextSwitchesHelper : IDisposable
private readonly bool? _useConnectionPoolV2Original;
private readonly bool? _useLegacyIdleTimeoutBehaviorOriginal;
private readonly bool? _useOverallConnectTimeoutForPoolWaitOriginal;
#if NET && _WINDOWS
#if NET
// The s_useManagedNetworking field only exists in the SqlClient assembly
// when it is built for .NET on Windows, so it is captured/restored at
// runtime only when running on Windows. See UseManagedNetworking below.
private readonly bool? _useManagedNetworkingOriginal;
#endif
#endif
private readonly bool? _useMinimumLoginTimeoutOriginal;

#endregion
Expand Down Expand Up @@ -121,9 +127,12 @@ public LocalAppContextSwitchesHelper()
GetSwitchValue("s_useLegacyIdleTimeoutBehavior");
_useOverallConnectTimeoutForPoolWaitOriginal =
GetSwitchValue("s_useOverallConnectTimeoutForPoolWait");
#if NET && _WINDOWS
_useManagedNetworkingOriginal =
GetSwitchValue("s_useManagedNetworking");
#if NET
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_useManagedNetworkingOriginal =
GetSwitchValue("s_useManagedNetworking");
}
#endif
_useMinimumLoginTimeoutOriginal =
GetSwitchValue("s_useMinimumLoginTimeout");
Expand Down Expand Up @@ -194,10 +203,13 @@ public void Dispose()
SetSwitchValue(
"s_useOverallConnectTimeoutForPoolWait",
_useOverallConnectTimeoutForPoolWaitOriginal);
#if NET && _WINDOWS
SetSwitchValue(
"s_useManagedNetworking",
_useManagedNetworkingOriginal);
#if NET
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
SetSwitchValue(
"s_useManagedNetworking",
_useManagedNetworkingOriginal);
}
#endif
SetSwitchValue(
"s_useMinimumLoginTimeout",
Expand Down Expand Up @@ -358,10 +370,20 @@ public bool? UseOverallConnectTimeoutForPoolWait
set => SetSwitchValue("s_useOverallConnectTimeoutForPoolWait", value);
}

#if NET && _WINDOWS
#if NET
/// <summary>
/// Get or set the UseManagedNetworking switch value.
/// </summary>
/// <remarks>
/// The underlying s_useManagedNetworking field only exists in the SqlClient
/// assembly when it is built for .NET on Windows. The getter reads the
/// public LocalAppContextSwitches.UseManagedNetworking property, which
/// exists on all platforms and is safe to read anywhere. Only the setter
/// relies on the s_useManagedNetworking field, so callers must set this
/// property only when running on Windows (see
/// RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); otherwise the
/// reflection lookup of the field will fail.
/// </remarks>
public bool? UseManagedNetworking
{
get => GetSwitchPropertyValue(nameof(UseManagedNetworking));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using Microsoft.Data.SqlClient.Tests.Common;
#if NET
using System.Runtime.InteropServices;
#endif
using Xunit;
Comment thread
paulmedynski marked this conversation as resolved.

namespace Microsoft.Data.SqlClient.UnitTests;
Expand Down Expand Up @@ -42,9 +45,10 @@ public void TestDefaultAppContextSwitchValues()
switchesHelper.UseMinimumLoginTimeout = null;
#if NET
switchesHelper.GlobalizationInvariantMode = null;
#endif
#if NET && _WINDOWS
switchesHelper.UseManagedNetworking = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
switchesHelper.UseManagedNetworking = null;
}
#endif
#if NETFRAMEWORK
switchesHelper.DisableTnirByDefault = null;
Expand All @@ -66,9 +70,16 @@ public void TestDefaultAppContextSwitchValues()
Assert.False(switchesHelper.EnableMultiSubnetFailoverByDefault);
#if NET
Assert.False(switchesHelper.GlobalizationInvariantMode);
#endif
#if NET && _WINDOWS
Assert.False(switchesHelper.UseManagedNetworking);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.False(switchesHelper.UseManagedNetworking);
}
else
{
// On .NET Unix, native SNI is unavailable, so UseManagedNetworking
// is a constant true.
Assert.True(switchesHelper.UseManagedNetworking);
}
#endif
#if NETFRAMEWORK
Assert.False(switchesHelper.DisableTnirByDefault);
Expand Down
Loading