Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve background batch uploading and session management #238

Merged
merged 24 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
285fc9a
Start refactoring out background logic
einsteinx2 Oct 25, 2023
caed401
Remove resignedActive state tracking because the if statement will ne…
einsteinx2 Oct 25, 2023
6fbc573
Mostly done refactoring background upload logic
einsteinx2 Oct 26, 2023
effbc78
Improved upload timer logic to prevent extra batch uploads and reduce…
einsteinx2 Oct 26, 2023
3a79ca0
Increased default upload time interval in debug mode to better test b…
einsteinx2 Oct 26, 2023
29b2b1b
Refactored out local messageQueue variable
einsteinx2 Oct 26, 2023
440216a
Refactored out duplicate app extension logic
einsteinx2 Oct 26, 2023
d045443
Background session logic should be correct now, remove BEN log statem…
einsteinx2 Nov 6, 2023
9d26025
MPBackendControllerTests no longer needs to be Obj-C++
einsteinx2 Nov 6, 2023
90fbd1e
Remove DEFAULT_DEBUG_UPLOAD_INTERVAL (just use standard one when deve…
einsteinx2 Nov 6, 2023
e994fbf
Fixed some logic with session timeouts
einsteinx2 Nov 7, 2023
6f3e0f1
Fixed background session logic when app sleeps longer than session ti…
einsteinx2 Nov 7, 2023
7790b88
Don't allow negative foreground time
einsteinx2 Nov 7, 2023
0581dbe
Remove log statements
einsteinx2 Nov 7, 2023
0bf5ded
Fix native tests CI (github runner image changed)
einsteinx2 Nov 8, 2023
13b8c5c
Updated MAXIMUM_SESSION_TIMEOUT to reflect correct value even though …
einsteinx2 Nov 8, 2023
d1f6782
Fix tests compiling
einsteinx2 Nov 8, 2023
70aee94
Re-add DEFAULT_DEBUG_UPLOAD_INTERVAL
einsteinx2 Nov 10, 2023
565e4d1
Update comment on uploadInterval
einsteinx2 Nov 10, 2023
5d710a6
Capture strong reference to self in blocks
einsteinx2 Nov 10, 2023
d4853ec
Fix unit tests
einsteinx2 Nov 10, 2023
cabdae8
Fixed appledoc comments and added note to sessionTimeout regarding ma…
einsteinx2 Nov 10, 2023
34a6d54
Immediately upload when session ends
einsteinx2 Nov 10, 2023
ea71566
Modern tvOS works just like iOS, so aligning session timeout and uplo…
einsteinx2 Nov 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/native-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ jobs:
platform: [iOS, tvOS]
scheme: [mParticle-Apple-SDK, mParticle-Apple-SDK-NoLocation]
include:
- xcode: "15.0"
os: "17.0"
- platform: iOS
device: iPhone 14
device: iPhone 15
- platform: tvOS
device: Apple TV
runs-on: macos-13
Expand All @@ -31,4 +29,4 @@ jobs:
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app

- name: Run iOS unit tests
run: xcodebuild -project mParticle-Apple-SDK.xcodeproj -scheme ${{ matrix.scheme }} -destination 'platform=${{ matrix.platform }} Simulator,name=${{ matrix.device }},OS=${{ matrix.os }}' test
run: xcodebuild -project mParticle-Apple-SDK.xcodeproj -scheme ${{ matrix.scheme }} -destination 'platform=${{ matrix.platform }} Simulator,name=${{ matrix.device }},OS=latest' test
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,6 @@ - (MPNotificationController *)notificationController {
}
#endif

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"backendController.session"]) {
self.session = change[NSKeyValueChangeNewKey];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

- (void)testBeginSession {
XCTestExpectation *expectation = [self expectationWithDescription:@"Begin session test"];
dispatch_sync(messageQueue, ^{
Expand Down Expand Up @@ -376,8 +368,6 @@ - (void)testBackgroundBlock {
session = self.backendController.session;
sessions = [persistence fetchSessions];
XCTAssertEqual(sessions.count, 2);

[self.backendController backgroundTaskBlock];
XCTAssertNotNil(self.backendController.session);

MPMessage *message = [persistence fetchSessionEndMessageInSession:session];
Expand Down Expand Up @@ -1214,7 +1204,6 @@ - (void)testUserIdentityChanged {
[self.backendController beginSession];
});
self.session = self.backendController.session;
[self addObserver:self forKeyPath:@"backendController.session" options:NSKeyValueObservingOptionNew context:NULL];
XCTestExpectation *expectation = [self expectationWithDescription:@"User identity changed"];
__weak MPBackendControllerTests *weakSelf = self;
MPPersistenceController *persistence = [MParticle sharedInstance].persistenceController;
Expand Down Expand Up @@ -1290,7 +1279,6 @@ - (void)testUserIdentityChanged {
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:BACKEND_TESTS_EXPECTATIONS_TIMEOUT handler:nil];
[self removeObserver:self forKeyPath:@"backendController.session" context:NULL];
}

- (void)testIncrementUserAttribute {
Expand All @@ -1308,7 +1296,7 @@ - (void)testIncrementUserAttribute {
XCTAssertNotNil(userAttributeValue);
XCTAssertEqualObjects(userAttributeValue, @1);

[self.backendController removeUserAttribute:userAttributeKey timestamp:[NSDate date] completionHandler:{}];
[self.backendController removeUserAttribute:userAttributeKey timestamp:[NSDate date] completionHandler:nil];
userAttributeValue = [self.backendController userAttributesForUserId:[MPPersistenceController mpId]][userAttributeKey];
XCTAssertNil(userAttributeValue);
[expectation fulfill];
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/MPPersistenceControllerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ - (void)testForwardRecord {
sessionId:[NSNumber numberWithLong:session.sessionId]
messages:@[message]
sessionTimeout:DEFAULT_SESSION_TIMEOUT
uploadInterval:DEFAULT_DEBUG_UPLOAD_INTERVAL
uploadInterval:DEFAULT_UPLOAD_INTERVAL
dataPlanId:@"test"
dataPlanVersion:@(1)];

Expand Down
16 changes: 8 additions & 8 deletions UnitTests/MPUploadBuilderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ - (void)testInstanceWithSession {
sessionId:[NSNumber numberWithLong:session.sessionId]
messages:@[message]
sessionTimeout:DEFAULT_SESSION_TIMEOUT
uploadInterval:DEFAULT_DEBUG_UPLOAD_INTERVAL
uploadInterval:DEFAULT_UPLOAD_INTERVAL
dataPlanId:message.dataPlanId
dataPlanVersion:message.dataPlanVersion
];
Expand Down Expand Up @@ -214,7 +214,7 @@ - (void)testInstanceWithoutSession {

MPUploadBuilder *uploadBuilder = [MPUploadBuilder newBuilderWithMpid:[MPPersistenceController mpId]
messages:@[message]
uploadInterval:DEFAULT_DEBUG_UPLOAD_INTERVAL
uploadInterval:DEFAULT_UPLOAD_INTERVAL
dataPlanId:message.dataPlanId
dataPlanVersion:message.dataPlanVersion
];
Expand Down Expand Up @@ -294,7 +294,7 @@ - (void)testInstanceWithDataPlanId {
sessionId:[NSNumber numberWithLong:session.sessionId]
messages:@[message]
sessionTimeout:DEFAULT_SESSION_TIMEOUT
uploadInterval:DEFAULT_DEBUG_UPLOAD_INTERVAL
uploadInterval:DEFAULT_UPLOAD_INTERVAL
dataPlanId:message.dataPlanId
dataPlanVersion:message.dataPlanVersion
];
Expand Down Expand Up @@ -376,7 +376,7 @@ - (void)testInstanceWithDataPlanVersion {
sessionId:[NSNumber numberWithLong:session.sessionId]
messages:@[message]
sessionTimeout:DEFAULT_SESSION_TIMEOUT
uploadInterval:DEFAULT_DEBUG_UPLOAD_INTERVAL
uploadInterval:DEFAULT_UPLOAD_INTERVAL
dataPlanId:message.dataPlanId
dataPlanVersion:message.dataPlanVersion
];
Expand Down Expand Up @@ -455,7 +455,7 @@ - (void)testInstanceWithAdvertiserIdInSessionNoAttStatus {
sessionId:[NSNumber numberWithLong:session.sessionId]
messages:@[message]
sessionTimeout:DEFAULT_SESSION_TIMEOUT
uploadInterval:DEFAULT_DEBUG_UPLOAD_INTERVAL
uploadInterval:DEFAULT_UPLOAD_INTERVAL
dataPlanId:message.dataPlanId
dataPlanVersion:message.dataPlanVersion
];
Expand Down Expand Up @@ -537,7 +537,7 @@ - (void)testInstanceWithAdvertiserIdInSessionAuthorizedAttStatus {
sessionId:[NSNumber numberWithLong:session.sessionId]
messages:@[message]
sessionTimeout:DEFAULT_SESSION_TIMEOUT
uploadInterval:DEFAULT_DEBUG_UPLOAD_INTERVAL
uploadInterval:DEFAULT_UPLOAD_INTERVAL
dataPlanId:message.dataPlanId
dataPlanVersion:message.dataPlanVersion
];
Expand Down Expand Up @@ -622,7 +622,7 @@ - (void)testInstanceWithAdvertiserIdInSessionDeniedAttStatus {
sessionId:[NSNumber numberWithLong:session.sessionId]
messages:@[message]
sessionTimeout:DEFAULT_SESSION_TIMEOUT
uploadInterval:DEFAULT_DEBUG_UPLOAD_INTERVAL
uploadInterval:DEFAULT_UPLOAD_INTERVAL
dataPlanId:message.dataPlanId
dataPlanVersion:message.dataPlanVersion
];
Expand Down Expand Up @@ -703,7 +703,7 @@ - (MPUploadBuilder *)createTestUploadBuilder {
sessionId:[NSNumber numberWithLong:session.sessionId]
messages:@[message]
sessionTimeout:DEFAULT_SESSION_TIMEOUT
uploadInterval:DEFAULT_DEBUG_UPLOAD_INTERVAL
uploadInterval:DEFAULT_UPLOAD_INTERVAL
dataPlanId:message.dataPlanId
dataPlanVersion:message.dataPlanVersion
];
Expand Down
12 changes: 6 additions & 6 deletions mParticle-Apple-SDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
534CD26829CE2CE1008452B3 /* MPConnectorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8429CE019E00E7489F /* MPConnectorTests.m */; };
534CD26929CE2CE1008452B3 /* MPSurrogateAppDelegateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8529CE019F00E7489F /* MPSurrogateAppDelegateTests.m */; };
534CD26A29CE2CE1008452B3 /* MPGDPRConsentTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79CA129CE019F00E7489F /* MPGDPRConsentTests.m */; };
534CD26B29CE2CE1008452B3 /* MPBackendControllerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8629CE019F00E7489F /* MPBackendControllerTests.mm */; };
534CD26B29CE2CE1008452B3 /* MPBackendControllerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8629CE019F00E7489F /* MPBackendControllerTests.m */; };
534CD26C29CE2CE1008452B3 /* MPKitConfigurationTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 53A79CB029CE019F00E7489F /* MPKitConfigurationTests.mm */; };
534CD26E29CE2CE1008452B3 /* MPNetworkCommunicationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C6F29CE019E00E7489F /* MPNetworkCommunicationTests.m */; };
534CD26F29CE2CE1008452B3 /* MPAliasRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79CB129CE019F00E7489F /* MPAliasRequestTests.m */; };
Expand Down Expand Up @@ -284,7 +284,7 @@
53A79CCF29CE019F00E7489F /* MParticleOptionsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8329CE019E00E7489F /* MParticleOptionsTests.m */; };
53A79CD029CE019F00E7489F /* MPConnectorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8429CE019E00E7489F /* MPConnectorTests.m */; };
53A79CD129CE019F00E7489F /* MPSurrogateAppDelegateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8529CE019F00E7489F /* MPSurrogateAppDelegateTests.m */; };
53A79CD229CE019F00E7489F /* MPBackendControllerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8629CE019F00E7489F /* MPBackendControllerTests.mm */; };
53A79CD229CE019F00E7489F /* MPBackendControllerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8629CE019F00E7489F /* MPBackendControllerTests.m */; };
53A79CD329CE019F00E7489F /* OCMock.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = 53A79C8829CE019F00E7489F /* OCMock.xcframework */; };
53A79CD429CE019F00E7489F /* MPConvertJSTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8A29CE019F00E7489F /* MPConvertJSTests.m */; };
53A79CD529CE019F00E7489F /* MPKitContainerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79C8B29CE019F00E7489F /* MPKitContainerTests.m */; };
Expand Down Expand Up @@ -747,7 +747,7 @@
53A79C8329CE019E00E7489F /* MParticleOptionsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MParticleOptionsTests.m; sourceTree = "<group>"; };
53A79C8429CE019E00E7489F /* MPConnectorTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPConnectorTests.m; sourceTree = "<group>"; };
53A79C8529CE019F00E7489F /* MPSurrogateAppDelegateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPSurrogateAppDelegateTests.m; sourceTree = "<group>"; };
53A79C8629CE019F00E7489F /* MPBackendControllerTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MPBackendControllerTests.mm; sourceTree = "<group>"; };
53A79C8629CE019F00E7489F /* MPBackendControllerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPBackendControllerTests.m; sourceTree = "<group>"; };
53A79C8829CE019F00E7489F /* OCMock.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; path = OCMock.xcframework; sourceTree = "<group>"; };
53A79C8A29CE019F00E7489F /* MPConvertJSTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPConvertJSTests.m; sourceTree = "<group>"; };
53A79C8B29CE019F00E7489F /* MPKitContainerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPKitContainerTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1238,7 +1238,7 @@
53A79C8329CE019E00E7489F /* MParticleOptionsTests.m */,
53A79C8429CE019E00E7489F /* MPConnectorTests.m */,
53A79C8529CE019F00E7489F /* MPSurrogateAppDelegateTests.m */,
53A79C8629CE019F00E7489F /* MPBackendControllerTests.mm */,
53A79C8629CE019F00E7489F /* MPBackendControllerTests.m */,
53A79C8A29CE019F00E7489F /* MPConvertJSTests.m */,
53A79C8B29CE019F00E7489F /* MPKitContainerTests.m */,
53A79C8C29CE019F00E7489F /* MPCCPAConsentTests.m */,
Expand Down Expand Up @@ -1681,7 +1681,7 @@
534CD26829CE2CE1008452B3 /* MPConnectorTests.m in Sources */,
534CD26929CE2CE1008452B3 /* MPSurrogateAppDelegateTests.m in Sources */,
534CD26A29CE2CE1008452B3 /* MPGDPRConsentTests.m in Sources */,
534CD26B29CE2CE1008452B3 /* MPBackendControllerTests.mm in Sources */,
534CD26B29CE2CE1008452B3 /* MPBackendControllerTests.m in Sources */,
534CD26C29CE2CE1008452B3 /* MPKitConfigurationTests.mm in Sources */,
534CD26E29CE2CE1008452B3 /* MPNetworkCommunicationTests.m in Sources */,
534CD26F29CE2CE1008452B3 /* MPAliasRequestTests.m in Sources */,
Expand Down Expand Up @@ -1844,7 +1844,7 @@
53A79CD029CE019F00E7489F /* MPConnectorTests.m in Sources */,
53A79CD129CE019F00E7489F /* MPSurrogateAppDelegateTests.m in Sources */,
53A79CE729CE019F00E7489F /* MPGDPRConsentTests.m in Sources */,
53A79CD229CE019F00E7489F /* MPBackendControllerTests.mm in Sources */,
53A79CD229CE019F00E7489F /* MPBackendControllerTests.m in Sources */,
53A79CF329CE019F00E7489F /* MPKitConfigurationTests.mm in Sources */,
53A79CDC29CE019F00E7489F /* MPSwiftTests.swift in Sources */,
53A79CBE29CE019F00E7489F /* MPNetworkCommunicationTests.m in Sources */,
Expand Down
6 changes: 4 additions & 2 deletions mParticle-Apple-SDK/Data Model/MPSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ - (instancetype)initWithSessionId:(int64_t)sessionId
}

- (NSString *)description {
return [NSString stringWithFormat:@"Session\n Id: %lld\n UUID: %@\n Background time: %.0f\n Start: %.0f\n End: %.0f\n Length: %.0f\n EventCounter: %d\n Persisted: %d\n Attributes: %@\n Interruptions: %d\n", self.sessionId, self.uuid, self.backgroundTime, self.startTime, self.endTime, self.length, self.eventCounter, self.persisted, self.attributesDictionary, self.numberOfInterruptions];
return [NSString stringWithFormat:@"Session\n Id: %lld\n UUID: %@\n Background time: %.0f\n Foreground time: %.0f\n Start: %.0f\n End: %.0f\n Length: %.0f\n EventCounter: %d\n Persisted: %d\n Interruptions: %d\n Attributes: %@\n", self.sessionId, self.uuid, self.backgroundTime, self.foregroundTime, self.startTime, self.endTime, self.length, self.eventCounter, self.persisted, self.numberOfInterruptions, self.attributesDictionary];
}

- (BOOL)isEqual:(MPSession *)object {
Expand Down Expand Up @@ -132,7 +132,9 @@ - (id)copyWithZone:(NSZone *)zone {

#pragma mark Public accessors
- (NSTimeInterval)foregroundTime {
return _length - _backgroundTime;//sl
NSTimeInterval foreground = _length - _backgroundTime;
// Don't allow negative foreground time
return foreground < 0.0 ? 0.0 : foreground;
}

- (void)setEndTime:(NSTimeInterval)endTime {
Expand Down
Loading