Skip to content

Commit

Permalink
Fixed flaky tests (#3122)
Browse files Browse the repository at this point in the history
* Fixed WriteCache_WritesInL1L2_TestAsync by DisableParallelization=true

* Fixed TestTokenAcquisitionHostNotAspNetCore by resetting TokenAcquirerFactory default state

---------

Co-authored-by: Alex Holub <alexholub@microsoft.com>
  • Loading branch information
alexholub113 and Alex Holub authored Nov 1, 2024
1 parent 8dba2e8 commit 45beafc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions tests/Microsoft.Identity.Web.Test/L1L2CacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Microsoft.Identity.Web.Test
{
[CollectionDefinition(nameof(L1L2CacheTests), DisableParallelization = true)]
public class L1L2CacheTests
{
private const string DefaultCacheKey = "default-key";
Expand Down Expand Up @@ -51,12 +52,12 @@ public async Task WriteCache_WritesInL1L2_TestAsync(bool enableAsyncL2Write)
Assert.Empty(L2Cache._dict);

// Act
TestDistributedCache.ResetEvent.Reset();
L2Cache.ResetEvent.Reset();
await _testCacheAdapter.TestWriteCacheBytesAsync(DefaultCacheKey, cache);

// Assert
Assert.Equal(1, _testCacheAdapter._memoryCache.Count);
TestDistributedCache.ResetEvent.Wait();
L2Cache.ResetEvent.Wait();
Assert.Single(L2Cache._dict);
}

Expand Down Expand Up @@ -168,12 +169,12 @@ private async Task CreateL1L2TestWithSerializerHintsAsync(
cacheSerializerHints.SuggestedCacheExpiry = dateTimeOffset;

// Act
TestDistributedCache.ResetEvent.Reset();
L2Cache.ResetEvent.Reset();
await _testCacheAdapter.TestWriteCacheBytesAsync(DefaultCacheKey, cache, cacheSerializerHints);

// Assert
Assert.Equal(memoryCacheExpectedCount, _testCacheAdapter._memoryCache.Count);
TestDistributedCache.ResetEvent.Wait();
L2Cache.ResetEvent.Wait();
Assert.Single(L2Cache._dict);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Microsoft.Identity.Web.Test/TestDistributedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Identity.Web.Test
public class TestDistributedCache : IDistributedCache
{
internal readonly ConcurrentDictionary<string, Entry> _dict = new ConcurrentDictionary<string, Entry>();
internal static ManualResetEventSlim ResetEvent { get; set; } = new ManualResetEventSlim(initialState: false);
internal ManualResetEventSlim ResetEvent { get; set; } = new ManualResetEventSlim(initialState: false);

public byte[]? Get(string key)
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Microsoft.Identity.Web.Test/TestTokenAcquisitionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class TestTokenAcquisitionHost
[Fact]
public void TestTokenAcquisitionHostNotAspNetCore()
{
TokenAcquirerFactory.ResetDefaultInstance();

// When not adding Services.AddAuthentication, the host should be
TokenAcquirerFactory tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance();
var host = tokenAcquirerFactory.Services.First(s => s.ServiceType.FullName == "Microsoft.Identity.Web.ITokenAcquisitionHost");
Expand Down

0 comments on commit 45beafc

Please sign in to comment.