From 3d89dece629e766e26a3834c6f215c92664a2023 Mon Sep 17 00:00:00 2001 From: martincostello Date: Wed, 2 Jun 2021 14:50:03 +0100 Subject: [PATCH 1/2] Use Random.Shared Use the new Random.Shared property to remove the need to allocate a new instance. --- src/EFCore/Storage/ExecutionStrategy.cs | 4 ++++ .../Query/NorthwindMiscellaneousQueryTestBase.cs | 6 +++--- .../ProceduralQueryExpressionGenerator.cs | 2 +- .../TestUtilities/SqlServerTestStore.cs | 2 +- .../Internal/ObservableBackedBindingListTest.cs | 2 +- .../ChangeTracking/Internal/SortableBindingListTest.cs | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/EFCore/Storage/ExecutionStrategy.cs b/src/EFCore/Storage/ExecutionStrategy.cs index 7e70d2aa9f0..6441fdf4b18 100644 --- a/src/EFCore/Storage/ExecutionStrategy.cs +++ b/src/EFCore/Storage/ExecutionStrategy.cs @@ -93,7 +93,11 @@ protected ExecutionStrategy( /// /// A pseudo-random number generator that can be used to vary the delay between retries. /// +#if NET6_0_OR_GREATER + protected virtual Random Random { get; } = Random.Shared; +#else protected virtual Random Random { get; } = new(); +#endif /// /// The maximum number of retry attempts. diff --git a/test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs b/test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs index 337e7bbe72f..e8a95af504f 100644 --- a/test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs +++ b/test/EFCore.Specification.Tests/Query/NorthwindMiscellaneousQueryTestBase.cs @@ -3644,7 +3644,7 @@ public virtual Task Random_next_is_not_funcletized_1(bool async) { return AssertQuery( async, - ss => ss.Set().Where(o => o.OrderID < (new Random().Next() - 2147483647))); + ss => ss.Set().Where(o => o.OrderID < (Random.Shared.Next() - 2147483647))); } [ConditionalTheory] @@ -3653,7 +3653,7 @@ public virtual Task Random_next_is_not_funcletized_2(bool async) { return AssertQuery( async, - ss => ss.Set().Where(o => o.OrderID > new Random().Next(5)), + ss => ss.Set().Where(o => o.OrderID > Random.Shared.Next(5)), entryCount: 830); } @@ -3663,7 +3663,7 @@ public virtual Task Random_next_is_not_funcletized_3(bool async) { return AssertQuery( async, - ss => ss.Set().Where(o => o.OrderID > new Random().Next(0, 10)), + ss => ss.Set().Where(o => o.OrderID > Random.Shared.Next(0, 10)), entryCount: 830); } diff --git a/test/EFCore.Specification.Tests/TestUtilities/QueryTestGeneration/ProceduralQueryExpressionGenerator.cs b/test/EFCore.Specification.Tests/TestUtilities/QueryTestGeneration/ProceduralQueryExpressionGenerator.cs index c1958aaf6c0..6e54c1a36b6 100644 --- a/test/EFCore.Specification.Tests/TestUtilities/QueryTestGeneration/ProceduralQueryExpressionGenerator.cs +++ b/test/EFCore.Specification.Tests/TestUtilities/QueryTestGeneration/ProceduralQueryExpressionGenerator.cs @@ -308,7 +308,7 @@ private static void AddExpectedFailure(string testName, string expectedException public void Execute(IQueryable query, DbContext context, string testMethodName) { - var seed = ProceduralQueryExpressionGenerator.Seed ?? new Random().Next(); + var seed = ProceduralQueryExpressionGenerator.Seed ?? Random.Shared.Next(); var random = new Random(seed); var depth = 2; diff --git a/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerTestStore.cs b/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerTestStore.cs index a9e1e619ca3..8e89ada26c6 100644 --- a/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerTestStore.cs +++ b/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerTestStore.cs @@ -442,7 +442,7 @@ public static string CreateConnectionString(string name, string fileName = null, { var builder = new SqlConnectionStringBuilder(TestEnvironment.DefaultConnection) { - MultipleActiveResultSets = multipleActiveResultSets ?? new Random().Next(0, 2) == 1, InitialCatalog = name + MultipleActiveResultSets = multipleActiveResultSets ?? Random.Shared.Next(0, 2) == 1, InitialCatalog = name }; if (fileName != null) { diff --git a/test/EFCore.Tests/ChangeTracking/Internal/ObservableBackedBindingListTest.cs b/test/EFCore.Tests/ChangeTracking/Internal/ObservableBackedBindingListTest.cs index dccacbf5924..abda440d805 100644 --- a/test/EFCore.Tests/ChangeTracking/Internal/ObservableBackedBindingListTest.cs +++ b/test/EFCore.Tests/ChangeTracking/Internal/ObservableBackedBindingListTest.cs @@ -541,7 +541,7 @@ public ListElement(int i) NullableInt = i; String = i.ToString(); XNode = new NotXText(i.ToString()); - Random = new Random(); + Random = Random.Shared; ByteArray = new[] { (byte)i, (byte)i, (byte)i, (byte)i }; } diff --git a/test/EFCore.Tests/ChangeTracking/Internal/SortableBindingListTest.cs b/test/EFCore.Tests/ChangeTracking/Internal/SortableBindingListTest.cs index e72337b5a31..d5bba325481 100644 --- a/test/EFCore.Tests/ChangeTracking/Internal/SortableBindingListTest.cs +++ b/test/EFCore.Tests/ChangeTracking/Internal/SortableBindingListTest.cs @@ -198,7 +198,7 @@ public ListElement(int i) Int = i; NullableInt = i; String = i.ToString(); - Random = new Random(); + Random = Random.Shared; ByteArray = new[] { (byte)i, (byte)i, (byte)i, (byte)i }; } From e3e0c2e7dd2947393cd64933df4d3dad2003a44b Mon Sep 17 00:00:00 2001 From: martincostello Date: Sat, 5 Jun 2021 10:58:27 +0100 Subject: [PATCH 2/2] Revert change to ExecutionStrategy Revert change as suggested in PR feedback. --- src/EFCore/Storage/ExecutionStrategy.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/EFCore/Storage/ExecutionStrategy.cs b/src/EFCore/Storage/ExecutionStrategy.cs index 6441fdf4b18..7e70d2aa9f0 100644 --- a/src/EFCore/Storage/ExecutionStrategy.cs +++ b/src/EFCore/Storage/ExecutionStrategy.cs @@ -93,11 +93,7 @@ protected ExecutionStrategy( /// /// A pseudo-random number generator that can be used to vary the delay between retries. /// -#if NET6_0_OR_GREATER - protected virtual Random Random { get; } = Random.Shared; -#else protected virtual Random Random { get; } = new(); -#endif /// /// The maximum number of retry attempts.