Skip to content

Commit f5f4142

Browse files
committed
Make sure we turn of fetchInProgress flag on any fetch completion for error scenarios. Also add UT and add UT file to podspec. (#4844)
(cherry picked from commit cb9e083)
1 parent b8ba09b commit f5f4142

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

FirebaseRemoteConfig.podspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ app update.
6161
# 'FirebaseRemoteConfig/Tests/Unit/RCNThrottlingTests.m',
6262
'FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.m',
6363
'FirebaseRemoteConfig/Tests/Unit/RCNUserDefaultsManagerTests.m',
64-
'FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h'
64+
'FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h',
65+
'FirebaseRemoteConfig/Tests/Unit/RCNInstanceIDTest.m'
6566
# Supply plist custom plist testing.
6667
unit_tests.resources =
6768
'FirebaseRemoteConfig/Tests/Unit/Defaults-testInfo.plist',

FirebaseRemoteConfig/Sources/RCNFetch.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ - (void)fetchConfigWithExpirationDuration:(NSTimeInterval)expirationDuration
165165
[NSError errorWithDomain:FIRRemoteConfigErrorDomain
166166
code:FIRErrorCodeConfigFailed
167167
userInfo:@{
168-
@"FetchError" : @"Duplicate request while the previous one is pending"
168+
NSLocalizedDescriptionKey :
169+
@"FetchError: Duplicate request while the previous one is pending"
169170
}];
170171
return [strongSelf reportCompletionOnHandler:completionHandler
171172
withStatus:FIRRemoteConfigFetchStatusFailure
@@ -206,6 +207,7 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
206207
NSString *errorDescription = @"Failed to get GCMSenderID";
207208
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000074", @"%@",
208209
[NSString stringWithFormat:@"%@", errorDescription]);
210+
self->_settings.isFetchInProgress = NO;
209211
return [self
210212
reportCompletionOnHandler:completionHandler
211213
withStatus:FIRRemoteConfigFetchStatusFailure
@@ -221,6 +223,7 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
221223
[NSString stringWithFormat:@"Failed to get InstanceID token. Error : %@.", error];
222224
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000073", @"%@",
223225
[NSString stringWithFormat:@"%@", errorDescription]);
226+
self->_settings.isFetchInProgress = NO;
224227
return [self
225228
reportCompletionOnHandler:completionHandler
226229
withStatus:FIRRemoteConfigFetchStatusFailure
@@ -249,7 +252,8 @@ - (void)refreshInstanceIDTokenAndFetchCheckInInfoWithCompletionHandler:
249252
[NSString stringWithFormat:@"Error getting iid : %@.", error];
250253
FIRLogError(kFIRLoggerRemoteConfig, @"I-RCN000055", @"%@",
251254
[NSString stringWithFormat:@"%@", errorDescription]);
252-
return [self
255+
strongSelfQueue->_settings.isFetchInProgress = NO;
256+
return [strongSelfQueue
253257
reportCompletionOnHandler:completionHandler
254258
withStatus:FIRRemoteConfigFetchStatusFailure
255259
withError:[NSError
@@ -345,6 +349,7 @@ - (void)fetchWithUserProperties:(NSDictionary *)userProperties
345349
NSString *errString = [NSString stringWithFormat:@"Failed to compress the config request."];
346350
FIRLogWarning(kFIRLoggerRemoteConfig, @"I-RCN000033", @"%@", errString);
347351

352+
self->_settings.isFetchInProgress = NO;
348353
return [self
349354
reportCompletionOnHandler:completionHandler
350355
withStatus:FIRRemoteConfigFetchStatusFailure
@@ -361,6 +366,10 @@ - (void)fetchWithUserProperties:(NSDictionary *)userProperties
361366
FIRLogDebug(kFIRLoggerRemoteConfig, @"I-RCN000050",
362367
@"config fetch completed. Error: %@ StatusCode: %ld", (error ? error : @"nil"),
363368
(long)[((NSHTTPURLResponse *)response) statusCode]);
369+
370+
// The fetch has completed.
371+
self->_settings.isFetchInProgress = NO;
372+
364373
RCNConfigFetch *fetcherCompletionSelf = weakSelf;
365374
if (!fetcherCompletionSelf) {
366375
return;
@@ -372,7 +381,6 @@ - (void)fetchWithUserProperties:(NSDictionary *)userProperties
372381
return;
373382
}
374383

375-
strongSelf->_settings.isFetchInProgress = NO;
376384
NSInteger statusCode = [((NSHTTPURLResponse *)response) statusCode];
377385

378386
if (error || (statusCode != kRCNFetchResponseHTTPStatusCodeOK)) {

FirebaseRemoteConfig/Tests/Unit/RCNInstanceIDTest.m

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,50 @@ - (void)testNilInstanceIDTokenAndError {
229229
}];
230230
}
231231

232+
// Test IID error. Subsequent request also fails with same error (b/148975341).
233+
- (void)testMultipleFetchCallsFailing {
234+
NSMutableArray<XCTestExpectation *> *expectations =
235+
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
236+
237+
// Set the token as nil.
238+
[self mockInstanceIDMethodForTokenAndIdentity:nil
239+
tokenError:[NSError errorWithDomain:@"com.google.instanceid"
240+
code:FIRInstanceIDErrorUnknown
241+
userInfo:nil]
242+
identity:nil
243+
identityError:nil];
244+
// Test for each RC FIRApp, namespace instance.
245+
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
246+
expectations[i] =
247+
[self expectationWithDescription:
248+
[NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
249+
XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
250+
FIRRemoteConfigFetchCompletion fetchCompletion =
251+
^void(FIRRemoteConfigFetchStatus status, NSError *error) {
252+
XCTAssertNotNil(error);
253+
XCTAssertFalse([[error.userInfo objectForKey:@"NSLocalizedDescription"]
254+
rangeOfString:@"Failed to get InstanceID token"]
255+
.location == NSNotFound);
256+
// Make a second fetch call.
257+
[_configInstances[i]
258+
fetchWithExpirationDuration:43200
259+
completionHandler:^void(FIRRemoteConfigFetchStatus status, NSError *error) {
260+
XCTAssertNotNil(error);
261+
XCTAssertFalse([[error.userInfo objectForKey:@"NSLocalizedDescription"]
262+
rangeOfString:@"Failed to get InstanceID token"]
263+
.location == NSNotFound);
264+
[expectations[i] fulfill];
265+
}];
266+
};
267+
[_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
268+
}
269+
270+
[self waitForExpectationsWithTimeout:_expectationTimeout
271+
handler:^(NSError *error) {
272+
XCTAssertNil(error);
273+
}];
274+
}
275+
232276
// Instance ID token is not nil. Error is not nil. Verify fetch fails.
233277
- (void)testValidInstanceIDTokenAndValidError {
234278
NSMutableArray<XCTestExpectation *> *expectations =

0 commit comments

Comments
 (0)