Skip to content

Commit 5719062

Browse files
authored
Use .queued event priority by default (#4335)
* Add ability to change telemetry events priority, use .queued by default
1 parent 4535745 commit 5719062

File tree

8 files changed

+210
-27
lines changed

8 files changed

+210
-27
lines changed

MapboxNavigation.xcodeproj/xcshareddata/xcschemes/MapboxCoreNavigation.xcscheme

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES"
30+
codeCoverageEnabled = "YES"
3031
onlyGenerateCoverageForSpecifiedTargets = "YES">
3132
<MacroExpansion>
3233
<BuildableReference

Sources/MapboxCoreNavigation/Feedback/NavigationEventsManager.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,32 +366,32 @@ open class NavigationEventsManager {
366366

367367
public func sendCarPlayConnectEvent() {
368368
let attributes = eventAttributes(type: .carplayConnect)
369-
eventsAPI.sendImmediateEvent(with: attributes)
369+
sendEvent(with: attributes)
370370
}
371371

372372
public func sendCarPlayDisconnectEvent() {
373373
let attributes = eventAttributes(type: .carplayDisconnect)
374-
eventsAPI.sendImmediateEvent(with: attributes)
374+
sendEvent(with: attributes)
375375
}
376376

377377
func sendRouteRetrievalEvent() {
378378
guard let attributes = try? navigationRouteRetrievalEvent()?.asDictionary() else { return }
379-
eventsAPI.sendImmediateEvent(with: attributes)
379+
sendEvent(with: attributes)
380380
}
381381

382382
func sendDepartEvent() {
383383
guard let attributes = try? navigationDepartEvent()?.asDictionary() else { return }
384-
eventsAPI.sendImmediateEvent(with: attributes)
384+
sendEvent(with: attributes)
385385
}
386386

387387
func sendArriveEvent() {
388388
guard let attributes = try? navigationArriveEvent()?.asDictionary() else { return }
389-
eventsAPI.sendImmediateEvent(with: attributes)
389+
sendEvent(with: attributes)
390390
}
391391

392392
func sendCancelEvent(rating: Int? = nil, comment: String? = nil) {
393393
guard let attributes = try? navigationCancelEvent(rating: rating, comment: comment)?.asDictionary() else { return }
394-
eventsAPI.sendImmediateEvent(with: attributes)
394+
sendEvent(with: attributes)
395395
}
396396

397397
func sendPassiveNavigationStart() {
@@ -401,12 +401,12 @@ open class NavigationEventsManager {
401401
}
402402

403403
guard let attributes = try? passiveNavigationEvent(type: .start)?.asDictionary() else { return }
404-
eventsAPI.sendImmediateEvent(with: attributes)
404+
sendEvent(with: attributes)
405405
}
406406

407407
func sendPassiveNavigationStop() {
408408
guard let attributes = try? passiveNavigationEvent(type: .stop)?.asDictionary() else { return }
409-
eventsAPI.sendImmediateEvent(with: attributes)
409+
sendEvent(with: attributes)
410410
}
411411

412412
func sendFeedbackEvents(_ events: [CoreFeedbackEvent]) {
@@ -417,7 +417,7 @@ open class NavigationEventsManager {
417417
}
418418

419419
let eventDictionary = navigationFeedbackEventWithLocationsAdded(event: event)
420-
eventsAPI.sendImmediateEvent(with: eventDictionary)
420+
sendEvent(with: eventDictionary)
421421
}
422422
}
423423

@@ -541,4 +541,12 @@ open class NavigationEventsManager {
541541
func record(_ locations: [CLLocation]) {
542542
locations.forEach(sessionState.pastLocations.push(_:))
543543
}
544+
545+
private func sendEvent(with attributes: [String : Any]) {
546+
if shouldDelayEvents() {
547+
eventsAPI.sendQueuedEvent(with: attributes)
548+
} else {
549+
eventsAPI.sendImmediateEvent(with: attributes)
550+
}
551+
}
544552
}

Tests/MapboxCoreNavigationIntegrationTests/MapboxNavigationServiceIntegrationTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,15 @@ class MapboxNavigationServiceIntegrationTests: TestCase {
527527

528528
// It queues and flushes a Depart event
529529
let eventsManagerSpy = navigation.eventsManager as! NavigationEventsManagerSpy
530-
XCTAssertTrue(eventsManagerSpy.hasImmediateEvent(with: EventType.depart.rawValue))
530+
XCTAssertTrue(eventsManagerSpy.hasQueuedEvent(with: EventType.depart.rawValue))
531531
// When at a valid location just before the last location
532532
XCTAssertTrue(delegate.recentMessages.contains("navigationService(_:willArriveAt:after:distance:)"))
533533
// It tells the delegate that the user did arrive
534534
XCTAssertTrue(delegate.recentMessages.contains("navigationService(_:didArriveAt:)"))
535535

536536
// It enqueues and flushes an arrival event
537537
let expectedEventName = EventType.arrive.rawValue
538-
XCTAssertTrue(eventsManagerSpy.hasImmediateEvent(with: expectedEventName))
538+
XCTAssertTrue(eventsManagerSpy.hasQueuedEvent(with: expectedEventName))
539539
}
540540

541541
func testNoReroutesAfterArriving() {
@@ -559,7 +559,7 @@ class MapboxNavigationServiceIntegrationTests: TestCase {
559559

560560
let eventsManagerSpy = navigation.eventsManager as! NavigationEventsManagerSpy
561561
expectation(description: "Depart Event Flushed") {
562-
eventsManagerSpy.hasImmediateEvent(with: EventType.depart.rawValue)
562+
eventsManagerSpy.hasQueuedEvent(with: EventType.depart.rawValue)
563563
}
564564

565565
// MARK: It tells the delegate that the user did arrive
@@ -594,7 +594,7 @@ class MapboxNavigationServiceIntegrationTests: TestCase {
594594

595595
// It enqueues and flushes an arrival event
596596
let expectedEventName = EventType.arrive.rawValue
597-
XCTAssertTrue(eventsManagerSpy.hasImmediateEvent(with: expectedEventName))
597+
XCTAssertTrue(eventsManagerSpy.hasQueuedEvent(with: expectedEventName))
598598
}
599599

600600
func testRouteControllerDoesNotHaveRetainCycle() {

Tests/MapboxCoreNavigationTests/MapboxCoreNavigationTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ func makeRoute() -> Route {
2525
func makeRouteWithNoDistance() -> Route {
2626
return Fixture.route(from: jsonFileNameEmptyDistance, options: routeOptions)
2727
}
28+
29+
func makeRouteProgress() -> RouteProgress {
30+
return RouteProgress(route: makeRoute(), options: routeOptions)
31+
}

Tests/MapboxCoreNavigationTests/NavigationEventsManagerTests.swift

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,178 @@ import CoreLocation
33
@testable import TestHelper
44
@testable import MapboxCoreNavigation
55

6+
final class ActiveNavigationEventsManagerDataSourceSpy: ActiveNavigationEventsManagerDataSource {
7+
var routeProgress: MapboxCoreNavigation.RouteProgress = makeRouteProgress()
8+
var router: MapboxCoreNavigation.Router = RouterSpy(indexedRouteResponse: IndexedRouteResponse(routeResponse: makeRouteResponse(),
9+
routeIndex: 0),
10+
dataSource: RouterDataSourceSpy())
11+
var desiredAccuracy: CLLocationAccuracy = -1
12+
var locationManagerType: MapboxCoreNavigation.NavigationLocationManager.Type = NavigationLocationManagerSpy.self
13+
}
14+
15+
final class PassiveNavigationEventsManagerDataSourceSpy: PassiveNavigationEventsManagerDataSource {
16+
var rawLocation: CLLocation? = nil
17+
var locationManagerType: MapboxCoreNavigation.NavigationLocationManager.Type = NavigationLocationManagerSpy.self
18+
}
19+
620
class NavigationEventsManagerTests: TestCase {
7-
21+
private var eventManager: NavigationEventsManager!
22+
private var eventsAPI: EventsAPIMock!
23+
private var activeNavigationDataSource: ActiveNavigationEventsManagerDataSourceSpy!
24+
private var passiveNavigationDataSource: PassiveNavigationEventsManagerDataSourceSpy!
25+
26+
override func setUp() {
27+
super.setUp()
28+
eventsAPI = EventsAPIMock()
29+
activeNavigationDataSource = ActiveNavigationEventsManagerDataSourceSpy()
30+
passiveNavigationDataSource = PassiveNavigationEventsManagerDataSourceSpy()
31+
eventManager = NavigationEventsManager(activeNavigationDataSource: activeNavigationDataSource,
32+
passiveNavigationDataSource: passiveNavigationDataSource,
33+
accessToken: "fake token",
34+
eventsAPI: eventsAPI)
35+
}
36+
37+
func testSendCarPlayConnectEventIfDelaysFlushing() {
38+
eventManager.sendCarPlayConnectEvent()
39+
XCTAssertTrue(eventsAPI.hasQueuedEvent(with: EventType.carplayConnect.rawValue))
40+
}
41+
42+
func testSendCarPlayConnectEventIfDoesNotDelayFlushing() {
43+
eventManager.delaysEventFlushing = false
44+
eventManager.sendCarPlayConnectEvent()
45+
XCTAssertTrue(eventsAPI.hasImmediateEvent(with: EventType.carplayConnect.rawValue))
46+
}
47+
48+
func testSendCarPlayDisconnectEventIfDelaysFlushing() {
49+
eventManager.sendCarPlayDisconnectEvent()
50+
XCTAssertTrue(eventsAPI.hasQueuedEvent(with: EventType.carplayDisconnect.rawValue))
51+
}
52+
53+
func testSendCarPlayDisconnectEventIfDoesNotDelayFlushing() {
54+
eventManager.delaysEventFlushing = false
55+
eventManager.sendCarPlayDisconnectEvent()
56+
XCTAssertTrue(eventsAPI.hasImmediateEvent(with: EventType.carplayDisconnect.rawValue))
57+
}
58+
59+
func testSendRouteRetrievalEventIfDelaysFlushing() {
60+
eventManager.reportReroute(progress: makeRouteProgress(), proactive: false)
61+
62+
eventManager.sendRouteRetrievalEvent()
63+
XCTAssertTrue(eventsAPI.hasQueuedEvent(with: EventType.routeRetrieval.rawValue))
64+
}
65+
66+
func testSendRouteRetrievalEventIfDoesNotDelayFlushing() {
67+
eventManager.reportReroute(progress: makeRouteProgress(), proactive: false)
68+
eventManager.delaysEventFlushing = false
69+
70+
eventManager.sendRouteRetrievalEvent()
71+
XCTAssertTrue(eventsAPI.hasImmediateEvent(with: EventType.routeRetrieval.rawValue))
72+
}
73+
74+
func testNoDepartEventIfNoDatasource() {
75+
eventManager.activeNavigationDataSource = nil
76+
eventManager.sendDepartEvent()
77+
XCTAssertFalse(eventsAPI.hasQueuedEvent(with: EventType.depart.rawValue))
78+
}
79+
80+
func testSendDepartEventIfDelaysFlushing() {
81+
eventManager.sendDepartEvent()
82+
XCTAssertTrue(eventsAPI.hasQueuedEvent(with: EventType.depart.rawValue))
83+
}
84+
85+
func testSendDepartEventIfDoesNotDelayFlushing() {
86+
eventManager.delaysEventFlushing = false
87+
eventManager.sendDepartEvent()
88+
XCTAssertTrue(eventsAPI.hasImmediateEvent(with: EventType.depart.rawValue))
89+
}
90+
91+
func testNoArriveEventIfNoDatasource() {
92+
eventManager.activeNavigationDataSource = nil
93+
eventManager.sendArriveEvent()
94+
XCTAssertFalse(eventsAPI.hasQueuedEvent(with: EventType.arrive.rawValue))
95+
}
96+
97+
func testSendArriveEventIfDelaysFlushing() {
98+
eventManager.sendArriveEvent()
99+
XCTAssertTrue(eventsAPI.hasQueuedEvent(with: EventType.arrive.rawValue))
100+
}
101+
102+
func testSendArriveEventIfDoesNotDelayFlushing() {
103+
eventManager.delaysEventFlushing = false
104+
eventManager.sendArriveEvent()
105+
XCTAssertTrue(eventsAPI.hasImmediateEvent(with: EventType.arrive.rawValue))
106+
}
107+
108+
func testNoCancelEventIfNoDatasource() {
109+
eventManager.activeNavigationDataSource = nil
110+
eventManager.sendCancelEvent()
111+
XCTAssertFalse(eventsAPI.hasQueuedEvent(with: EventType.cancel.rawValue))
112+
}
113+
114+
func testSendCancelEventIfDelaysFlushing() {
115+
eventManager.sendCancelEvent()
116+
XCTAssertTrue(eventsAPI.hasQueuedEvent(with: EventType.cancel.rawValue))
117+
}
118+
119+
func testSendCancelEventIfDoesNotDelayFlushing() {
120+
eventManager.delaysEventFlushing = false
121+
eventManager.sendCancelEvent()
122+
XCTAssertTrue(eventsAPI.hasImmediateEvent(with: EventType.cancel.rawValue))
123+
}
124+
125+
func testNoPassiveNavigationStartEventIfNoDatasource() {
126+
let eventManager = NavigationEventsManager(activeNavigationDataSource: activeNavigationDataSource,
127+
passiveNavigationDataSource: nil,
128+
accessToken: "fake token",
129+
eventsAPI: eventsAPI)
130+
eventManager.sendPassiveNavigationStart()
131+
XCTAssertFalse(eventsAPI.hasQueuedEvent(with: EventType.freeDrive.rawValue))
132+
}
133+
134+
func testSendPassiveNavigationStartEventIfDelaysFlushing() {
135+
eventManager.sendPassiveNavigationStart()
136+
XCTAssertTrue(eventsAPI.hasQueuedEvent(with: EventType.freeDrive.rawValue))
137+
}
138+
139+
func testSendPassiveNavigationStartEventIfDoesNotDelayFlushing() {
140+
eventManager.delaysEventFlushing = false
141+
eventManager.sendPassiveNavigationStart()
142+
XCTAssertTrue(eventsAPI.hasImmediateEvent(with: EventType.freeDrive.rawValue))
143+
}
144+
145+
func testNoPassiveNavigationStopEventIfNoDatasource() {
146+
let eventManager = NavigationEventsManager(activeNavigationDataSource: activeNavigationDataSource,
147+
passiveNavigationDataSource: nil,
148+
accessToken: "fake token",
149+
eventsAPI: eventsAPI)
150+
eventManager.sendPassiveNavigationStop()
151+
XCTAssertFalse(eventsAPI.hasQueuedEvent(with: EventType.freeDrive.rawValue))
152+
}
153+
154+
func testSendPassiveNavigationStopEventIfDelaysFlushing() {
155+
eventManager.sendPassiveNavigationStop()
156+
XCTAssertTrue(eventsAPI.hasQueuedEvent(with: EventType.freeDrive.rawValue))
157+
}
158+
159+
func testSendPassiveNavigationStopEventIfDoesNotDelayFlushing() {
160+
eventManager.delaysEventFlushing = false
161+
eventManager.sendPassiveNavigationStop()
162+
XCTAssertTrue(eventsAPI.hasImmediateEvent(with: EventType.freeDrive.rawValue))
163+
}
164+
165+
func testSendFeedbackEventIfDelaysFlushing() {
166+
let coreEvent = CoreFeedbackEvent(timestamp: Date(), eventDictionary: ["event":"feedback"])
167+
eventManager.sendFeedbackEvents([coreEvent])
168+
XCTAssertTrue(eventsAPI.hasQueuedEvent(with: "feedback"))
169+
}
170+
171+
func testSendFeedbackEventIfDoesNotDelayFlushing() {
172+
eventManager.delaysEventFlushing = false
173+
let coreEvent = CoreFeedbackEvent(timestamp: Date(), eventDictionary: ["event":"feedback"])
174+
eventManager.sendFeedbackEvents([coreEvent])
175+
XCTAssertTrue(eventsAPI.hasImmediateEvent(with: "feedback"))
176+
}
177+
8178
func skipped_testDepartRerouteArrive() {
9179

10180
let firstRouteOptions = NavigationRouteOptions(coordinates: [

Tests/MapboxCoreNavigationTests/NavigationServiceTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class NavigationServiceTests: TestCase {
129129

130130
XCTAssertTrue(locationManager.startUpdatingHeadingCalled)
131131
XCTAssertTrue(locationManager.startUpdatingLocationCalled)
132-
XCTAssertTrue(eventsManager.hasImmediateEvent(with: EventType.routeRetrieval.rawValue))
132+
XCTAssertTrue(eventsManager.hasQueuedEvent(with: EventType.routeRetrieval.rawValue))
133133

134134
XCTAssertTrue(routerSpy.delegate === service)
135135
waitForExpectations(timeout: expectationsTimeout)
@@ -144,7 +144,7 @@ class NavigationServiceTests: TestCase {
144144

145145
XCTAssertTrue(locationManager.startUpdatingHeadingCalled)
146146
XCTAssertTrue(locationManager.startUpdatingLocationCalled)
147-
XCTAssertTrue(eventsManager.hasImmediateEvent(with: EventType.routeRetrieval.rawValue))
147+
XCTAssertTrue(eventsManager.hasQueuedEvent(with: EventType.routeRetrieval.rawValue))
148148

149149
XCTAssertTrue(routerSpy.delegate === service)
150150
waitForExpectations(timeout: expectationsTimeout)
@@ -256,7 +256,7 @@ class NavigationServiceTests: TestCase {
256256

257257
XCTAssertTrue(locationManager.stopUpdatingHeadingCalled)
258258
XCTAssertTrue(locationManager.stopUpdatingLocationCalled)
259-
XCTAssertFalse(eventsManager.hasImmediateEvent(with: EventType.cancel.rawValue))
259+
XCTAssertFalse(eventsManager.hasQueuedEvent(with: EventType.cancel.rawValue))
260260

261261
waitForExpectations(timeout: expectationsTimeout)
262262
}
@@ -427,7 +427,7 @@ class NavigationServiceTests: TestCase {
427427
let feedback = EndOfRouteFeedback(rating: 5, comment: "comment")
428428
service.endNavigation(feedback: feedback)
429429

430-
XCTAssertTrue(eventsManager.hasImmediateEvent(with: EventType.cancel.rawValue))
430+
XCTAssertTrue(eventsManager.hasQueuedEvent(with: EventType.cancel.rawValue))
431431
XCTAssertTrue(locationManager.stopUpdatingHeadingCalled)
432432
XCTAssertTrue(locationManager.stopUpdatingLocationCalled)
433433

@@ -459,7 +459,7 @@ class NavigationServiceTests: TestCase {
459459
let feedback = EndOfRouteFeedback(rating: 5, comment: "comment")
460460
service.endNavigation(feedback: feedback)
461461

462-
XCTAssertTrue(eventsManager.hasImmediateEvent(with: EventType.cancel.rawValue))
462+
XCTAssertTrue(eventsManager.hasQueuedEvent(with: EventType.cancel.rawValue))
463463
XCTAssertTrue(locationManager.stopUpdatingHeadingCalled)
464464
XCTAssertTrue(locationManager.stopUpdatingLocationCalled)
465465

@@ -479,7 +479,7 @@ class NavigationServiceTests: TestCase {
479479
let feedback = EndOfRouteFeedback(rating: 5, comment: "comment")
480480
service.endNavigation(feedback: feedback)
481481

482-
XCTAssertTrue(eventsManager.hasImmediateEvent(with: EventType.cancel.rawValue))
482+
XCTAssertTrue(eventsManager.hasQueuedEvent(with: EventType.cancel.rawValue))
483483
XCTAssertTrue(locationManager.stopUpdatingHeadingCalled)
484484
XCTAssertTrue(locationManager.stopUpdatingLocationCalled)
485485
XCTAssertEqual(delegate.recentMessages, [])
@@ -529,7 +529,7 @@ class NavigationServiceTests: TestCase {
529529

530530
func testDidUpdate() {
531531
service.router(routerSpy, didUpdate: routeProgress, with: location, rawLocation: lastLocation)
532-
XCTAssertTrue(eventsManager.hasImmediateEvent(with: EventType.depart.rawValue))
532+
XCTAssertTrue(eventsManager.hasQueuedEvent(with: EventType.depart.rawValue))
533533
let expectedCalls = ["navigationService(_:didUpdate:with:rawLocation:)"]
534534
XCTAssertEqual(delegate.recentMessages, expectedCalls)
535535
}

0 commit comments

Comments
 (0)