-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix #14273: Prevent race condition crash in FPRTraceBackgroundActivityTracker #15382
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
base: main
Are you sure you want to change the base?
Changes from all commits
f17995d
a0b811f
cfcf2b6
0810b73
5f1bc14
bc03222
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,4 +61,82 @@ - (void)testBackgroundTracking { | |
}]; | ||
} | ||
|
||
/** Tests that synchronous observer registration works correctly and observers are immediately | ||
* available. */ | ||
- (void)testObservers_synchronousRegistrationAddsObserver { | ||
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; | ||
FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init]; | ||
XCTAssertNotNil(tracker); | ||
|
||
[notificationCenter postNotificationName:UIApplicationDidBecomeActiveNotification | ||
object:[UIApplication sharedApplication]]; | ||
XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly); | ||
|
||
tracker = nil; | ||
XCTAssertNil(tracker); | ||
XCTAssertNoThrow([notificationCenter postNotificationName:UIApplicationDidBecomeActiveNotification | ||
object:[UIApplication sharedApplication]]); | ||
XCTAssertNoThrow([notificationCenter | ||
postNotificationName:UIApplicationDidEnterBackgroundNotification | ||
object:[UIApplication sharedApplication]]); | ||
} | ||
|
||
/** Tests rapid creation and deallocation to verify race condition. */ | ||
- (void)testRapidCreationAndDeallocation_noRaceCondition { | ||
for (int i = 0; i < 100; i++) { | ||
@autoreleasepool { | ||
FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init]; | ||
XCTAssertNotNil(tracker); | ||
|
||
[[NSNotificationCenter defaultCenter] | ||
postNotificationName:UIApplicationDidBecomeActiveNotification | ||
object:[UIApplication sharedApplication]]; | ||
} | ||
} | ||
|
||
XCTAssertNoThrow([[NSNotificationCenter defaultCenter] | ||
postNotificationName:UIApplicationDidBecomeActiveNotification | ||
object:[UIApplication sharedApplication]]); | ||
XCTAssertNoThrow([[NSNotificationCenter defaultCenter] | ||
postNotificationName:UIApplicationDidEnterBackgroundNotification | ||
object:[UIApplication sharedApplication]]); | ||
} | ||
|
||
/** Tests that observers are registered immediately after init on main thread. */ | ||
- (void)testObservers_immediateRegistrationOnMainThread { | ||
XCTAssertTrue([NSThread isMainThread]); | ||
|
||
FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init]; | ||
|
||
[[NSNotificationCenter defaultCenter] | ||
postNotificationName:UIApplicationDidBecomeActiveNotification | ||
object:[UIApplication sharedApplication]]; | ||
|
||
XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly); | ||
} | ||
Comment on lines
+106
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT:We usually don't want to duplicate code if possible, if possible try to make it a single tests, so we don't have to maintain extra tests. |
||
|
||
/** Tests observer registration when created from background thread. */ | ||
- (void)testObservers_registrationFromBackgroundThread { | ||
XCTestExpectation *expectation = [self expectationWithDescription:@"Background thread creation"]; | ||
|
||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | ||
FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init]; | ||
XCTAssertNotNil(tracker); | ||
|
||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
[[NSNotificationCenter defaultCenter] | ||
postNotificationName:UIApplicationDidBecomeActiveNotification | ||
object:[UIApplication sharedApplication]]; | ||
|
||
XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly); | ||
[expectation fulfill]; | ||
}); | ||
}); | ||
|
||
[self waitForExpectationsWithTimeout:5.0 | ||
handler:^(NSError *error) { | ||
XCTAssertNil(error, @"Test timed out"); | ||
}]; | ||
} | ||
|
||
@end |
Uh oh!
There was an error while loading. Please reload this page.