From 3808aca664a5fefb1d7a70aa4e43e8499a016b95 Mon Sep 17 00:00:00 2001 From: Mert Buran Date: Wed, 24 Mar 2021 17:10:24 +0100 Subject: [PATCH] RUMM-1064 PR comments addressed --- .../Core/System/AppStateListener.swift | 32 ++++++---------- .../Datadog/Core/Utils/ValuePublisher.swift | 5 --- .../URLSessionTracingHandler.swift | 13 +++++-- .../Interception/TaskInterception.swift | 6 +-- .../Interception/URLSessionInterceptor.swift | 9 +---- .../Core/Utils/ValuePublisherTests.swift | 26 ------------- .../URLSessionRUMResourcesHandlerTests.swift | 12 +++--- .../Datadog/RUMMonitorTests.swift | 2 +- Tests/DatadogTests/Datadog/TracerTests.swift | 2 +- .../URLSessionTracingHandlerTests.swift | 38 +++++++++---------- .../Interception/AppStateListenerTests.swift | 24 ++++++------ .../Interception/TaskInterceptionTests.swift | 6 +-- .../URLSessionInterceptorTests.swift | 11 +++--- 13 files changed, 68 insertions(+), 118 deletions(-) diff --git a/Sources/Datadog/Core/System/AppStateListener.swift b/Sources/Datadog/Core/System/AppStateListener.swift index 2ab021ccc8..1c5134538d 100644 --- a/Sources/Datadog/Core/System/AppStateListener.swift +++ b/Sources/Datadog/Core/System/AppStateListener.swift @@ -10,18 +10,13 @@ import class UIKit.UIApplication /// A data structure to represent recorded app states in a given period of time internal struct AppStateHistory: Equatable { /// Snapshot of the app state at `date` - struct Snapshot: Equatable, Comparable { + struct Snapshot: Equatable { let isActive: Bool let date: Date - - static func < (lhs: Snapshot, rhs: Snapshot) -> Bool { - return lhs.date < rhs.date - } } var initialState: Snapshot - private(set) var changes = [Snapshot]() - // NOTE: RUMM-1064 changes.last.date > finalDate case isn't handled as not realistic + var changes = [Snapshot]() var finalDate: Date var finalState: Snapshot { return Snapshot( @@ -30,11 +25,6 @@ internal struct AppStateHistory: Equatable { ) } - mutating func add(change: Snapshot) { - changes.append(change) - changes.sort() - } - /// Limits or extrapolates app state history to the given range /// This is useful when you record between 0...3t but you are concerned of t...2t only /// - Parameter range: if outside of `initialState` and `finalState`, it extrapolates; otherwise it limits @@ -82,14 +72,14 @@ internal struct AppStateHistory: Equatable { // and no change after final state return finalState.isActive } - var active = initialState.isActive + var active = initialState for change in changes { if date < change.date { break } - active = change.isActive + active = change } - return active + return active.isActive } } @@ -134,15 +124,15 @@ internal class AppStateListener: AppStateListening { @objc private func appWillResignActive() { let now = dateProvider.currentDate() - publisher.mutateAsync { - $0.add(change: Snapshot(isActive: false, date: now)) - } + var value = publisher.currentValue + value.changes.append(Snapshot(isActive: false, date: now)) + publisher.publishAsync(value) } @objc private func appDidBecomeActive() { let now = dateProvider.currentDate() - publisher.mutateAsync { - $0.add(change: Snapshot(isActive: true, date: now)) - } + var value = publisher.currentValue + value.changes.append(Snapshot(isActive: true, date: now)) + publisher.publishAsync(value) } } diff --git a/Sources/Datadog/Core/Utils/ValuePublisher.swift b/Sources/Datadog/Core/Utils/ValuePublisher.swift index c8ae382e69..62a5b1287f 100644 --- a/Sources/Datadog/Core/Utils/ValuePublisher.swift +++ b/Sources/Datadog/Core/Utils/ValuePublisher.swift @@ -66,11 +66,6 @@ internal class ValuePublisher { concurrentQueue.async(flags: .barrier) { self.unsafeValue = newValue } } - /// Mutates the `currentValue` asynchronously, without blocking the caller thread. - func mutateAsync(_ block: @escaping (inout Value) -> Void) { - concurrentQueue.async(flags: .barrier) { block(&self.unsafeValue) } - } - /// Registers an observer that will be notified on all value changes. /// All calls to the `observer` will be synchronised using internal concurrent queue. func subscribe(_ observer: Observer) where Observer.ObservedValue == Value { diff --git a/Sources/Datadog/Tracing/AutoInstrumentation/URLSessionTracingHandler.swift b/Sources/Datadog/Tracing/AutoInstrumentation/URLSessionTracingHandler.swift index 6e02294caf..aba5df539e 100644 --- a/Sources/Datadog/Tracing/AutoInstrumentation/URLSessionTracingHandler.swift +++ b/Sources/Datadog/Tracing/AutoInstrumentation/URLSessionTracingHandler.swift @@ -7,6 +7,13 @@ import Foundation internal class URLSessionTracingHandler: URLSessionInterceptionHandler { + /// Listening to app state changes and use it to report `foreground_duration` + let appStateListener: AppStateListening + + init(appStateListener: AppStateListening) { + self.appStateListener = appStateListener + } + // MARK: - URLSessionInterceptionHandler func notify_taskInterceptionStarted(interception: TaskInterception) { @@ -68,12 +75,12 @@ internal class URLSessionTracingHandler: URLSessionInterceptionHandler { } } } - let appStateHistory = interception.appStateListener.history.take( + let appStateHistory = appStateListener.history.take( between: resourceMetrics.fetch.start...resourceMetrics.fetch.end ) // TODO: RUMM-1064 tag doesn't seem like the best fit but can't find anything better - span.setTag(key: "foreground_duration", value: "\(appStateHistory.foregroundDuration)") - span.setTag(key: "is_background", value: "\(appStateHistory.didRunInBackground)") + span.setTag(key: "foreground_duration", value: appStateHistory.foregroundDuration.toNanoseconds) + span.setTag(key: "is_background", value: appStateHistory.didRunInBackground) span.finish(at: resourceMetrics.fetch.end) } diff --git a/Sources/Datadog/URLSessionAutoInstrumentation/Interception/TaskInterception.swift b/Sources/Datadog/URLSessionAutoInstrumentation/Interception/TaskInterception.swift index 8214abafe3..a7f6f7798d 100644 --- a/Sources/Datadog/URLSessionAutoInstrumentation/Interception/TaskInterception.swift +++ b/Sources/Datadog/URLSessionAutoInstrumentation/Interception/TaskInterception.swift @@ -21,18 +21,14 @@ internal class TaskInterception { /// Trace information propagated with the task. Not available when Tracing is disabled /// or when the task was created through `URLSession.dataTask(with:url)` on some iOS13+. private(set) var spanContext: DDSpanContext? - /// Listening to app state changes and use it to report `foreground_duration` - let appStateListener: AppStateListening init( request: URLRequest, - isFirstParty: Bool, - appStateListener: AppStateListening + isFirstParty: Bool ) { self.identifier = UUID() self.request = request self.isFirstPartyRequest = isFirstParty - self.appStateListener = appStateListener } func register(metrics: ResourceMetrics) { diff --git a/Sources/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptor.swift b/Sources/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptor.swift index 41699bb4e6..1867fa64aa 100644 --- a/Sources/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptor.swift +++ b/Sources/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptor.swift @@ -23,9 +23,6 @@ public class URLSessionInterceptor: URLSessionInterceptorType { private let defaultFirstPartyURLsFilter: FirstPartyURLsFilter /// Filters internal `URLs` used by the SDK. private let internalURLsFilter: InternalURLsFilter - /// Listening to app state changes and use it to report `foreground_duration` - internal let appStateListener: AppStateListening - /// Handles resources interception. /// Depending on which instrumentation is enabled, this can be either RUM or Tracing handler sending respectively: RUM Resource or tracing Span. internal let handler: URLSessionInterceptionHandler @@ -48,7 +45,7 @@ public class URLSessionInterceptor: URLSessionInterceptorType { if configuration.instrumentRUM { handler = URLSessionRUMResourcesHandler(dateProvider: dateProvider) } else { - handler = URLSessionTracingHandler() + handler = URLSessionTracingHandler(appStateListener: appStateListener) } self.init(configuration: configuration, handler: handler, appStateListener: appStateListener) @@ -62,7 +59,6 @@ public class URLSessionInterceptor: URLSessionInterceptorType { self.defaultFirstPartyURLsFilter = FirstPartyURLsFilter(hosts: configuration.userDefinedFirstPartyHosts) self.internalURLsFilter = InternalURLsFilter(urls: configuration.sdkInternalURLs) self.handler = handler - self.appStateListener = appStateListener if configuration.instrumentTracing { self.injectTracingHeadersToFirstPartyRequests = true @@ -118,8 +114,7 @@ public class URLSessionInterceptor: URLSessionInterceptorType { let isFirstPartyRequest = self.isFirstParty(request: request, for: session) let interception = TaskInterception( request: request, - isFirstParty: isFirstPartyRequest, - appStateListener: self.appStateListener + isFirstParty: isFirstPartyRequest ) self.interceptionByTask[task] = interception diff --git a/Tests/DatadogTests/Datadog/Core/Utils/ValuePublisherTests.swift b/Tests/DatadogTests/Datadog/Core/Utils/ValuePublisherTests.swift index b59587e50f..c05ea686d5 100644 --- a/Tests/DatadogTests/Datadog/Core/Utils/ValuePublisherTests.swift +++ b/Tests/DatadogTests/Datadog/Core/Utils/ValuePublisherTests.swift @@ -55,32 +55,6 @@ class ValuePublisherTests: XCTestCase { waitForExpectations(timeout: 1, handler: nil) } - func testWhenValueMutates_itNotifiesObservers() { - let numberOfObservers = 10 - let initialValue: Int = .mockRandom() - let newValue: Int = .mockRandom() - - let expectation = self.expectation(description: "All observers received new value") - expectation.expectedFulfillmentCount = numberOfObservers - - let observers: [ValueObserverMock] = (0.. { old, new in - XCTAssertEqual(old, initialValue) - XCTAssertEqual(new, newValue) - expectation.fulfill() - } - } - - let publisher = ValuePublisher(initialValue: initialValue) - observers.forEach { publisher.subscribe($0) } - - // When - publisher.mutateAsync { $0 = newValue } - - // Then - waitForExpectations(timeout: 1, handler: nil) - } - func testWhenNonEquatableValueChanges_itNotifiesObserversOnAllChanges() { struct NonEquatableValue { let value: Int diff --git a/Tests/DatadogTests/Datadog/RUM/AutoInstrumentation/Resources/URLSessionRUMResourcesHandlerTests.swift b/Tests/DatadogTests/Datadog/RUM/AutoInstrumentation/Resources/URLSessionRUMResourcesHandlerTests.swift index e9995dfdfe..cae42320db 100644 --- a/Tests/DatadogTests/Datadog/RUM/AutoInstrumentation/Resources/URLSessionRUMResourcesHandlerTests.swift +++ b/Tests/DatadogTests/Datadog/RUM/AutoInstrumentation/Resources/URLSessionRUMResourcesHandlerTests.swift @@ -26,7 +26,7 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase { // Given var request = URLRequest(url: .mockRandom()) request.httpMethod = ["GET", "POST", "PUT", "DELETE"].randomElement()! - let taskInterception = TaskInterception(request: request, isFirstParty: .random(), appStateListener: AppStateListener.mockAny()) + let taskInterception = TaskInterception(request: request, isFirstParty: .random()) XCTAssertNil(taskInterception.spanContext) // When @@ -49,7 +49,7 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase { commandSubscriber.onCommandReceived = { _ in receiveCommand.fulfill() } // Given - let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: true, appStateListener: AppStateListener.mockAny()) + let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: true) // When handler.notify_taskInterceptionStarted(interception: taskInterception) @@ -66,7 +66,7 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase { commandSubscriber.onCommandReceived = { _ in receiveCommand.fulfill() } // Given - let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: false, appStateListener: AppStateListener.mockAny()) + let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: false) // When handler.notify_taskInterceptionStarted(interception: taskInterception) @@ -83,7 +83,7 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase { commandSubscriber.onCommandReceived = { _ in receiveCommand.fulfill() } // Given - let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: .random(), appStateListener: AppStateListener.mockAny()) + let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: .random()) taskInterception.register(spanContext: .mockWith(traceID: 1, spanID: 2)) XCTAssertNotNil(taskInterception.spanContext) @@ -108,7 +108,7 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase { } // Given - let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: .random(), appStateListener: AppStateListener.mockAny()) + let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: .random()) let resourceMetrics: ResourceMetrics = .mockAny() let resourceCompletion: ResourceCompletion = .mockWith(response: .mockResponseWith(statusCode: 200), error: nil) taskInterception.register(metrics: resourceMetrics) @@ -145,7 +145,7 @@ class URLSessionRUMResourcesHandlerTests: XCTestCase { } // Given - let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: .random(), appStateListener: AppStateListener.mockAny()) + let taskInterception = TaskInterception(request: .mockAny(), isFirstParty: .random()) let taskError = NSError(domain: "domain", code: 123, userInfo: [NSLocalizedDescriptionKey: "network error"]) let resourceMetrics: ResourceMetrics = .mockAny() let resourceCompletion: ResourceCompletion = .mockWith(response: nil, error: taskError) diff --git a/Tests/DatadogTests/Datadog/RUMMonitorTests.swift b/Tests/DatadogTests/Datadog/RUMMonitorTests.swift index bcf7e5d9b5..b46fe9aac6 100644 --- a/Tests/DatadogTests/Datadog/RUMMonitorTests.swift +++ b/Tests/DatadogTests/Datadog/RUMMonitorTests.swift @@ -1136,7 +1136,7 @@ class RUMMonitorTests: XCTestCase { XCTAssertTrue(Global.rum is DDNoopRUMMonitor) // Then - resourcesHandler.notify_taskInterceptionCompleted(interception: TaskInterception(request: .mockAny(), isFirstParty: .mockAny(), appStateListener: AppStateListener.mockAny())) + resourcesHandler.notify_taskInterceptionCompleted(interception: TaskInterception(request: .mockAny(), isFirstParty: .mockAny())) XCTAssertEqual(output.recordedLog?.status, .warn) XCTAssertEqual( output.recordedLog?.message, diff --git a/Tests/DatadogTests/Datadog/TracerTests.swift b/Tests/DatadogTests/Datadog/TracerTests.swift index 6cae5ba3b3..3e81f69bbd 100644 --- a/Tests/DatadogTests/Datadog/TracerTests.swift +++ b/Tests/DatadogTests/Datadog/TracerTests.swift @@ -1100,7 +1100,7 @@ class TracerTests: XCTestCase { XCTAssertTrue(Global.sharedTracer is DDNoopTracer) // Then - tracingHandler.notify_taskInterceptionCompleted(interception: TaskInterception(request: .mockAny(), isFirstParty: true, appStateListener: AppStateListener.mockAny())) + tracingHandler.notify_taskInterceptionCompleted(interception: TaskInterception(request: .mockAny(), isFirstParty: true)) XCTAssertEqual(output.recordedLog?.status, .warn) XCTAssertEqual( output.recordedLog?.message, diff --git a/Tests/DatadogTests/Datadog/Tracing/Autoinstrumentation/URLSessionTracingHandlerTests.swift b/Tests/DatadogTests/Datadog/Tracing/Autoinstrumentation/URLSessionTracingHandlerTests.swift index 2d4bfe1aa6..64beb8013c 100644 --- a/Tests/DatadogTests/Datadog/Tracing/Autoinstrumentation/URLSessionTracingHandlerTests.swift +++ b/Tests/DatadogTests/Datadog/Tracing/Autoinstrumentation/URLSessionTracingHandlerTests.swift @@ -7,10 +7,17 @@ import XCTest @testable import Datadog +private class MockAppStateListener: AppStateListening { + let history = AppStateHistory( + initialState: .init(isActive: true, date: .mockDecember15th2019At10AMUTC()), + finalDate: .mockDecember15th2019At10AMUTC() + 10 + ) +} + class URLSessionTracingHandlerTests: XCTestCase { private let spanOutput = SpanOutputMock() private let logOutput = LogOutputMock() - private let handler = URLSessionTracingHandler() + private let handler = URLSessionTracingHandler(appStateListener: MockAppStateListener()) override func setUp() { Global.sharedTracer = Tracer.mockWith( @@ -33,7 +40,7 @@ class URLSessionTracingHandlerTests: XCTestCase { spanOutput.onSpanRecorded = { _ in spanSentExpectation.fulfill() } // Given - let interception = TaskInterception(request: .mockAny(), isFirstParty: true, appStateListener: AppStateListener.mockAny()) + let interception = TaskInterception(request: .mockAny(), isFirstParty: true) interception.register(completion: .mockAny()) interception.register( metrics: .mockWith( @@ -70,7 +77,7 @@ class URLSessionTracingHandlerTests: XCTestCase { // Given let request: URLRequest = .mockWith(httpMethod: "POST") - let interception = TaskInterception(request: request, isFirstParty: true, appStateListener: AppStateListener.mockAny()) + let interception = TaskInterception(request: request, isFirstParty: true) interception.register(completion: .mockWith(response: .mockResponseWith(statusCode: 200), error: nil)) interception.register( metrics: .mockWith( @@ -108,7 +115,7 @@ class URLSessionTracingHandlerTests: XCTestCase { // Given let request: URLRequest = .mockWith(httpMethod: "GET") let error = NSError(domain: "domain", code: 123, userInfo: [NSLocalizedDescriptionKey: "network error"]) - let interception = TaskInterception(request: request, isFirstParty: true, appStateListener: AppStateListener.mockAny()) + let interception = TaskInterception(request: request, isFirstParty: true) interception.register(completion: .mockWith(response: nil, error: error)) interception.register( metrics: .mockWith( @@ -170,7 +177,7 @@ class URLSessionTracingHandlerTests: XCTestCase { // Given let request: URLRequest = .mockWith(httpMethod: "GET") - let interception = TaskInterception(request: request, isFirstParty: true, appStateListener: AppStateListener.mockAny()) + let interception = TaskInterception(request: request, isFirstParty: true) interception.register(completion: .mockWith(response: .mockResponseWith(statusCode: 404), error: nil)) interception.register( metrics: .mockWith( @@ -233,7 +240,7 @@ class URLSessionTracingHandlerTests: XCTestCase { spanOutput.onSpanRecorded = { _ in spanNotSentExpectation.fulfill() } // Given - let incompleteInterception = TaskInterception(request: .mockAny(), isFirstParty: true, appStateListener: AppStateListener.mockAny()) + let incompleteInterception = TaskInterception(request: .mockAny(), isFirstParty: true) // `incompleteInterception` has no metrics and no completion // When @@ -250,7 +257,7 @@ class URLSessionTracingHandlerTests: XCTestCase { spanOutput.onSpanRecorded = { _ in spanNotSentExpectation.fulfill() } // Given - let interception = TaskInterception(request: .mockAny(), isFirstParty: false, appStateListener: AppStateListener.mockAny()) + let interception = TaskInterception(request: .mockAny(), isFirstParty: false) interception.register(completion: .mockAny()) interception.register( metrics: .mockWith( @@ -271,19 +278,8 @@ class URLSessionTracingHandlerTests: XCTestCase { } func testGivenAnyInterception_itAddsAppStateInformationToSpan() throws { - class MockAppStateListener: AppStateListening { - let history = AppStateHistory( - initialState: .init(isActive: true, date: .mockDecember15th2019At10AMUTC()), - finalDate: .mockDecember15th2019At10AMUTC() + 10 - ) - } - // Given - let interception = TaskInterception( - request: .mockAny(), - isFirstParty: true, - appStateListener: MockAppStateListener() - ) + let interception = TaskInterception(request: .mockAny(), isFirstParty: true) interception.register(completion: .mockAny()) interception.register( metrics: .mockWith( @@ -299,7 +295,7 @@ class URLSessionTracingHandlerTests: XCTestCase { // Then let recordedSpan = try XCTUnwrap(spanOutput.recordedSpan) - XCTAssertEqual(recordedSpan.tags["foreground_duration"]?.encodable.value as? String, "10.0") - XCTAssertEqual(recordedSpan.tags["is_background"]?.encodable.value as? String, "false") + XCTAssertEqual(recordedSpan.tags["foreground_duration"]?.encodable.value as? UInt64, 10_000_000_000) + XCTAssertEqual(recordedSpan.tags["is_background"]?.encodable.value as? Bool, false) } } diff --git a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/AppStateListenerTests.swift b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/AppStateListenerTests.swift index 89c0be3849..574b7fa0e7 100644 --- a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/AppStateListenerTests.swift +++ b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/AppStateListenerTests.swift @@ -88,27 +88,28 @@ class AppStateHistoryTests: XCTestCase { } func testLimitingWithChanges() { - let startDate = Date.mockDecember15th2019At10AMUTC() + let startDate = Date(timeIntervalSinceReferenceDate: 0.0) let firstChanges = (0...100).map { _ in AppStateHistory.Snapshot( - isActive: Bool.random(), - date: startDate + TimeInterval.random(in: 0...1_000) + isActive: false, + date: startDate + TimeInterval.random(in: 1...1_000) ) } let lastChanges = (0...100).map { _ in AppStateHistory.Snapshot( - isActive: Bool.random(), + isActive: true, date: startDate + TimeInterval.random(in: 2_000...3_000) ) } - var history = AppStateHistory( + var allChanges = (firstChanges + lastChanges) + allChanges.append(.init(isActive: true, date: startDate + 1_200)) + allChanges.append(.init(isActive: false, date: startDate + 1_500)) + allChanges.sort { $0.date < $1.date } + let history = AppStateHistory( initialState: .init(isActive: true, date: startDate), - changes: firstChanges + lastChanges, + changes: allChanges, finalDate: startDate + 4_000 ) - history.add(change: .init(isActive: true, date: startDate + 1_200)) - history.add(change: .init(isActive: false, date: startDate + 1_500)) - history.add(change: .init(isActive: true, date: startDate + 1_700)) let limitedHistory = history.take( between: (startDate + 1_250)...(startDate + 1_750) @@ -116,10 +117,7 @@ class AppStateHistoryTests: XCTestCase { let expectedHistory = AppStateHistory( initialState: .init(isActive: true, date: startDate + 1_250), - changes: [ - .init(isActive: false, date: startDate + 1_500), - .init(isActive: true, date: startDate + 1_700), - ], + changes: [.init(isActive: false, date: startDate + 1_500)], finalDate: startDate + 1_750 ) XCTAssertEqual(limitedHistory, expectedHistory) diff --git a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/TaskInterceptionTests.swift b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/TaskInterceptionTests.swift index 148ab3d5a0..bc621709f0 100644 --- a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/TaskInterceptionTests.swift +++ b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/TaskInterceptionTests.swift @@ -10,8 +10,8 @@ import XCTest class TaskInterceptionTests: XCTestCase { func testWhenInterceptionIsCreated_itHasUniqueIdentifier() { // When - let interception1 = TaskInterception(request: .mockAny(), isFirstParty: true, appStateListener: AppStateListener.mockAny()) - let interception2 = TaskInterception(request: .mockAny(), isFirstParty: false, appStateListener: AppStateListener.mockAny()) + let interception1 = TaskInterception(request: .mockAny(), isFirstParty: true) + let interception2 = TaskInterception(request: .mockAny(), isFirstParty: false) // Then XCTAssertNotEqual(interception1.identifier, interception2.identifier) @@ -20,7 +20,7 @@ class TaskInterceptionTests: XCTestCase { } func testWhenInterceptionReceivesBothMetricsAndCompletion_itIsConsideredDone() { - let interception = TaskInterception(request: .mockAny(), isFirstParty: .mockAny(), appStateListener: AppStateListener.mockAny()) + let interception = TaskInterception(request: .mockAny(), isFirstParty: .mockAny()) // When interception.register(completion: .mockAny()) diff --git a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptorTests.swift b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptorTests.swift index 1474f85b96..fd1dd28541 100644 --- a/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptorTests.swift +++ b/Tests/DatadogTests/Datadog/URLSessionAutoInstrumentation/Interception/URLSessionInterceptorTests.swift @@ -38,20 +38,22 @@ class URLSessionInterceptorTests: XCTestCase { // MARK: - Initialization - func testGivenOnlyTracingInstrumentationEnabled_whenInitializing_itRegistersTracingHandler() { + func testGivenOnlyTracingInstrumentationEnabled_whenInitializing_itRegistersTracingHandler() throws { // Given let instrumentTracing = true let instrumentRUM = false // When + let appStateListener = AppStateListener.mockAny() let interceptor = URLSessionInterceptor( configuration: .mockWith(instrumentTracing: instrumentTracing, instrumentRUM: instrumentRUM), dateProvider: SystemDateProvider(), - appStateListener: AppStateListener.mockAny() + appStateListener: appStateListener ) // Then - XCTAssertTrue(interceptor.handler is URLSessionTracingHandler) + let tracingHandler = try XCTUnwrap(interceptor.handler as? URLSessionTracingHandler) + XCTAssert(tracingHandler.appStateListener === appStateListener) XCTAssertTrue( interceptor.injectTracingHeadersToFirstPartyRequests, "Tracing headers should be injected when only Tracing instrumentation is enabled." @@ -332,9 +334,6 @@ class URLSessionInterceptorTests: XCTestCase { startedInterceptions.contains { $0.request.url == alternativeFirstPartyRequest.url && $0.spanContext != nil }, "Interception should be started and span context should be set for custom 1st party request." ) - for interception in startedInterceptions { - XCTAssert(interception.appStateListener === interceptor.appStateListener) - } let completedInterceptions = handler.completedInterceptions XCTAssertEqual(completedInterceptions.count, 3)