From 3c4b60eea2f9f94d69d92dba04f3bb34e6699a13 Mon Sep 17 00:00:00 2001 From: Enrico Zannini Date: Tue, 19 Dec 2023 12:23:47 +0000 Subject: [PATCH 1/7] First attempt to disable the module on config.isEnabled change --- tealium/core/ModulesManager.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tealium/core/ModulesManager.swift b/tealium/core/ModulesManager.swift index 28c15343..fe3faa67 100644 --- a/tealium/core/ModulesManager.swift +++ b/tealium/core/ModulesManager.swift @@ -183,6 +183,7 @@ public class ModulesManager { if self.config.shouldUseRemotePublishSettings == true { self.remotePublishSettingsRetriever?.refresh() } + guard config.isEnabled != false else { return } let requestData = gatherTrackData(for: request.trackDictionary) let newRequest = TealiumTrackRequest(data: requestData) dispatchManager?.processTrack(newRequest) @@ -256,6 +257,10 @@ extension ModulesManager { } func setupCollectors(config: TealiumConfig) { + guard context.config.isEnabled != false else { + collectors.removeAll() + return + } collectorTypes.forEach { collector in guard !collectors.contains(where: { type(of: $0) == collector }) else { return @@ -280,6 +285,10 @@ extension ModulesManager { } func setupDispatchers(context: TealiumContext) { + guard context.config.isEnabled != false else { + dispatchers.removeAll() + return + } self.config.dispatchers?.forEach { dispatcherType in guard !dispatchers.contains(where: { type(of: $0) == dispatcherType }) else { return From 478ee9828b605d153f225784cc08e24d88b33030 Mon Sep 17 00:00:00 2001 From: Enrico Zannini Date: Wed, 20 Dec 2023 15:57:58 +0000 Subject: [PATCH 2/7] Make sure collectors are also removed when config is disabled and add tests --- .../TealiumModulesManagerTests.swift | 98 ++++++++++++++++++- tealium/core/ModulesManager.swift | 1 + 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/support/tests/test_tealium_core/TealiumModulesManagerTests.swift b/support/tests/test_tealium_core/TealiumModulesManagerTests.swift index ab44faf8..31243b62 100644 --- a/support/tests/test_tealium_core/TealiumModulesManagerTests.swift +++ b/support/tests/test_tealium_core/TealiumModulesManagerTests.swift @@ -403,6 +403,98 @@ class TealiumModulesManagerTests: XCTestCase { } } #endif + + func testDisableLibraryRemovesAllModules() { + sharedConfig.shouldUseRemotePublishSettings = true + var defaultSettings = RemotePublishSettings() + let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings) + context.config.dispatchers = [Dispatchers.Collect, Dispatchers.TagManagement] + TealiumQueues.backgroundSerialQueue.sync { + let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever) + XCTAssertNotEqual(modulesManager.dispatchers.count, 0) + XCTAssertNotEqual(modulesManager.collectors.count, 0) + + defaultSettings.isEnabled = false + modulesManager.didUpdate(defaultSettings) + XCTAssertEqual(modulesManager.dispatchers.count, 0) + XCTAssertEqual(modulesManager.collectors.count, 0) + } + } + + func testReEnableLibraryAddsAllModulesBack() { + sharedConfig.shouldUseRemotePublishSettings = true + var defaultSettings = RemotePublishSettings() + let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings) + context.config.dispatchers = [Dispatchers.Collect, Dispatchers.TagManagement] + TealiumQueues.backgroundSerialQueue.sync { + let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever) + XCTAssertNotEqual(modulesManager.dispatchers.count, 0) + XCTAssertNotEqual(modulesManager.collectors.count, 0) + + defaultSettings.isEnabled = false + modulesManager.didUpdate(defaultSettings) + XCTAssertEqual(modulesManager.dispatchers.count, 0) + XCTAssertEqual(modulesManager.collectors.count, 0) + + defaultSettings.isEnabled = true + modulesManager.didUpdate(defaultSettings) + XCTAssertNotEqual(modulesManager.dispatchers.count, 0) + XCTAssertNotEqual(modulesManager.collectors.count, 0) + } + } + + func testDisableLibraryStopsFutureTrackingCalls() { + let trackNotSent = expectation(description: "Track is not sent") + trackNotSent.isInverted = true + TealiumExpectations.expectations["sendTrack"] = trackNotSent + sharedConfig.shouldUseRemotePublishSettings = true + var defaultSettings = RemotePublishSettings() + let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings) + context.config.dispatchers = [Dispatchers.Collect, Dispatchers.TagManagement] + TealiumQueues.backgroundSerialQueue.sync { + let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever) + let connectivity = ConnectivityModule(context: modulesManager.context, delegate: nil, diskStorage: nil) { _ in } + modulesManager.dispatchManager = DummyDispatchManagerSendTrack(dispatchers: nil, dispatchValidators: nil, dispatchListeners: nil, connectivityManager: connectivity, config: testTealiumConfig) + + let getCollect = {modulesManager.dispatchers.first(where: { $0.id == ModuleNames.collect})} + let getTagManagement = {modulesManager.dispatchers.first(where: { $0.id == ModuleNames.tagmanagement})} + XCTAssertNotEqual(modulesManager.dispatchers.count, 0) + XCTAssertNotEqual(modulesManager.collectors.count, 0) + + defaultSettings.isEnabled = false + modulesManager.didUpdate(defaultSettings) + + modulesManager.sendTrack(TealiumTrackRequest(data: [:])) + } + waitForExpectations(timeout: 1.0) + } + + func testDisableLibraryDoesntStopRefreshingRemoteSettings() { + let refreshSettings = expectation(description: "Settings are refreshed") + refreshSettings.assertForOverFulfill = false // other events are also being sent so mulitple refresh are expected when running multiple tests at the same time + let notRefreshSettings = expectation(description: "Settings are not refreshed yet") + notRefreshSettings.isInverted = true + sharedConfig.shouldUseRemotePublishSettings = true + var defaultSettings = RemotePublishSettings() + let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings) + context.config.dispatchers = [Dispatchers.Collect, Dispatchers.TagManagement] + TealiumQueues.backgroundSerialQueue.sync { + let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever) + modulesManager.remotePublishSettingsRetriever = MockPublishSettingsRetriever { + notRefreshSettings.fulfill() + } + defaultSettings.isEnabled = false + modulesManager.didUpdate(defaultSettings) + XCTAssertEqual(modulesManager.dispatchers.count, 0) + XCTAssertEqual(modulesManager.collectors.count, 0) + wait(for: [notRefreshSettings], timeout: 1.0) + modulesManager.remotePublishSettingsRetriever = MockPublishSettingsRetriever { + refreshSettings.fulfill() + } + modulesManager.sendTrack(TealiumTrackRequest(data: [:])) + wait(for: [refreshSettings], timeout: 1.0) + } + } } extension TealiumModulesManagerTests: ModuleDelegate { @@ -496,13 +588,15 @@ class DummyDispatchManagerSendTrack: DispatchManagerProtocol { struct MockPublishSettingsRetriever: TealiumPublishSettingsRetrieverProtocol { - init(cachedSettings: RemotePublishSettings? = nil) { + let onRefresh: () -> Void + init(cachedSettings: RemotePublishSettings? = nil, onRefresh: @escaping () -> Void = { }) { sharedConfig.shouldUseRemotePublishSettings = true + self.onRefresh = onRefresh self.cachedSettings = cachedSettings } var cachedSettings: RemotePublishSettings? func refresh() { - + onRefresh() } } diff --git a/tealium/core/ModulesManager.swift b/tealium/core/ModulesManager.swift index fe3faa67..4b23917d 100644 --- a/tealium/core/ModulesManager.swift +++ b/tealium/core/ModulesManager.swift @@ -72,6 +72,7 @@ public class ModulesManager { } self.setupDispatchers(context: context) + self.setupCollectors(config: newValue) } } private var cachedTrackData: [String: Any]? From 67de7683b92e2907cfede6936c375fea6583ec83 Mon Sep 17 00:00:00 2001 From: Enrico Zannini Date: Thu, 21 Dec 2023 10:12:45 +0000 Subject: [PATCH 3/7] Fix warnings in tests --- .../test_tealium_core/TealiumModulesManagerTests.swift | 7 ------- 1 file changed, 7 deletions(-) diff --git a/support/tests/test_tealium_core/TealiumModulesManagerTests.swift b/support/tests/test_tealium_core/TealiumModulesManagerTests.swift index 31243b62..c301eb3f 100644 --- a/support/tests/test_tealium_core/TealiumModulesManagerTests.swift +++ b/support/tests/test_tealium_core/TealiumModulesManagerTests.swift @@ -455,15 +455,8 @@ class TealiumModulesManagerTests: XCTestCase { let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever) let connectivity = ConnectivityModule(context: modulesManager.context, delegate: nil, diskStorage: nil) { _ in } modulesManager.dispatchManager = DummyDispatchManagerSendTrack(dispatchers: nil, dispatchValidators: nil, dispatchListeners: nil, connectivityManager: connectivity, config: testTealiumConfig) - - let getCollect = {modulesManager.dispatchers.first(where: { $0.id == ModuleNames.collect})} - let getTagManagement = {modulesManager.dispatchers.first(where: { $0.id == ModuleNames.tagmanagement})} - XCTAssertNotEqual(modulesManager.dispatchers.count, 0) - XCTAssertNotEqual(modulesManager.collectors.count, 0) - defaultSettings.isEnabled = false modulesManager.didUpdate(defaultSettings) - modulesManager.sendTrack(TealiumTrackRequest(data: [:])) } waitForExpectations(timeout: 1.0) From 7efb5e15c9ec6fad49d58d8087e5a432cfa2406b Mon Sep 17 00:00:00 2001 From: Enrico Zannini Date: Thu, 21 Dec 2023 10:29:46 +0000 Subject: [PATCH 4/7] Fix tests for MacOS --- .../test_tealium_core/TealiumModulesManagerTests.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/support/tests/test_tealium_core/TealiumModulesManagerTests.swift b/support/tests/test_tealium_core/TealiumModulesManagerTests.swift index c301eb3f..17f1ad26 100644 --- a/support/tests/test_tealium_core/TealiumModulesManagerTests.swift +++ b/support/tests/test_tealium_core/TealiumModulesManagerTests.swift @@ -408,7 +408,7 @@ class TealiumModulesManagerTests: XCTestCase { sharedConfig.shouldUseRemotePublishSettings = true var defaultSettings = RemotePublishSettings() let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings) - context.config.dispatchers = [Dispatchers.Collect, Dispatchers.TagManagement] + context.config.dispatchers = [Dispatchers.Collect] TealiumQueues.backgroundSerialQueue.sync { let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever) XCTAssertNotEqual(modulesManager.dispatchers.count, 0) @@ -425,7 +425,7 @@ class TealiumModulesManagerTests: XCTestCase { sharedConfig.shouldUseRemotePublishSettings = true var defaultSettings = RemotePublishSettings() let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings) - context.config.dispatchers = [Dispatchers.Collect, Dispatchers.TagManagement] + context.config.dispatchers = [Dispatchers.Collect] TealiumQueues.backgroundSerialQueue.sync { let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever) XCTAssertNotEqual(modulesManager.dispatchers.count, 0) @@ -450,7 +450,7 @@ class TealiumModulesManagerTests: XCTestCase { sharedConfig.shouldUseRemotePublishSettings = true var defaultSettings = RemotePublishSettings() let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings) - context.config.dispatchers = [Dispatchers.Collect, Dispatchers.TagManagement] + context.config.dispatchers = [Dispatchers.Collect] TealiumQueues.backgroundSerialQueue.sync { let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever) let connectivity = ConnectivityModule(context: modulesManager.context, delegate: nil, diskStorage: nil) { _ in } @@ -470,7 +470,7 @@ class TealiumModulesManagerTests: XCTestCase { sharedConfig.shouldUseRemotePublishSettings = true var defaultSettings = RemotePublishSettings() let retriever = MockPublishSettingsRetriever(cachedSettings: defaultSettings) - context.config.dispatchers = [Dispatchers.Collect, Dispatchers.TagManagement] + context.config.dispatchers = [Dispatchers.Collect] TealiumQueues.backgroundSerialQueue.sync { let modulesManager = getModulesManager(context, remotePublishSettingsRetriever: retriever) modulesManager.remotePublishSettingsRetriever = MockPublishSettingsRetriever { From a38cd17b8bd7c49629b3f6aa343efb11cf1641cf Mon Sep 17 00:00:00 2001 From: Enrico Zannini Date: Thu, 21 Dec 2023 10:39:16 +0000 Subject: [PATCH 5/7] Fix labeler version to 4.3.0 to avoid breaking changes on new majors --- .github/workflows/pr-labeler.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-labeler.yml b/.github/workflows/pr-labeler.yml index 63235cc2..10f474f4 100644 --- a/.github/workflows/pr-labeler.yml +++ b/.github/workflows/pr-labeler.yml @@ -6,6 +6,6 @@ jobs: triage: runs-on: ubuntu-latest steps: - - uses: actions/labeler@main + - uses: actions/labeler@v4.3.0 with: repo-token: "${{ secrets.GITHUB_TOKEN }}" \ No newline at end of file From d3413e1b4ae574819ccbfe97086a974517fcf122 Mon Sep 17 00:00:00 2001 From: Enrico Zannini Date: Thu, 21 Dec 2023 11:03:34 +0000 Subject: [PATCH 6/7] Make sure to reset config's shouldUseRremotePublishSettings --- support/tests/test_tealium_core/TealiumModulesManagerTests.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/support/tests/test_tealium_core/TealiumModulesManagerTests.swift b/support/tests/test_tealium_core/TealiumModulesManagerTests.swift index 17f1ad26..177659d4 100644 --- a/support/tests/test_tealium_core/TealiumModulesManagerTests.swift +++ b/support/tests/test_tealium_core/TealiumModulesManagerTests.swift @@ -54,6 +54,7 @@ class TealiumModulesManagerTests: XCTestCase { override func setUp() { super.setUp() + sharedConfig.shouldUseRemotePublishSettings = false // Put setup code here. This method is called before the invocation of each test method in the class. } From f549ae5ad6548d7f2d6ebbf70df79731e6f49523 Mon Sep 17 00:00:00 2001 From: Enrico Zannini Date: Thu, 21 Dec 2023 11:43:12 +0000 Subject: [PATCH 7/7] increase timeout for failing tagmanagement test --- .../WKWebViewIntegrationTests.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/support/tests/test_tealium_tagmanagement/WKWebViewIntegrationTests.swift b/support/tests/test_tealium_tagmanagement/WKWebViewIntegrationTests.swift index 6b700d2e..051eef93 100644 --- a/support/tests/test_tealium_tagmanagement/WKWebViewIntegrationTests.swift +++ b/support/tests/test_tealium_tagmanagement/WKWebViewIntegrationTests.swift @@ -21,7 +21,7 @@ class WKWebViewIntegrationTests: XCTestCase { var expect: XCTestExpectation! var module: TagManagementModule! - var config: TealiumConfig! + let config = TealiumConfig(account: "testAccount", profile: "testProfile", environment: "testEnv") var mockTagmanagement: MockTagManagementWebView! static var processPool = WKProcessPool() static var wkConfig: WKWebViewConfiguration = { @@ -34,7 +34,6 @@ class WKWebViewIntegrationTests: XCTestCase { override func setUp() { super.setUp() userDefaults?.removePersistentDomain(forName: #file) - config = TealiumConfig(account: "testAccount", profile: "testProfile", environment: "testEnv") } func testWKWebViewInstance() { @@ -204,8 +203,7 @@ class WKWebViewIntegrationTests: XCTestCase { } }) }) - - wait(for: [expect], timeout: 5.0) + wait(for: [expect], timeout: 10.0) } func testModuleWithQueryParamProviderChangesUrl() {