Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Nov 17, 2023
1 parent 4efb8c6 commit 66a7ef6
Show file tree
Hide file tree
Showing 6 changed files with 652 additions and 95 deletions.
39 changes: 4 additions & 35 deletions src/ConfigCat.Client.Tests/ConfigCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using ConfigCat.Client.Cache;
using ConfigCat.Client.Tests.Fakes;
using ConfigCat.Client.Tests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
Expand Down Expand Up @@ -70,20 +71,20 @@ public async Task ConfigCache_Override_ManualPoll_Works()
});

configCacheMock.Verify(c => c.SetAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
configCacheMock.Verify(c => c.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
configCacheMock.Verify(c => c.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once);

var actual = await client.GetValueAsync("stringDefaultCat", "N/A");

Assert.AreEqual("N/A", actual);
configCacheMock.Verify(c => c.SetAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
configCacheMock.Verify(c => c.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once);
configCacheMock.Verify(c => c.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Exactly(2));

await client.ForceRefreshAsync();

actual = await client.GetValueAsync("stringDefaultCat", "N/A");
Assert.AreEqual("Cat", actual);
configCacheMock.Verify(c => c.SetAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once);
configCacheMock.Verify(c => c.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Exactly(3));
configCacheMock.Verify(c => c.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Exactly(4));
}

[TestMethod]
Expand Down Expand Up @@ -246,36 +247,4 @@ public void CachePayloadSerialization_ShouldBePlatformIndependent(string configJ

Assert.AreEqual(expectedPayload, ProjectConfig.Serialize(pc));
}

private sealed class FakeExternalCache : IConfigCatCache
{
public volatile string? CachedValue = null;

public string? Get(string key) => this.CachedValue;

public Task<string?> GetAsync(string key, CancellationToken cancellationToken = default) => Task.FromResult(Get(key));

public void Set(string key, string value) => this.CachedValue = value;

public Task SetAsync(string key, string value, CancellationToken cancellationToken = default)
{
Set(key, value);
return Task.FromResult(0);
}
}

private sealed class FaultyFakeExternalCache : IConfigCatCache
{
public string? Get(string key) => throw new ApplicationException("Operation failed :(");

public Task<string?> GetAsync(string key, CancellationToken cancellationToken = default) => Task.FromResult(Get(key));

public void Set(string key, string value) => throw new ApplicationException("Operation failed :(");

public Task SetAsync(string key, string value, CancellationToken cancellationToken = default)
{
Set(key, value);
return Task.FromResult(0);
}
}
}
Loading

0 comments on commit 66a7ef6

Please sign in to comment.