diff --git a/Splitio.Redis/Services/Client/Classes/RedisClient.cs b/Splitio.Redis/Services/Client/Classes/RedisClient.cs index 4cfc76eac..f80273207 100644 --- a/Splitio.Redis/Services/Client/Classes/RedisClient.cs +++ b/Splitio.Redis/Services/Client/Classes/RedisClient.cs @@ -28,12 +28,10 @@ public class RedisClient : SplitClient private IImpressionsCache _impressionsCache; private ConnectionPoolManager _connectionPoolManager; private IFeatureFlagCacheConsumer _featureFlagCacheConsumer; - private readonly new FallbackTreatmentCalculator _fallbackTreatmentCalculator; - public RedisClient(ConfigurationOptions config, string apiKey, FallbackTreatmentCalculator fallbackTreatmentCalculator) : base(apiKey, fallbackTreatmentCalculator) + public RedisClient(ConfigurationOptions config, string apiKey) : base(apiKey, config) { _config = new RedisConfig(); - _fallbackTreatmentCalculator = fallbackTreatmentCalculator; ReadConfig(config); diff --git a/src/Splitio/Services/Cache/Classes/InMemoryReadinessGatesCache.cs b/src/Splitio/Services/Cache/Classes/InMemoryReadinessGatesCache.cs index d1d42e51c..f784fbbed 100644 --- a/src/Splitio/Services/Cache/Classes/InMemoryReadinessGatesCache.cs +++ b/src/Splitio/Services/Cache/Classes/InMemoryReadinessGatesCache.cs @@ -1,4 +1,7 @@ -using Splitio.Services.Cache.Interfaces; +using Splitio.Domain; +using Splitio.Services.Cache.Interfaces; +using Splitio.Services.Common; +using System.Collections.Generic; using System.Threading; namespace Splitio.Services.Client.Classes @@ -7,6 +10,12 @@ public class InMemoryReadinessGatesCache : IStatusManager { private readonly CountdownEvent _sdkReady = new CountdownEvent(1); private readonly CountdownEvent _sdkDestroyed = new CountdownEvent(1); + private readonly IEventsManager _eventsManager; + + public InMemoryReadinessGatesCache(IEventsManager eventsManager) + { + _eventsManager = eventsManager; + } public bool IsReady() { @@ -21,6 +30,8 @@ public bool WaitUntilReady(int milliseconds) public void SetReady() { _sdkReady.Signal(); + _eventsManager.NotifyInternalEvent(SdkInternalEvent.SdkReady, + new EventMetadata(new Dictionary())); } public void SetDestroy() diff --git a/src/Splitio/Services/Client/Classes/JSONFileClient.cs b/src/Splitio/Services/Client/Classes/JSONFileClient.cs index ec5a40aa6..5c2faeba1 100644 --- a/src/Splitio/Services/Client/Classes/JSONFileClient.cs +++ b/src/Splitio/Services/Client/Classes/JSONFileClient.cs @@ -24,7 +24,7 @@ public class JSONFileClient : SplitClient public JSONFileClient(string splitsFilePath, string segmentsFilePath, - FallbackTreatmentCalculator fallbackTreatmentCalculator, + ConfigurationOptions config, ISegmentCache segmentCacheInstance = null, IFeatureFlagCache featureFlagCacheInstance = null, IImpressionsLog impressionsLog = null, @@ -33,11 +33,10 @@ public JSONFileClient(string splitsFilePath, ITrafficTypeValidator trafficTypeValidator = null, IImpressionsManager impressionsManager = null, IRuleBasedSegmentCache ruleBasedSegmentCache = null - ) : base("localhost", fallbackTreatmentCalculator) + ) : base("localhost", config) { - var eventsManager = new EventsManager(new EventsManagerConfig(), new EventDelivery()); - _segmentCache = segmentCacheInstance ?? new InMemorySegmentCache(new ConcurrentDictionary(), eventsManager); - var rbsCache = ruleBasedSegmentCache ?? new InMemoryRuleBasedSegmentCache(new ConcurrentDictionary(), eventsManager); + _segmentCache = segmentCacheInstance ?? new InMemorySegmentCache(new ConcurrentDictionary(), _eventsManager); + var rbsCache = ruleBasedSegmentCache ?? new InMemoryRuleBasedSegmentCache(new ConcurrentDictionary(), _eventsManager); var segmentFetcher = new JSONFileSegmentFetcher(segmentsFilePath, _segmentCache); var splitChangeFetcher = new JSONFileSplitChangeFetcher(splitsFilePath); @@ -55,13 +54,13 @@ public JSONFileClient(string splitsFilePath, } BuildFlagSetsFilter(new HashSet()); - _featureFlagCache = featureFlagCacheInstance ?? new InMemorySplitCache(new ConcurrentDictionary(parsedSplits), _flagSetsFilter, eventsManager); + _featureFlagCache = featureFlagCacheInstance ?? new InMemorySplitCache(new ConcurrentDictionary(parsedSplits), _flagSetsFilter, _eventsManager); _impressionsLog = impressionsLog; _eventsLog = eventsLog; _trafficTypeValidator = trafficTypeValidator; _blockUntilReadyService = new NoopBlockUntilReadyService(); _manager = new SplitManager(_featureFlagCache, _blockUntilReadyService); - _evaluator = new Evaluator.Evaluator(_featureFlagCache, new Splitter(), null, fallbackTreatmentCalculator); + _evaluator = new Evaluator.Evaluator(_featureFlagCache, new Splitter(), null, _fallbackTreatmentCalculator); _uniqueKeysTracker = new NoopUniqueKeysTracker(); _impressionsCounter = new NoopImpressionsCounter(); _impressionsObserver = new NoopImpressionsObserver(); diff --git a/src/Splitio/Services/Client/Classes/LocalhostClient.cs b/src/Splitio/Services/Client/Classes/LocalhostClient.cs index e42fca1ec..96495a1a7 100644 --- a/src/Splitio/Services/Client/Classes/LocalhostClient.cs +++ b/src/Splitio/Services/Client/Classes/LocalhostClient.cs @@ -1,7 +1,6 @@ using Splitio.Domain; using Splitio.Services.Cache.Classes; using Splitio.Services.Cache.Interfaces; -using Splitio.Services.Common; using Splitio.Services.EngineEvaluator; using Splitio.Services.Impressions.Classes; using Splitio.Services.InputValidation.Classes; @@ -28,7 +27,7 @@ public class LocalhostClient : SplitClient private readonly object _lock = new object(); - public LocalhostClient(ConfigurationOptions configurationOptions, FallbackTreatmentCalculator fallbackTreatmentCalculator) : base("localhost", fallbackTreatmentCalculator) + public LocalhostClient(ConfigurationOptions configurationOptions) : base("localhost", configurationOptions) { var configs = (LocalhostClientConfigurations)_configService.ReadConfig(configurationOptions, ConfigTypes.Localhost, _statusManager); @@ -47,9 +46,8 @@ public LocalhostClient(ConfigurationOptions configurationOptions, FallbackTreatm BuildFlagSetsFilter(new HashSet()); - var eventsManager = new EventsManager(new EventsManagerConfig(), new EventDelivery()); var splits = _localhostFileService.ParseSplitFile(_fullPath); - _featureFlagCache = new InMemorySplitCache(splits, _flagSetsFilter, eventsManager); + _featureFlagCache = new InMemorySplitCache(splits, _flagSetsFilter, _eventsManager); if (configs.FileSync != null) @@ -64,7 +62,7 @@ public LocalhostClient(ConfigurationOptions configurationOptions, FallbackTreatm _blockUntilReadyService = new NoopBlockUntilReadyService(); _manager = new SplitManager(_featureFlagCache, _blockUntilReadyService); _trafficTypeValidator = new TrafficTypeValidator(_featureFlagCache, _blockUntilReadyService); - _evaluator = new Evaluator.Evaluator(_featureFlagCache, new Splitter(), null, fallbackTreatmentCalculator); + _evaluator = new Evaluator.Evaluator(_featureFlagCache, new Splitter(), null, _fallbackTreatmentCalculator); _uniqueKeysTracker = new NoopUniqueKeysTracker(); _impressionsCounter = new NoopImpressionsCounter(); _impressionsObserver = new NoopImpressionsObserver(); diff --git a/src/Splitio/Services/Client/Classes/SelfRefreshingClient.cs b/src/Splitio/Services/Client/Classes/SelfRefreshingClient.cs index 370e6a2b8..14fae995b 100644 --- a/src/Splitio/Services/Client/Classes/SelfRefreshingClient.cs +++ b/src/Splitio/Services/Client/Classes/SelfRefreshingClient.cs @@ -51,17 +51,12 @@ public class SelfRefreshingClient : SplitClient private IUpdater _featureFlagUpdater; private IRuleBasedSegmentCache _ruleBasedSegmentCache; private IUpdater _ruleBasedSegmentUpdater; - private readonly new FallbackTreatmentCalculator _fallbackTreatmentCalculator; - private EventsManager _eventsManager; - public SelfRefreshingClient(string apiKey, ConfigurationOptions config, - FallbackTreatmentCalculator fallbackTreatmentCalculator) : base(apiKey, fallbackTreatmentCalculator) + public SelfRefreshingClient(string apiKey, ConfigurationOptions config) : base(apiKey, config) { _config = (SelfRefreshingConfig)_configService.ReadConfig(config, ConfigTypes.InMemory); - _fallbackTreatmentCalculator = fallbackTreatmentCalculator; BuildFlagSetsFilter(_config.FlagSetsFilter); - BuildEventsManager(); BuildSplitCache(); BuildSegmentCache(); BuildRuleBasedSegmentCache(); @@ -90,10 +85,6 @@ public SelfRefreshingClient(string apiKey, ConfigurationOptions config, } #region Private Methods - private void BuildEventsManager() - { - _eventsManager = new EventsManager(new EventsManagerConfig(), new EventDelivery()); - } private void BuildSplitCache() { _featureFlagCache = new InMemorySplitCache(new ConcurrentDictionary(_config.ConcurrencyLevel, InitialCapacity), _flagSetsFilter, _eventsManager); @@ -218,7 +209,7 @@ private void BuildManager() private void BuildBlockUntilReadyService() { - _blockUntilReadyService = new SelfRefreshingBlockUntilReadyService(_statusManager, _telemetryInitProducer); + _blockUntilReadyService = new SelfRefreshingBlockUntilReadyService(_statusManager, _telemetryInitProducer, _eventsManager); } private void BuildTelemetrySyncTask() diff --git a/src/Splitio/Services/Client/Classes/SplitClient.cs b/src/Splitio/Services/Client/Classes/SplitClient.cs index c8d383f9d..82c99b1e1 100644 --- a/src/Splitio/Services/Client/Classes/SplitClient.cs +++ b/src/Splitio/Services/Client/Classes/SplitClient.cs @@ -63,16 +63,22 @@ public abstract class SplitClient : ISplitClient protected IImpressionsObserver _impressionsObserver; protected IClientExtensionService _clientExtensionService; protected IFlagSetsFilter _flagSetsFilter; + protected IEventsManager _eventsManager; public event EventHandler SdkReady; public event EventHandler SdkUpdate; public event EventHandler SdkTimedOut; - protected SplitClient(string apikey, FallbackTreatmentCalculator fallbackTreatmentCalculator) + protected SplitClient(string apikey, ConfigurationOptions options) { ApiKey = apikey; + _fallbackTreatmentCalculator = new FallbackTreatmentCalculator(options.FallbackTreatments); + _eventsManager = new EventsManager(new EventsManagerConfig(), new EventDelivery()); + + _eventsManager.Register(SdkEvent.SdkReady, TriggerSdkReadyEvent); + _eventsManager.Register(SdkEvent.SdkUpdate, TriggerSdkUpdateEvent); + _eventsManager.Register(SdkEvent.SdkReadyTimeout, TriggerSdkTimeoutEvent); - _fallbackTreatmentCalculator = fallbackTreatmentCalculator; _wrapperAdapter = WrapperAdapter.Instance(); _keyValidator = new KeyValidator(); _splitNameValidator = new SplitNameValidator(); @@ -81,7 +87,7 @@ protected SplitClient(string apikey, FallbackTreatmentCalculator fallbackTreatme _factoryInstantiationsService = FactoryInstantiationsService.Instance(); _flagSetsValidator = new FlagSetsValidator(); _configService = new ConfigService(_wrapperAdapter, _flagSetsValidator, new SdkMetadataValidator()); - _statusManager = new InMemoryReadinessGatesCache(); + _statusManager = new InMemoryReadinessGatesCache(_eventsManager); _tasksManager = new TasksManager(_statusManager); } @@ -573,6 +579,21 @@ private static SplitResult TreatmentWithConfig(List results) return new SplitResult(result.Treatment, result.Config); } + + private void TriggerSdkReadyEvent(EventMetadata metadata) + { + SdkReady?.Invoke(this, metadata); + } + + private void TriggerSdkUpdateEvent(EventMetadata metadata) + { + SdkUpdate?.Invoke(this, metadata); + } + + private void TriggerSdkTimeoutEvent(EventMetadata metadata) + { + SdkTimedOut?.Invoke(this, metadata); + } #endregion } } \ No newline at end of file diff --git a/src/Splitio/Services/Client/Classes/SplitFactory.cs b/src/Splitio/Services/Client/Classes/SplitFactory.cs index 92f8b46ec..1cb95e988 100644 --- a/src/Splitio/Services/Client/Classes/SplitFactory.cs +++ b/src/Splitio/Services/Client/Classes/SplitFactory.cs @@ -1,6 +1,5 @@ using Splitio.Domain; using Splitio.Services.Client.Interfaces; -using Splitio.Services.Impressions.Classes; using Splitio.Services.InputValidation.Classes; using Splitio.Services.InputValidation.Interfaces; using Splitio.Services.Shared.Classes; @@ -58,7 +57,6 @@ public ISplitManager Manager() private void BuildSplitClient() { - FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculator(_options.FallbackTreatments); switch (_options.Mode) { case Mode.Standalone: @@ -66,11 +64,11 @@ private void BuildSplitClient() if (_apiKey == "localhost") { - _client = new LocalhostClient(_options, fallbackTreatmentCalculator); + _client = new LocalhostClient(_options); } else { - _client = new SelfRefreshingClient(_apiKey, _options, fallbackTreatmentCalculator); + _client = new SelfRefreshingClient(_apiKey, _options); } break; case Mode.Consumer: @@ -81,7 +79,7 @@ private void BuildSplitClient() var redisAssembly = Assembly.Load(new AssemblyName("Splitio.Redis")); var redisType = redisAssembly.GetType("Splitio.Redis.Services.Client.Classes.RedisClient"); - _client = (ISplitClient)Activator.CreateInstance(redisType, new object[] { _options, _apiKey, fallbackTreatmentCalculator }); + _client = (ISplitClient)Activator.CreateInstance(redisType, new object[] { _options, _apiKey }); } catch (ArgumentException ex) { diff --git a/src/Splitio/Services/Common/EventDelivery.cs b/src/Splitio/Services/Common/EventDelivery.cs index 288cb018b..38b04ffe9 100644 --- a/src/Splitio/Services/Common/EventDelivery.cs +++ b/src/Splitio/Services/Common/EventDelivery.cs @@ -1,4 +1,5 @@ -using Splitio.Services.Logger; +using Splitio.Domain; +using Splitio.Services.Logger; using Splitio.Services.Shared.Classes; using System; diff --git a/src/Splitio/Services/Shared/Classes/SelfRefreshingBlockUntilReadyService.cs b/src/Splitio/Services/Shared/Classes/SelfRefreshingBlockUntilReadyService.cs index fcc27eecb..5edb873c5 100644 --- a/src/Splitio/Services/Shared/Classes/SelfRefreshingBlockUntilReadyService.cs +++ b/src/Splitio/Services/Shared/Classes/SelfRefreshingBlockUntilReadyService.cs @@ -1,8 +1,11 @@ -using Splitio.Services.Cache.Interfaces; +using Splitio.Domain; +using Splitio.Services.Cache.Interfaces; +using Splitio.Services.Common; using Splitio.Services.Logger; using Splitio.Services.Shared.Interfaces; using Splitio.Telemetry.Storages; using System; +using System.Collections.Generic; namespace Splitio.Services.Shared.Classes { @@ -12,11 +15,14 @@ public class SelfRefreshingBlockUntilReadyService : IBlockUntilReadyService private readonly IStatusManager _statusManager; private readonly ITelemetryInitProducer _telemetryInitProducer; + private readonly IEventsManager _eventsManager; - public SelfRefreshingBlockUntilReadyService(IStatusManager statusManager, ITelemetryInitProducer telemetryInitProducer) + public SelfRefreshingBlockUntilReadyService(IStatusManager statusManager, ITelemetryInitProducer telemetryInitProducer, + IEventsManager eventsManager) { _statusManager = statusManager; _telemetryInitProducer = telemetryInitProducer; + _eventsManager = eventsManager; } public void BlockUntilReady(int blockMilisecondsUntilReady) @@ -30,6 +36,8 @@ public void BlockUntilReady(int blockMilisecondsUntilReady) if (!_statusManager.WaitUntilReady(blockMilisecondsUntilReady)) { + _eventsManager.NotifyInternalEvent(SdkInternalEvent.SdkTimedOut, + new EventMetadata(new Dictionary())); _telemetryInitProducer.RecordBURTimeout(); throw new TimeoutException($"SDK was not ready in {blockMilisecondsUntilReady} milliseconds"); } diff --git a/tests/Splitio-tests/Integration Tests/BaseLocalhostClientTests.cs b/tests/Splitio-tests/Integration Tests/BaseLocalhostClientTests.cs index 4f035f950..263133942 100644 --- a/tests/Splitio-tests/Integration Tests/BaseLocalhostClientTests.cs +++ b/tests/Splitio-tests/Integration Tests/BaseLocalhostClientTests.cs @@ -1,9 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Splitio.Domain; -using Splitio.Redis.Services.Client.Classes; using Splitio.Services.Client.Classes; using Splitio.Services.Impressions.Classes; -using Splitio.Telemetry.Domain; using System; using System.Collections.Generic; using System.IO; @@ -23,14 +21,12 @@ public abstract class BaseLocalhostClientTests { private readonly string rootFilePath; private readonly string _mode; - private readonly FallbackTreatmentCalculator _fallbackTreatmentCalculator; public BaseLocalhostClientTests(string mode) { // This line is to clean the warnings. rootFilePath = string.Empty; _mode = mode; - _fallbackTreatmentCalculator = new FallbackTreatmentCalculator(new FallbackTreatmentsConfiguration()); #if NET_LATEST rootFilePath = @"Resources\"; @@ -42,7 +38,7 @@ public async Task GetTreatmentAsync() { // Arrange. var config = GetConfiguration($"{rootFilePath}test.splits"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -81,7 +77,7 @@ public void GetTreatmentSuccessfully() { //Arrange var config = GetConfiguration($"{rootFilePath}test.splits"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -105,7 +101,7 @@ public void GetTreatmentSuccessfullyWhenUpdatingSplitsFile() // Arrange var filePath = $"{rootFilePath}test2-{_mode}.splits"; var config = GetConfiguration(filePath); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -148,7 +144,7 @@ public void GetTreatmentSuccessfullyWhenUpdatingSplitsFileSameFile() Thread.Sleep(1000); var config = GetConfiguration(filePath); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -171,7 +167,7 @@ public void ClientDestroySuccessfully() { //Arrange var config = GetConfiguration($"{rootFilePath}test.splits"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -193,7 +189,7 @@ public void GetTreatment_WhenIsYamlFile_Successfully() { //Arrange var config = GetConfiguration($"{rootFilePath}split.yml"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -225,7 +221,7 @@ public void GetTreatmentWithConfig_WhenIsYamlFile_Successfully() { //Arrange var config = GetConfiguration($"{rootFilePath}split.yml"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -264,7 +260,7 @@ public void GetTreatment_WhenIsYmlFile_Successfully() { //Arrange var config = GetConfiguration($"{rootFilePath}split.yml"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -296,7 +292,7 @@ public void GetTreatmentWithConfig_WhenIsYmlFile_Successfully() { //Arrange var config = GetConfiguration($"{rootFilePath}split.yml"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -335,7 +331,7 @@ public void GetTreatments_WhenIsYamlFile_Successfully() { //Arrange var config = GetConfiguration($"{rootFilePath}split.yml"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -372,7 +368,7 @@ public void GetTreatmentsWithConfig_WhenIsYamlFile_Successfully() { //Arrange var config = GetConfiguration($"{rootFilePath}split.yml"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -423,7 +419,7 @@ public void GetTreatments_WhenIsYmlFile_Successfully() { //Arrange var config = GetConfiguration($"{rootFilePath}split.yml"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -460,7 +456,7 @@ public void GetTreatmentsWithConfig_WhenIsYmlFile_Successfully() { //Arrange var config = GetConfiguration($"{rootFilePath}split.yml"); - var client = new LocalhostClient(config, _fallbackTreatmentCalculator); + var client = new LocalhostClient(config); client.BlockUntilReady(1000); @@ -511,10 +507,10 @@ public void FallbackTreatments_WhenFeatureDoesNotExist() { var features = new List { "testing_split_on", "feature", "feature2" }; FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global", "\"prop\":\"global\""), new Dictionary() { { "feature", new FallbackTreatment("off-local", "\"prop\":\"local\"") } }); - FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculator(fallbackTreatmentsConfiguration); var config = GetConfiguration($"{rootFilePath}split.yml"); - var client = new LocalhostClient(config, fallbackTreatmentCalculator); + config.FallbackTreatments = fallbackTreatmentsConfiguration; + var client = new LocalhostClient(config); client.BlockUntilReady(1000); diff --git a/tests/Splitio-tests/Integration Tests/InMemoryClientTests.cs b/tests/Splitio-tests/Integration Tests/InMemoryClientTests.cs index 5732ce6c3..cf591e65e 100644 --- a/tests/Splitio-tests/Integration Tests/InMemoryClientTests.cs +++ b/tests/Splitio-tests/Integration Tests/InMemoryClientTests.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json.Serialization; using Splitio.Domain; using Splitio.Services.Client.Classes; +using Splitio.Services.Common; using Splitio.Services.Impressions.Classes; namespace Splitio_Tests.Integration_Tests @@ -11,13 +12,11 @@ namespace Splitio_Tests.Integration_Tests public class InMemoryClientTests { private readonly string rootFilePath; - private readonly FallbackTreatmentCalculator _fallbackTreatmentCalculator; public InMemoryClientTests() { // This line is to clean the warnings. rootFilePath = string.Empty; - _fallbackTreatmentCalculator = new FallbackTreatmentCalculator(new FallbackTreatmentsConfiguration()); #if NET_LATEST rootFilePath = @"Resources\"; @@ -36,7 +35,7 @@ public void OverridingJsonConvertSettingSnakeCaseNamingStrategy() NamingStrategy = new SnakeCaseNamingStrategy() } }; - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); //Act diff --git a/tests/Splitio-tests/Integration Tests/JSONFileClientTests.cs b/tests/Splitio-tests/Integration Tests/JSONFileClientTests.cs index efc6e8b71..5ad0ea235 100644 --- a/tests/Splitio-tests/Integration Tests/JSONFileClientTests.cs +++ b/tests/Splitio-tests/Integration Tests/JSONFileClientTests.cs @@ -3,6 +3,7 @@ using Splitio.Domain; using Splitio.Services.Cache.Interfaces; using Splitio.Services.Client.Classes; +using Splitio.Services.Common; using Splitio.Services.Events.Interfaces; using Splitio.Services.Impressions.Classes; using Splitio.Services.Impressions.Interfaces; @@ -18,13 +19,11 @@ namespace Splitio_Tests.Integration_Tests public class JSONFileClientTests { private readonly string rootFilePath; - private readonly FallbackTreatmentCalculator _fallbackTreatmentCalculator; public JSONFileClientTests() { // This line is to clean the warnings. rootFilePath = string.Empty; - _fallbackTreatmentCalculator = new FallbackTreatmentCalculator(new FallbackTreatmentsConfiguration()); #if NET_LATEST rootFilePath = @"Resources\"; @@ -37,7 +36,7 @@ public JSONFileClientTests() public void ExecuteGetTreatmentOnFailedParsingSplitShouldReturnControl() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); //Act var result = client.GetTreatment("test", "fail", null); @@ -52,7 +51,7 @@ public void ExecuteGetTreatmentOnFailedParsingSplitShouldReturnControl() public void ExecuteGetTreatmentOnFailedParsingSplitShouldNotAffectOtherSplits() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -69,7 +68,7 @@ public void ExecuteGetTreatmentOnFailedParsingSplitShouldNotAffectOtherSplits() public void ExecuteGetTreatmentOnDeletedSplitShouldReturnControl() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -97,7 +96,7 @@ public void ExecuteGetTreatmentOnExceptionShouldReturnControl() .Setup(x => x.GetSplit(It.IsAny())) .Throws(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, null, splitCacheMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions(), featureFlagCacheInstance: splitCacheMock.Object, impressionsLog: impressionsLogMock.Object); //Act var result = client.GetTreatment("test", "asd", null); @@ -113,7 +112,7 @@ public void ExecuteGetTreatmentOnExceptionShouldReturnControl() public void ExecuteGetTreatmentOnRemovedUserFromSegmentShouldReturnOff() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", $"{rootFilePath}segment_payed.json", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", $"{rootFilePath}segment_payed.json", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -135,7 +134,7 @@ public void ExecuteGetTreatmentOnRemovedUserFromSegmentShouldReturnOff() public void ExecuteGetTreatmentOnSplitWithOnOffOnPartition() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -159,7 +158,7 @@ public void ExecuteGetTreatmentOnSplitWithOnOffOnPartition() public void ExecuteGetTreatmentOnSplitWithTrafficAllocation() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -183,7 +182,7 @@ public void ExecuteGetTreatmentOnSplitWithTrafficAllocation() public void ExecuteGetTreatmentOnSplitWithTrafficAllocationWhenAllocationIsDifferentThan100() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -207,7 +206,7 @@ public void ExecuteGetTreatmentOnSplitWithTrafficAllocationWhenAllocationIsDiffe public void ExecuteGetTreatmentOnSplitWithTrafficAllocationWhenAllocationIs1ReturnsRolloutTreatment() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_7.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_7.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -223,7 +222,7 @@ public void ExecuteGetTreatmentOnSplitWithTrafficAllocationWhenAllocationIs1Retu public void ExecuteGetTreatmentOnSplitWithTrafficAllocationWhenAllocationIs1ReturnsDefaultTreatment() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_7.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_7.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -239,7 +238,7 @@ public void ExecuteGetTreatmentOnSplitWithTrafficAllocationWhenAllocationIs1Retu public void ExecuteGetTreatmentOnSplitWithSegmentNotInitialized() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -259,7 +258,7 @@ public void ExecuteGetTreatmentAndLogLabelKilled() { //Arrange var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions(), impressionsLog: impressionsLogMock.Object); client.BlockUntilReady(1000); @@ -283,7 +282,7 @@ public void ExecuteGetTreatmentAndLogLabelNoConditionMatched() { //Arrange var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions(), impressionsLog: impressionsLogMock.Object); client.BlockUntilReady(1000); @@ -307,7 +306,7 @@ public void ExecuteGetTreatmentAndLogLabelSplitNotFound() { //Arrange var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); //Act client.RemoveSplitFromCache("asd"); @@ -336,7 +335,7 @@ public void ExecuteGetTreatmentAndLogLabelException() .Setup(x => x.GetSplit(It.IsAny())) .Throws(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, featureFlagCacheInstance: splitCacheMock.Object, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions { ImpressionsMode = ImpressionsMode.Debug }, impressionsLog: impressionsLogMock.Object, featureFlagCacheInstance: splitCacheMock.Object); client.BlockUntilReady(1000); @@ -360,7 +359,7 @@ public void ExecuteGetTreatmentAndLogLabelTrafficAllocationFailed() { //Arrange var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", new ConfigurationOptions(), impressionsLog: impressionsLogMock.Object); client.BlockUntilReady(1000); @@ -383,7 +382,7 @@ public void ExecuteGetTreatmentAndLogLabelForTreatment() { //Arrange var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions(), impressionsLog: impressionsLogMock.Object); client.BlockUntilReady(1000); @@ -406,7 +405,7 @@ public void ExecuteGetTreatmentAndLogLabelForTreatment() public void ExecuteGetTreatmentWhenUnknownMatcherIsIncluded() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); //Act var result = client.GetTreatment("xs", "Unknown_Matcher", null); @@ -421,7 +420,7 @@ public void ExecuteGetTreatmentAndNotLogLabelForTreatmentIfLabelsNotEnabled() { //Arrange var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object, isLabelsEnabled: false); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions(), impressionsLog: impressionsLogMock.Object, isLabelsEnabled: false); client.BlockUntilReady(1000); @@ -444,7 +443,7 @@ public void ExecuteGetTreatmentAndLogLabelAndBucketingKeyForTreatment() { //Arrange var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions { ImpressionsMode = ImpressionsMode.Debug }, impressionsLog: impressionsLogMock.Object); client.BlockUntilReady(1000); @@ -468,7 +467,7 @@ public void ExecuteGetTreatmentAndLogLabelAndBucketingKeyForTreatment() public void ExecuteGetTreatmentWithBooleanAttribute() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_4.json", "", new ConfigurationOptions()); var attributes = new Dictionary { @@ -490,7 +489,7 @@ public void ExecuteGetTreatmentWithBooleanAttribute() public void ExecuteGetTreatmentWithSetMatcherReturnsOff() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", new ConfigurationOptions()); var attributes = new Dictionary { @@ -512,7 +511,7 @@ public void ExecuteGetTreatmentWithSetMatcherReturnsOff() public void ExecuteGetTreatmentWithSetMatcherReturnsOn() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", new ConfigurationOptions()); var attributes = new Dictionary { @@ -534,7 +533,7 @@ public void ExecuteGetTreatmentWithSetMatcherReturnsOn() public void ExecuteGetTreatmentWithStringMatcherReturnsOff() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", new ConfigurationOptions()); var attributes = new Dictionary { @@ -556,7 +555,7 @@ public void ExecuteGetTreatmentWithStringMatcherReturnsOff() public void ExecuteGetTreatmentWithStringMatcherReturnsOn() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", new ConfigurationOptions()); var attributes = new Dictionary { @@ -578,7 +577,7 @@ public void ExecuteGetTreatmentWithStringMatcherReturnsOn() public void ExecuteGetTreatmentWithDependencyMatcherReturnsOn() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -595,7 +594,7 @@ public void ExecuteGetTreatmentWithDependencyMatcherReturnsOn() public void ExecuteGetTreatmentWithDependencyMatcherReturnsOff() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -613,7 +612,7 @@ public void ExecuteGetTreatmentWithDependencyMatcherImpressionOnChild() { //Arrange var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", new ConfigurationOptions(),impressionsLog: impressionsLogMock.Object); client.BlockUntilReady(1000); @@ -631,7 +630,7 @@ public void GetTreatment_WhenNameDoesntExist_DontLogImpression() { // Arrange. var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); var splitName = "not_exist"; client.BlockUntilReady(1000); @@ -649,7 +648,7 @@ public void GetTreatment_WhenNameDoesntExist_DontLogImpression() public void GetTreatment_WithoutBlockUntiltReady_ReturnsOff() { // Arrange. - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); // Act. var result = client.GetTreatment("key", "anding"); @@ -665,7 +664,7 @@ public void GetTreatment_WithoutBlockUntiltReady_ReturnsOff() public void ExecuteGetTreatments() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); List features = new List { "fail", @@ -696,7 +695,7 @@ public void ExecuteGetTreatments() public void ExecuteGetTreatmentsWithBucketing() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); List features = new List { "fail", @@ -728,7 +727,7 @@ public void ExecuteGetTreatmentsWithBucketing() public void ExecuteGetTreatmentsWithDependencyMatcherReturnsOn() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -751,7 +750,7 @@ public void ExecuteGetTreatmentsWithDependencyMatcherReturnsOn() public void ExecuteGetTreatmentsWithDependencyMatcherWithAttributesReturnsOn() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_6.json", "", new ConfigurationOptions()); client.BlockUntilReady(1000); @@ -779,7 +778,7 @@ public void GetTreatments_WhenNameDoesntExist_DontLogImpression() { // Arrange. var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); var splitNames = new List { "not_exist", "not_exist_1" }; client.BlockUntilReady(1000); @@ -801,7 +800,7 @@ public void GetTreatments_WhenNameDoesntExist_DontLogImpression() public void GetTreatments_WithoutBlockUntiltReady_ReturnsEmptyList() { // Arrange. - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); // Act. var result = client.GetTreatments("key", new List()); @@ -815,7 +814,7 @@ public void GetTreatments_WithoutBlockUntiltReady_ReturnsEmptyList() public void GetTreatments_WithoutBlockUntiltReady_ReturnsTreatments() { // Arrange. - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); // Act. var result = client.GetTreatments("key", new List { "anding", "in_ten_keys" }); @@ -833,7 +832,7 @@ public void GetTreatments_WithoutBlockUntiltReady_ReturnsTreatments() public void GetTreatments_WhenClientIsReadyAndFeaturesIsEmpty_ReturnsEmptyList() { // Arrange. - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); client.BlockUntilReady(100); // Act. @@ -850,7 +849,7 @@ public void GetTreatments_WhenClientIsReadyAndFeaturesIsEmpty_ReturnsEmptyList() public void DestroySucessfully() { //Arrange - var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_5.json", "", new ConfigurationOptions()); var attributes = new Dictionary { @@ -886,7 +885,7 @@ public void Track_WhenClientIsNotReady_ReturnsTrue() // Arrange. var trafficTypeValidator = new Mock(); var eventLog = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, trafficTypeValidator: trafficTypeValidator.Object, eventsLog: eventLog.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions(), trafficTypeValidator: trafficTypeValidator.Object, eventsLog: eventLog.Object); trafficTypeValidator .Setup(mock => mock.IsValid(It.IsAny(), It.IsAny())) @@ -907,7 +906,7 @@ public void GetTreatmentWithConfig_WhenNameDoesntExist_DontLogImpression() { // Arrange. var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); var splitName = "not_exist"; client.BlockUntilReady(1000); @@ -926,7 +925,7 @@ public void GetTreatmentWithConfig_WhenNameDoesntExist_DontLogImpression() public void GetTreatmentWithConfig_WithoutBlockUntiltReady_ReturnsOff() { // Arrange. - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); // Act. var result = client.GetTreatmentWithConfig("key", "anding"); @@ -944,7 +943,7 @@ public void GetTreatmentsWithConfig_WhenNameDoesntExist_DontLogImpression() { // Arrange. var impressionsLogMock = new Mock(); - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator, impressionsLog: impressionsLogMock.Object); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); var splitNames = new List { "not_exist", "not_exist_1" }; client.BlockUntilReady(1000); @@ -967,7 +966,7 @@ public void GetTreatmentsWithConfig_WhenNameDoesntExist_DontLogImpression() public void GetTreatmentsWithConfig_WithoutBlockUntiltReady_ReturnsEmptyList() { // Arrange. - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); // Act. var result = client.GetTreatmentsWithConfig("anding", new List()); @@ -981,7 +980,7 @@ public void GetTreatmentsWithConfig_WithoutBlockUntiltReady_ReturnsEmptyList() public void GetTreatmentsWithConfig_WithoutBlockUntiltReady_ReturnsTreatments() { // Arrange. - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); // Act. var result = client.GetTreatmentsWithConfig("key", new List { "anding", "whitelisting_elements" }); @@ -1001,7 +1000,7 @@ public void GetTreatmentsWithConfig_WithoutBlockUntiltReady_ReturnsTreatments() public void GetTreatmentsWithConfig_WhenClientIsReadyAndFeaturesIsEmpty_ReturnsEmptyList() { // Arrange. - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); client.BlockUntilReady(100); // Act. @@ -1018,7 +1017,7 @@ public void GetTreatmentsWithConfig_WhenClientIsReadyAndFeaturesIsEmpty_ReturnsE public void Split_Manager_WhenNameDoesntExist_ReturnsNull() { // Arrange. - var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", _fallbackTreatmentCalculator); + var client = new JSONFileClient($"{rootFilePath}splits_staging_3.json", "", new ConfigurationOptions()); var manager = client.GetSplitManager(); var splitName = "not_exist"; diff --git a/tests/Splitio-tests/Integration Tests/RedisClientTests.cs b/tests/Splitio-tests/Integration Tests/RedisClientTests.cs index 84ebcbd79..5b7ea4c79 100644 --- a/tests/Splitio-tests/Integration Tests/RedisClientTests.cs +++ b/tests/Splitio-tests/Integration Tests/RedisClientTests.cs @@ -4,6 +4,7 @@ using Splitio.Redis.Services.Client.Classes; using Splitio.Redis.Services.Domain; using Splitio.Services.Client.Classes; +using Splitio.Services.Common; using Splitio.Services.Impressions.Classes; using Splitio.Tests.Common.Resources; using Splitio_Tests.Resources; @@ -25,6 +26,7 @@ public class RedisClientTests private ConfigurationOptions config; private RedisAdapterForTests _redisAdapter; private FallbackTreatmentCalculator _fallbackTreatmentCalculator; + private EventsManager _eventsManager; [TestInitialize] public void Initialization() @@ -38,6 +40,7 @@ public void Initialization() UserPrefix = _prefix }; _fallbackTreatmentCalculator = new FallbackTreatmentCalculator(new FallbackTreatmentsConfiguration()); + _eventsManager = new EventsManager(new EventsManagerConfig(), new EventDelivery()); config = new ConfigurationOptions { CacheAdapterConfig = cacheAdapterConfig, @@ -64,7 +67,7 @@ public void Initialization() public void GetTreatment_WhenFeatureExists_ReturnsOn() { //Arrange - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); client.BlockUntilReady(5000); @@ -75,7 +78,7 @@ public void GetTreatment_WhenFeatureExists_ReturnsOn() Assert.IsNotNull(result); Assert.AreEqual("on", result); - var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY, _fallbackTreatmentCalculator); + var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY); client2.BlockUntilReady(5000); @@ -91,7 +94,7 @@ public void GetTreatment_WhenFeatureExists_ReturnsOn() public void GetTreatment_WhenFeatureExists_ReturnsOff() { //Arrange - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); client.BlockUntilReady(5000); @@ -102,7 +105,7 @@ public void GetTreatment_WhenFeatureExists_ReturnsOff() Assert.IsNotNull(result); Assert.AreEqual("off", result); - var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY, _fallbackTreatmentCalculator); + var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY); client2.BlockUntilReady(5000); result = client2.GetTreatment("test", "always_off", null); @@ -116,7 +119,7 @@ public void GetTreatment_WhenFeatureExists_ReturnsOff() public void GetTreatment_WhenFeatureDoenstExist_ReturnsControl() { //Arrange - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); client.BlockUntilReady(5000); //Act @@ -126,7 +129,7 @@ public void GetTreatment_WhenFeatureDoenstExist_ReturnsControl() Assert.IsNotNull(result); Assert.AreEqual("control", result); - var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY, _fallbackTreatmentCalculator); + var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY); client2.BlockUntilReady(5000); result = client2.GetTreatment("test", "always_control", null); @@ -145,7 +148,7 @@ public void GetTreatments_WhenFeaturesExists_ReturnsOnOff() var features = new List { alwaysOn, alwaysOff }; - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); client.BlockUntilReady(5000); @@ -159,7 +162,7 @@ public void GetTreatments_WhenFeaturesExists_ReturnsOnOff() var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), - API_KEY, _fallbackTreatmentCalculator); + API_KEY); client2.BlockUntilReady(5000); result = client2.GetTreatments("test", features, null); @@ -180,7 +183,7 @@ public void GetTreatments_WhenOneFeatureDoenstExist_ReturnsOnOffControl() var features = new List { alwaysOn, alwaysOff, alwaysControl }; - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); client.BlockUntilReady(5000); @@ -193,8 +196,7 @@ public void GetTreatments_WhenOneFeatureDoenstExist_ReturnsOnOffControl() Assert.AreEqual("on", result[alwaysOn]); Assert.AreEqual("control", result[alwaysControl]); - var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), - API_KEY, _fallbackTreatmentCalculator); + var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY); client2.BlockUntilReady(5000); result = client2.GetTreatments("test", features, null); @@ -210,7 +212,7 @@ public void GetTreatments_WhenOneFeatureDoenstExist_ReturnsOnOffControl() public void GetTreatmentsWithConfig_WhenClientIsNotReady_ReturnsControl() { // Arrange. - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); // Act. var result = client.GetTreatmentsWithConfig("key", new List()); @@ -222,8 +224,7 @@ public void GetTreatmentsWithConfig_WhenClientIsNotReady_ReturnsControl() Assert.IsNull(res.Value.Config); } - var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), -API_KEY, _fallbackTreatmentCalculator); + var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY); client2.BlockUntilReady(5000); result = client2.GetTreatmentsWithConfig("key", new List()); @@ -240,7 +241,7 @@ public void GetTreatmentsWithConfig_WhenClientIsNotReady_ReturnsControl() public void GetTreatmentWithConfig_WhenClientIsNotReady_ReturnsControl() { // Arrange. - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); // Act. var result = client.GetTreatmentWithConfig("key", string.Empty); @@ -249,8 +250,7 @@ public void GetTreatmentWithConfig_WhenClientIsNotReady_ReturnsControl() Assert.AreEqual("control", result.Treatment); Assert.IsNull(result.Config); - var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), -API_KEY, _fallbackTreatmentCalculator); + var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY); client2.BlockUntilReady(5000); result = client2.GetTreatmentWithConfig("key", string.Empty); @@ -264,7 +264,7 @@ public void GetTreatmentWithConfig_WhenClientIsNotReady_ReturnsControl() public void GetTreatment_WhenClientIsNotReady_ReturnsControl() { // Arrange. - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); // Act. var result = client.GetTreatment("key", string.Empty); @@ -272,8 +272,7 @@ public void GetTreatment_WhenClientIsNotReady_ReturnsControl() // Assert. Assert.AreEqual("control", result); - var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), -API_KEY, _fallbackTreatmentCalculator); + var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY); client2.BlockUntilReady(5000); result = client2.GetTreatment("key", string.Empty); @@ -287,7 +286,7 @@ public void GetTreatments_WhenClientIsNotReady_ReturnsControl() { // Arrange. config.CacheAdapterConfig.Host = "fake-host"; - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); // Act. var result = client.GetTreatments("key", new List()); @@ -298,7 +297,7 @@ public void GetTreatments_WhenClientIsNotReady_ReturnsControl() Assert.AreEqual("control", res.Value); } - var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY, _fallbackTreatmentCalculator); + var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY); client2.BlockUntilReady(5000); result = client2.GetTreatments("key", new List()); @@ -314,7 +313,7 @@ public void GetTreatments_WhenClientIsNotReady_ReturnsControl() public void Track_WhenClientIsNotReady_ReturnsTrue() { // Arrange. - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); // Act. var result = client.Track("key", "traffic_type", "event_type"); @@ -322,8 +321,7 @@ public void Track_WhenClientIsNotReady_ReturnsTrue() // Assert. Assert.IsTrue(result); - var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), -API_KEY, _fallbackTreatmentCalculator); + var client2 = new RedisClient(GetRedisClusterConfigurationOptions(), API_KEY); client2.BlockUntilReady(5000); // Act. @@ -340,9 +338,9 @@ public void FallbackTreatments_WhenFeatureDoesNotExist() var features = new List { alwaysOn, "feature", alwaysControl }; FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global", "\"prop\":\"global\""), new Dictionary() { { "feature", new FallbackTreatment("off-local", "\"prop\":\"local\"") } }); - FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculator(fallbackTreatmentsConfiguration); + config.FallbackTreatments = fallbackTreatmentsConfiguration; - var client = new RedisClient(config, API_KEY, fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); client.BlockUntilReady(5000); @@ -377,10 +375,10 @@ public void FallbackTreatments_WhenClientNotReady() var features = new List { "feature" }; FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global", "\"prop\":\"global\""), new Dictionary() { { "feature", new FallbackTreatment("off-local", "\"prop\":\"local\"") } }); - FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculator(fallbackTreatmentsConfiguration); config.CacheAdapterConfig.Host = "fake-host"; - var client = new RedisClient(config, API_KEY, fallbackTreatmentCalculator); + config.FallbackTreatments = fallbackTreatmentsConfiguration; + var client = new RedisClient(config, API_KEY); //Act var result = client.GetTreatmentsWithConfig("test", features, null); @@ -400,9 +398,9 @@ public void FallbackTreatments_WhenExceptionOccurrs() var features = new List { "always_off" }; FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global", "\"prop\":\"global\""), new Dictionary() { { "feature", new FallbackTreatment("off-local", "\"prop\":\"local\"") } }); - FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculator(fallbackTreatmentsConfiguration); + config.FallbackTreatments = fallbackTreatmentsConfiguration; - var client = new RedisClient(config, API_KEY, fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); client.BlockUntilReady(5000); @@ -431,7 +429,7 @@ public void FallbackTreatments_WhenExceptionOccurrs() public void Destroy() { //Arrange - var client = new RedisClient(config, API_KEY, _fallbackTreatmentCalculator); + var client = new RedisClient(config, API_KEY); client.BlockUntilReady(5000); //Act diff --git a/tests/Splitio-tests/Integration Tests/TargetingRulesFetcherTests.cs b/tests/Splitio-tests/Integration Tests/TargetingRulesFetcherTests.cs index c949489c5..7d4e8f532 100644 --- a/tests/Splitio-tests/Integration Tests/TargetingRulesFetcherTests.cs +++ b/tests/Splitio-tests/Integration Tests/TargetingRulesFetcherTests.cs @@ -46,7 +46,7 @@ public async Task ExecuteGetSuccessfulWithResultsFromJSONFile() var splitChangeFetcher = new JSONFileSplitChangeFetcher($"{rootFilePath}splits_staging.json"); var flagSetsFilter = new FlagSetsFilter(new HashSet()); var splitCache = new InMemorySplitCache(new ConcurrentDictionary(), flagSetsFilter, eventsManager); - var gates = new InMemoryReadinessGatesCache(); + var gates = new InMemoryReadinessGatesCache(eventsManager); var taskManager = new TasksManager(gates); var task = taskManager.NewPeriodicTask(Splitio.Enums.Task.FeatureFlagsFetcher, 250); var featureFlagSyncService = new FeatureFlagUpdater(splitParser, splitCache, flagSetsFilter, rbsCache); @@ -92,7 +92,7 @@ public async Task ExecuteGetSuccessfulWithResultsFromJSONFileIncludingTrafficAll var splitChangeFetcher = new JSONFileSplitChangeFetcher($"{rootFilePath}splits_staging_4.json"); var flagSetsFilter = new FlagSetsFilter(new HashSet()); var splitCache = new InMemorySplitCache(new ConcurrentDictionary(), flagSetsFilter, eventsManager); - var gates = new InMemoryReadinessGatesCache(); + var gates = new InMemoryReadinessGatesCache(eventsManager); var taskManager = new TasksManager(gates); var task = taskManager.NewPeriodicTask(Splitio.Enums.Task.FeatureFlagsFetcher, 250); var featureFlagSyncService = new FeatureFlagUpdater(splitParser, splitCache, flagSetsFilter, rbsCache); @@ -141,8 +141,8 @@ public async Task ExecuteGetWithoutResults() var apiSplitChangeFetcher = new ApiSplitChangeFetcher(sdkApiClient); var sdkSegmentApiClient = new SegmentSdkApiClient(httpClient, telemetryStorage, baseUrl); var apiSegmentChangeFetcher = new ApiSegmentChangeFetcher(sdkSegmentApiClient); - var gates = new InMemoryReadinessGatesCache(); var eventsManager = new EventsManager(new EventsManagerConfig(), new EventDelivery()); + var gates = new InMemoryReadinessGatesCache(eventsManager); var segmentCache = new InMemorySegmentCache(new ConcurrentDictionary(), eventsManager); var segmentsQueue = new SplitQueue(); var taskManager = new TasksManager(gates); diff --git a/tests/Splitio-tests/Unit Tests/Client/LocalhostClientForTesting.cs b/tests/Splitio-tests/Unit Tests/Client/LocalhostClientForTesting.cs index 09c6b39ea..49a16aeea 100644 --- a/tests/Splitio-tests/Unit Tests/Client/LocalhostClientForTesting.cs +++ b/tests/Splitio-tests/Unit Tests/Client/LocalhostClientForTesting.cs @@ -1,11 +1,10 @@ using Splitio.Services.Client.Classes; -using Splitio.Services.Impressions.Classes; namespace Splitio_Tests.Unit_Tests.Client { public class LocalhostClientForTesting : LocalhostClient { - public LocalhostClientForTesting(string filePath, FallbackTreatmentCalculator fallbackTreatmentCalculator) : base(new ConfigurationOptions { LocalhostFilePath = filePath }, fallbackTreatmentCalculator) + public LocalhostClientForTesting(string filePath) : base(new ConfigurationOptions { LocalhostFilePath = filePath }) { } } } diff --git a/tests/Splitio-tests/Unit Tests/Client/LocalhostClientUnitTests.cs b/tests/Splitio-tests/Unit Tests/Client/LocalhostClientUnitTests.cs index aaf4a6a2c..4609cd16e 100644 --- a/tests/Splitio-tests/Unit Tests/Client/LocalhostClientUnitTests.cs +++ b/tests/Splitio-tests/Unit Tests/Client/LocalhostClientUnitTests.cs @@ -1,6 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Splitio.Domain; using Splitio.Services.Client.Classes; +using Splitio.Services.Common; using Splitio.Services.Impressions.Classes; using Splitio.Services.Shared.Classes; @@ -8,15 +9,18 @@ namespace Splitio_Tests.Unit_Tests.Client { [TestClass] public class LocalhostClientUnitTests - { + { private readonly string rootFilePath; private readonly FallbackTreatmentCalculator _fallbackTreatmentCalculator; + private readonly EventsManager _eventsManager; + private bool SdkReady = false; public LocalhostClientUnitTests() { // This line is to clean the warnings. rootFilePath = string.Empty; _fallbackTreatmentCalculator = new FallbackTreatmentCalculator(new FallbackTreatmentsConfiguration()); + _eventsManager = new EventsManager(new EventsManagerConfig(), new EventDelivery()); #if NET_LATEST rootFilePath = @"Resources\"; @@ -28,7 +32,7 @@ public LocalhostClientUnitTests() public void GetTreatmentShouldReturnControlIfSplitNotFound() { //Arrange - var splitClient = new LocalhostClient(new ConfigurationOptions { LocalhostFilePath = $"{rootFilePath}test.splits" }, _fallbackTreatmentCalculator); + var splitClient = new LocalhostClient(new ConfigurationOptions { LocalhostFilePath = $"{rootFilePath}test.splits" }); //Act var result = splitClient.GetTreatment("test", "test"); @@ -41,7 +45,7 @@ public void GetTreatmentShouldReturnControlIfSplitNotFound() [DeploymentItem(@"Resources\test.splits")] public void GetTreatmentShouldRunAsSingleKeyUsingNullBucketingKey() { - var splitClient = new LocalhostClient(new ConfigurationOptions { LocalhostFilePath = $"{rootFilePath}test.splits" }, _fallbackTreatmentCalculator); + var splitClient = new LocalhostClient(new ConfigurationOptions { LocalhostFilePath = $"{rootFilePath}test.splits" }); splitClient.BlockUntilReady(1000); //Act @@ -57,9 +61,9 @@ public void GetTreatmentShouldRunAsSingleKeyUsingNullBucketingKey() public void TrackShouldNotStoreEvents() { //Arrange - var splitClient = new LocalhostClientForTesting($"{rootFilePath}test.splits", _fallbackTreatmentCalculator); + var splitClient = new LocalhostClientForTesting($"{rootFilePath}test.splits"); splitClient.BlockUntilReady(1000); - + //Act var result = splitClient.Track("test", "test", "test"); @@ -73,7 +77,7 @@ public void Destroy() { //Arrange var _factoryInstantiationsService = FactoryInstantiationsService.Instance(); - var splitClient = new LocalhostClientForTesting($"{rootFilePath}test.splits", _fallbackTreatmentCalculator); + var splitClient = new LocalhostClientForTesting($"{rootFilePath}test.splits"); //Act splitClient.BlockUntilReady(10000); diff --git a/tests/Splitio-tests/Unit Tests/Client/SdkReadinessGatesUnitTests.cs b/tests/Splitio-tests/Unit Tests/Client/SdkReadinessGatesUnitTests.cs index bf1e7b1ba..d17713b5c 100644 --- a/tests/Splitio-tests/Unit Tests/Client/SdkReadinessGatesUnitTests.cs +++ b/tests/Splitio-tests/Unit Tests/Client/SdkReadinessGatesUnitTests.cs @@ -1,16 +1,23 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Splitio.Domain; using Splitio.Services.Client.Classes; +using Splitio.Services.Common; +using System; namespace Splitio_Tests.Unit_Tests.Client { [TestClass] public class InMemoryReadinessGatesCacheUnitTests { + private bool SdkReadyFlag = false; + private EventMetadata eMetadata = null; + public event EventHandler SdkReady; + [TestMethod] public void IsSDKReadyShouldReturnFalseIfSplitsAreNotReady() { //Arrange - var gates = new InMemoryReadinessGatesCache(); + var gates = new InMemoryReadinessGatesCache(new EventsManager(new EventsManagerConfig(), new EventDelivery())); //Act var result = gates.IsReady(); @@ -18,5 +25,33 @@ public void IsSDKReadyShouldReturnFalseIfSplitsAreNotReady() //Assert Assert.IsFalse(result); } + + [TestMethod] + public void TestFireReadyEvent() + { + //Arrange + EventsManager eventsManager = new EventsManager(new EventsManagerConfig(), new EventDelivery()); + var gates = new InMemoryReadinessGatesCache(eventsManager); + SdkReady += sdkReady_callback; + eventsManager.Register(SdkEvent.SdkReady, TriggerSdkReady); + + //Act + gates.SetReady(); + + // Assert. + Assert.IsTrue(SdkReadyFlag); + Assert.AreEqual(0, eMetadata.GetData().Count); + } + + private void sdkReady_callback(object sender, EventMetadata metadata) + { + SdkReadyFlag = true; + eMetadata = metadata; + } + + private void TriggerSdkReady(EventMetadata metaData) + { + SdkReady?.Invoke(this, metaData); + } } } diff --git a/tests/Splitio-tests/Unit Tests/Client/SplitClientAsyncTests.cs b/tests/Splitio-tests/Unit Tests/Client/SplitClientAsyncTests.cs index ebacf3498..9b377b342 100644 --- a/tests/Splitio-tests/Unit Tests/Client/SplitClientAsyncTests.cs +++ b/tests/Splitio-tests/Unit Tests/Client/SplitClientAsyncTests.cs @@ -2,6 +2,7 @@ using Moq; using Splitio.Domain; using Splitio.Services.Cache.Interfaces; +using Splitio.Services.Client.Classes; using Splitio.Services.Client.Interfaces; using Splitio.Services.Common; using Splitio.Services.EngineEvaluator; @@ -40,7 +41,7 @@ public SplitClientAsyncTests() _syncManager = new Mock(); _telemetryEvaluationProducer = new Mock(); - _splitClient = new SplitClientForTesting(_splitCache.Object, _eventsLog.Object, _impressionsLog.Object, _blockUntilReadyService.Object, _evaluator.Object, _impressionsManager.Object, _syncManager.Object, new FallbackTreatmentCalculator(new FallbackTreatmentsConfiguration()), _telemetryEvaluationProducer.Object); + _splitClient = new SplitClientForTesting(_splitCache.Object, _eventsLog.Object, _impressionsLog.Object, _blockUntilReadyService.Object, _evaluator.Object, _impressionsManager.Object, _syncManager.Object, _telemetryEvaluationProducer.Object, new ConfigurationOptions()); } #region GetTreatmentAsync @@ -1005,7 +1006,8 @@ public async Task FallbackTreatments_WhenFlagDoesNotExist() FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global", "\"prop\":\"global\""), new Dictionary() { { splitName, new FallbackTreatment("off-local", "\"prop\":\"local\"") } }); FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculator(fallbackTreatmentsConfiguration); IEvaluator _evaluator = new Splitio.Services.Evaluator.Evaluator(_splitCache.Object, _splitter.Object, _telemetryEvaluationProducer.Object, fallbackTreatmentCalculator); - _splitClient = new SplitClientForTesting(_splitCache.Object, _eventsLog.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluator, _impressionsManager.Object, _syncManager.Object, fallbackTreatmentCalculator, _telemetryEvaluationProducer.Object); + _splitClient = new SplitClientForTesting(_splitCache.Object, _eventsLog.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluator, _impressionsManager.Object, _syncManager.Object, _telemetryEvaluationProducer.Object, + new ConfigurationOptions { FallbackTreatments = fallbackTreatmentsConfiguration }); _splitClient.BlockUntilReady(1000); string treatment = await _splitClient.GetTreatmentAsync("key", splitName); @@ -1077,13 +1079,13 @@ public async Task FallbackTreatments_WhenExceptionInClient() .Returns(true); FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global", "\"prop\":\"global\""), new Dictionary() { { splitName, new FallbackTreatment("off-local", "\"prop\":\"local\"") } }); - FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculator(fallbackTreatmentsConfiguration); _evaluator = new Mock(); _evaluator .Setup(mock => mock.EvaluateFeaturesAsync(It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny>(), true)) .Throws(); - _splitClient = new SplitClientForTesting(_splitCache.Object, _eventsLog.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluator.Object, _impressionsManager.Object, _syncManager.Object, fallbackTreatmentCalculator, _telemetryEvaluationProducer.Object); + _splitClient = new SplitClientForTesting(_splitCache.Object, _eventsLog.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluator.Object, _impressionsManager.Object, _syncManager.Object, _telemetryEvaluationProducer.Object, + new ConfigurationOptions { FallbackTreatments = fallbackTreatmentsConfiguration }); _splitClient.BlockUntilReady(1000); string treatment = await _splitClient.GetTreatmentAsync("key", splitName); diff --git a/tests/Splitio-tests/Unit Tests/Client/SplitClientForTesting.cs b/tests/Splitio-tests/Unit Tests/Client/SplitClientForTesting.cs index 80587cd2c..2cc6936ba 100644 --- a/tests/Splitio-tests/Unit Tests/Client/SplitClientForTesting.cs +++ b/tests/Splitio-tests/Unit Tests/Client/SplitClientForTesting.cs @@ -3,7 +3,6 @@ using Splitio.Services.Common; using Splitio.Services.Evaluator; using Splitio.Services.Events.Interfaces; -using Splitio.Services.Impressions.Classes; using Splitio.Services.Impressions.Interfaces; using Splitio.Services.InputValidation.Classes; using Splitio.Services.Shared.Interfaces; @@ -20,9 +19,9 @@ public SplitClientForTesting(IFeatureFlagCacheConsumer featureFlagCacheConsumer, IEvaluator evaluator, IImpressionsManager impressionsManager, ISyncManager syncManager, - FallbackTreatmentCalculator fallbackTreatmentCalculator, - ITelemetryEvaluationProducer telemetryEvaluationProducer) - : base("SplitClientForTesting", fallbackTreatmentCalculator) + ITelemetryEvaluationProducer telemetryEvaluationProducer, + ConfigurationOptions config) + : base("SplitClientForTesting", config) { _eventsLog = eventsLog; _impressionsLog = impressionsLog; diff --git a/tests/Splitio-tests/Unit Tests/Client/SplitClientUnitTests.cs b/tests/Splitio-tests/Unit Tests/Client/SplitClientUnitTests.cs index 842b81a7d..1b9c56360 100644 --- a/tests/Splitio-tests/Unit Tests/Client/SplitClientUnitTests.cs +++ b/tests/Splitio-tests/Unit Tests/Client/SplitClientUnitTests.cs @@ -45,7 +45,7 @@ public void TestInitialize() _syncManager = new Mock(); _telemetryEvaluationProducer = new Mock(); - _splitClientForTesting = new SplitClientForTesting(_splitCacheMock.Object, _eventsLogMock.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluatorMock.Object, _impressionsManager.Object, _syncManager.Object, new FallbackTreatmentCalculator(new FallbackTreatmentsConfiguration()), _telemetryEvaluationProducer.Object); + _splitClientForTesting = new SplitClientForTesting(_splitCacheMock.Object, _eventsLogMock.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluatorMock.Object, _impressionsManager.Object, _syncManager.Object, _telemetryEvaluationProducer.Object, new ConfigurationOptions()); _splitClientForTesting.BlockUntilReady(1000); } @@ -779,7 +779,8 @@ public void FallbackTreatments_WhenFlagDoesNotExist() FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global", "\"prop\":\"global\""), new Dictionary() { { splitName, new FallbackTreatment("off-local", "\"prop\":\"local\"") } }); FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculator(fallbackTreatmentsConfiguration); IEvaluator _evaluator = new Splitio.Services.Evaluator.Evaluator(_splitCache.Object, _splitter.Object, _telemetryEvaluationProducer.Object, fallbackTreatmentCalculator); - _splitClientForTesting = new SplitClientForTesting(_splitCache.Object, _eventsLogMock.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluator, _impressionsManager.Object, _syncManager.Object, fallbackTreatmentCalculator, _telemetryEvaluationProducer.Object); + _splitClientForTesting = new SplitClientForTesting(_splitCache.Object, _eventsLogMock.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluator, _impressionsManager.Object, _syncManager.Object, _telemetryEvaluationProducer.Object, + new ConfigurationOptions { FallbackTreatments = fallbackTreatmentsConfiguration }); _splitClientForTesting.BlockUntilReady(1000); string treatment = _splitClientForTesting.GetTreatment("key", splitName); @@ -851,13 +852,13 @@ public void FallbackTreatments_WhenExceptionInClient() .Returns(true); FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global", "\"prop\":\"global\""), new Dictionary() { { splitName, new FallbackTreatment("off-local", "\"prop\":\"local\"") } }); - FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculator(fallbackTreatmentsConfiguration); _evaluatorMock = new Mock(); _evaluatorMock .Setup(mock => mock.EvaluateFeatures(It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny>(), true)) .Throws(); - _splitClientForTesting = new SplitClientForTesting(_splitCache.Object, _eventsLogMock.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluatorMock.Object, _impressionsManager.Object, _syncManager.Object, fallbackTreatmentCalculator, _telemetryEvaluationProducer.Object); + _splitClientForTesting = new SplitClientForTesting(_splitCache.Object, _eventsLogMock.Object, new Mock().Object, _blockUntilReadyService.Object, _evaluatorMock.Object, _impressionsManager.Object, _syncManager.Object, _telemetryEvaluationProducer.Object, + new ConfigurationOptions { FallbackTreatments = fallbackTreatmentsConfiguration }); _splitClientForTesting.BlockUntilReady(1000); string treatment = _splitClientForTesting.GetTreatment("key", splitName); diff --git a/tests/Splitio-tests/Unit Tests/Impressions/ImpressionsCounterTests.cs b/tests/Splitio-tests/Unit Tests/Impressions/ImpressionsCounterTests.cs index 77ce67e35..46424da13 100644 --- a/tests/Splitio-tests/Unit Tests/Impressions/ImpressionsCounterTests.cs +++ b/tests/Splitio-tests/Unit Tests/Impressions/ImpressionsCounterTests.cs @@ -1,6 +1,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; +using Splitio.Domain; using Splitio.Services.Client.Classes; +using Splitio.Services.Common; using Splitio.Services.Impressions.Classes; using Splitio.Services.Impressions.Interfaces; using Splitio.Services.Tasks; @@ -27,7 +29,7 @@ public void Start_ShouldSendImpressionsCount() { // Arrange. var config = new ComponentConfig(5, 5); - var statusManager = new InMemoryReadinessGatesCache(); + var statusManager = new InMemoryReadinessGatesCache(new EventsManager(new EventsManagerConfig(), new EventDelivery())); var taskManager = new TasksManager(statusManager); var task = taskManager.NewPeriodicTask(Splitio.Enums.Task.ImpressionsCountSender, 1); var sendBulkDataTask = taskManager.NewOnTimeTask(Splitio.Enums.Task.ImpressionCounterSendBulkData); @@ -51,7 +53,7 @@ public void Start_ShouldNotSendImpressionsCount() { // Arrange. var config = new ComponentConfig(5, 5); - var statusManager = new InMemoryReadinessGatesCache(); + var statusManager = new InMemoryReadinessGatesCache(new Mock>().Object); var taskManager = new TasksManager(statusManager); var task = taskManager.NewPeriodicTask(Splitio.Enums.Task.ImpressionsCountSender, 1); var sendBulkDataTask = taskManager.NewOnTimeTask(Splitio.Enums.Task.ImpressionCounterSendBulkData); @@ -70,7 +72,7 @@ public async Task Stop_ShouldSendImpressionsCount() { // Arrange. var config = new ComponentConfig(5, 5); - var statusManager = new InMemoryReadinessGatesCache(); + var statusManager = new InMemoryReadinessGatesCache(new Mock>().Object); var taskManager = new TasksManager(statusManager); var task = taskManager.NewPeriodicTask(Splitio.Enums.Task.ImpressionsCountSender, 100); var sendBulkDataTask = taskManager.NewOnTimeTask(Splitio.Enums.Task.ImpressionCounterSendBulkData); @@ -94,7 +96,7 @@ public async Task Stop_ShouldSendImpressionsCount() public async Task ShouldSend2BulksOfImpressions() { var config = new ComponentConfig(6, 3); - var statusManager = new InMemoryReadinessGatesCache(); + var statusManager = new InMemoryReadinessGatesCache(new Mock>().Object); var taskManager = new TasksManager(statusManager); var task = taskManager.NewPeriodicTask(Splitio.Enums.Task.ImpressionsCountSender, 100); var sendBulkDataTask = taskManager.NewOnTimeTask(Splitio.Enums.Task.ImpressionCounterSendBulkData); diff --git a/tests/Splitio-tests/Unit Tests/SegmentFetcher/SelfRefreshingSegmentFetcherUnitTests.cs b/tests/Splitio-tests/Unit Tests/SegmentFetcher/SelfRefreshingSegmentFetcherUnitTests.cs index e16e8ca3d..2b8456988 100644 --- a/tests/Splitio-tests/Unit Tests/SegmentFetcher/SelfRefreshingSegmentFetcherUnitTests.cs +++ b/tests/Splitio-tests/Unit Tests/SegmentFetcher/SelfRefreshingSegmentFetcherUnitTests.cs @@ -8,7 +8,6 @@ using Splitio.Services.SegmentFetcher.Classes; using Splitio.Services.SegmentFetcher.Interfaces; using Splitio.Services.Shared.Classes; -using Splitio.Services.Shared.Interfaces; using Splitio.Services.SplitFetcher.Interfaces; using Splitio.Services.Tasks; using System.Collections.Concurrent; @@ -27,7 +26,7 @@ public class SelfRefreshingSegmentFetcherUnitTests public void InitializeSegmentNotExistent() { // Arrange - var gates = new InMemoryReadinessGatesCache(); + var gates = new InMemoryReadinessGatesCache(new EventsManager(new EventsManagerConfig(), new EventDelivery())); gates.SetReady(); var apiClient = new Mock(); var apiFetcher = new ApiSegmentChangeFetcher(apiClient.Object); diff --git a/tests/Splitio-tests/Unit Tests/Shared/SelfRefreshingBlockUntilReadyServiceTests.cs b/tests/Splitio-tests/Unit Tests/Shared/SelfRefreshingBlockUntilReadyServiceTests.cs new file mode 100644 index 000000000..6f63501d7 --- /dev/null +++ b/tests/Splitio-tests/Unit Tests/Shared/SelfRefreshingBlockUntilReadyServiceTests.cs @@ -0,0 +1,56 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using Splitio.Domain; +using Splitio.Services.Cache.Interfaces; +using Splitio.Services.Common; +using Splitio.Services.Shared.Classes; +using Splitio.Telemetry.Storages; +using System; + +namespace Splitio_Tests.Unit_Tests.Shared +{ + [TestClass] + public class SelfRefreshingBlockUntilReadyServiceTests + { + private bool SdkTimedOutFlag = false; + private EventMetadata eMetadata = null; + public event EventHandler SdkTimedOut; + + [TestMethod] + public void TestFireTimedOutEvent() + { + //Arrange + Mock statusManager = new Mock(); + Mock telemetryProducer = new Mock(); + EventsManager eventsManager = new EventsManager(new EventsManagerConfig(), new EventDelivery()); + var bur = new SelfRefreshingBlockUntilReadyService(statusManager.Object, telemetryProducer.Object, eventsManager); + SdkTimedOut += sdkTimeout_callback; + eventsManager.Register(SdkEvent.SdkReadyTimeout, TriggerSdkTimedOut); + statusManager + .Setup(mock => mock.WaitUntilReady(1)) + .Returns(false); + + //Act + try + { + bur.BlockUntilReady(1); + } + catch { } + + // Assert. + Assert.IsTrue(SdkTimedOutFlag); + Assert.AreEqual(0, eMetadata.GetData().Count); + } + + private void sdkTimeout_callback(object sender, EventMetadata metadata) + { + SdkTimedOutFlag = true; + eMetadata = metadata; + } + + private void TriggerSdkTimedOut(EventMetadata metaData) + { + SdkTimedOut?.Invoke(this, metaData); + } + } +} diff --git a/tests/Splitio.Integration-tests/EventSourceClientTests.cs b/tests/Splitio.Integration-tests/EventSourceClientTests.cs index ba7766438..b7850c78f 100644 --- a/tests/Splitio.Integration-tests/EventSourceClientTests.cs +++ b/tests/Splitio.Integration-tests/EventSourceClientTests.cs @@ -370,7 +370,7 @@ private static (IEventSourceClient, BlockingCollection, var sseHttpClient = new SplitioHttpClient("api-key", config, new Dictionary()); var telemetryRuntimeProducer = new InMemoryTelemetryStorage(); var notificationManagerKeeper = new NotificationManagerKeeper(telemetryRuntimeProducer, streamingStatusQueue); - var statusManager = new InMemoryReadinessGatesCache(); + var statusManager = new InMemoryReadinessGatesCache(new EventsManager(new EventsManagerConfig(), new EventDelivery())); var tasksManager = new TasksManager(statusManager); var task = tasksManager.NewOnTimeTask(Enums.Task.SSEConnect); diff --git a/tests/Splitio.Integration-tests/StreamingClientTests.cs b/tests/Splitio.Integration-tests/StreamingClientTests.cs index 7623dadd1..6dab8142b 100644 --- a/tests/Splitio.Integration-tests/StreamingClientTests.cs +++ b/tests/Splitio.Integration-tests/StreamingClientTests.cs @@ -22,6 +22,10 @@ namespace Splitio.Integration_tests public class StreamingClientTests { public static string EventSourcePath => "/eventsource"; + private bool SdkReadyFlag = false; + private bool SdkReadyFlag2 = false; + private bool SdkUpdateFlag = false; + private EventMetadata eMetadata; [TestMethod] public void GetTreatment_SplitUpdate_ShouldFetch() @@ -60,14 +64,17 @@ public void GetTreatment_SplitUpdate_ShouldFetch() }; var apikey = "apikey1"; - var splitFactory = new SplitFactory(apikey, config); - var client = splitFactory.Client(); + var client = (SplitClient)splitFactory.Client(); + client.SdkReady += sdkReady_callback; + client.SdkReady += sdkReady_callback2; client.BlockUntilReady(10000); var result = EvaluateWithDelay("admin", "push_test", "after_fetch", client); Assert.AreEqual("after_fetch", result); + Assert.IsTrue(SdkReadyFlag); + Assert.IsTrue(SdkReadyFlag2); client.Destroy(); } @@ -169,7 +176,6 @@ public void GetTreatment_SplitKill_ShouldFetch() Thread.Sleep(5000); var result = client.GetTreatment("admin", "push_test"); - Assert.AreEqual("after_fetch", result); client.Destroy(); @@ -765,5 +771,15 @@ public static string EvaluateWithDelay(string key, string splitName, string expe return result; } + + private void sdkReady_callback(object sender, EventMetadata metadata) + { + SdkReadyFlag = true; + } + + private void sdkReady_callback2(object sender, EventMetadata metadata) + { + SdkReadyFlag2 = true; + } } } diff --git a/tests/Splitio.TestSupport/Samples/SampleTest.cs b/tests/Splitio.TestSupport/Samples/SampleTest.cs index ecaee10a3..9633110a7 100644 --- a/tests/Splitio.TestSupport/Samples/SampleTest.cs +++ b/tests/Splitio.TestSupport/Samples/SampleTest.cs @@ -1,6 +1,4 @@ -using Moq; -using Splitio.Services.Client.Classes; -using Splitio.Services.Logger; +using Splitio.Services.Client.Classes; using Xunit; namespace Splitio.TestSupport.Samples @@ -11,7 +9,7 @@ public class SampleTest public SampleTest() { - splitClient = new SplitClientForTest(); + splitClient = new SplitClientForTest(new ConfigurationOptions()); } [Theory] diff --git a/tests/Splitio.TestSupport/SplitClientForTest.cs b/tests/Splitio.TestSupport/SplitClientForTest.cs index ddffe2eb0..101d52107 100644 --- a/tests/Splitio.TestSupport/SplitClientForTest.cs +++ b/tests/Splitio.TestSupport/SplitClientForTest.cs @@ -1,4 +1,5 @@ using Splitio.Domain; +using Splitio.Services.Common; using Splitio.Services.Impressions.Classes; using System.Collections.Generic; using System.Threading.Tasks; @@ -9,7 +10,7 @@ public class SplitClientForTest : SplitClient { private readonly Dictionary _tests; - public SplitClientForTest() : base("SplitClientForTest", new FallbackTreatmentCalculator(new FallbackTreatmentsConfiguration())) + public SplitClientForTest(ConfigurationOptions config) : base("SplitClientForTest", config) { _tests = new Dictionary(); }