소스 검색

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
Andrew Casey 1 년 전
부모
커밋
22bf0d0bf0

+ 17 - 13
src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/KeyManagement/KeyRingProviderTests.cs

@@ -807,6 +807,7 @@ public class KeyRingProviderTests
     [Theory]
     [InlineData(true)]
     [InlineData(false)]
+    [QuarantinedTest("https://github.com/dotnet/aspnetcore/issues/55227")]
     public async Task MultipleThreadsForceRefresh(bool failsToReadKeyRing)
     {
         const int taskCount = 10;
@@ -816,21 +817,24 @@ public class KeyRingProviderTests
         var expectedException = new InvalidOperationException(nameof(MultipleThreadsForceRefresh));
 
         var mockCacheableKeyRingProvider = new Mock<ICacheableKeyRingProvider>();
-        if (failsToReadKeyRing)
-        {
-            mockCacheableKeyRingProvider
-                .Setup(o => o.GetCacheableKeyRing(now))
-                .Throws(expectedException);
-        }
-        else
-        {
-            mockCacheableKeyRingProvider
-                .Setup(o => o.GetCacheableKeyRing(now))
-                .Returns(new CacheableKeyRing(
+        mockCacheableKeyRingProvider
+            .Setup(o => o.GetCacheableKeyRing(now))
+            .Returns<DateTimeOffset>(_ =>
+            {
+                // Simulate doing actual work.  We need this so that other threads have an opportunity
+                // to bypass the critical section.
+                Thread.Sleep(200);
+
+                if (failsToReadKeyRing)
+                {
+                    throw expectedException;
+                }
+
+                return new CacheableKeyRing(
                     expirationToken: CancellationToken.None,
                     expirationTime: now.AddDays(1),
-                    keyRing: expectedKeyRing.Object));
-        }
+                    keyRing: expectedKeyRing.Object);
+            });
 
         var keyRingProvider = CreateKeyRingProvider(mockCacheableKeyRingProvider.Object);