Skip to content

Commit 45beafc

Browse files
alexholub113Alex Holub
andauthored
Fixed flaky tests (#3122)
* Fixed WriteCache_WritesInL1L2_TestAsync by DisableParallelization=true * Fixed TestTokenAcquisitionHostNotAspNetCore by resetting TokenAcquirerFactory default state --------- Co-authored-by: Alex Holub <[email protected]>
1 parent 8dba2e8 commit 45beafc

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

tests/Microsoft.Identity.Web.Test/L1L2CacheTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace Microsoft.Identity.Web.Test
1818
{
19+
[CollectionDefinition(nameof(L1L2CacheTests), DisableParallelization = true)]
1920
public class L1L2CacheTests
2021
{
2122
private const string DefaultCacheKey = "default-key";
@@ -51,12 +52,12 @@ public async Task WriteCache_WritesInL1L2_TestAsync(bool enableAsyncL2Write)
5152
Assert.Empty(L2Cache._dict);
5253

5354
// Act
54-
TestDistributedCache.ResetEvent.Reset();
55+
L2Cache.ResetEvent.Reset();
5556
await _testCacheAdapter.TestWriteCacheBytesAsync(DefaultCacheKey, cache);
5657

5758
// Assert
5859
Assert.Equal(1, _testCacheAdapter._memoryCache.Count);
59-
TestDistributedCache.ResetEvent.Wait();
60+
L2Cache.ResetEvent.Wait();
6061
Assert.Single(L2Cache._dict);
6162
}
6263

@@ -168,12 +169,12 @@ private async Task CreateL1L2TestWithSerializerHintsAsync(
168169
cacheSerializerHints.SuggestedCacheExpiry = dateTimeOffset;
169170

170171
// Act
171-
TestDistributedCache.ResetEvent.Reset();
172+
L2Cache.ResetEvent.Reset();
172173
await _testCacheAdapter.TestWriteCacheBytesAsync(DefaultCacheKey, cache, cacheSerializerHints);
173174

174175
// Assert
175176
Assert.Equal(memoryCacheExpectedCount, _testCacheAdapter._memoryCache.Count);
176-
TestDistributedCache.ResetEvent.Wait();
177+
L2Cache.ResetEvent.Wait();
177178
Assert.Single(L2Cache._dict);
178179
}
179180

tests/Microsoft.Identity.Web.Test/TestDistributedCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.Identity.Web.Test
1111
public class TestDistributedCache : IDistributedCache
1212
{
1313
internal readonly ConcurrentDictionary<string, Entry> _dict = new ConcurrentDictionary<string, Entry>();
14-
internal static ManualResetEventSlim ResetEvent { get; set; } = new ManualResetEventSlim(initialState: false);
14+
internal ManualResetEventSlim ResetEvent { get; set; } = new ManualResetEventSlim(initialState: false);
1515

1616
public byte[]? Get(string key)
1717
{

tests/Microsoft.Identity.Web.Test/TestTokenAcquisitionHost.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class TestTokenAcquisitionHost
1313
[Fact]
1414
public void TestTokenAcquisitionHostNotAspNetCore()
1515
{
16+
TokenAcquirerFactory.ResetDefaultInstance();
17+
1618
// When not adding Services.AddAuthentication, the host should be
1719
TokenAcquirerFactory tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance();
1820
var host = tokenAcquirerFactory.Services.First(s => s.ServiceType.FullName == "Microsoft.Identity.Web.ITokenAcquisitionHost");

0 commit comments

Comments
 (0)