-
Notifications
You must be signed in to change notification settings - Fork 311
Create app context switch test helper #3371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
355 changes: 355 additions & 0 deletions
355
src/Microsoft.Data.SqlClient/tests/Common/LocalAppContextSwitchesHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,355 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.Tests.Common; | ||
|
||
// This class provides read/write access to LocalAppContextSwitches values | ||
// for the duration of a test. It is intended to be constructed at the start | ||
// of a test and disposed at the end. It captures the original values of | ||
// the switches and restores them when disposed. | ||
// | ||
// This follows the RAII pattern to ensure that the switches are always | ||
// restored, which is important for global state like LocalAppContextSwitches. | ||
// | ||
// https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization | ||
// | ||
public sealed class LocalAppContextSwitchesHelper : IDisposable | ||
paulmedynski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
#region Public Types | ||
benrr101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// This enum is used to represent the state of a switch. | ||
paulmedynski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// It is a copy of the Tristate enum from LocalAppContextSwitches. | ||
// | ||
public enum Tristate : byte | ||
{ | ||
NotInitialized = 0, | ||
False = 1, | ||
True = 2 | ||
} | ||
|
||
#endregion | ||
|
||
#region Construction | ||
|
||
// Construct to capture all existing switch values. | ||
// | ||
// Fails the test if any values cannot be captured. | ||
// | ||
public LocalAppContextSwitchesHelper() | ||
{ | ||
// Acquire a handle to the LocalAppContextSwitches type. | ||
var assembly = typeof(SqlCommandBuilder).Assembly; | ||
var switchesType = assembly.GetType( | ||
"Microsoft.Data.SqlClient.LocalAppContextSwitches"); | ||
benrr101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (switchesType == null) | ||
{ | ||
Assert.Fail("Unable to find LocalAppContextSwitches type."); | ||
paulmedynski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// A local helper to acquire a handle to a property. | ||
void InitProperty(string name, out PropertyInfo property) | ||
benrr101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var prop = switchesType.GetProperty( | ||
name, BindingFlags.Public | BindingFlags.Static); | ||
if (prop == null) | ||
{ | ||
Assert.Fail($"Unable to find {name} property."); | ||
} | ||
property = prop; | ||
} | ||
|
||
// Acquire handles to all of the public properties of | ||
// LocalAppContextSwitches. | ||
InitProperty( | ||
"LegacyRowVersionNullBehavior", | ||
out _legacyRowVersionNullBehaviorProperty); | ||
InitProperty( | ||
"SuppressInsecureTLSWarning", | ||
out _suppressInsecureTLSWarningProperty); | ||
InitProperty( | ||
"MakeReadAsyncBlocking", | ||
out _makeReadAsyncBlockingProperty); | ||
InitProperty( | ||
"UseMinimumLoginTimeout", | ||
out _useMinimumLoginTimeoutProperty); | ||
InitProperty( | ||
"LegacyVarTimeZeroScaleBehaviour", | ||
out _legacyVarTimeZeroScaleBehaviourProperty); | ||
InitProperty( | ||
"UseCompatibilityProcessSni", | ||
out _useCompatProcessSniProperty); | ||
InitProperty( | ||
"UseCompatibilityAsyncBehaviour", | ||
out _useCompatAsyncBehaviourProperty); | ||
#if NETFRAMEWORK | ||
InitProperty( | ||
"DisableTNIRByDefault", | ||
out _disableTNIRByDefaultProperty); | ||
#endif | ||
|
||
// A local helper to capture the original value of a switch. | ||
void InitField(string name, out FieldInfo field, out Tristate value) | ||
{ | ||
var fieldInfo = | ||
paulmedynski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switchesType.GetField( | ||
name, BindingFlags.NonPublic | BindingFlags.Static); | ||
if (fieldInfo == null) | ||
{ | ||
Assert.Fail($"Unable to find {name} field."); | ||
} | ||
field = fieldInfo; | ||
value = GetValue(field); | ||
} | ||
|
||
// Capture the original value of each switch. | ||
InitField( | ||
"s_legacyRowVersionNullBehavior", | ||
out _legacyRowVersionNullBehaviorField, | ||
out _legacyRowVersionNullBehaviorOriginal); | ||
|
||
InitField( | ||
"s_suppressInsecureTLSWarning", | ||
out _suppressInsecureTLSWarningField, | ||
out _suppressInsecureTLSWarningOriginal); | ||
|
||
InitField( | ||
"s_makeReadAsyncBlocking", | ||
out _makeReadAsyncBlockingField, | ||
out _makeReadAsyncBlockingOriginal); | ||
|
||
InitField( | ||
"s_useMinimumLoginTimeout", | ||
out _useMinimumLoginTimeoutField, | ||
out _useMinimumLoginTimeoutOriginal); | ||
|
||
InitField( | ||
"s_legacyVarTimeZeroScaleBehaviour", | ||
out _legacyVarTimeZeroScaleBehaviourField, | ||
out _legacyVarTimeZeroScaleBehaviourOriginal); | ||
|
||
InitField( | ||
"s_useCompatProcessSni", | ||
out _useCompatProcessSniField, | ||
out _useCompatProcessSniOriginal); | ||
|
||
InitField( | ||
"s_useCompatAsyncBehaviour", | ||
out _useCompatAsyncBehaviourField, | ||
out _useCompatAsyncBehaviourOriginal); | ||
|
||
#if NETFRAMEWORK | ||
InitField( | ||
"s_disableTNIRByDefault", | ||
out _disableTNIRByDefaultField, | ||
out _disableTNIRByDefaultOriginal); | ||
#endif | ||
} | ||
|
||
// Disposal restores all original switch values as a best effort. | ||
public void Dispose() | ||
{ | ||
List<string> failedFields = new(); | ||
|
||
void RestoreField(FieldInfo field, Tristate value) | ||
{ | ||
try | ||
{ | ||
SetValue(field, value); | ||
} | ||
catch (Exception) | ||
{ | ||
failedFields.Add(field.Name); | ||
} | ||
} | ||
|
||
RestoreField( | ||
_legacyRowVersionNullBehaviorField, | ||
_legacyRowVersionNullBehaviorOriginal); | ||
RestoreField( | ||
_suppressInsecureTLSWarningField, | ||
_suppressInsecureTLSWarningOriginal); | ||
RestoreField( | ||
_makeReadAsyncBlockingField, | ||
_makeReadAsyncBlockingOriginal); | ||
RestoreField( | ||
_useMinimumLoginTimeoutField, | ||
_useMinimumLoginTimeoutOriginal); | ||
RestoreField( | ||
_legacyVarTimeZeroScaleBehaviourField, | ||
_legacyVarTimeZeroScaleBehaviourOriginal); | ||
RestoreField( | ||
_useCompatProcessSniField, | ||
_useCompatProcessSniOriginal); | ||
RestoreField( | ||
_useCompatAsyncBehaviourField, | ||
_useCompatAsyncBehaviourOriginal); | ||
#if NETFRAMEWORK | ||
RestoreField( | ||
_disableTNIRByDefaultField, | ||
_disableTNIRByDefaultOriginal); | ||
#endif | ||
if (failedFields.Count > 0) | ||
{ | ||
Assert.Fail( | ||
$"Failed to restore the following fields: " + | ||
paulmedynski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
string.Join(", ", failedFields)); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region Public Properties | ||
|
||
// These properties expose the like-named LocalAppContextSwitches | ||
// properties. | ||
public bool LegacyRowVersionNullBehavior | ||
{ | ||
get => (bool)_legacyRowVersionNullBehaviorProperty.GetValue(null); | ||
} | ||
public bool SuppressInsecureTLSWarning | ||
{ | ||
get => (bool)_suppressInsecureTLSWarningProperty.GetValue(null); | ||
} | ||
public bool MakeReadAsyncBlocking | ||
{ | ||
get => (bool)_makeReadAsyncBlockingProperty.GetValue(null); | ||
} | ||
public bool UseMinimumLoginTimeout | ||
{ | ||
get => (bool)_useMinimumLoginTimeoutProperty.GetValue(null); | ||
} | ||
public bool LegacyVarTimeZeroScaleBehaviour | ||
{ | ||
get => (bool)_legacyVarTimeZeroScaleBehaviourProperty.GetValue(null); | ||
} | ||
public bool UseCompatibilityProcessSni | ||
{ | ||
get => (bool)_useCompatProcessSniProperty.GetValue(null); | ||
} | ||
public bool UseCompatibilityAsyncBehaviour | ||
{ | ||
get => (bool)_useCompatAsyncBehaviourProperty.GetValue(null); | ||
} | ||
#if NETFRAMEWORK | ||
public bool DisableTNIRByDefault | ||
{ | ||
get => (bool)_disableTNIRByDefaultProperty.GetValue(null); | ||
} | ||
#endif | ||
|
||
// These properties get or set the like-named underlying switch field value. | ||
// | ||
// They all fail the test if the value cannot be retrieved or set. | ||
|
||
public Tristate LegacyRowVersionNullBehaviorField | ||
{ | ||
get => GetValue(_legacyRowVersionNullBehaviorField); | ||
set => SetValue(_legacyRowVersionNullBehaviorField, value); | ||
} | ||
|
||
public Tristate SuppressInsecureTLSWarningField | ||
{ | ||
get => GetValue(_suppressInsecureTLSWarningField); | ||
set => SetValue(_suppressInsecureTLSWarningField, value); | ||
} | ||
|
||
public Tristate MakeReadAsyncBlockingField | ||
{ | ||
get => GetValue(_makeReadAsyncBlockingField); | ||
set => SetValue(_makeReadAsyncBlockingField, value); | ||
} | ||
|
||
public Tristate UseMinimumLoginTimeoutField | ||
{ | ||
get => GetValue(_useMinimumLoginTimeoutField); | ||
set => SetValue(_useMinimumLoginTimeoutField, value); | ||
} | ||
|
||
public Tristate LegacyVarTimeZeroScaleBehaviourField | ||
{ | ||
get => GetValue(_legacyVarTimeZeroScaleBehaviourField); | ||
set => SetValue(_legacyVarTimeZeroScaleBehaviourField, value); | ||
} | ||
|
||
public Tristate UseCompatProcessSniField | ||
{ | ||
get => GetValue(_useCompatProcessSniField); | ||
set => SetValue(_useCompatProcessSniField, value); | ||
} | ||
|
||
public Tristate UseCompatAsyncBehaviourField | ||
{ | ||
get => GetValue(_useCompatAsyncBehaviourField); | ||
set => SetValue(_useCompatAsyncBehaviourField, value); | ||
} | ||
|
||
#if NETFRAMEWORK | ||
public Tristate DisableTNIRByDefaultField | ||
{ | ||
get => GetValue(_disableTNIRByDefaultField); | ||
set => SetValue(_disableTNIRByDefaultField, value); | ||
} | ||
#endif | ||
|
||
#endregion | ||
|
||
#region Private Helpers | ||
|
||
private static Tristate GetValue(FieldInfo field) | ||
{ | ||
var value = field.GetValue(null); | ||
if (value is null) | ||
{ | ||
Assert.Fail($"Field {field.Name} has a null value."); | ||
} | ||
|
||
return (Tristate)value; | ||
} | ||
|
||
private static void SetValue(FieldInfo field, Tristate value) | ||
{ | ||
field.SetValue(null, (byte)value); | ||
} | ||
|
||
#endregion | ||
|
||
#region Private Members | ||
benrr101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// These fields are used to expose LocalAppContextSwitches's properties. | ||
private readonly PropertyInfo _legacyRowVersionNullBehaviorProperty; | ||
private readonly PropertyInfo _suppressInsecureTLSWarningProperty; | ||
private readonly PropertyInfo _makeReadAsyncBlockingProperty; | ||
private readonly PropertyInfo _useMinimumLoginTimeoutProperty; | ||
private readonly PropertyInfo _legacyVarTimeZeroScaleBehaviourProperty; | ||
private readonly PropertyInfo _useCompatProcessSniProperty; | ||
private readonly PropertyInfo _useCompatAsyncBehaviourProperty; | ||
#if NETFRAMEWORK | ||
private readonly PropertyInfo _disableTNIRByDefaultProperty; | ||
benrr101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif | ||
|
||
// These fields are used to capture the original switch values. | ||
private readonly FieldInfo _legacyRowVersionNullBehaviorField; | ||
private readonly Tristate _legacyRowVersionNullBehaviorOriginal; | ||
private readonly FieldInfo _suppressInsecureTLSWarningField; | ||
private readonly Tristate _suppressInsecureTLSWarningOriginal; | ||
private readonly FieldInfo _makeReadAsyncBlockingField; | ||
private readonly Tristate _makeReadAsyncBlockingOriginal; | ||
private readonly FieldInfo _useMinimumLoginTimeoutField; | ||
private readonly Tristate _useMinimumLoginTimeoutOriginal; | ||
private readonly FieldInfo _legacyVarTimeZeroScaleBehaviourField; | ||
private readonly Tristate _legacyVarTimeZeroScaleBehaviourOriginal; | ||
private readonly FieldInfo _useCompatProcessSniField; | ||
private readonly Tristate _useCompatProcessSniOriginal; | ||
private readonly FieldInfo _useCompatAsyncBehaviourField; | ||
private readonly Tristate _useCompatAsyncBehaviourOriginal; | ||
#if NETFRAMEWORK | ||
private readonly FieldInfo _disableTNIRByDefaultField; | ||
private readonly Tristate _disableTNIRByDefaultOriginal; | ||
#endif | ||
|
||
#endregion | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.