Skip to content

Commit

Permalink
Implement SDK key format validation + fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Jul 3, 2023
1 parent 79bb238 commit 8085424
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ static void Configure(ConfigCatClientOptions options)
public async Task Http_Timeout_Test_Async()
{
var response = $"{{ \"f\": {{ \"fakeKey\": {{ \"v\": \"fakeValue\", \"p\": [] ,\"r\": [] }} }} }}";
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake", options =>
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake-67890123456789012/1234567890123456789012", options =>
{
options.PollingMode = PollingModes.ManualPoll;
options.Logger = ConsoleLogger;
Expand All @@ -357,7 +357,7 @@ public async Task Http_Timeout_Test_Async()
public void Http_Timeout_Test_Sync()
{
var response = $"{{ \"f\": {{ \"fakeKey\": {{ \"v\": \"fakeValue\", \"p\": [] ,\"r\": [] }} }} }}";
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake", options =>
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake-67890123456789012/1234567890123456789012", options =>
{
options.PollingMode = PollingModes.ManualPoll;
options.Logger = ConsoleLogger;
Expand All @@ -374,7 +374,7 @@ public async Task Ensure_MaxInitWait_Overrides_Timeout()
{
var now = DateTimeOffset.UtcNow;
var response = $"{{ \"f\": {{ \"fakeKey\": {{ \"v\": \"fakeValue\", \"p\": [] ,\"r\": [] }} }} }}";
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake", options =>
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake-67890123456789012/1234567890123456789012", options =>
{
options.PollingMode = PollingModes.AutoPoll(maxInitWaitTime: TimeSpan.FromSeconds(1));
options.Logger = ConsoleLogger;
Expand All @@ -390,7 +390,7 @@ public void Ensure_MaxInitWait_Overrides_Timeout_Sync()
{
var now = DateTimeOffset.UtcNow;
var response = $"{{ \"f\": {{ \"fakeKey\": {{ \"v\": \"fakeValue\", \"p\": [] ,\"r\": [] }} }} }}";
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake", options =>
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake-67890123456789012/1234567890123456789012", options =>
{
options.PollingMode = PollingModes.AutoPoll(maxInitWaitTime: TimeSpan.FromSeconds(1));
options.Logger = ConsoleLogger;
Expand All @@ -407,7 +407,7 @@ public void Ensure_Client_Dispose_Kill_Hanging_Http_Call()
var defer = new ManualResetEvent(false);
var now = DateTimeOffset.UtcNow;
var response = $"{{ \"f\": {{ \"fakeKey\": {{ \"v\": \"fakeValue\", \"p\": [] ,\"r\": [] }} }} }}";
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake", options =>
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake-67890123456789012/1234567890123456789012", options =>
{
options.Logger = ConsoleLogger;
options.HttpClientHandler = new FakeHttpClientHandler(System.Net.HttpStatusCode.OK, response, TimeSpan.FromSeconds(5));
Expand All @@ -426,7 +426,7 @@ public void Ensure_Client_Dispose_Kill_Hanging_Http_Call_Sync()
var defer = new ManualResetEvent(false);
var now = DateTimeOffset.UtcNow;
var response = $"{{ \"f\": {{ \"fakeKey\": {{ \"v\": \"fakeValue\", \"p\": [] ,\"r\": [] }} }} }}";
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake", options =>
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake-67890123456789012/1234567890123456789012", options =>
{
options.Logger = ConsoleLogger;
options.HttpClientHandler = new FakeHttpClientHandler(System.Net.HttpStatusCode.OK, response, TimeSpan.FromSeconds(5));
Expand All @@ -447,7 +447,7 @@ public void Ensure_Client_Dispose_Kill_Hanging_Http_Call_Sync()
public void Ensure_Multiple_Requests_Doesnt_Interfere_In_ValueTasks()
{
var response = $"{{ \"f\": {{ \"fakeKey\": {{ \"v\": \"fakeValue\", \"p\": [] ,\"r\": [] }} }} }}";
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake", options =>
using IConfigCatClient manualPollClient = ConfigCatClient.Get("fake-67890123456789012/1234567890123456789012", options =>
{
options.Logger = ConsoleLogger;
options.PollingMode = PollingModes.ManualPoll;
Expand Down
38 changes: 25 additions & 13 deletions src/ConfigCat.Client.Tests/ConfigCatClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void CreateAnInstance_WhenLazyLoadConfigurationTimeToLiveSecondsIsZero_Sh
[DoNotParallelize]
public void CreateAnInstance_WhenLoggerIsNull_ShouldCreateAnInstance()
{
using var client = ConfigCatClient.Get("hsdrTr4sxbHdSgdhHRZds346hdgsS2vfsgf/GsdrTr4sxbHdSgdhHRZds346hdOPsSgvfsgf", options =>
using var client = ConfigCatClient.Get("hsdrTr4sxbHdSgdhHRZds3/GsdrTr4sxbHdSgdhHRZds3", options =>
{
options.Logger = null;
});
Expand All @@ -95,7 +95,7 @@ public void CreateAnInstance_WhenLoggerIsNull_ShouldCreateAnInstance()
[DoNotParallelize]
public void CreateAnInstance_WithSdkKey_ShouldCreateAnInstance()
{
using var _ = ConfigCatClient.Get("hsdrTr4sxbHdSgdhHRZds346hdgsS2vfsgf/GsdrTr4sxbHdSgdhHRZds346hdOPsSgvfsgf");
using var _ = ConfigCatClient.Get("hsdrTr4sxbHdSgdhHRZds3/GsdrTr4sxbHdSgdhHRZds3");
}

[TestMethod]
Expand Down Expand Up @@ -149,6 +149,10 @@ public void GetValue_EvaluateServiceThrowException_ShouldReturnDefaultValue()

const string defaultValue = "Victory for the Firstborn!";

this.configServiceMock
.Setup(m => m.GetConfig())
.Throws<Exception>();

this.evaluatorMock
.Setup(m => m.Evaluate(ref It.Ref<EvaluateContext>.IsAny))
.Throws<Exception>();
Expand Down Expand Up @@ -178,6 +182,10 @@ public async Task GetValueAsync_EvaluateServiceThrowException_ShouldReturnDefaul

const string defaultValue = "Victory for the Firstborn!";

this.configServiceMock
.Setup(m => m.GetConfigAsync(It.IsAny<CancellationToken>()))
.Throws<Exception>();

this.evaluatorMock
.Setup(m => m.Evaluate(ref It.Ref<EvaluateContext>.IsAny))
.Throws<Exception>();
Expand Down Expand Up @@ -622,6 +630,10 @@ public async Task GetAllValueDetails_ConfigServiceThrowException_ShouldReturnEmp
{
// Arrange

this.configServiceMock
.Setup(m => m.GetConfig())
.Throws<Exception>();

this.configServiceMock
.Setup(m => m.GetConfigAsync(It.IsAny<CancellationToken>()))
.Throws<Exception>();
Expand Down Expand Up @@ -779,7 +791,7 @@ public void GetAllKeys_DeserializerThrowException_ShouldReturnsWithEmptyArray()
{
// Arrange

this.configServiceMock.Setup(m => m.GetConfigAsync(It.IsAny<CancellationToken>())).ReturnsAsync(ProjectConfig.Empty);
this.configServiceMock.Setup(m => m.GetConfig()).Returns(ProjectConfig.Empty);
var o = new Config();

IConfigCatClient instance = new ConfigCatClient(
Expand Down Expand Up @@ -1065,7 +1077,7 @@ public async Task ForceRefreshAsync_ShouldBeCancelable()

private static IConfigCatClient CreateClientFromLocalFile(string fileName, User? defaultUser = null)
{
return ConfigCatClient.Get("localhost", options =>
return ConfigCatClient.Get("localhost-123456789012/1234567890123456789012", options =>
{
options.FlagOverrides = FlagOverrides.LocalFile(
Path.Combine("data", fileName),
Expand Down Expand Up @@ -1160,11 +1172,11 @@ void Configure(ConfigCatClientOptions options)

// Act

using var client1 = ConfigCatClient.Get("test", Configure);
using var client1 = ConfigCatClient.Get("test-67890123456789012/1234567890123456789012", Configure);
var warnings1 = warnings.ToArray();

warnings.Clear();
using var client2 = ConfigCatClient.Get("test", passConfigureToSecondGet ? Configure : null);
using var client2 = ConfigCatClient.Get("test-67890123456789012/1234567890123456789012", passConfigureToSecondGet ? Configure : null);
var warnings2 = warnings.ToArray();

// Assert
Expand All @@ -1189,7 +1201,7 @@ public void Dispose_CachedInstanceRemoved()
{
// Arrange

var client1 = ConfigCatClient.Get("test", options => options.PollingMode = PollingModes.ManualPoll);
var client1 = ConfigCatClient.Get("test-67890123456789012/1234567890123456789012", options => options.PollingMode = PollingModes.ManualPoll);

// Act

Expand All @@ -1211,7 +1223,7 @@ public void Dispose_CanRemoveCurrentCachedInstanceOnly()
{
// Arrange

var client1 = ConfigCatClient.Get("test", options => options.PollingMode = PollingModes.ManualPoll);
var client1 = ConfigCatClient.Get("test-67890123456789012/1234567890123456789012", options => options.PollingMode = PollingModes.ManualPoll);

// Act

Expand All @@ -1221,7 +1233,7 @@ public void Dispose_CanRemoveCurrentCachedInstanceOnly()

var instanceCount2 = ConfigCatClient.Instances.GetAliveCount();

var client2 = ConfigCatClient.Get("test", options => options.PollingMode = PollingModes.ManualPoll);
var client2 = ConfigCatClient.Get("test-67890123456789012/1234567890123456789012", options => options.PollingMode = PollingModes.ManualPoll);

var instanceCount3 = ConfigCatClient.Instances.GetAliveCount();

Expand All @@ -1248,8 +1260,8 @@ public void DisposeAll_CachedInstancesRemoved()
{
// Arrange

var client1 = ConfigCatClient.Get("test1", options => options.PollingMode = PollingModes.AutoPoll());
var client2 = ConfigCatClient.Get("test2", options => options.PollingMode = PollingModes.ManualPoll);
var client1 = ConfigCatClient.Get("test1-7890123456789012/1234567890123456789012", options => options.PollingMode = PollingModes.AutoPoll());
var client2 = ConfigCatClient.Get("test2-7890123456789012/1234567890123456789012", options => options.PollingMode = PollingModes.ManualPoll);

// Act

Expand Down Expand Up @@ -1283,8 +1295,8 @@ static void CreateClients(out int instanceCount)
// because that could interfere with this test: when raising the event, the service acquires a strong reference to the client,
// which would temporarily prevent the client from being GCd. This could break the test in the case of unlucky timing.
// Setting maxInitWaitTime to zero prevents this because then the event is raised immediately at creation.
var client1 = ConfigCatClient.Get("test1", options => options.PollingMode = PollingModes.AutoPoll(maxInitWaitTime: TimeSpan.Zero));
var client2 = ConfigCatClient.Get("test2", options => options.PollingMode = PollingModes.ManualPoll);
var client1 = ConfigCatClient.Get("test1-7890123456789012/1234567890123456789012", options => options.PollingMode = PollingModes.AutoPoll(maxInitWaitTime: TimeSpan.Zero));
var client2 = ConfigCatClient.Get("test2-7890123456789012/1234567890123456789012", options => options.PollingMode = PollingModes.ManualPoll);

instanceCount = ConfigCatClient.Instances.GetAliveCount();

Expand Down
Loading

0 comments on commit 8085424

Please sign in to comment.