This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing HashCode Seed Initialization on BCL package (#43004)
* Fixing HashCode Seed Initialization on BCL package * Adding Branding and building of HashCode package * Addressing PR Feedback * Adding Unit Test * Address PR feedback * Adding test for appcontext switch too * Disable EnsureSeedReturnsEqualValuesWhenAppContextSwitchIsSet on netcoreapp * Adding retry helper in case the test fails in Linux due to timing Co-authored-by: Eric StJohn <ericstj@microsoft.com>
- Loading branch information
Showing
8 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,18 @@ | ||
// 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.Runtime.CompilerServices; | ||
|
||
namespace System | ||
{ | ||
internal static partial class LocalAppContextSwitches | ||
{ | ||
private static int s_useNonRandomizedHashSeed; | ||
public static bool UseNonRandomizedHashSeed | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => GetCachedSwitchValue("Switch.System.Data.UseNonRandomizedHashSeed", ref s_useNonRandomizedHashSeed); | ||
} | ||
} | ||
} |
This file contains 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 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,8 @@ | ||
<Project DefaultTargets="Build"> | ||
<PropertyGroup> | ||
<BuildConfigurations> | ||
netfx; | ||
netcoreapp; | ||
</BuildConfigurations> | ||
</PropertyGroup> | ||
</Project> |
68 changes: 68 additions & 0 deletions
68
src/Microsoft.Bcl.HashCode/tests/HashCodeSeedInitializerTests.cs
This file contains 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,68 @@ | ||
// 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 Microsoft.DotNet.RemoteExecutor; | ||
using System; | ||
using System.Reflection; | ||
using Xunit; | ||
|
||
namespace Microsoft.Bcl.HashCode.Tests | ||
{ | ||
public class HashCodeSeedInitializerTests | ||
{ | ||
[Fact] | ||
public void EnsureSeedReturnsDifferentValuesTest() | ||
{ | ||
// Adding 5 retries since in Netcoreapp-Linux the seed that is generated is time-dependant so | ||
// this test might fail there. This will ensure we reduce flakiness while still providing | ||
// regression coverage. | ||
RetryHelper.Execute(() => | ||
{ | ||
int FirstSeed = CalculateHashCodeInRemoteProcess(setAppContextSwitch: false); | ||
int SecondSeed = CalculateHashCodeInRemoteProcess(setAppContextSwitch: false); | ||
Assert.NotEqual(FirstSeed, SecondSeed); | ||
}); | ||
} | ||
|
||
[Fact] | ||
[SkipOnTargetFramework(TargetFrameworkMonikers.Netcoreapp, "AppContextSwitch is not supported on .NETCore")] | ||
public void EnsureSeedReturnsEqualValuesWhenAppContextSwitchIsSet() | ||
{ | ||
int FirstSeed = CalculateHashCodeInRemoteProcess(setAppContextSwitch: true); | ||
int SecondSeed = CalculateHashCodeInRemoteProcess(setAppContextSwitch: true); | ||
|
||
Assert.Equal(FirstSeed, SecondSeed); | ||
} | ||
|
||
private int CalculateHashCodeInRemoteProcess(bool setAppContextSwitch) | ||
{ | ||
RemoteInvokeOptions options = new RemoteInvokeOptions() | ||
{ | ||
CheckExitCode = false | ||
}; | ||
var executor = (setAppContextSwitch) ? | ||
RemoteExecutor.Invoke(() => | ||
{ | ||
AppContext.SetSwitch("Switch.System.Data.UseNonRandomizedHashSeed", true); | ||
return GetHashCodeSeed(); | ||
}, options) : | ||
RemoteExecutor.Invoke(GetHashCodeSeed, options); | ||
|
||
int hashedResult = executor.ExitCode; | ||
executor.Dispose(); | ||
|
||
return hashedResult; | ||
|
||
int GetHashCodeSeed() | ||
{ | ||
System.HashCode hc = new System.HashCode(); | ||
hc.Add(5); | ||
return hc.ToHashCode(); | ||
}; | ||
} | ||
|
||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/Microsoft.Bcl.HashCode/tests/Microsoft.Bcl.HashCode.Tests.csproj
This file contains 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,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<Configurations>netfx-Debug;netfx-Release;netcoreapp-Debug;netcoreapp-Release</Configurations> | ||
<IncludeRemoteExecutor>true</IncludeRemoteExecutor> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="HashCodeSeedInitializerTests.cs" /> | ||
</ItemGroup> | ||
</Project> |
This file contains 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