From 16cd37333193b6e3c89fa48f09ea4fb0b5999b51 Mon Sep 17 00:00:00 2001 From: muhammadnoman Date: Thu, 6 May 2021 22:13:07 +0500 Subject: [PATCH 1/4] Added set polling interval method and set blocking timeout period in optimizelyFactory --- OptimizelySDK.Tests/OptimizelyFactoryTest.cs | 47 ++++++++++++++++++++ OptimizelySDK/OptimizelyFactory.cs | 23 ++++++++-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/OptimizelySDK.Tests/OptimizelyFactoryTest.cs b/OptimizelySDK.Tests/OptimizelyFactoryTest.cs index 1ae01b44..5bd94d97 100644 --- a/OptimizelySDK.Tests/OptimizelyFactoryTest.cs +++ b/OptimizelySDK.Tests/OptimizelyFactoryTest.cs @@ -62,6 +62,53 @@ public void TestOptimizelyInstanceUsingConfigFile() optimizely.Dispose(); } + [Test] + public void TestOptimizelyInstanceUsingConfigFileWithPollingInterval() + { + OptimizelyFactory.SetPollingInterval(TimeSpan.FromMilliseconds(2023)); + var optimizely = OptimizelyFactory.NewDefaultInstance(); + // Check values are loaded from app.config or not. + var projectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager; + Assert.NotNull(projectConfigManager); + + var actualConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager); + var expectedConfigManagerProps = new ProjectConfigManagerProps + { + Url = "www.testurl.com", + LastModified = "", + AutoUpdate = true, + DatafileAccessToken = "testingtoken123", + BlockingTimeout = TimeSpan.FromSeconds(10), + PollingInterval = TimeSpan.FromMilliseconds(2023) + }; + + Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps); + optimizely.Dispose(); + } + + [Test] + public void TestOptimizelyInstanceUsingConfigFileWithBlockingTimeOutInterval() + { + OptimizelyFactory.SetBlockingTimeOutPeriod(TimeSpan.FromSeconds(30)); + var optimizely = OptimizelyFactory.NewDefaultInstance(); + // Check values are loaded from app.config or not. + var projectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager; + Assert.NotNull(projectConfigManager); + + var actualConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager); + var expectedConfigManagerProps = new ProjectConfigManagerProps + { + Url = "www.testurl.com", + LastModified = "", + AutoUpdate = true, + DatafileAccessToken = "testingtoken123", + BlockingTimeout = TimeSpan.FromSeconds(30), + PollingInterval = TimeSpan.FromSeconds(2) + }; + + Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps); + optimizely.Dispose(); + } [Test] public void TestProjectConfigManagerUsingSDKKey() diff --git a/OptimizelySDK/OptimizelyFactory.cs b/OptimizelySDK/OptimizelyFactory.cs index 95d74960..06d53b35 100644 --- a/OptimizelySDK/OptimizelyFactory.cs +++ b/OptimizelySDK/OptimizelyFactory.cs @@ -35,6 +35,8 @@ public static class OptimizelyFactory { private static int MaxEventBatchSize; private static TimeSpan MaxEventFlushInterval; + private static TimeSpan PollingInterval; + private static TimeSpan BlockingTimeOutPeriod; private static ILogger OptimizelyLogger; private const string ConfigSectionName = "optlySDKConfigSection"; @@ -49,6 +51,16 @@ public static void SetFlushInterval(TimeSpan flushInterval) MaxEventFlushInterval = flushInterval; } + public static void SetPollingInterval(TimeSpan pollingInterval) + { + PollingInterval = pollingInterval; + } + + public static void SetBlockingTimeOutPeriod(TimeSpan blockingTimeOutPeriod) + { + BlockingTimeOutPeriod = blockingTimeOutPeriod; + } + public static void SetLogger(ILogger logger) { OptimizelyLogger = logger; @@ -76,13 +88,12 @@ public static Optimizely NewDefaultInstance() var eventDispatcher = new DefaultEventDispatcher(logger); var builder = new HttpProjectConfigManager.Builder(); var notificationCenter = new NotificationCenter(); - var configManager = builder .WithSdkKey(httpProjectConfigElement.SDKKey) .WithUrl(httpProjectConfigElement.Url) .WithFormat(httpProjectConfigElement.Format) - .WithPollingInterval(TimeSpan.FromMilliseconds(httpProjectConfigElement.PollingInterval)) - .WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(httpProjectConfigElement.BlockingTimeOutPeriod)) + .WithPollingInterval(PollingInterval == TimeSpan.Zero ? TimeSpan.FromMilliseconds(httpProjectConfigElement.PollingInterval) : PollingInterval) + .WithBlockingTimeoutPeriod(BlockingTimeOutPeriod == TimeSpan.Zero ? TimeSpan.FromMilliseconds(httpProjectConfigElement.BlockingTimeOutPeriod) : BlockingTimeOutPeriod) #if !NET40 && !NET35 .WithAccessToken(httpProjectConfigElement.DatafileAccessToken) #endif @@ -111,7 +122,7 @@ public static Optimizely NewDefaultInstance() } #endif - public static Optimizely NewDefaultInstance(string sdkKey) + public static Optimizely NewDefaultInstance(string sdkKey) { return NewDefaultInstance(sdkKey, null); } @@ -128,6 +139,8 @@ public static Optimizely NewDefaultInstance(string sdkKey, string fallback, stri .WithSdkKey(sdkKey) .WithDatafile(fallback) .WithLogger(logger) + .WithPollingInterval(PollingInterval) + .WithBlockingTimeoutPeriod(BlockingTimeOutPeriod) .WithErrorHandler(errorHandler) .WithAccessToken(datafileAuthToken) .WithNotificationCenter(notificationCenter) @@ -160,6 +173,8 @@ public static Optimizely NewDefaultInstance(string sdkKey, string fallback) .WithSdkKey(sdkKey) .WithDatafile(fallback) .WithLogger(logger) + .WithPollingInterval(PollingInterval) + .WithBlockingTimeoutPeriod(BlockingTimeOutPeriod) .WithErrorHandler(errorHandler) .WithNotificationCenter(notificationCenter) .Build(true); From 0d78c853b1f3e96aa34324f44d52888549d928ac Mon Sep 17 00:00:00 2001 From: muhammadnoman Date: Thu, 6 May 2021 22:29:40 +0500 Subject: [PATCH 2/4] Fixed UnitTests --- OptimizelySDK.Tests/OptimizelyFactoryTest.cs | 61 ++++++-------------- 1 file changed, 19 insertions(+), 42 deletions(-) diff --git a/OptimizelySDK.Tests/OptimizelyFactoryTest.cs b/OptimizelySDK.Tests/OptimizelyFactoryTest.cs index 5bd94d97..92c3de54 100644 --- a/OptimizelySDK.Tests/OptimizelyFactoryTest.cs +++ b/OptimizelySDK.Tests/OptimizelyFactoryTest.cs @@ -63,34 +63,10 @@ public void TestOptimizelyInstanceUsingConfigFile() } [Test] - public void TestOptimizelyInstanceUsingConfigFileWithPollingInterval() + public void TestProjectConfigManagerUsingSDKKey() { - OptimizelyFactory.SetPollingInterval(TimeSpan.FromMilliseconds(2023)); - var optimizely = OptimizelyFactory.NewDefaultInstance(); - // Check values are loaded from app.config or not. - var projectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager; - Assert.NotNull(projectConfigManager); - - var actualConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager); - var expectedConfigManagerProps = new ProjectConfigManagerProps - { - Url = "www.testurl.com", - LastModified = "", - AutoUpdate = true, - DatafileAccessToken = "testingtoken123", - BlockingTimeout = TimeSpan.FromSeconds(10), - PollingInterval = TimeSpan.FromMilliseconds(2023) - }; - - Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps); - optimizely.Dispose(); - } + var optimizely = OptimizelyFactory.NewDefaultInstance("my-sdk-key"); - [Test] - public void TestOptimizelyInstanceUsingConfigFileWithBlockingTimeOutInterval() - { - OptimizelyFactory.SetBlockingTimeOutPeriod(TimeSpan.FromSeconds(30)); - var optimizely = OptimizelyFactory.NewDefaultInstance(); // Check values are loaded from app.config or not. var projectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager; Assert.NotNull(projectConfigManager); @@ -98,12 +74,11 @@ public void TestOptimizelyInstanceUsingConfigFileWithBlockingTimeOutInterval() var actualConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager); var expectedConfigManagerProps = new ProjectConfigManagerProps { - Url = "www.testurl.com", + Url = "https://cdn.optimizely.com/datafiles/my-sdk-key.json", LastModified = "", AutoUpdate = true, - DatafileAccessToken = "testingtoken123", BlockingTimeout = TimeSpan.FromSeconds(30), - PollingInterval = TimeSpan.FromSeconds(2) + PollingInterval = TimeSpan.FromMilliseconds(2023) }; Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps); @@ -111,9 +86,9 @@ public void TestOptimizelyInstanceUsingConfigFileWithBlockingTimeOutInterval() } [Test] - public void TestProjectConfigManagerUsingSDKKey() + public void TestProjectConfigManagerWithDatafileAccessToken() { - var optimizely = OptimizelyFactory.NewDefaultInstance("my-sdk-key"); + var optimizely = OptimizelyFactory.NewDefaultInstance("my-sdk-key", null, "access-token"); // Check values are loaded from app.config or not. var projectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager; @@ -122,22 +97,25 @@ public void TestProjectConfigManagerUsingSDKKey() var actualConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager); var expectedConfigManagerProps = new ProjectConfigManagerProps { - Url = "https://cdn.optimizely.com/datafiles/my-sdk-key.json", + Url = "https://config.optimizely.com/datafiles/auth/my-sdk-key.json", LastModified = "", + DatafileAccessToken = "access-token", AutoUpdate = true, - BlockingTimeout = TimeSpan.FromSeconds(15), - PollingInterval = TimeSpan.FromMinutes(5) + BlockingTimeout = TimeSpan.FromSeconds(30), + PollingInterval = TimeSpan.FromMilliseconds(2023) }; Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps); + optimizely.Dispose(); } [Test] - public void TestProjectConfigManagerWithDatafileAccessToken() + public void TestOptimizelyInstanceUsingConfigFileWithBlockingAndPollingInterval() { - var optimizely = OptimizelyFactory.NewDefaultInstance("my-sdk-key", null, "access-token"); - + OptimizelyFactory.SetBlockingTimeOutPeriod(TimeSpan.FromSeconds(30)); + OptimizelyFactory.SetPollingInterval(TimeSpan.FromMilliseconds(2023)); + var optimizely = OptimizelyFactory.NewDefaultInstance(); // Check values are loaded from app.config or not. var projectConfigManager = optimizely.ProjectConfigManager as HttpProjectConfigManager; Assert.NotNull(projectConfigManager); @@ -145,16 +123,15 @@ public void TestProjectConfigManagerWithDatafileAccessToken() var actualConfigManagerProps = new ProjectConfigManagerProps(projectConfigManager); var expectedConfigManagerProps = new ProjectConfigManagerProps { - Url = "https://config.optimizely.com/datafiles/auth/my-sdk-key.json", + Url = "www.testurl.com", LastModified = "", - DatafileAccessToken = "access-token", AutoUpdate = true, - BlockingTimeout = TimeSpan.FromSeconds(15), - PollingInterval = TimeSpan.FromMinutes(5) + DatafileAccessToken = "testingtoken123", + BlockingTimeout = TimeSpan.FromSeconds(30), + PollingInterval = TimeSpan.FromMilliseconds(2023) }; Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps); - optimizely.Dispose(); } From 5bdbe432709ed6f6bcc26a33270c04b692707a2d Mon Sep 17 00:00:00 2001 From: muhammadnoman Date: Mon, 10 May 2021 18:27:56 +0500 Subject: [PATCH 3/4] headers updated --- OptimizelySDK.Tests/OptimizelyFactoryTest.cs | 2 +- OptimizelySDK/OptimizelyFactory.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/OptimizelySDK.Tests/OptimizelyFactoryTest.cs b/OptimizelySDK.Tests/OptimizelyFactoryTest.cs index 92c3de54..ae4c3f2e 100644 --- a/OptimizelySDK.Tests/OptimizelyFactoryTest.cs +++ b/OptimizelySDK.Tests/OptimizelyFactoryTest.cs @@ -1,6 +1,6 @@ /** * - * Copyright 2020, Optimizely and contributors + * Copyright 2020-2021, Optimizely and contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/OptimizelySDK/OptimizelyFactory.cs b/OptimizelySDK/OptimizelyFactory.cs index 06d53b35..bbcdaf78 100644 --- a/OptimizelySDK/OptimizelyFactory.cs +++ b/OptimizelySDK/OptimizelyFactory.cs @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020, Optimizely + * Copyright 2019-2021, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use file except in compliance with the License. From 6c56c8889debcb6d162bad9199b7f7beb4ec6c66 Mon Sep 17 00:00:00 2001 From: Sohail Hussain Date: Wed, 12 May 2021 14:25:05 -0700 Subject: [PATCH 4/4] addressed feedback --- OptimizelySDK.Tests/OptimizelyFactoryTest.cs | 6 +++--- OptimizelySDK/OptimizelyFactory.cs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/OptimizelySDK.Tests/OptimizelyFactoryTest.cs b/OptimizelySDK.Tests/OptimizelyFactoryTest.cs index ae4c3f2e..92d2a23b 100644 --- a/OptimizelySDK.Tests/OptimizelyFactoryTest.cs +++ b/OptimizelySDK.Tests/OptimizelyFactoryTest.cs @@ -111,7 +111,7 @@ public void TestProjectConfigManagerWithDatafileAccessToken() } [Test] - public void TestOptimizelyInstanceUsingConfigFileWithBlockingAndPollingInterval() + public void TestOptimizelyInstanceUsingConfigNotUseFactoryClassBlockingTimeoutAndPollingInterval() { OptimizelyFactory.SetBlockingTimeOutPeriod(TimeSpan.FromSeconds(30)); OptimizelyFactory.SetPollingInterval(TimeSpan.FromMilliseconds(2023)); @@ -127,8 +127,8 @@ public void TestOptimizelyInstanceUsingConfigFileWithBlockingAndPollingInterval( LastModified = "", AutoUpdate = true, DatafileAccessToken = "testingtoken123", - BlockingTimeout = TimeSpan.FromSeconds(30), - PollingInterval = TimeSpan.FromMilliseconds(2023) + BlockingTimeout = TimeSpan.FromMilliseconds(10000), + PollingInterval = TimeSpan.FromMilliseconds(2000) }; Assert.AreEqual(actualConfigManagerProps, expectedConfigManagerProps); diff --git a/OptimizelySDK/OptimizelyFactory.cs b/OptimizelySDK/OptimizelyFactory.cs index bbcdaf78..8e78bcf8 100644 --- a/OptimizelySDK/OptimizelyFactory.cs +++ b/OptimizelySDK/OptimizelyFactory.cs @@ -92,8 +92,8 @@ public static Optimizely NewDefaultInstance() .WithSdkKey(httpProjectConfigElement.SDKKey) .WithUrl(httpProjectConfigElement.Url) .WithFormat(httpProjectConfigElement.Format) - .WithPollingInterval(PollingInterval == TimeSpan.Zero ? TimeSpan.FromMilliseconds(httpProjectConfigElement.PollingInterval) : PollingInterval) - .WithBlockingTimeoutPeriod(BlockingTimeOutPeriod == TimeSpan.Zero ? TimeSpan.FromMilliseconds(httpProjectConfigElement.BlockingTimeOutPeriod) : BlockingTimeOutPeriod) + .WithPollingInterval(TimeSpan.FromMilliseconds(httpProjectConfigElement.PollingInterval)) + .WithBlockingTimeoutPeriod(TimeSpan.FromMilliseconds(httpProjectConfigElement.BlockingTimeOutPeriod)) #if !NET40 && !NET35 .WithAccessToken(httpProjectConfigElement.DatafileAccessToken) #endif