Skip to content

Commit

Permalink
Attempt at fixing occasionally failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Aug 21, 2024
1 parent a96e8c6 commit 2f42107
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/ConfigCat.Client.Tests/ConfigServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -456,6 +458,8 @@ public async Task ManualPollConfigService_GetConfigAsync_ShouldInvokeCacheGet()

var projectConfig = await service.GetConfigAsync();

GC.KeepAlive(hooks);

// Assert

Assert.AreEqual(cachedPc, projectConfig);
Expand Down Expand Up @@ -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<string>(), It.IsAny<CancellationToken>()), Times.Exactly(2));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 2f42107

Please sign in to comment.