Skip to content

Commit

Permalink
fix: dispatch identify interceptor callbacks on correct queue
Browse files Browse the repository at this point in the history
  • Loading branch information
crleona committed Jun 6, 2024
1 parent 7281ec5 commit 0ae55ff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Sources/Amplitude/Plugins/AmplitudeDestinationPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public class AmplitudeDestinationPlugin: DestinationPlugin {
pipeline = EventPipeline(amplitude: amplitude)
identifyInterceptor = IdentifyInterceptor(
configuration: amplitude.configuration,
pipeline: pipeline!
pipeline: pipeline!,
queue: amplitude.trackingQueue
)
pipeline?.start()

Expand Down
5 changes: 4 additions & 1 deletion Sources/Amplitude/Utilities/IdentifyInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class IdentifyInterceptor {
private var identifyTransferTimer: QueueTimer?
private let identifyBatchIntervalMillis: Int
private var lastIdentity: Identity?
private let queue: DispatchQueue

private lazy var storage: any Storage = {
return self.configuration.identifyStorageProvider
Expand All @@ -30,6 +31,7 @@ public class IdentifyInterceptor {
init(
configuration: Configuration,
pipeline: EventPipeline,
queue: DispatchQueue,
identifyBatchIntervalMillis: Int = Constants.Configuration.IDENTIFY_BATCH_INTERVAL_MILLIS
) {
self.configuration = configuration
Expand All @@ -40,6 +42,7 @@ public class IdentifyInterceptor {
}
self.identifyBatchIntervalMillis = max(identifyBatchIntervalMillis, Constants.MIN_IDENTIFY_BATCH_INTERVAL_MILLIS)
self.lastIdentity = Identity(nil, nil)
self.queue = queue
}

public func intercept(event: BaseEvent) -> BaseEvent? {
Expand Down Expand Up @@ -176,7 +179,7 @@ public class IdentifyInterceptor {
return
}

identifyTransferTimer = QueueTimer(interval: getIdentifyBatchInterval(), once: true) { [weak self] in
identifyTransferTimer = QueueTimer(interval: getIdentifyBatchInterval(), once: true, queue: queue) { [weak self] in
let transferInterceptedIdentifyEvent = self?.transferInterceptedIdentifyEvent
self?.identifyTransferTimer = nil
transferInterceptedIdentifyEvent?()
Expand Down
16 changes: 10 additions & 6 deletions Tests/AmplitudeTests/Utilities/IdentifyInterceptorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class IdentifyInterceptorTests: XCTestCase {
private var configuration: Configuration!
private var pipeline: EventPipeline!
private var mockPathCreation: MockPathCreation!
private var amplitude: Amplitude!

override func setUp() {
super.setUp()
Expand All @@ -25,15 +26,16 @@ final class IdentifyInterceptorTests: XCTestCase {
identifyBatchIntervalMillis: identifyBatchIntervalMillis,
offline: NetworkConnectivityCheckerPlugin.Disabled
)
let amplitude = Amplitude(configuration: configuration)
amplitude = Amplitude(configuration: configuration)
mockPathCreation = MockPathCreation()
amplitude.add(plugin: NetworkConnectivityCheckerPlugin(pathCreation: mockPathCreation))
httpClient = FakeHttpClient(configuration: configuration, diagnostics: configuration.diagonostics)
pipeline = EventPipeline(amplitude: amplitude)
pipeline.httpClient = httpClient
interceptor = TestIdentifyInterceptor(
configuration: configuration,
pipeline: pipeline
pipeline: pipeline,
queue: amplitude.trackingQueue
)
interceptor.setIdentifyBatchInterval(identifyBatchIntervalMillis)
}
Expand All @@ -44,19 +46,19 @@ final class IdentifyInterceptorTests: XCTestCase {

func testMinimumIdentifyBatchInterval() {
var identifyBatchIntervalMillis = 0
var interceptor1 = IdentifyInterceptor(configuration: configuration, pipeline: pipeline, identifyBatchIntervalMillis: identifyBatchIntervalMillis)
var interceptor1 = IdentifyInterceptor(configuration: configuration, pipeline: pipeline, queue: .main, identifyBatchIntervalMillis: identifyBatchIntervalMillis)
XCTAssertEqual(interceptor1.getIdentifyBatchInterval(), TimeInterval.milliseconds(Constants.MIN_IDENTIFY_BATCH_INTERVAL_MILLIS))

identifyBatchIntervalMillis = Constants.MIN_IDENTIFY_BATCH_INTERVAL_MILLIS - 1
interceptor1 = IdentifyInterceptor(configuration: configuration, pipeline: pipeline, identifyBatchIntervalMillis: identifyBatchIntervalMillis)
interceptor1 = IdentifyInterceptor(configuration: configuration, pipeline: pipeline, queue: .main, identifyBatchIntervalMillis: identifyBatchIntervalMillis)
XCTAssertEqual(interceptor1.getIdentifyBatchInterval(), TimeInterval.milliseconds(Constants.MIN_IDENTIFY_BATCH_INTERVAL_MILLIS))

identifyBatchIntervalMillis = Constants.MIN_IDENTIFY_BATCH_INTERVAL_MILLIS
interceptor1 = IdentifyInterceptor(configuration: configuration, pipeline: pipeline, identifyBatchIntervalMillis: identifyBatchIntervalMillis)
interceptor1 = IdentifyInterceptor(configuration: configuration, pipeline: pipeline, queue: .main, identifyBatchIntervalMillis: identifyBatchIntervalMillis)
XCTAssertEqual(interceptor1.getIdentifyBatchInterval(), TimeInterval.milliseconds(identifyBatchIntervalMillis))

identifyBatchIntervalMillis = Constants.MIN_IDENTIFY_BATCH_INTERVAL_MILLIS * 2
interceptor1 = IdentifyInterceptor(configuration: configuration, pipeline: pipeline, identifyBatchIntervalMillis: identifyBatchIntervalMillis)
interceptor1 = IdentifyInterceptor(configuration: configuration, pipeline: pipeline, queue: .main, identifyBatchIntervalMillis: identifyBatchIntervalMillis)
XCTAssertEqual(interceptor1.getIdentifyBatchInterval(), TimeInterval.milliseconds(identifyBatchIntervalMillis))
}

Expand Down Expand Up @@ -385,6 +387,7 @@ final class IdentifyInterceptorTests: XCTestCase {
// intercepted event should be transferred on batch interval
let dummy1Expectation = expectation(description: "dummy1")
_ = XCTWaiter.wait(for: [dummy1Expectation], timeout: TimeInterval.seconds(Int(Self.IDENTIFY_UPLOAD_INTERVAL_SECONDS + 1)))
amplitude.waitForTrackingQueue()
XCTAssertEqual(pipeline.eventCount, 1)
interceptedIdentifies = identifyStorage.events()
XCTAssertEqual(interceptedIdentifies.count, 0)
Expand All @@ -409,6 +412,7 @@ final class IdentifyInterceptorTests: XCTestCase {
// intercepted event should be transferred on batch interval
let dummy2Expectation = expectation(description: "dummy2")
_ = XCTWaiter.wait(for: [dummy2Expectation], timeout: TimeInterval.seconds(Int(Self.IDENTIFY_UPLOAD_INTERVAL_SECONDS + 1)))
amplitude.waitForTrackingQueue()
XCTAssertEqual(pipeline.eventCount, 2)
interceptedIdentifies = identifyStorage.events()
XCTAssertEqual(interceptedIdentifies.count, 0)
Expand Down

0 comments on commit 0ae55ff

Please sign in to comment.