Skip to content

Commit

Permalink
RUMM-1064 AppStateListener added to TaskInterception
Browse files Browse the repository at this point in the history
  • Loading branch information
buranmert committed Mar 12, 2021
1 parent e210daf commit 9276c8d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ internal class URLSessionTracingHandler: URLSessionInterceptionHandler {
}
}
}
let appStateHistory = interception.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.finish(at: resourceMetrics.fetch.end)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal struct AppStateHistory: Equatable {
}

internal protocol AppStateListening {
var appStateHistory: AppStateHistory { get }
var history: AppStateHistory { get }
}

internal class AppStateListener: AppStateListening {
Expand All @@ -105,7 +105,7 @@ internal class AppStateListener: AppStateListening {

private let dateProvider: DateProvider
private var _appStateHistory: AppStateHistory
var appStateHistory: AppStateHistory {
var history: AppStateHistory {
objc_sync_enter(self)
defer { objc_sync_exit(self) }
_appStateHistory.finalDate = dateProvider.currentDate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ 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?

init(request: URLRequest, isFirstParty: Bool) {
/// Listening to app state changes and use it to report `foreground_duration`
let appStateListener: AppStateListening

init(
request: URLRequest,
isFirstParty: Bool,
appStateListener: AppStateListening = AppStateListener()
) {
self.identifier = UUID()
self.request = request
self.isFirstPartyRequest = isFirstParty
// TODO: RUMM-1064
// create AppStateListening.build()
// take between resource.fetch.start/end
// add foregroundDuration to the span
self.appStateListener = appStateListener
}

func register(metrics: ResourceMetrics) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ class AppStateListenerTests: XCTestCase {

NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
let history = listener.appStateHistory

let expected = AppStateHistory(
initialState: .init(isActive: true, date: startDate),
Expand All @@ -145,7 +144,7 @@ class AppStateListenerTests: XCTestCase {
],
finalDate: startDate + 3.0
)
XCTAssertEqual(history, expected)
XCTAssertEqual(listener.history, expected)
}

func testWhenAppBecomeActiveAndResignActive_thenAppStateHistoryIsRecorded() {
Expand All @@ -155,7 +154,6 @@ class AppStateListenerTests: XCTestCase {
)
NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil)
let history = listener.appStateHistory

let expected = AppStateHistory(
initialState: .init(isActive: true, date: startDate),
Expand All @@ -165,16 +163,16 @@ class AppStateListenerTests: XCTestCase {
],
finalDate: startDate + 3.0
)
XCTAssertEqual(history, expected)
XCTAssertEqual(listener.history, expected)
}

func testWhenAppStateHistoryIsRetrieved_thenFinalDateOfHistoryChanges() {
let startDate = Date.mockDecember15th2019At10AMUTC()
let listener = AppStateListener(
dateProvider: RelativeDateProvider(startingFrom: startDate, advancingBySeconds: 1.0)
)
let history1 = listener.appStateHistory
let history2 = listener.appStateHistory
let history1 = listener.history
let history2 = listener.history

XCTAssertEqual(history2.finalState.date.timeIntervalSince(history1.finalState.date), 1.0)
}
Expand All @@ -191,7 +189,7 @@ class AppStateListenerTests: XCTestCase {
object: nil
)
// read
XCTAssertFalse(listener.appStateHistory.changes.isEmpty)
XCTAssertFalse(listener.history.changes.isEmpty)
}
}

Expand All @@ -212,7 +210,7 @@ class AppStateListenerTests: XCTestCase {
object: nil
)
// read
XCTAssertFalse(listeners[iteration].appStateHistory.changes.isEmpty)
XCTAssertFalse(listeners[iteration].history.changes.isEmpty)
}
}
}

0 comments on commit 9276c8d

Please sign in to comment.