From 2f42107cc511b77a62609f605a3678fc6c3373c9 Mon Sep 17 00:00:00 2001 From: Adam Simon Date: Wed, 21 Aug 2024 12:11:41 +0200 Subject: [PATCH] Attempt at fixing occasionally failing tests --- .../ConfigServiceTests.cs | 25 +++++++++++++++++++ .../SynchronizationContextDeadlockTests.cs | 4 +-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/ConfigCat.Client.Tests/ConfigServiceTests.cs b/src/ConfigCat.Client.Tests/ConfigServiceTests.cs index feb462c3..f9d39139 100644 --- a/src/ConfigCat.Client.Tests/ConfigServiceTests.cs +++ b/src/ConfigCat.Client.Tests/ConfigServiceTests.cs @@ -194,6 +194,8 @@ public async Task LazyLoadConfigService_RefreshConfigAsync_ConfigChanged_ShouldR await service.RefreshConfigAsync(); + GC.KeepAlive(hooks); + // Assert Assert.IsTrue(configChangedEvents.TryDequeue(out var configChangedEvent)); @@ -456,6 +458,8 @@ public async Task ManualPollConfigService_GetConfigAsync_ShouldInvokeCacheGet() var projectConfig = await service.GetConfigAsync(); + GC.KeepAlive(hooks); + // Assert Assert.AreEqual(cachedPc, projectConfig); @@ -508,10 +512,13 @@ public async Task ManualPollConfigService_RefreshConfigAsync_ShouldInvokeCacheGe new CacheParameters(this.cacheMock.Object, cacheKey: null!), this.loggerMock.Object.AsWrapper(), hooks: hooks); + // Act await service.RefreshConfigAsync(); + GC.KeepAlive(hooks); + // Assert this.cacheMock.Verify(m => m.GetAsync(It.IsAny(), It.IsAny()), Times.Exactly(2)); @@ -566,6 +573,8 @@ public async Task ManualPollConfigService_RefreshConfigAsync_ConfigChanged_Shoul await service.RefreshConfigAsync(); + GC.KeepAlive(hooks); + // Assert Assert.IsTrue(configChangedEvents.TryDequeue(out var configChangedEvent)); @@ -699,6 +708,8 @@ public async Task AutoPollConfigService_GetConfig_ReturnsCachedConfigWhenCachedC actualPc = isAsync ? await service.GetConfigAsync() : service.GetConfig(); } + GC.KeepAlive(hooks); + // Assert Assert.AreEqual(cachedPc, actualPc); @@ -762,10 +773,14 @@ public async Task AutoPollConfigService_GetConfig_FetchesConfigWhenCachedConfigI cts.Cancel(); clientReadyCalled = task == clientReadyTask && task.Status == TaskStatus.RanToCompletion; + await Task.Yield(); + // Wait for the hook event handlers to execute (as that might not happen if the service got disposed immediately). SpinWait.SpinUntil(() => configFetchedEvents.TryPeek(out _), TimeSpan.FromSeconds(1)); } + GC.KeepAlive(hooks); + // Assert Assert.AreEqual(fetchedPc, actualPc); @@ -835,10 +850,14 @@ public async Task AutoPollConfigService_GetConfig_ReturnsExpiredConfigWhenCantRe cts.Cancel(); clientReadyCalled = task == clientReadyTask && task.Status == TaskStatus.RanToCompletion; + await Task.Yield(); + // Wait for the hook event handlers to execute (as that might not happen if the service got disposed immediately). SpinWait.SpinUntil(() => configFetchedEvents.TryPeek(out _), TimeSpan.FromSeconds(1)); } + GC.KeepAlive(hooks); + // Assert Assert.AreEqual(fetchedPc, actualPc); @@ -900,6 +919,9 @@ public async Task LazyLoadConfigService_GetConfig_ReturnsCachedConfigWhenCachedC { actualPc = isAsync ? await service.GetConfigAsync() : service.GetConfig(); } + + GC.KeepAlive(hooks); + // Assert Assert.AreEqual(cachedPc, actualPc); @@ -964,6 +986,9 @@ public async Task LazyLoadConfigService_GetConfig_FetchesConfigWhenCachedConfigI { actualPc = isAsync ? await service.GetConfigAsync() : service.GetConfig(); } + + GC.KeepAlive(hooks); + // Assert Assert.AreEqual(fetchedPc, actualPc); diff --git a/src/ConfigCat.Client.Tests/SynchronizationContextDeadlockTests.cs b/src/ConfigCat.Client.Tests/SynchronizationContextDeadlockTests.cs index 18f3a45d..2d4961d3 100644 --- a/src/ConfigCat.Client.Tests/SynchronizationContextDeadlockTests.cs +++ b/src/ConfigCat.Client.Tests/SynchronizationContextDeadlockTests.cs @@ -60,7 +60,7 @@ public void AutoPollDeadLockCheck() [TestMethod] public void ManualPollDeadLockCheck() { - var client = ConfigCatClient.Get(SDKKEY, options => + using var client = ConfigCatClient.Get(SDKKEY, options => { options.Logger = new ConsoleLogger(LogLevel.Off); options.HttpClientHandler = SharedHandler; @@ -72,7 +72,7 @@ public void ManualPollDeadLockCheck() [TestMethod] public void LazyLoadDeadLockCheck() { - var client = ConfigCatClient.Get(SDKKEY, options => + using var client = ConfigCatClient.Get(SDKKEY, options => { options.Logger = new ConsoleLogger(LogLevel.Off); options.HttpClientHandler = SharedHandler;