Skip to content

Commit

Permalink
implement datagovernance logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-cat committed Oct 8, 2020
1 parent 2000189 commit 2630454
Show file tree
Hide file tree
Showing 39 changed files with 1,774 additions and 614 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 ConfigCat
Copyright (c) 2020 ConfigCat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ notifications:
to:
- [email protected]
on_build_success: false
on_build_failure: true
on_build_failure: false
2 changes: 1 addition & 1 deletion src/ConfigCat.Client.Tests/BasicConfigEvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ConfigCat.Client.Tests
[TestClass]
public class BasicConfigEvaluatorTests : ConfigEvaluatorTestsBase
{
protected override string SampleJsonFileName => "sample_v4.json";
protected override string SampleJsonFileName => "sample_v5.json";

protected override string MatrixResultFileName => "testmatrix.csv";

Expand Down
11 changes: 8 additions & 3 deletions src/ConfigCat.Client.Tests/ConfigCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ public void ConfigCache_Override_AutoPoll_Works()
});

configCacheMock.Setup(c => c.GetAsync(It.IsAny<string>(), CancellationToken.None)).ReturnsAsync(() => cachedConfig);

var client = ConfigCatClientBuilder.Initialize(SDKKEY).WithAutoPoll().WithConfigCache(configCacheMock.Object).Create();


var client = ConfigCatClientBuilder
.Initialize(SDKKEY)
.WithLogger(new ConsoleLogger(LogLevel.Debug))
.WithAutoPoll()
.WithConfigCache(configCacheMock.Object)
.Create();

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

Assert.AreEqual("Cat", actual);
Expand Down
12 changes: 6 additions & 6 deletions src/ConfigCat.Client.Tests/ConfigCat.Client.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
</ItemGroup>

<ItemGroup>
<None Update="data\sample_number_v4.json">
<None Update="data\sample_number_v5.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\sample_sensitive_v4.json">
<None Update="data\sample_sensitive_v5.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\sample_semantic_2_v4.json">
<None Update="data\sample_semantic_2_v5.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\sample_semantic_v4.json">
<None Update="data\sample_semantic_v5.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\sample_v4.json">
<None Update="data\sample_v5.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\sample_variationid_v4.json">
<None Update="data\sample_variationid_v5.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="data\testmatrix.csv">
Expand Down
8 changes: 4 additions & 4 deletions src/ConfigCat.Client.Tests/ConfigServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public async Task LazyLoadConfigService_RefreshConfigAsync_ShouldNotInvokeCacheG
}

[TestMethod]
public async Task AutoPollConfigService_GetConfigAsync_WithoutTimer_ShouldInvokeFetchAndCacheSetAndCacheGet2x()
public async Task AutoPollConfigService_GetConfigAsync_WithoutTimerWithCachedConfig_ShouldInvokeCacheGet1xAndSetNeverFetchNever()
{
// Arrange

Expand Down Expand Up @@ -171,9 +171,9 @@ public async Task AutoPollConfigService_GetConfigAsync_WithoutTimer_ShouldInvoke

// Assert

this.cacheMock.Verify(m => m.GetAsync(It.IsAny<string>(), CancellationToken.None), Times.Exactly(2));
this.cacheMock.Verify(m => m.SetAsync(It.IsAny<string>(), fetchedPc), Times.Once);
this.fetcherMock.Verify(m => m.Fetch(cachedPc), Times.Once);
this.cacheMock.Verify(m => m.GetAsync(It.IsAny<string>(), CancellationToken.None), Times.Once);
this.cacheMock.Verify(m => m.SetAsync(It.IsAny<string>(), fetchedPc), Times.Never);
this.fetcherMock.Verify(m => m.Fetch(cachedPc), Times.Never);
}

[TestMethod]
Expand Down
3 changes: 3 additions & 0 deletions src/ConfigCat.Client.Tests/CustomHttpClientHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public void AutoPoll_WithHttpClientHandlerOverride_ShouldReturnCatUseCustomImple
// Arrange

var client = ConfigCatClientBuilder.Initialize(SDKKEY)
.WithDataGovernance(DataGovernance.EuOnly)
.WithAutoPoll()
.WithHttpClientHandler(httpClientHandler)
.Create();
Expand All @@ -42,6 +43,7 @@ public void ManualPoll_WithHttpClientHandlerOverride_ShouldReturnCatUseCustomImp
// Arrange

var client = ConfigCatClientBuilder.Initialize(SDKKEY)
.WithDataGovernance(DataGovernance.EuOnly)
.WithManualPoll()
.WithHttpClientHandler(httpClientHandler)
.Create();
Expand All @@ -63,6 +65,7 @@ public void LazyLoad_WithHttpClientHandlerOverride_ShouldReturnCatUseCustomImple
// Arrange

var client = ConfigCatClientBuilder.Initialize(SDKKEY)
.WithDataGovernance(DataGovernance.EuOnly)
.WithLazyLoad()
.WithHttpClientHandler(httpClientHandler)
.Create();
Expand Down
54 changes: 54 additions & 0 deletions src/ConfigCat.Client.Tests/DataGovernanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Moq.Protected;

namespace ConfigCat.Client.Tests
{
[TestClass]
public class DataGovernanceTests
{
Uri GlobalUri = new Uri(ConfigurationBase.BaseUrlGlobal);

[TestMethod]
public async Task WithDefaultDataGovernanceSetting_ShouldUseGlobalCdnEveryRequests()
{
var configuration = new AutoPollConfiguration
{
SdkKey = "DEMO"
};

var handlerMock = new Mock<HttpClientHandler>(MockBehavior.Strict);
handlerMock
.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)

.ReturnsAsync(new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("{\"p\": {\"u\": \"http://example.com\", \"r\": 0}}"),
})
.Verifiable();

IConfigFetcher fetcher = new HttpConfigFetcher(configuration.CreateUri(), "DEMO", Mock.Of<ILogger>(), handlerMock.Object, configuration.IsCustomBaseUrl);

// Act

await fetcher.Fetch(ProjectConfig.Empty);

// Assert

handlerMock.VerifyAll();
// TODO invoke count + URL check
}
}
}
10 changes: 6 additions & 4 deletions src/ConfigCat.Client.Tests/HttpConfigFetcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async Task HttpConfigFetcher_WithCustomHttpClientHandler_ShouldUsePassedH

var myHandler = new MyFakeHttpClientHandler();

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

// Act

Expand All @@ -33,7 +33,7 @@ public void HttpConfigFetcher_WithCustomHttpClientHandler_HandlersDisposeShouldN

var myHandler = new MyFakeHttpClientHandler();

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

// Act

Expand All @@ -45,13 +45,13 @@ public void HttpConfigFetcher_WithCustomHttpClientHandler_HandlersDisposeShouldN
}

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

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

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

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

Expand All @@ -63,5 +63,7 @@ public async Task HttpConfigFetcher_ThrowAnException_ShouldReturPassedConfig()

Assert.AreEqual(lastConfig, actual);
}

// TODO race condition tests for Fetch()!!!
}
}
16 changes: 14 additions & 2 deletions src/ConfigCat.Client.Tests/MyFakeHttpClientHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -7,15 +8,26 @@ namespace ConfigCat.Client.Tests
{
internal class MyFakeHttpClientHandler : HttpClientHandler
{
private readonly HttpStatusCode httpStatusCode;

public byte SendInvokeCount { get; private set; } = 0;

public bool Disposed { get; private set; } = false;

public SortedList<byte, HttpRequestMessage> Requests = new SortedList<byte, HttpRequestMessage>();

public MyFakeHttpClientHandler(HttpStatusCode httpStatusCode = HttpStatusCode.NotModified)
{
this.httpStatusCode = httpStatusCode;
}

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

var response = new HttpResponseMessage(HttpStatusCode.NotModified);
Requests.Add(SendInvokeCount, request);

var response = new HttpResponseMessage(this.httpStatusCode);

return Task.FromResult(response);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigCat.Client.Tests/NumericConfigEvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ConfigCat.Client.Tests
[TestClass]
public class NumericConfigEvaluatorTests : ConfigEvaluatorTestsBase
{
protected override string SampleJsonFileName => "sample_number_v4.json";
protected override string SampleJsonFileName => "sample_number_v5.json";

protected override string MatrixResultFileName => "testmatrix_number.csv";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ConfigCat.Client.Tests
[TestClass]
public class SemanticVersion2ConfigEvaluatorTests : ConfigEvaluatorTestsBase
{
protected override string SampleJsonFileName => "sample_semantic_2_v4.json";
protected override string SampleJsonFileName => "sample_semantic_2_v5.json";

protected override string MatrixResultFileName => "testmatrix_semantic_2.csv";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ConfigCat.Client.Tests
[TestClass]
public class SemanticVersionConfigEvaluatorTests : ConfigEvaluatorTestsBase
{
protected override string SampleJsonFileName => "sample_semantic_v4.json";
protected override string SampleJsonFileName => "sample_semantic_v5.json";

protected override string MatrixResultFileName => "testmatrix_semantic.csv";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ConfigCat.Client.Tests
[TestClass]
public class SensitiveEvaluatorTests : ConfigEvaluatorTestsBase
{
protected override string SampleJsonFileName => "sample_sensitive_v4.json";
protected override string SampleJsonFileName => "sample_sensitive_v5.json";

protected override string MatrixResultFileName => "testmatrix_sensitive.csv";
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigCat.Client.Tests/VariationIdEvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ConfigCat.Client.Tests
[TestClass]
public class VariationIdEvaluatorTests : ConfigEvaluatorTestsBase
{
protected override string SampleJsonFileName => "sample_variationid_v4.json";
protected override string SampleJsonFileName => "sample_variationid_v5.json";

protected override string MatrixResultFileName => "testmatrix_variationid.csv";

Expand Down
1 change: 0 additions & 1 deletion src/ConfigCat.Client.Tests/data/sample_number_v4.json

This file was deleted.

85 changes: 85 additions & 0 deletions src/ConfigCat.Client.Tests/data/sample_number_v5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"f" : {
"numberWithPercentage": {
"v": "Default",
"t": 1,
"p": [
{
"o": 0,
"v": "80%",
"p": 80
},
{
"o": 1,
"v": "20%",
"p": 20
}
],
"r": [
{
"o": 0,
"a": "Custom1",
"t": 10,
"c": "sajt",
"v": "=sajt"
},
{
"o": 1,
"a": "Custom1",
"t": 12,
"c": "2.1",
"v": "<2.1"
},
{
"o": 2,
"a": "Custom1",
"t": 13,
"c": "2,1",
"v": "<=2,1"
},
{
"o": 3,
"a": "Custom1",
"t": 10,
"c": "3.5",
"v": "=3.5"
},
{
"o": 4,
"a": "Custom1",
"t": 14,
"c": "5",
"v": ">5"
},
{
"o": 5,
"a": "Custom1",
"t": 15,
"c": "5",
"v": ">=5"
},
{
"o": 6,
"a": "Custom1",
"t": 11,
"c": "4.2",
"v": "<>4.2"
}
]
},
"number": {
"v": "Default",
"t": 1,
"p": [],
"r": [
{
"o": 0,
"a": "Custom1",
"t": 11,
"c": "5",
"v": "<>5"
}
]
}
}
}
1 change: 0 additions & 1 deletion src/ConfigCat.Client.Tests/data/sample_semantic_2_v4.json

This file was deleted.

Loading

0 comments on commit 2630454

Please sign in to comment.