Ejemplo n.º 1
0
        public void ExecuteGetSuccessfulWithResults()
        {
            //Arrange
            var baseUrl = "https://sdk-aws-staging.split.io/api/";
            var headers = new Dictionary <string, string>
            {
                { "SplitSDKMachineIP", "1.0.0.0" },
                { "SplitSDKMachineName", "localhost" },
                { "SplitSDKVersion", "1" }
            };

            var telemetryStorage        = new InMemoryTelemetryStorage();
            var sdkApiClient            = new SegmentSdkApiClient("///PUT API KEY HERE///", headers, baseUrl, 10000, 10000, telemetryStorage);
            var apiSegmentChangeFetcher = new ApiSegmentChangeFetcher(sdkApiClient);
            var gates                        = new InMemoryReadinessGatesCache();
            var segmentCache                 = new InMemorySegmentCache(new ConcurrentDictionary <string, Segment>());
            var segmentTaskQueue             = new SegmentTaskQueue();
            var wrapperAdapter               = new WrapperAdapter();
            var selfRefreshingSegmentFetcher = new SelfRefreshingSegmentFetcher(apiSegmentChangeFetcher, gates, 30, segmentCache, 4, segmentTaskQueue, new TasksManager(wrapperAdapter), wrapperAdapter);

            //Act
            var name = "payed";

            selfRefreshingSegmentFetcher.InitializeSegment(name);

            //Assert
            Assert.IsTrue(segmentCache.IsInSegment(name, "abcdz"));
        }
        public void InitializeSegmentNotExistent()
        {
            //Arrange
            var gates     = new InMemoryReadinessGatesCache();
            var apiClient = new Mock <ISegmentSdkApiClient>();

            apiClient
            .Setup(x => x.FetchSegmentChanges(It.IsAny <string>(), It.IsAny <long>()))
            .Returns(Task.FromResult(@"{
                          'name': 'payed',
                          'added': [
                            'abcdz',
                            'bcadz',
                            'xzydz'
                          ],
                          'removed': [],
                          'since': -1,
                          'till': 10001
                        }"));
            var apiFetcher       = new ApiSegmentChangeFetcher(apiClient.Object);
            var segments         = new ConcurrentDictionary <string, Segment>();
            var cache            = new InMemorySegmentCache(segments);
            var segmentTaskQueue = new SegmentTaskQueue();
            var segmentFetcher   = new SelfRefreshingSegmentFetcher(apiFetcher, gates, 10, cache, 1, segmentTaskQueue);

            //Act
            segmentFetcher.InitializeSegment("payed");

            //Assert
            Thread.Sleep(5000);
            Assert.IsTrue(gates.AreSegmentsReady(1));
            Assert.IsTrue(cache.IsInSegment("payed", "abcdz"));
        }
Ejemplo n.º 3
0
        public void RefreshSegmentShouldUpdateReadinessGatesWhenNoMoreChanges()
        {
            //Arrange
            var gates     = new InMemoryReadinessGatesCache();
            var apiClient = new Mock <ISegmentSdkApiClient>();

            apiClient
            .Setup(x => x.FetchSegmentChanges(It.IsAny <string>(), It.IsAny <long>(), It.IsAny <CancellationToken>()))
            .Returns(TaskEx.FromResult(@"{
                          'name': 'payed',
                          'added': [
                            'abcdz',
                            'bcadz',
                            'xzydz'
                          ],
                          'removed': [],
                          'since': -1,
                          'till': -1
                        }"));
            var apiFetcher     = new ApiSegmentChangeFetcher(apiClient.Object);
            var segments       = new ConcurrentDictionary <string, Segment>();
            var cache          = new InMemorySegmentCache(segments);
            var segmentFetcher = new SelfRefreshingSegment("payed", apiFetcher, gates, cache);

            //Act
            segmentFetcher.RefreshSegment();

            //Assert
            Assert.IsTrue(gates.AreSegmentsReady(1));
        }
Ejemplo n.º 4
0
        public void RefreshSegmentShouldUpdateSegmentsCache()
        {
            //Arrange
            var gates     = new InMemoryReadinessGatesCache();
            var apiClient = new Mock <ISegmentSdkApiClient>();

            apiClient
            .Setup(x => x.FetchSegmentChanges(It.IsAny <string>(), It.IsAny <long>()))
            .Returns(@"{
                          'name': 'payed',
                          'added': [
                            'abcdz',
                            'bcadz',
                            'xzydz'
                          ],
                          'removed': [],
                          'since': -1,
                          'till': 10001
                        }");
            var apiFetcher     = new ApiSegmentChangeFetcher(apiClient.Object);
            var segments       = new ConcurrentDictionary <string, Segment>();
            var cache          = new InMemorySegmentCache(segments);
            var segmentFetcher = new SelfRefreshingSegment("payed", apiFetcher, gates, cache);

            //Act
            segmentFetcher.RefreshSegment();

            //Assert
            Assert.AreEqual(1, segments.Count);
        }
Ejemplo n.º 5
0
        public void ExecuteGetSuccessfulWithResultsFromJSONFileIncludingTrafficAllocation()
        {
            //Arrange
            var segmentCache       = new InMemorySegmentCache(new ConcurrentDictionary <string, Segment>());
            var splitParser        = new InMemorySplitParser(new JSONFileSegmentFetcher("segment_payed.json", segmentCache), segmentCache);
            var splitChangeFetcher = new JSONFileSplitChangeFetcher("splits_staging_4.json");
            var splitChangesResult = splitChangeFetcher.Fetch(-1);
            var splitCache         = new InMemorySplitCache(new ConcurrentDictionary <string, ParsedSplit>());
            var gates = new InMemoryReadinessGatesCache();
            var selfRefreshingSplitFetcher = new SelfRefreshingSplitFetcher(splitChangeFetcher, splitParser, gates, 30, splitCache);

            selfRefreshingSplitFetcher.Start();
            gates.IsSDKReady(1000);

            //Act
            ParsedSplit result = (ParsedSplit)splitCache.GetSplit("Traffic_Allocation_UI");

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.name == "Traffic_Allocation_UI");
            Assert.IsTrue(result.trafficAllocation == 100);
            Assert.IsTrue(result.trafficAllocationSeed == 0);
            Assert.IsTrue(result.conditions.Count > 0);
            Assert.IsNotNull(result.conditions.Find(x => x.conditionType == ConditionType.ROLLOUT));
        }
Ejemplo n.º 6
0
        public void ExecuteGetWithoutResults()
        {
            //Arrange
            var baseUrl    = "https://sdk-aws-staging.split.io/api/";
            var httpHeader = new HTTPHeader()
            {
                authorizationApiKey = "0",
                splitSDKMachineIP   = "1.0.0.0",
                splitSDKMachineName = "localhost",
                splitSDKVersion     = "net-0.0.0",
                splitSDKSpecVersion = "1.2",
            };
            var sdkApiClient            = new SplitSdkApiClient(httpHeader, baseUrl, 10000, 10000);
            var apiSplitChangeFetcher   = new ApiSplitChangeFetcher(sdkApiClient);
            var sdkSegmentApiClient     = new SegmentSdkApiClient(httpHeader, baseUrl, 10000, 10000);
            var apiSegmentChangeFetcher = new ApiSegmentChangeFetcher(sdkSegmentApiClient);
            var gates = new InMemoryReadinessGatesCache();

            var segmentCache = new InMemorySegmentCache(new ConcurrentDictionary <string, Segment>());

            var selfRefreshingSegmentFetcher = new SelfRefreshingSegmentFetcher(apiSegmentChangeFetcher, gates, 30, segmentCache, 4);
            var splitParser = new InMemorySplitParser(selfRefreshingSegmentFetcher, segmentCache);
            var splitCache  = new InMemorySplitCache(new ConcurrentDictionary <string, ParsedSplit>());
            var selfRefreshingSplitFetcher = new SelfRefreshingSplitFetcher(apiSplitChangeFetcher, splitParser, gates, 30, splitCache);

            selfRefreshingSplitFetcher.Start();

            //Act
            gates.IsSDKReady(10);

            var result = splitCache.GetSplit("condition_and");

            //Assert
            Assert.IsNull(result);
        }
Ejemplo n.º 7
0
        public void StartSchedullerSuccessfully()
        {
            //Arrange
            var gates     = new InMemoryReadinessGatesCache();
            var apiClient = new Mock <ISegmentSdkApiClient>();

            apiClient
            .Setup(x => x.FetchSegmentChanges(It.IsAny <string>(), It.IsAny <long>()))
            .Returns(@"{
                          'name': 'payed',
                          'added': [
                            'abcdz',
                            'bcadz',
                            'xzydz'
                          ],
                          'removed': [],
                          'since': -1,
                          'till': 10001
                        }");
            var apiFetcher     = new ApiSegmentChangeFetcher(apiClient.Object);
            var segments       = new ConcurrentDictionary <string, Segment>();
            var cache          = new InMemorySegmentCache(segments);
            var segmentFetcher = new SelfRefreshingSegmentFetcher(apiFetcher, gates, 10, cache, 1);

            //Act
            segmentFetcher.InitializeSegment("payed");
            segmentFetcher.StartScheduler();

            //Assert
            SelfRefreshingSegment segment;

            Assert.IsTrue(SegmentTaskQueue.segmentsQueue.TryTake(out segment, -1));
        }
Ejemplo n.º 8
0
        public void ExecuteGetWithoutResults()
        {
            //Arrange
            var baseUrl = "https://sdk-aws-staging.split.io/api/";
            var headers = new Dictionary <string, string>
            {
                { "SplitSDKMachineIP", "1.0.0.0" },
                { "SplitSDKMachineName", "localhost" },
                { "SplitSDKVersion", "1" }
            };

            var telemetryStorage        = new InMemoryTelemetryStorage();
            var sdkApiClient            = new SplitSdkApiClient("0", headers, baseUrl, 10000, 10000, telemetryStorage);
            var apiSplitChangeFetcher   = new ApiSplitChangeFetcher(sdkApiClient);
            var sdkSegmentApiClient     = new SegmentSdkApiClient("0", headers, baseUrl, 10000, 10000, telemetryStorage);
            var apiSegmentChangeFetcher = new ApiSegmentChangeFetcher(sdkSegmentApiClient);
            var gates                        = new InMemoryReadinessGatesCache();
            var segmentCache                 = new InMemorySegmentCache(new ConcurrentDictionary <string, Segment>());
            var segmentTaskQueue             = new SegmentTaskQueue();
            var wrapperAdapter               = new WrapperAdapter();
            var selfRefreshingSegmentFetcher = new SelfRefreshingSegmentFetcher(apiSegmentChangeFetcher, gates, 30, segmentCache, 4, segmentTaskQueue, new TasksManager(wrapperAdapter), wrapperAdapter);
            var splitParser                  = new InMemorySplitParser(selfRefreshingSegmentFetcher, segmentCache);
            var splitCache                   = new InMemorySplitCache(new ConcurrentDictionary <string, ParsedSplit>());
            var selfRefreshingSplitFetcher   = new SelfRefreshingSplitFetcher(apiSplitChangeFetcher, splitParser, gates, 30, new TasksManager(wrapperAdapter), splitCache);

            selfRefreshingSplitFetcher.Start();

            //Act
            gates.WaitUntilReady(10);

            var result = splitCache.GetSplit("condition_and");

            //Assert
            Assert.IsNull(result);
        }
Ejemplo n.º 9
0
        public void ExecuteGetSuccessfulWithResults()
        {
            //Arrange
            var baseUrl    = "https://sdk-aws-staging.split.io/api/";
            var httpHeader = new HTTPHeader()
            {
                authorizationApiKey = "///PUT API KEY HERE///",
                splitSDKMachineIP   = "1.0.0.0",
                splitSDKMachineName = "localhost",
                splitSDKVersion     = "net-0.0.0",
                splitSDKSpecVersion = "1.2",
            };
            var sdkApiClient            = new SegmentSdkApiClient(httpHeader, baseUrl, 10000, 10000);
            var apiSegmentChangeFetcher = new ApiSegmentChangeFetcher(sdkApiClient);
            var gates        = new InMemoryReadinessGatesCache();
            var segmentCache = new InMemorySegmentCache(new ConcurrentDictionary <string, Segment>());
            var selfRefreshingSegmentFetcher = new SelfRefreshingSegmentFetcher(apiSegmentChangeFetcher, gates, 30, segmentCache, 4);

            //Act
            var name = "payed";

            selfRefreshingSegmentFetcher.InitializeSegment(name);

            while (!gates.AreSegmentsReady(1000))
            {
                Thread.Sleep(10);
            }

            //Assert
            Assert.IsTrue(segmentCache.IsInSegment(name, "abcdz"));
        }
Ejemplo n.º 10
0
        public void InitializeSegmentNotExistent()
        {
            // Arrange
            var gates = new InMemoryReadinessGatesCache();

            gates.SetReady();
            var apiClient        = new Mock <ISegmentSdkApiClient>();
            var apiFetcher       = new ApiSegmentChangeFetcher(apiClient.Object);
            var segments         = new ConcurrentDictionary <string, Segment>();
            var cache            = new InMemorySegmentCache(segments);
            var segmentTaskQueue = new SegmentTaskQueue();
            var segmentFetcher   = new SelfRefreshingSegmentFetcher(apiFetcher, gates, 1, cache, 1, segmentTaskQueue, new TasksManager(wrapperAdapter), wrapperAdapter);

            segmentFetcher.Start();

            apiClient
            .Setup(x => x.FetchSegmentChanges(It.IsAny <string>(), It.IsAny <long>(), It.IsAny <FetchOptions>()))
            .Returns(Task.FromResult(PayedSplitJson));

            // Act
            segmentFetcher.InitializeSegment("payed");

            // Assert
            Thread.Sleep(5000);
            Assert.IsTrue(cache.IsInSegment("payed", "abcdz"));
        }
Ejemplo n.º 11
0
        public void IsSDKReadyShouldReturnFalseIfSplitsAreNotReady()
        {
            //Arrange
            var gates = new InMemoryReadinessGatesCache();

            //Act
            var result = gates.IsReady();

            //Assert
            Assert.IsFalse(result);
        }
Ejemplo n.º 12
0
        public void RegisterSegmentShouldReturnFalseIfSegmentNameEmpty()
        {
            //Arrange
            var gates = new InMemoryReadinessGatesCache();

            //Act
            var result = gates.RegisterSegment("");

            //Assert
            Assert.IsFalse(result);
        }
Ejemplo n.º 13
0
        public void RegisterSegmentShouldReturnFalseIfSplitsAreReady()
        {
            //Arrange
            var gates = new InMemoryReadinessGatesCache();

            gates.SplitsAreReady();

            //Act
            var result = gates.RegisterSegment("any");

            //Assert
            Assert.IsFalse(result);
        }
Ejemplo n.º 14
0
        public void IsSDKReadyShouldReturnTrueIfSplitsAndSegmentsAreReady()
        {
            //Arrange
            var gates = new InMemoryReadinessGatesCache();

            gates.RegisterSegment("any");
            gates.RegisterSegment("other");
            gates.SplitsAreReady();
            gates.SegmentIsReady("other");
            gates.SegmentIsReady("any");

            //Act
            var result = gates.IsSDKReady(0);

            //Assert
            Assert.IsTrue(result);
        }
Ejemplo n.º 15
0
        public void RefreshSegmentNullChangesFetcherResponseShouldNotUpdateCache()
        {
            //Arrange
            var gates     = new InMemoryReadinessGatesCache();
            var apiClient = new Mock <ISegmentSdkApiClient>();

            apiClient
            .Setup(x => x.FetchSegmentChanges(It.IsAny <string>(), It.IsAny <long>()))
            .Throws(new Exception());
            var apiFetcher     = new ApiSegmentChangeFetcher(apiClient.Object);
            var segments       = new ConcurrentDictionary <string, Segment>();
            var cache          = new InMemorySegmentCache(segments);
            var segmentFetcher = new SelfRefreshingSegment("payed", apiFetcher, gates, cache);

            //Act
            segmentFetcher.RefreshSegment();

            //Assert
            Assert.AreEqual(0, segments.Count);
        }
Ejemplo n.º 16
0
        public void ExecuteGetSuccessfulWithResults()
        {
            //Arrange
            var baseUrl = "https://sdk-aws-staging.split.io/api/";
            //var baseUrl = "http://localhost:3000/api/";
            var headers = new Dictionary <string, string>
            {
                { "SplitSDKMachineIP", "1.0.0.0" },
                { "SplitSDKMachineName", "localhost" },
                { "SplitSDKVersion", "1" }
            };

            var telemetryStorage        = new InMemoryTelemetryStorage();
            var sdkApiClient            = new SplitSdkApiClient("///PUT API KEY HERE///", headers, baseUrl, 10000, 10000, telemetryStorage);
            var apiSplitChangeFetcher   = new ApiSplitChangeFetcher(sdkApiClient);
            var sdkSegmentApiClient     = new SegmentSdkApiClient("///PUT API KEY HERE///", headers, baseUrl, 10000, 10000, telemetryStorage);
            var apiSegmentChangeFetcher = new ApiSegmentChangeFetcher(sdkSegmentApiClient);
            var gates                        = new InMemoryReadinessGatesCache();
            var segmentCache                 = new InMemorySegmentCache(new ConcurrentDictionary <string, Segment>());
            var segmentTaskQueue             = new SegmentTaskQueue();
            var wrapperAdapter               = new WrapperAdapter();
            var selfRefreshingSegmentFetcher = new SelfRefreshingSegmentFetcher(apiSegmentChangeFetcher, gates, 30, segmentCache, 4, segmentTaskQueue, new TasksManager(wrapperAdapter), wrapperAdapter);

            var splitParser = new InMemorySplitParser(selfRefreshingSegmentFetcher, segmentCache);
            var splitCache  = new InMemorySplitCache(new ConcurrentDictionary <string, ParsedSplit>());
            var selfRefreshingSplitFetcher = new SelfRefreshingSplitFetcher(apiSplitChangeFetcher, splitParser, gates, 30, new TasksManager(wrapperAdapter), splitCache);

            selfRefreshingSplitFetcher.Start();

            //Act
            gates.WaitUntilReady(1000);
            selfRefreshingSplitFetcher.Stop();
            ParsedSplit result  = (ParsedSplit)splitCache.GetSplit("Pato_Test_1");
            ParsedSplit result2 = (ParsedSplit)splitCache.GetSplit("Manu_Test_1");

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.name == "Pato_Test_1");
            Assert.IsTrue(result.conditions.Count > 0);
        }
Ejemplo n.º 17
0
        public void ExecuteGetSuccessfulWithResultsFromJSONFile()
        {
            //Arrange
            var segmentCache       = new InMemorySegmentCache(new ConcurrentDictionary <string, Segment>());
            var splitParser        = new InMemorySplitParser(new JSONFileSegmentFetcher("segment_payed.json", segmentCache), segmentCache);
            var splitChangeFetcher = new JSONFileSplitChangeFetcher("splits_staging.json");
            var splitChangesResult = splitChangeFetcher.Fetch(-1);
            var splitCache         = new InMemorySplitCache(new ConcurrentDictionary <string, ParsedSplit>());
            var gates = new InMemoryReadinessGatesCache();
            var selfRefreshingSplitFetcher = new SelfRefreshingSplitFetcher(splitChangeFetcher, splitParser, gates, 30, splitCache);

            selfRefreshingSplitFetcher.Start();
            gates.IsSDKReady(1000);

            //Act
            ParsedSplit result = (ParsedSplit)splitCache.GetSplit("Pato_Test_1");

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.name == "Pato_Test_1");
            Assert.IsTrue(result.conditions.Count > 0);
        }
Ejemplo n.º 18
0
        public void ExecuteGetSuccessfulWithResults()
        {
            //Arrange
            var baseUrl = "https://sdk-aws-staging.split.io/api/";
            //var baseUrl = "http://localhost:3000/api/";
            var httpHeader = new HTTPHeader()
            {
                authorizationApiKey = "///PUT API KEY HERE///",
                splitSDKMachineIP   = "1.0.0.0",
                splitSDKMachineName = "localhost",
                splitSDKVersion     = "net-0.0.0",
                splitSDKSpecVersion = "1.2",
                encoding            = "gzip"
            };
            var sdkApiClient            = new SplitSdkApiClient(httpHeader, baseUrl, 10000, 10000);
            var apiSplitChangeFetcher   = new ApiSplitChangeFetcher(sdkApiClient);
            var sdkSegmentApiClient     = new SegmentSdkApiClient(httpHeader, baseUrl, 10000, 10000);
            var apiSegmentChangeFetcher = new ApiSegmentChangeFetcher(sdkSegmentApiClient);
            var gates        = new InMemoryReadinessGatesCache();
            var segmentCache = new InMemorySegmentCache(new ConcurrentDictionary <string, Segment>());
            var selfRefreshingSegmentFetcher = new SelfRefreshingSegmentFetcher(apiSegmentChangeFetcher, gates, 30, segmentCache, 4);

            var splitParser = new InMemorySplitParser(selfRefreshingSegmentFetcher, segmentCache);
            var splitCache  = new InMemorySplitCache(new ConcurrentDictionary <string, ParsedSplit>());
            var selfRefreshingSplitFetcher = new SelfRefreshingSplitFetcher(apiSplitChangeFetcher, splitParser, gates, 30, splitCache);

            selfRefreshingSplitFetcher.Start();

            //Act
            gates.IsSDKReady(1000);
            selfRefreshingSplitFetcher.Stop();
            ParsedSplit result  = (ParsedSplit)splitCache.GetSplit("Pato_Test_1");
            ParsedSplit result2 = (ParsedSplit)splitCache.GetSplit("Manu_Test_1");

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.name == "Pato_Test_1");
            Assert.IsTrue(result.conditions.Count > 0);
        }