Skip to content

Commit

Permalink
default value handling fix (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: andrew-cat <[email protected]>
  • Loading branch information
endret and andrew-cat authored Jul 2, 2020
1 parent 694cd31 commit 8ced777
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,8 @@ public class BasicConfigCatClientIntegrationTests
{
private const string SDKKEY = "PKDVCLf-Hq-h-kCzMp-L7Q/psuH7BGHoUmdONrzzUOY7A";

private static readonly IConfigCatClient client = new ConfigCatClient(SDKKEY);

private static readonly ILogger consoleLogger = new ConsoleLogger(LogLevel.Debug);

[ClassCleanup()]
public static void ClassCleanup()
{
client?.Dispose();
}

[TestMethod]
public void ManualPollGetValue()
{
Expand Down
5 changes: 2 additions & 3 deletions src/ConfigCat.Client.Tests/ConfigServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public async Task AutoPollConfigService_GetConfigAsync_WithTimer_ShouldInvokeFet
}

[TestMethod]
public async Task AutoPollConfigService_RefreshConfigAsync_ShouldNotInvokeCacheGetAndFetchAndCacheSet()
public async Task AutoPollConfigService_RefreshConfigAsync_ShouldOnceInvokeCacheGetAndFetchAndCacheSet()
{
// Arrange

Expand Down Expand Up @@ -279,7 +279,6 @@ public async Task AutoPollConfigService_RefreshConfigAsync_ConfigCahged_ShouldRa
// Assert

Assert.AreEqual(1, eventChanged);

}

[TestMethod]
Expand Down Expand Up @@ -405,7 +404,7 @@ public void ConfigService_WithNonDisposableConfigFetcher_DisposeShouldWork()
// Act

configService.Dispose();
}
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/ConfigCat.Client.Tests/ExceptionThrowerHttpClientHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace ConfigCat.Client.Tests
{
internal sealed class ExceptionThrowerHttpClientHandler : HttpClientHandler
{
private readonly Exception exception;

public ExceptionThrowerHttpClientHandler(Exception ex = null)
{
this.exception = ex ?? new NotImplementedException();
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
throw exception;
}
}
}
27 changes: 22 additions & 5 deletions src/ConfigCat.Client.Tests/HttpConfigFetcherTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using ConfigCat.Client.ConfigService;
using ConfigCat.Client.Evaluate;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Threading.Tasks;

namespace ConfigCat.Client.Tests
Expand Down Expand Up @@ -46,5 +43,25 @@ public void HttpConfigFetcher_WithCustomHttpClientHandler_HandlersDisposeShouldN

Assert.IsFalse(myHandler.Disposed);
}

[TestMethod]
public async Task HttpConfigFetcher_ThrowAnException_ShouldReturPassedConfig()
{
// Arrange

var myHandler = new ExceptionThrowerHttpClientHandler(new WebException());

var instance = new HttpConfigFetcher(new Uri("http://example.com"), "1.0", new MyCounterLogger(), myHandler);

var lastConfig = new ProjectConfig("{ }", DateTime.UtcNow, "\"ETAG\"");

// Act

var actual = await instance.Fetch(lastConfig);

// Assert

Assert.AreEqual(lastConfig, actual);
}
}
}
6 changes: 3 additions & 3 deletions src/ConfigCat.Client.Tests/MyHttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace ConfigCat.Client.Tests
{
internal sealed class MyHttpClientHandler : HttpClientHandler
{
public byte SendAsyncInvokeCount { get; private set; } = 0;
public byte SendAsyncInvokeCount { get; private set; } = 0;

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
SendAsyncInvokeCount++;

return base.SendAsync(request, cancellationToken);
}
}

public void Reset()
{
SendAsyncInvokeCount = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/ConfigCatClient/ConfigCatClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
<PackageProjectUrl>https://github.com/ConfigCat/.net-sdk</PackageProjectUrl>
<RepositoryUrl>https://github.com/ConfigCat/.net-sdk</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Version 5.1.0
<PackageReleaseNotes>Version 5.2.0
* Bugfix (config fetch, caching)
Version 5.1.0
* Remove semver nuget packages
Version 5.0.0
* Breaking change: Renamed `API Key` to `SDK Key`.
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigCatClient/ConfigService/AutoPollConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private async Task RefreshLogicAsync(object sender)

var newConfig = await this.configFetcher.Fetch(latestConfig);

if (!latestConfig.Equals(newConfig))
if (!latestConfig.Equals(newConfig) && !newConfig.Equals(ProjectConfig.Empty))
{
this.log.Debug("config changed");

Expand Down
26 changes: 10 additions & 16 deletions src/ConfigCatClient/HttpConfigFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class HttpConfigFetcher : IConfigFetcher, IDisposable
private HttpClient httpClient;

private readonly Uri requestUri;

public HttpConfigFetcher(Uri requestUri, string productVersion, ILogger logger, HttpClientHandler httpClientHandler)
{
this.requestUri = requestUri;
Expand All @@ -42,29 +42,25 @@ public async Task<ProjectConfig> Fetch(ProjectConfig lastConfig)
RequestUri = this.requestUri
};

if (lastConfig.HttpETag != null)
{
request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(lastConfig.HttpETag));
}

try
{
var response = await this.httpClient.SendAsync(request).ConfigureAwait(false);

if (response.StatusCode == System.Net.HttpStatusCode.NotModified)
if (lastConfig.HttpETag != null)
{
newConfig = lastConfig;
request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue(lastConfig.HttpETag));
}
else if (response.IsSuccessStatusCode)

newConfig = lastConfig;

var response = await this.httpClient.SendAsync(request).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
newConfig.HttpETag = response.Headers.ETag.Tag;

newConfig.JsonString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
newConfig = lastConfig;

this.log.Error("Double-check your SDK Key at https://app.configcat.com/sdkkey");
}
else
Expand Down Expand Up @@ -97,7 +93,7 @@ private void ReInitializeHttpClient()
}
else
{
this.httpClient = new HttpClient(this.httpClientHandler, false);
this.httpClient = new HttpClient(this.httpClientHandler, false);
}

this.httpClient.Timeout = TimeSpan.FromSeconds(30);
Expand All @@ -114,6 +110,4 @@ public void Dispose()
}
}
}

internal sealed class WrapClientHandler : DelegatingHandler { }
}

0 comments on commit 8ced777

Please sign in to comment.