Skip to content

Commit 22bf0d0

Browse files
authored
Sleep in MultipleThreadsForceRefresh (#55231)
* Sleep in MultipleThreadsForceRefresh ...to increase the likelihood of two threads racing to enter the critical section. For #55227 * Also quarantine test
1 parent 31b48af commit 22bf0d0

File tree

1 file changed

+17
-13
lines changed
  • src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement

1 file changed

+17
-13
lines changed

src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyRingProviderTests.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ private static ICacheableKeyRingProvider SetupCreateCacheableKeyRingTestAndCreat
807807
[Theory]
808808
[InlineData(true)]
809809
[InlineData(false)]
810+
[QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/55227")]
810811
public async Task MultipleThreadsForceRefresh(bool failsToReadKeyRing)
811812
{
812813
const int taskCount = 10;
@@ -816,21 +817,24 @@ public async Task MultipleThreadsForceRefresh(bool failsToReadKeyRing)
816817
var expectedException = new InvalidOperationException(nameof(MultipleThreadsForceRefresh));
817818

818819
var mockCacheableKeyRingProvider = new Mock<ICacheableKeyRingProvider>();
819-
if (failsToReadKeyRing)
820-
{
821-
mockCacheableKeyRingProvider
822-
.Setup(o => o.GetCacheableKeyRing(now))
823-
.Throws(expectedException);
824-
}
825-
else
826-
{
827-
mockCacheableKeyRingProvider
828-
.Setup(o => o.GetCacheableKeyRing(now))
829-
.Returns(new CacheableKeyRing(
820+
mockCacheableKeyRingProvider
821+
.Setup(o => o.GetCacheableKeyRing(now))
822+
.Returns<DateTimeOffset>(_ =>
823+
{
824+
// Simulate doing actual work. We need this so that other threads have an opportunity
825+
// to bypass the critical section.
826+
Thread.Sleep(200);
827+
828+
if (failsToReadKeyRing)
829+
{
830+
throw expectedException;
831+
}
832+
833+
return new CacheableKeyRing(
830834
expirationToken: CancellationToken.None,
831835
expirationTime: now.AddDays(1),
832-
keyRing: expectedKeyRing.Object));
833-
}
836+
keyRing: expectedKeyRing.Object);
837+
});
834838

835839
var keyRingProvider = CreateKeyRingProvider(mockCacheableKeyRingProvider.Object);
836840

0 commit comments

Comments
 (0)