From 205d7aee612b8cea6c53e0588b813681699451a4 Mon Sep 17 00:00:00 2001 From: Miranda Wilson Date: Fri, 29 Sep 2023 16:08:23 +0100 Subject: [PATCH] Respond to review comments --- Sources/Core/Tracker/Tracker.swift | 10 +++++----- Sources/Core/Utils/DispatchQueueWrapper.swift | 4 ++++ Sources/Core/Utils/DispatchQueueWrapperProtocol.swift | 1 + Tests/Configurations/TestTrackerConfiguration.swift | 4 ++-- Tests/Configurations/TestTrackerController.swift | 6 +++--- Tests/Utils/MockDispatchQueueWrapper.swift | 4 ++++ 6 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Sources/Core/Tracker/Tracker.swift b/Sources/Core/Tracker/Tracker.swift index 7f03e944e..0e52fb775 100644 --- a/Sources/Core/Tracker/Tracker.swift +++ b/Sources/Core/Tracker/Tracker.swift @@ -174,7 +174,7 @@ class Tracker: NSObject { return _deepLinkContext } set(deepLinkContext) { - serialQueue.async { + serialQueue.sync { self._deepLinkContext = deepLinkContext if deepLinkContext { self.addOrReplace(stateMachine: DeepLinkStateMachine()) @@ -191,7 +191,7 @@ class Tracker: NSObject { return _screenContext } set(screenContext) { - serialQueue.async { + serialQueue.sync { self._screenContext = screenContext if screenContext { self.addOrReplace(stateMachine: ScreenStateMachine()) @@ -240,7 +240,7 @@ class Tracker: NSObject { return _lifecycleEvents } set(lifecycleEvents) { - serialQueue.async { + serialQueue.sync { self._lifecycleEvents = lifecycleEvents if lifecycleEvents { self.addOrReplace(stateMachine: LifecycleStateMachine()) @@ -461,9 +461,9 @@ class Tracker: NSObject { if let payload = self.payload(with: trackerEvent) { emitter.addPayload(toBuffer: payload) stateManager.afterTrack(event: trackerEvent) - return + } else { + logDebug(message: "Event not tracked due to filtering") } - logDebug(message: "Event not tracked due to filtering") } func payload(with event: TrackerEvent) -> Payload? { diff --git a/Sources/Core/Utils/DispatchQueueWrapper.swift b/Sources/Core/Utils/DispatchQueueWrapper.swift index cceb2530f..dff08b648 100644 --- a/Sources/Core/Utils/DispatchQueueWrapper.swift +++ b/Sources/Core/Utils/DispatchQueueWrapper.swift @@ -20,6 +20,10 @@ class DispatchQueueWrapper: DispatchQueueWrapperProtocol { queue = DispatchQueue(label: label) } + func sync(_ callback: @escaping () -> Void) { + queue.sync(execute: callback) + } + func async(_ callback: @escaping () -> Void) { queue.async(execute: callback) } diff --git a/Sources/Core/Utils/DispatchQueueWrapperProtocol.swift b/Sources/Core/Utils/DispatchQueueWrapperProtocol.swift index e91802417..db52ffcd0 100644 --- a/Sources/Core/Utils/DispatchQueueWrapperProtocol.swift +++ b/Sources/Core/Utils/DispatchQueueWrapperProtocol.swift @@ -14,5 +14,6 @@ import Foundation protocol DispatchQueueWrapperProtocol: AnyObject { + func sync(_ callback: @escaping () -> Void) func async(_ callback: @escaping () -> Void) } diff --git a/Tests/Configurations/TestTrackerConfiguration.swift b/Tests/Configurations/TestTrackerConfiguration.swift index c1b41e47c..110fe6cd3 100644 --- a/Tests/Configurations/TestTrackerConfiguration.swift +++ b/Tests/Configurations/TestTrackerConfiguration.swift @@ -186,10 +186,10 @@ class TestTrackerConfiguration: XCTestCase { let tracker = Snowplow.createTracker(namespace: "namespace", network: networkConfig, configurations: [trackerConfig, sessionConfig]) _ = tracker?.track(Timing(category: "cat", variable: "var", timing: 123)) - Thread.sleep(forTimeInterval: 3) + Thread.sleep(forTimeInterval: 0.1) tracker?.session?.startNewSession() _ = tracker?.track(Timing(category: "cat", variable: "var", timing: 123)) - Thread.sleep(forTimeInterval: 3) + Thread.sleep(forTimeInterval: 0.1) wait(for: [expectation], timeout: 10) } diff --git a/Tests/Configurations/TestTrackerController.swift b/Tests/Configurations/TestTrackerController.swift index 5c2353f6c..201df62ff 100644 --- a/Tests/Configurations/TestTrackerController.swift +++ b/Tests/Configurations/TestTrackerController.swift @@ -50,19 +50,19 @@ class TestTrackerController: XCTestCase { tracker?.emitter?.pause() _ = tracker?.track(Structured(category: "c", action: "a")) - Thread.sleep(forTimeInterval: 3) + Thread.sleep(forTimeInterval: 0.1) let sessionIdBefore = tracker?.session?.sessionId tracker?.userAnonymisation = true _ = tracker?.track(Structured(category: "c", action: "a")) - Thread.sleep(forTimeInterval: 3) + Thread.sleep(forTimeInterval: 0.1) let sessionIdAnonymous = tracker?.session?.sessionId XCTAssertFalse((sessionIdBefore == sessionIdAnonymous)) tracker?.userAnonymisation = false _ = tracker?.track(Structured(category: "c", action: "a")) - Thread.sleep(forTimeInterval: 3) + Thread.sleep(forTimeInterval: 0.1) let sessionIdNotAnonymous = tracker?.session?.sessionId XCTAssertFalse((sessionIdAnonymous == sessionIdNotAnonymous)) diff --git a/Tests/Utils/MockDispatchQueueWrapper.swift b/Tests/Utils/MockDispatchQueueWrapper.swift index 437216430..b4d39eee0 100644 --- a/Tests/Utils/MockDispatchQueueWrapper.swift +++ b/Tests/Utils/MockDispatchQueueWrapper.swift @@ -21,6 +21,10 @@ class MockDispatchQueueWrapper: DispatchQueueWrapperProtocol { queue = DispatchQueue(label: label) } + func sync(_ callback: @escaping () -> Void) { + queue.sync(execute: callback) + } + func async(_ callback: @escaping () -> Void) { // execute synchronously! queue.sync(execute: callback)