Skip to content

Commit 4c4ba12

Browse files
authored
Fix main thread crashing issue, reenable tests. (#3974)
* Fix main thread crashing issue, reenable tests. * Added log, extra check for event generation to prevent early exit. * Temporarily run cron tests for travis. * Revert travis.yml update * Change property to atomic based on review comments.
1 parent 3d91a0f commit 4c4ba12

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

GoogleDataTransport/GDTCORLibrary/GDTCORPlatform.m

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ BOOL GDTCORReachabilityFlagsContainWWAN(SCNetworkReachabilityFlags flags) {
3737
#endif // TARGET_OS_IOS
3838
}
3939

40+
@interface GDTCORApplication ()
41+
/**
42+
Private flag to match the existing `readonly` public flag. This will be accurate for all platforms,
43+
since we handle each platform's lifecycle notifications separately.
44+
*/
45+
@property(atomic, readwrite) BOOL isRunningInBackground;
46+
47+
@end
48+
4049
@implementation GDTCORApplication
4150

4251
+ (void)load {
@@ -61,6 +70,9 @@ + (nullable GDTCORApplication *)sharedApplication {
6170
- (instancetype)init {
6271
self = [super init];
6372
if (self) {
73+
// This class will be instantiated in the foreground.
74+
_isRunningInBackground = NO;
75+
6476
#if TARGET_OS_IOS || TARGET_OS_TV
6577
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
6678
[notificationCenter addObserver:self
@@ -125,16 +137,6 @@ - (BOOL)isAppExtension {
125137
#endif
126138
}
127139

128-
- (BOOL)isRunningInBackground {
129-
#if TARGET_OS_IOS || TARGET_OS_TV
130-
BOOL runningInBackground =
131-
[[self sharedApplicationForBackgroundTask] applicationState] == UIApplicationStateBackground;
132-
return runningInBackground;
133-
#else // For macoS and Catalyst apps.
134-
return NO;
135-
#endif
136-
}
137-
138140
/** Returns a UIApplication instance if on the appropriate platform.
139141
*
140142
* @return The shared UIApplication if on the appropriate platform.
@@ -160,11 +162,15 @@ - (nullable id)sharedApplicationForBackgroundTask {
160162

161163
#if TARGET_OS_IOS || TARGET_OS_TV
162164
- (void)iOSApplicationDidEnterBackground:(NSNotification *)notif {
165+
_isRunningInBackground = YES;
166+
163167
NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
164168
[notifCenter postNotificationName:kGDTCORApplicationDidEnterBackgroundNotification object:nil];
165169
}
166170

167171
- (void)iOSApplicationWillEnterForeground:(NSNotification *)notif {
172+
_isRunningInBackground = NO;
173+
168174
NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
169175
[notifCenter postNotificationName:kGDTCORApplicationWillEnterForegroundNotification object:nil];
170176
}

GoogleDataTransport/GDTCORLibrary/Public/GDTCORPlatform.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,15 @@ FOUNDATION_EXPORT const GDTCORBackgroundIdentifier GDTCORBackgroundIdentifierInv
6161
/** A cross-platform application class. */
6262
@interface GDTCORApplication : NSObject <GDTCORApplicationDelegate>
6363

64+
/** Flag to determine if the application is running in the background. */
65+
@property(atomic, readonly) BOOL isRunningInBackground;
66+
6467
/** Creates and/or returns the shared application instance.
6568
*
6669
* @return The shared application instance.
6770
*/
6871
+ (nullable GDTCORApplication *)sharedApplication;
6972

70-
/** Flag to determine if the application is running in the background.
71-
*
72-
* @return YES if the app is running in the background, otherwise NO.
73-
*/
74-
- (BOOL)isRunningInBackground;
75-
7673
/** Creates a background task with the returned identifier if on a suitable platform.
7774
*
7875
* @name name The name of the task, useful for debugging which background tasks are running.

GoogleDataTransport/GDTCORTests/Lifecycle/GDTCORLifecycleTest.m

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ - (void)setUp {
9090
[[GDTCORUploadCoordinator sharedInstance] reset];
9191
}
9292

93+
// Backgrounding and foregrounding are only applicable for iOS and tvOS.
94+
#if TARGET_OS_IOS || TARGET_OS_TV
95+
9396
/** Tests that the library serializes itself to disk when the app backgrounds. */
94-
- (void)DISABLED_testBackgrounding {
97+
- (void)testBackgrounding {
9598
GDTCORTransport *transport = [[GDTCORTransport alloc] initWithMappingID:@"test"
9699
transformers:nil
97100
target:kGDTCORTargetTest];
@@ -105,10 +108,8 @@ - (void)DISABLED_testBackgrounding {
105108
},
106109
5.0);
107110

108-
// TODO(#3973): This notification no longer triggers the `isRunningInBackground` flag. Find
109-
// another way to test it.
110111
NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
111-
[notifCenter postNotificationName:kGDTCORApplicationDidEnterBackgroundNotification object:nil];
112+
[notifCenter postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil];
112113
XCTAssertTrue([GDTCORApplication sharedApplication].isRunningInBackground);
113114

114115
GDTCORWaitForBlock(
@@ -120,7 +121,7 @@ - (void)DISABLED_testBackgrounding {
120121
}
121122

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

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

142141
GDTCORWaitForBlock(
143142
^BOOL {
@@ -146,17 +145,16 @@ - (void)DISABLED_testForegrounding {
146145
},
147146
5.0);
148147

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

161159
/** Tests that the library gracefully stops doing stuff when terminating. */
162160
- (void)testTermination {

GoogleDataTransportCCTSupport/GDTCCTTests/Integration/GDTCCTIntegrationTest.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ - (void)testRunsWithoutCrashing {
157157
}
158158

159159
eventsSent += eventsUploaded.integerValue;
160-
if (eventsSent == self.totalEventsGenerated) {
160+
NSLog(@"Single upload event of %ld, combined for %ld/%ld total expected.",
161+
(long)eventsUploaded.integerValue, (long)eventsSent,
162+
(long)self.totalEventsGenerated);
163+
// Only fulfill the expectation once event generation is done and the numbers match.
164+
if (self.generateEvents == NO && eventsSent == self.totalEventsGenerated) {
161165
[eventCountsMatchExpectation fulfill];
162166
}
163167
}];

0 commit comments

Comments
 (0)