Skip to content

Commit

Permalink
Add tests for shared caching (#73)
Browse files Browse the repository at this point in the history
* Add tests for ensuring that shared caching works correctly across SDKs

Co-authored-by: Peter Csajtai <[email protected]>

---------

Co-authored-by: Peter Csajtai <[email protected]>
  • Loading branch information
adams85 and z4kn4fein authored Jun 14, 2023
1 parent 19d647a commit 4b3efff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/ConfigCat.Client.Tests/ConfigCacheTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -227,6 +228,25 @@ public async Task ConfigCache_ShouldHandleWhenExternalCacheFails(bool isAsync)
loggerMock.Verify(l => l.Log(LogLevel.Error, 2200, ref It.Ref<FormattableLogMessage>.IsAny, It.Is<Exception>(ex => ex is ApplicationException)), Times.Once);
}

[DataRow("test1", "147c5b4c2b2d7c77e1605b1a4309f0ea6684a0c6")]
[DataRow("test2", "c09513b1756de9e4bc48815ec7a142b2441ed4d5")]
[DataTestMethod]
public void CacheKeyGeneration_ShouldBePlatformIndependent(string sdkKey, string expectedCacheKey)
{
Assert.AreEqual(expectedCacheKey, ConfigCatClient.GetCacheKey(sdkKey));
}

private const string PayloadTestConfigJson = "{\"p\":{\"u\":\"https://cdn-global.configcat.com\",\"r\":0},\"f\":{\"testKey\":{\"v\":\"testValue\",\"t\":1,\"p\":[],\"r\":[]}}}";
[DataRow(PayloadTestConfigJson, "2023-06-14T15:27:15.8440000Z", "test-etag", "1686756435844\ntest-etag\n" + PayloadTestConfigJson)]
[DataTestMethod]
public void CachePayloadSerialization_ShouldBePlatformIndependent(string configJson, string timeStamp, string httpETag, string expectedPayload)
{
var timeStampDateTime = DateTimeOffset.ParseExact(timeStamp, "o", CultureInfo.InvariantCulture).UtcDateTime;
var pc = new ProjectConfig(configJson, configJson.Deserialize<SettingsWithPreferences>(), timeStampDateTime, httpETag);

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

private sealed class FakeExternalCache : IConfigCatCache
{
public volatile string? CachedValue = null;
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigCatClient/ConfigCatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ private static IConfigService DetermineConfigService(PollingMode pollingMode, Ht
};
}

private static string GetCacheKey(string sdkKey)
internal static string GetCacheKey(string sdkKey)
{
var key = $"{sdkKey}_{ConfigCatClientOptions.ConfigFileName}_{ProjectConfig.SerializationFormatVersion}";
return key.Hash();
Expand Down

0 comments on commit 4b3efff

Please sign in to comment.