Skip to content

Commit 90aab1a

Browse files
Fix the flaky test EventFires2Async (#5200)
fix the flaky test Co-authored-by: Gladwin Johnson <[email protected]>
1 parent 631ef39 commit 90aab1a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

tests/Microsoft.Identity.Test.Unit/CacheExtension/MsalCacheHelperTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ public async Task CacheWorksWithAdfsAsync()
487487
Assert.AreEqual(1, accounts.Count());
488488
}
489489

490+
// [Bug] Fix the flaky test EventFires2Async #5198
490491
[TestMethod]
491492
[DeploymentItem(@"Resources/token_cache_one_acc_seed.json")]
492-
493493
public async Task EventFires2Async()
494494
{
495495
string cacheWithOneUser = File.ReadAllText(
@@ -508,21 +508,22 @@ public async Task EventFires2Async()
508508
var cache1 = new MockTokenCache();
509509
helper.RegisterCache(cache1);
510510

511-
var semaphore = new SemaphoreSlim(0);
511+
// Use TaskCompletionSource to handle the event firing
512+
var tcs = new TaskCompletionSource<bool>();
512513
int cacheChangedEventFired = 0;
513514

514-
// event is fired asynchronously, test has to wait for it for a while
515+
// Event is fired asynchronously, test has to wait for it for a while
515516
helper.CacheChanged += (_, _) =>
516517
{
517-
semaphore.Release();
518518
cacheChangedEventFired++;
519+
tcs.SetResult(true);
519520
};
520521

521522
// Act - simulate that an external process writes to the token cache
522523
helper.CacheStore.WriteData(Encoding.UTF8.GetBytes(cacheWithOneUser));
523524

524525
// Assert
525-
await semaphore.WaitAsync(5000).ConfigureAwait(false); // if event isn't fired in 5s, bail out
526+
await tcs.Task.ConfigureAwait(false); // if event isn't fired, this will wait indefinitely
526527
Assert.AreEqual(1, cacheChangedEventFired);
527528
}
528529

0 commit comments

Comments
 (0)