Skip to content

Fix main thread crashing issue, reenable tests. #3974

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

Merged
merged 5 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 16 additions & 10 deletions GoogleDataTransport/GDTCORLibrary/GDTCORPlatform.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ BOOL GDTCORReachabilityFlagsContainWWAN(SCNetworkReachabilityFlags flags) {
#endif // TARGET_OS_IOS
}

@interface GDTCORApplication ()
/**
Private flag to match the existing `readonly` public flag. This will be accurate for all platforms,
since we handle each platform's lifecycle notifications separately.
*/
@property(atomic, readwrite) BOOL isRunningInBackground;

@end

@implementation GDTCORApplication

+ (void)load {
Expand All @@ -61,6 +70,9 @@ + (nullable GDTCORApplication *)sharedApplication {
- (instancetype)init {
self = [super init];
if (self) {
// This class will be instantiated in the foreground.
_isRunningInBackground = NO;

#if TARGET_OS_IOS || TARGET_OS_TV
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
Expand Down Expand Up @@ -125,16 +137,6 @@ - (BOOL)isAppExtension {
#endif
}

- (BOOL)isRunningInBackground {
#if TARGET_OS_IOS || TARGET_OS_TV
BOOL runningInBackground =
[[self sharedApplicationForBackgroundTask] applicationState] == UIApplicationStateBackground;
return runningInBackground;
#else // For macoS and Catalyst apps.
return NO;
#endif
}

/** Returns a UIApplication instance if on the appropriate platform.
*
* @return The shared UIApplication if on the appropriate platform.
Expand All @@ -160,11 +162,15 @@ - (nullable id)sharedApplicationForBackgroundTask {

#if TARGET_OS_IOS || TARGET_OS_TV
- (void)iOSApplicationDidEnterBackground:(NSNotification *)notif {
_isRunningInBackground = YES;

NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
[notifCenter postNotificationName:kGDTCORApplicationDidEnterBackgroundNotification object:nil];
}

- (void)iOSApplicationWillEnterForeground:(NSNotification *)notif {
_isRunningInBackground = NO;

NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
[notifCenter postNotificationName:kGDTCORApplicationWillEnterForegroundNotification object:nil];
}
Expand Down
9 changes: 3 additions & 6 deletions GoogleDataTransport/GDTCORLibrary/Public/GDTCORPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,15 @@ FOUNDATION_EXPORT const GDTCORBackgroundIdentifier GDTCORBackgroundIdentifierInv
/** A cross-platform application class. */
@interface GDTCORApplication : NSObject <GDTCORApplicationDelegate>

/** Flag to determine if the application is running in the background. */
@property(atomic, readonly) BOOL isRunningInBackground;

/** Creates and/or returns the shared application instance.
*
* @return The shared application instance.
*/
+ (nullable GDTCORApplication *)sharedApplication;

/** Flag to determine if the application is running in the background.
*
* @return YES if the app is running in the background, otherwise NO.
*/
- (BOOL)isRunningInBackground;

/** Creates a background task with the returned identifier if on a suitable platform.
*
* @name name The name of the task, useful for debugging which background tasks are running.
Expand Down
20 changes: 9 additions & 11 deletions GoogleDataTransport/GDTCORTests/Lifecycle/GDTCORLifecycleTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ - (void)setUp {
[[GDTCORUploadCoordinator sharedInstance] reset];
}

// Backgrounding and foregrounding are only applicable for iOS and tvOS.
#if TARGET_OS_IOS || TARGET_OS_TV

/** Tests that the library serializes itself to disk when the app backgrounds. */
- (void)DISABLED_testBackgrounding {
- (void)testBackgrounding {
GDTCORTransport *transport = [[GDTCORTransport alloc] initWithMappingID:@"test"
transformers:nil
target:kGDTCORTargetTest];
Expand All @@ -105,10 +108,8 @@ - (void)DISABLED_testBackgrounding {
},
5.0);

// TODO(#3973): This notification no longer triggers the `isRunningInBackground` flag. Find
// another way to test it.
NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
[notifCenter postNotificationName:kGDTCORApplicationDidEnterBackgroundNotification object:nil];
[notifCenter postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil];
Comment on lines -111 to +112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change from the abstracted notification to an iOS-specific one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We previously used the abstract one because it triggered each classes' foreground/background flags, but now since the flag moved it's set before the abstracted notification is fired. Meaning: the abstract notification no longer triggers the flag to change, only the iOS specific one does.

XCTAssertTrue([GDTCORApplication sharedApplication].isRunningInBackground);

GDTCORWaitForBlock(
Expand All @@ -120,7 +121,7 @@ - (void)DISABLED_testBackgrounding {
}

/** Tests that the library deserializes itself from disk when the app foregrounds. */
- (void)DISABLED_testForegrounding {
- (void)testForegrounding {
GDTCORTransport *transport = [[GDTCORTransport alloc] initWithMappingID:@"test"
transformers:nil
target:kGDTCORTargetTest];
Expand All @@ -134,10 +135,8 @@ - (void)DISABLED_testForegrounding {
},
5.0);

// TODO(#3973): This notification no longer triggers the `isRunningInBackground` flag. Find
// another way to test it.
NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
[notifCenter postNotificationName:kGDTCORApplicationDidEnterBackgroundNotification object:nil];
[notifCenter postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil];

GDTCORWaitForBlock(
^BOOL {
Expand All @@ -146,17 +145,16 @@ - (void)DISABLED_testForegrounding {
},
5.0);

// TODO(#3973): This notification no longer triggers the `isRunningInBackground` flag. Find
// another way to test it.
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
[notifCenter postNotificationName:kGDTCORApplicationWillEnterForegroundNotification object:nil];
[notifCenter postNotificationName:UIApplicationWillEnterForegroundNotification object:nil];
XCTAssertFalse([GDTCORApplication sharedApplication].isRunningInBackground);
GDTCORWaitForBlock(
^BOOL {
return [GDTCORStorage sharedInstance].storedEvents.count > 0;
},
5.0);
}
#endif // #if TARGET_OS_IOS || TARGET_OS_TV

/** Tests that the library gracefully stops doing stuff when terminating. */
- (void)testTermination {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ - (void)testRunsWithoutCrashing {
}

eventsSent += eventsUploaded.integerValue;
if (eventsSent == self.totalEventsGenerated) {
NSLog(@"Single upload event of %ld, combined for %ld/%ld total expected.",
(long)eventsUploaded.integerValue, (long)eventsSent,
(long)self.totalEventsGenerated);
// Only fulfill the expectation once event generation is done and the numbers match.
if (self.generateEvents == NO && eventsSent == self.totalEventsGenerated) {
[eventCountsMatchExpectation fulfill];
}
}];
Expand Down