From 5cf13cf5c01067329517de1f45a90e3ce69c8467 Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Fri, 24 Nov 2023 22:30:54 +0600 Subject: [PATCH 1/9] Duplicate key warning added --- Sources/Optimizely/OptimizelyConfig.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/Optimizely/OptimizelyConfig.swift b/Sources/Optimizely/OptimizelyConfig.swift index af590d31b..55c3b12ea 100644 --- a/Sources/Optimizely/OptimizelyConfig.swift +++ b/Sources/Optimizely/OptimizelyConfig.swift @@ -95,7 +95,8 @@ struct OptimizelyConfigImp: OptimizelyConfig { var attributes: [OptimizelyAttribute] = [] var audiences: [OptimizelyAudience] = [] var events: [OptimizelyEvent] = [] - + private var logger = OPTLoggerFactory.getLogger() + init(projectConfig: ProjectConfig) { guard let project = projectConfig.project else { return } @@ -150,7 +151,11 @@ extension OptimizelyConfigImp { func makeExperimentsMap(project: Project, experiments: [Experiment]) -> [String: Experiment] { var map = [String: Experiment]() + experiments.forEach { + if map.keys.contains($0.key) { + logger.w("Duplicate experiment keys found in datafile: \($0.key)") + } map[$0.key] = $0 } return map From 9d9ecb80a9613e6d4e34288eb930ff2d660a82aa Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Fri, 24 Nov 2023 22:32:32 +0600 Subject: [PATCH 2/9] Add test case for duplicate key --- .../ProjectConfigTests.swift | 65 ++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift b/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift index aebde55fb..fb94a173d 100644 --- a/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift +++ b/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift @@ -79,7 +79,70 @@ class ProjectConfigTests: XCTestCase { XCTAssertEqual(featureMap["1003"], ["2002"]) XCTAssertEqual(featureMap["1004"], ["2002"]) } - + + func testProjectConfigWithDuplicateKey() { + let exp0: [String : Any] = [ + "id": "10001", + "key": "duplicate_key", + "status": "Running", + "layerId": "22222", + "variations": [], + "trafficAllocation": [], + "audienceIds": ["33333"], + "audienceConditions": [], + "forcedVariations": ["12345": "1234567890"] + ] + + let exp1: [String : Any] = [ + "id": "10005", + "key": "duplicate_key", + "status": "Running", + "layerId": "22222", + "variations": [], + "trafficAllocation": [], + "audienceIds": ["33333"], + "audienceConditions": [], + "forcedVariations": ["12345": "1234567890"] + ] + + var projectData: [String: Any] = [ + "version": "4", + "projectId": "11111", + "experiments": [], + "audiences": [], + "groups": [], + "attributes": [], + "accountId": "1234567890", + "events": [], + "revision": "5", + "anonymizeIP": true, + "rollouts": [], + "typedAudiences": [], + "integrations": [], + "featureFlags": [], + "botFiltering": false, + "sendFlagDecisions": true + ] + + projectData["experiments"] = [exp0, exp1] + let model: Project = try! OTUtils.model(from: projectData) + let projectConfig = ProjectConfig() + projectConfig.project = model + + optimizely = OptimizelyClient( + sdkKey: "demo_key", + logger: _TestLogger() + ) + + optimizely.config = projectConfig + + let optimizelyExpMap: [String: OptimizelyExperiment] = try! optimizely.getOptimizelyConfig().experimentsMap + + XCTAssertEqual(optimizelyExpMap.count, 1) + XCTAssertEqual(optimizelyExpMap["duplicate_key"]?.id, "10005") + } + + func testFlagVariations() { let datafile = OTUtils.loadJSONDatafile("decide_datafile")! let optimizely = OptimizelyClient(sdkKey: "12345", From 57438e740f87ecc4f67bd6ace9146c888d864ff8 Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Fri, 24 Nov 2023 22:54:01 +0600 Subject: [PATCH 3/9] Transfer test case from ProjectConfigTests to OptimizelyClientTest_OptimizelyConfig --- ...timizelyClientTests_OptimizelyConfig.swift | 61 ++++++++++++++++++ .../ProjectConfigTests.swift | 63 ------------------- 2 files changed, 61 insertions(+), 63 deletions(-) diff --git a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift index 53bfde5a8..8974227ea 100644 --- a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift +++ b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift @@ -257,6 +257,67 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase { let result = try? self.optimizely.getOptimizelyConfig() XCTAssertNil(result) } + + func testOptimizelyConfigWithDuplicateKeys() { + let exp0: [String : Any] = [ + "id": "10001", + "key": "duplicate_key", + "status": "Running", + "layerId": "22222", + "variations": [], + "trafficAllocation": [], + "audienceIds": ["33333"], + "audienceConditions": [], + "forcedVariations": ["12345": "1234567890"] + ] + + let exp1: [String : Any] = [ + "id": "10005", + "key": "duplicate_key", + "status": "Running", + "layerId": "22222", + "variations": [], + "trafficAllocation": [], + "audienceIds": ["33333"], + "audienceConditions": [], + "forcedVariations": ["12345": "1234567890"] + ] + + var projectData: [String: Any] = [ + "version": "4", + "projectId": "11111", + "experiments": [], + "audiences": [], + "groups": [], + "attributes": [], + "accountId": "1234567890", + "events": [], + "revision": "5", + "anonymizeIP": true, + "rollouts": [], + "typedAudiences": [], + "integrations": [], + "featureFlags": [], + "botFiltering": false, + "sendFlagDecisions": true + ] + + projectData["experiments"] = [exp0, exp1] + let model: Project = try! OTUtils.model(from: projectData) + let projectConfig = ProjectConfig() + projectConfig.project = model + + optimizely = OptimizelyClient( + sdkKey: "demo_key" + ) + + optimizely.config = projectConfig + + let optimizelyExpMap: [String: OptimizelyExperiment] = try! optimizely.getOptimizelyConfig().experimentsMap + + XCTAssertEqual(optimizelyExpMap.count, 1) + XCTAssertEqual(optimizelyExpMap["duplicate_key"]?.id, "10005") + } } diff --git a/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift b/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift index fb94a173d..fbe7cc2a9 100644 --- a/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift +++ b/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift @@ -79,69 +79,6 @@ class ProjectConfigTests: XCTestCase { XCTAssertEqual(featureMap["1003"], ["2002"]) XCTAssertEqual(featureMap["1004"], ["2002"]) } - - func testProjectConfigWithDuplicateKey() { - let exp0: [String : Any] = [ - "id": "10001", - "key": "duplicate_key", - "status": "Running", - "layerId": "22222", - "variations": [], - "trafficAllocation": [], - "audienceIds": ["33333"], - "audienceConditions": [], - "forcedVariations": ["12345": "1234567890"] - ] - - let exp1: [String : Any] = [ - "id": "10005", - "key": "duplicate_key", - "status": "Running", - "layerId": "22222", - "variations": [], - "trafficAllocation": [], - "audienceIds": ["33333"], - "audienceConditions": [], - "forcedVariations": ["12345": "1234567890"] - ] - - var projectData: [String: Any] = [ - "version": "4", - "projectId": "11111", - "experiments": [], - "audiences": [], - "groups": [], - "attributes": [], - "accountId": "1234567890", - "events": [], - "revision": "5", - "anonymizeIP": true, - "rollouts": [], - "typedAudiences": [], - "integrations": [], - "featureFlags": [], - "botFiltering": false, - "sendFlagDecisions": true - ] - - projectData["experiments"] = [exp0, exp1] - let model: Project = try! OTUtils.model(from: projectData) - let projectConfig = ProjectConfig() - projectConfig.project = model - - optimizely = OptimizelyClient( - sdkKey: "demo_key", - logger: _TestLogger() - ) - - optimizely.config = projectConfig - - let optimizelyExpMap: [String: OptimizelyExperiment] = try! optimizely.getOptimizelyConfig().experimentsMap - - XCTAssertEqual(optimizelyExpMap.count, 1) - XCTAssertEqual(optimizelyExpMap["duplicate_key"]?.id, "10005") - } - func testFlagVariations() { let datafile = OTUtils.loadJSONDatafile("decide_datafile")! From d760c6cc5731c537ba64b984af2913d4ca45a202 Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Tue, 28 Nov 2023 14:20:23 +0600 Subject: [PATCH 4/9] Logging message test case added for duplicated key --- Sources/Optimizely/OptimizelyConfig.swift | 2 +- ...timizelyClientTests_OptimizelyConfig.swift | 51 ++++++++++++++++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/Sources/Optimizely/OptimizelyConfig.swift b/Sources/Optimizely/OptimizelyConfig.swift index 55c3b12ea..b92c2e4f7 100644 --- a/Sources/Optimizely/OptimizelyConfig.swift +++ b/Sources/Optimizely/OptimizelyConfig.swift @@ -95,7 +95,7 @@ struct OptimizelyConfigImp: OptimizelyConfig { var attributes: [OptimizelyAttribute] = [] var audiences: [OptimizelyAudience] = [] var events: [OptimizelyEvent] = [] - private var logger = OPTLoggerFactory.getLogger() + var logger = OPTLoggerFactory.getLogger() init(projectConfig: ProjectConfig) { guard let project = projectConfig.project else { return } diff --git a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift index 8974227ea..d260c2a63 100644 --- a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift +++ b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift @@ -26,6 +26,7 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase { let datafile = OTUtils.loadJSONDatafile("optimizely_config_datafile")! self.optimizely = OptimizelyClient(sdkKey: "12345", + logger: TestLogger(), userProfileService: OTUtils.createClearUserProfileService()) try! self.optimizely.start(datafile: datafile) } @@ -307,13 +308,13 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase { let projectConfig = ProjectConfig() projectConfig.project = model - optimizely = OptimizelyClient( - sdkKey: "demo_key" - ) - optimizely.config = projectConfig - - let optimizelyExpMap: [String: OptimizelyExperiment] = try! optimizely.getOptimizelyConfig().experimentsMap + + let optiConfig = try! optimizely.getOptimizelyConfig() + let optimizelyExpMap: [String: OptimizelyExperiment] = optiConfig.experimentsMap + + let logger = (optiConfig as! OptimizelyConfigImp).logger as! TestLogger + XCTAssertEqual(logger.getMessages(.warning), ["Duplicate experiment keys found in datafile: duplicate_key"]) XCTAssertEqual(optimizelyExpMap.count, 1) XCTAssertEqual(optimizelyExpMap["duplicate_key"]?.id, "10005") @@ -426,3 +427,41 @@ extension OptimizelyEvent { } } +// MARK: - Mock Loggers + +fileprivate class TestLogger: OPTLogger { + private static var _logLevel: OptimizelyLogLevel? + public static var logLevel: OptimizelyLogLevel { + get { + return _logLevel ?? .info + } + set (newLevel) { + _logLevel = newLevel + } + } + + required public init() { + clearMessages() + } + + func log(level: OptimizelyLogLevel, message: String) { + logMessages[level.rawValue].append(message) + } + + // Utils + + var logMessages = [[String]]() + + var logCount: Int { + return logMessages.reduce(0) { $0 + $1.count } + } + + func getMessages(_ level: OptimizelyLogLevel) -> [String] { + return logMessages[level.rawValue] + } + + func clearMessages() { + logMessages = [[String]](repeating: [], count: OptimizelyLogLevel.debug.rawValue + 1) + } + +} From 6ce3703ccf7121e2a2ba8c42be1e0adad993053c Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Wed, 29 Nov 2023 16:14:56 +0600 Subject: [PATCH 5/9] Fix code intendation --- Sources/Optimizely/OptimizelyConfig.swift | 2 +- ...timizelyClientTests_OptimizelyConfig.swift | 188 +++++++++--------- 2 files changed, 95 insertions(+), 95 deletions(-) diff --git a/Sources/Optimizely/OptimizelyConfig.swift b/Sources/Optimizely/OptimizelyConfig.swift index b92c2e4f7..9cbb6a65d 100644 --- a/Sources/Optimizely/OptimizelyConfig.swift +++ b/Sources/Optimizely/OptimizelyConfig.swift @@ -95,7 +95,7 @@ struct OptimizelyConfigImp: OptimizelyConfig { var attributes: [OptimizelyAttribute] = [] var audiences: [OptimizelyAudience] = [] var events: [OptimizelyEvent] = [] - var logger = OPTLoggerFactory.getLogger() + var logger = OPTLoggerFactory.getLogger() init(projectConfig: ProjectConfig) { guard let project = projectConfig.project else { return } diff --git a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift index d260c2a63..06d59060b 100644 --- a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift +++ b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift @@ -259,66 +259,66 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase { XCTAssertNil(result) } - func testOptimizelyConfigWithDuplicateKeys() { - let exp0: [String : Any] = [ - "id": "10001", - "key": "duplicate_key", - "status": "Running", - "layerId": "22222", - "variations": [], - "trafficAllocation": [], - "audienceIds": ["33333"], - "audienceConditions": [], - "forcedVariations": ["12345": "1234567890"] - ] - - let exp1: [String : Any] = [ - "id": "10005", - "key": "duplicate_key", - "status": "Running", - "layerId": "22222", - "variations": [], - "trafficAllocation": [], - "audienceIds": ["33333"], - "audienceConditions": [], - "forcedVariations": ["12345": "1234567890"] - ] - - var projectData: [String: Any] = [ - "version": "4", - "projectId": "11111", - "experiments": [], - "audiences": [], - "groups": [], - "attributes": [], - "accountId": "1234567890", - "events": [], - "revision": "5", - "anonymizeIP": true, - "rollouts": [], - "typedAudiences": [], - "integrations": [], - "featureFlags": [], - "botFiltering": false, - "sendFlagDecisions": true - ] - - projectData["experiments"] = [exp0, exp1] - let model: Project = try! OTUtils.model(from: projectData) - let projectConfig = ProjectConfig() - projectConfig.project = model - - optimizely.config = projectConfig - - let optiConfig = try! optimizely.getOptimizelyConfig() - let optimizelyExpMap: [String: OptimizelyExperiment] = optiConfig.experimentsMap - - let logger = (optiConfig as! OptimizelyConfigImp).logger as! TestLogger - XCTAssertEqual(logger.getMessages(.warning), ["Duplicate experiment keys found in datafile: duplicate_key"]) - - XCTAssertEqual(optimizelyExpMap.count, 1) - XCTAssertEqual(optimizelyExpMap["duplicate_key"]?.id, "10005") - } + func testOptimizelyConfigWithDuplicateKeys() { + let exp0: [String : Any] = [ + "id": "10001", + "key": "duplicate_key", + "status": "Running", + "layerId": "22222", + "variations": [], + "trafficAllocation": [], + "audienceIds": ["33333"], + "audienceConditions": [], + "forcedVariations": ["12345": "1234567890"] + ] + + let exp1: [String : Any] = [ + "id": "10005", + "key": "duplicate_key", + "status": "Running", + "layerId": "22222", + "variations": [], + "trafficAllocation": [], + "audienceIds": ["33333"], + "audienceConditions": [], + "forcedVariations": ["12345": "1234567890"] + ] + + var projectData: [String: Any] = [ + "version": "4", + "projectId": "11111", + "experiments": [], + "audiences": [], + "groups": [], + "attributes": [], + "accountId": "1234567890", + "events": [], + "revision": "5", + "anonymizeIP": true, + "rollouts": [], + "typedAudiences": [], + "integrations": [], + "featureFlags": [], + "botFiltering": false, + "sendFlagDecisions": true + ] + + projectData["experiments"] = [exp0, exp1] + let model: Project = try! OTUtils.model(from: projectData) + let projectConfig = ProjectConfig() + projectConfig.project = model + + optimizely.config = projectConfig + + let optiConfig = try! optimizely.getOptimizelyConfig() + let optimizelyExpMap: [String: OptimizelyExperiment] = optiConfig.experimentsMap + + let logger = (optiConfig as! OptimizelyConfigImp).logger as! TestLogger + XCTAssertEqual(logger.getMessages(.warning), ["Duplicate experiment keys found in datafile: duplicate_key"]) + + XCTAssertEqual(optimizelyExpMap.count, 1) + XCTAssertEqual(optimizelyExpMap["duplicate_key"]?.id, "10005") + } } @@ -430,38 +430,38 @@ extension OptimizelyEvent { // MARK: - Mock Loggers fileprivate class TestLogger: OPTLogger { - private static var _logLevel: OptimizelyLogLevel? - public static var logLevel: OptimizelyLogLevel { - get { - return _logLevel ?? .info - } - set (newLevel) { - _logLevel = newLevel - } - } - - required public init() { - clearMessages() - } - - func log(level: OptimizelyLogLevel, message: String) { - logMessages[level.rawValue].append(message) - } - - // Utils - - var logMessages = [[String]]() - - var logCount: Int { - return logMessages.reduce(0) { $0 + $1.count } - } - - func getMessages(_ level: OptimizelyLogLevel) -> [String] { - return logMessages[level.rawValue] - } - - func clearMessages() { - logMessages = [[String]](repeating: [], count: OptimizelyLogLevel.debug.rawValue + 1) - } - + private static var _logLevel: OptimizelyLogLevel? + public static var logLevel: OptimizelyLogLevel { + get { + return _logLevel ?? .info + } + set (newLevel) { + _logLevel = newLevel + } + } + + required public init() { + clearMessages() + } + + func log(level: OptimizelyLogLevel, message: String) { + logMessages[level.rawValue].append(message) + } + + // Utils + + var logMessages = [[String]]() + + var logCount: Int { + return logMessages.reduce(0) { $0 + $1.count } + } + + func getMessages(_ level: OptimizelyLogLevel) -> [String] { + return logMessages[level.rawValue] + } + + func clearMessages() { + logMessages = [[String]](repeating: [], count: OptimizelyLogLevel.debug.rawValue + 1) + } + } From 4aa9cc96b24b8715375dd564b24f79f8b686208b Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Wed, 29 Nov 2023 16:17:40 +0600 Subject: [PATCH 6/9] Fix code intendation --- .../OptimizelyClientTests_OptimizelyConfig.swift | 2 +- Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift index 06d59060b..13dc2d3c0 100644 --- a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift +++ b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift @@ -26,7 +26,7 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase { let datafile = OTUtils.loadJSONDatafile("optimizely_config_datafile")! self.optimizely = OptimizelyClient(sdkKey: "12345", - logger: TestLogger(), + logger: TestLogger(), userProfileService: OTUtils.createClearUserProfileService()) try! self.optimizely.start(datafile: datafile) } diff --git a/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift b/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift index fbe7cc2a9..aebde55fb 100644 --- a/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift +++ b/Tests/OptimizelyTests-DataModel/ProjectConfigTests.swift @@ -79,7 +79,7 @@ class ProjectConfigTests: XCTestCase { XCTAssertEqual(featureMap["1003"], ["2002"]) XCTAssertEqual(featureMap["1004"], ["2002"]) } - + func testFlagVariations() { let datafile = OTUtils.loadJSONDatafile("decide_datafile")! let optimizely = OptimizelyClient(sdkKey: "12345", From 037a4f2e1ff446c9eb0c27c520a87052647ac435 Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Wed, 29 Nov 2023 16:20:52 +0600 Subject: [PATCH 7/9] Code intendation fix --- Sources/Optimizely/OptimizelyConfig.swift | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Sources/Optimizely/OptimizelyConfig.swift b/Sources/Optimizely/OptimizelyConfig.swift index 9cbb6a65d..c38dee5ca 100644 --- a/Sources/Optimizely/OptimizelyConfig.swift +++ b/Sources/Optimizely/OptimizelyConfig.swift @@ -96,7 +96,7 @@ struct OptimizelyConfigImp: OptimizelyConfig { var audiences: [OptimizelyAudience] = [] var events: [OptimizelyEvent] = [] var logger = OPTLoggerFactory.getLogger() - + init(projectConfig: ProjectConfig) { guard let project = projectConfig.project else { return } @@ -151,11 +151,10 @@ extension OptimizelyConfigImp { func makeExperimentsMap(project: Project, experiments: [Experiment]) -> [String: Experiment] { var map = [String: Experiment]() - experiments.forEach { - if map.keys.contains($0.key) { - logger.w("Duplicate experiment keys found in datafile: \($0.key)") - } + if map.keys.contains($0.key) { + logger.w("Duplicate experiment keys found in datafile: \($0.key)") + } map[$0.key] = $0 } return map From 07a2debae0c00aba1590398bceb5bea381e9332d Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Thu, 30 Nov 2023 11:52:57 +0600 Subject: [PATCH 8/9] Logger inject to OptimizelyConfigImp to log warning for uplicate keys. --- Sources/Optimizely/OptimizelyClient.swift | 2 +- Sources/Optimizely/OptimizelyConfig.swift | 9 ++++----- .../OptimizelyClientTests_OptimizelyConfig.swift | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Sources/Optimizely/OptimizelyClient.swift b/Sources/Optimizely/OptimizelyClient.swift index 38524deb5..665bc0af6 100644 --- a/Sources/Optimizely/OptimizelyClient.swift +++ b/Sources/Optimizely/OptimizelyClient.swift @@ -782,7 +782,7 @@ open class OptimizelyClient: NSObject { public func getOptimizelyConfig() throws -> OptimizelyConfig { guard let config = self.config else { throw OptimizelyError.sdkNotReady } - return OptimizelyConfigImp(projectConfig: config) + return OptimizelyConfigImp(projectConfig: config, logger: logger) } } diff --git a/Sources/Optimizely/OptimizelyConfig.swift b/Sources/Optimizely/OptimizelyConfig.swift index c38dee5ca..e04d3002c 100644 --- a/Sources/Optimizely/OptimizelyConfig.swift +++ b/Sources/Optimizely/OptimizelyConfig.swift @@ -95,9 +95,8 @@ struct OptimizelyConfigImp: OptimizelyConfig { var attributes: [OptimizelyAttribute] = [] var audiences: [OptimizelyAudience] = [] var events: [OptimizelyEvent] = [] - var logger = OPTLoggerFactory.getLogger() - - init(projectConfig: ProjectConfig) { + + init(projectConfig: ProjectConfig, logger: OPTLogger = DefaultLogger()) { guard let project = projectConfig.project else { return } self.environmentKey = project.environmentKey ?? "" @@ -140,7 +139,7 @@ struct OptimizelyConfigImp: OptimizelyConfig { return updatedRollout } - self.experimentsMap = makeExperimentsMap(project: project, experiments: updatedExperiments) + self.experimentsMap = makeExperimentsMap(project: project, experiments: updatedExperiments, logger: logger) self.featuresMap = makeFeaturesMap(project: project, experiments: updatedExperiments, rollouts: updatedRollouts) } } @@ -149,7 +148,7 @@ struct OptimizelyConfigImp: OptimizelyConfig { extension OptimizelyConfigImp { - func makeExperimentsMap(project: Project, experiments: [Experiment]) -> [String: Experiment] { + func makeExperimentsMap(project: Project, experiments: [Experiment], logger: OPTLogger) -> [String: Experiment] { var map = [String: Experiment]() experiments.forEach { if map.keys.contains($0.key) { diff --git a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift index 13dc2d3c0..52865d70b 100644 --- a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift +++ b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift @@ -313,7 +313,7 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase { let optiConfig = try! optimizely.getOptimizelyConfig() let optimizelyExpMap: [String: OptimizelyExperiment] = optiConfig.experimentsMap - let logger = (optiConfig as! OptimizelyConfigImp).logger as! TestLogger + let logger = optimizely.logger as! TestLogger XCTAssertEqual(logger.getMessages(.warning), ["Duplicate experiment keys found in datafile: duplicate_key"]) XCTAssertEqual(optimizelyExpMap.count, 1) From ac595c081426e724929ae419159058746c3c90f3 Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Thu, 30 Nov 2023 13:42:06 +0600 Subject: [PATCH 9/9] Logger override issue fixed --- .../OptimizelyClientTests_OptimizelyConfig.swift | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift index 52865d70b..cf2b63c20 100644 --- a/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift +++ b/Tests/OptimizelyTests-APIs/OptimizelyClientTests_OptimizelyConfig.swift @@ -26,7 +26,6 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase { let datafile = OTUtils.loadJSONDatafile("optimizely_config_datafile")! self.optimizely = OptimizelyClient(sdkKey: "12345", - logger: TestLogger(), userProfileService: OTUtils.createClearUserProfileService()) try! self.optimizely.start(datafile: datafile) } @@ -308,12 +307,9 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase { let projectConfig = ProjectConfig() projectConfig.project = model - optimizely.config = projectConfig - - let optiConfig = try! optimizely.getOptimizelyConfig() - let optimizelyExpMap: [String: OptimizelyExperiment] = optiConfig.experimentsMap - - let logger = optimizely.logger as! TestLogger + let logger = TestLogger() + let optiConfigImpl = OptimizelyConfigImp(projectConfig: projectConfig, logger: logger) + let optimizelyExpMap: [String: OptimizelyExperiment] = optiConfigImpl.experimentsMap XCTAssertEqual(logger.getMessages(.warning), ["Duplicate experiment keys found in datafile: duplicate_key"]) XCTAssertEqual(optimizelyExpMap.count, 1)