Skip to content

Commit 4422243

Browse files
authored
Fix RC test flake from deallocated Expectations (#3681)
1 parent e47dc20 commit 4422243

File tree

1 file changed

+64
-36
lines changed

1 file changed

+64
-36
lines changed

FirebaseRemoteConfig/Tests/Unit/RCNRemoteConfigTest.m

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ - (void)testFetchConfigWithNilCallback {
233233
NSMutableArray<XCTestExpectation *> *expectations =
234234
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
235235
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
236-
expectations[i] = [self expectationWithDescription:@"Set defaults no callback expectation"];
237-
236+
expectations[i] = [self
237+
expectationWithDescription:
238+
[NSString stringWithFormat:@"Set defaults no callback expectation - instance %d", i]];
238239
XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
239240

240241
[_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:nil];
@@ -253,7 +254,9 @@ - (void)testFetchConfigsSuccessfully {
253254
NSMutableArray<XCTestExpectation *> *expectations =
254255
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
255256
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
256-
expectations[i] = [self expectationWithDescription:@"Test fetch configs successfully."];
257+
expectations[i] =
258+
[self expectationWithDescription:
259+
[NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
257260
XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
258261
FIRRemoteConfigFetchCompletion fetchCompletion =
259262
^void(FIRRemoteConfigFetchStatus status, NSError *error) {
@@ -294,7 +297,9 @@ - (void)testFetchAndActivate {
294297
NSMutableArray<XCTestExpectation *> *expectations =
295298
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
296299
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
297-
expectations[i] = [self expectationWithDescription:@"Test fetch configs successfully."];
300+
expectations[i] =
301+
[self expectationWithDescription:
302+
[NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
298303
XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
299304
FIRRemoteConfigFetchAndActivateCompletion fetchAndActivateCompletion = ^void(
300305
FIRRemoteConfigFetchAndActivateStatus status, NSError *error) {
@@ -339,7 +344,9 @@ - (void)testFetchConfigsSuccessfullyWithNewActivateMethodSignature {
339344
NSMutableArray<XCTestExpectation *> *expectations =
340345
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
341346
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
342-
expectations[i] = [self expectationWithDescription:@"Test fetch configs successfully."];
347+
expectations[i] =
348+
[self expectationWithDescription:
349+
[NSString stringWithFormat:@"Test fetch configs successfully - instance %d", i]];
343350
XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
344351
FIRRemoteConfigFetchCompletion fetchCompletion =
345352
^(FIRRemoteConfigFetchStatus status, NSError *error) {
@@ -384,8 +391,9 @@ - (void)testEnumeratingConfigResults {
384391
NSMutableArray<XCTestExpectation *> *expectations =
385392
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
386393
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
387-
expectations[i] = [self expectationWithDescription:@"Test enumerating configs successfully."];
388-
394+
expectations[i] = [self
395+
expectationWithDescription:
396+
[NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
389397
XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
390398
FIRRemoteConfigFetchCompletion fetchCompletion =
391399
^void(FIRRemoteConfigFetchStatus status, NSError *error) {
@@ -526,9 +534,11 @@ - (void)testFetchConfigsFailed {
526534
// Make the fetch calls for all instances.
527535
NSMutableArray<XCTestExpectation *> *expectations =
528536
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
529-
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
530-
expectations[i] = [self expectationWithDescription:@"Test enumerating configs successfully."];
531537

538+
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
539+
expectations[i] = [self
540+
expectationWithDescription:
541+
[NSString stringWithFormat:@"Test enumerating configs successfully - instance %d", i]];
532542
XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
533543
FIRRemoteConfigFetchCompletion fetchCompletion =
534544
^void(FIRRemoteConfigFetchStatus status, NSError *error) {
@@ -554,8 +564,9 @@ - (void)testConfigValueForKey {
554564
NSMutableArray<XCTestExpectation *> *expectations =
555565
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
556566
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
557-
expectations[i] = [self expectationWithDescription:@"Test configValueForKey: method."];
558-
567+
expectations[i] =
568+
[self expectationWithDescription:
569+
[NSString stringWithFormat:@"Test configValueForKey: method - instance %d", i]];
559570
XCTAssertEqual(_configInstances[i].lastFetchStatus, FIRRemoteConfigFetchStatusNoFetchYet);
560571
FIRRemoteConfigFetchCompletion fetchCompletion = ^void(FIRRemoteConfigFetchStatus status,
561572
NSError *error) {
@@ -602,16 +613,18 @@ - (void)testConfigValueForKey {
602613
}
603614

604615
- (void)testFetchConfigWithDefaultSets {
616+
NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
617+
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
605618
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
619+
fetchConfigsExpectation[i] = [self
620+
expectationWithDescription:
621+
[NSString stringWithFormat:@"Test fetch configs with defaults set - instance %d", i]];
606622
NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
607623
NSString *key2 = [NSString stringWithFormat:@"key2-%d", i];
608624
NSString *key0 = [NSString stringWithFormat:@"key0-%d", i];
609625
NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
610626
NSString *value2 = [NSString stringWithFormat:@"value2-%d", i];
611627

612-
XCTestExpectation *fetchConfigsExpectation =
613-
[self expectationWithDescription:@"Test fetch configs with defaults set."];
614-
615628
NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default key1", key0 : @"value0-0"};
616629
[_configInstances[i] setDefaults:defaults];
617630

@@ -639,7 +652,7 @@ - (void)testFetchConfigWithDefaultSets {
639652
XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
640653
@"Callback of first successful config "
641654
@"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
642-
[fetchConfigsExpectation fulfill];
655+
[fetchConfigsExpectation[i] fulfill];
643656
};
644657
[_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
645658
}
@@ -708,9 +721,12 @@ - (void)testDefaultsSetsOnly {
708721
}
709722

710723
- (void)testSetDefaultsWithNilParams {
724+
NSMutableArray<XCTestExpectation *> *expectations =
725+
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
711726
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
712-
XCTestExpectation *expectation =
713-
[self expectationWithDescription:@"Set defaults no callback expectation"];
727+
expectations[i] = [self
728+
expectationWithDescription:
729+
[NSString stringWithFormat:@"Set defaults no callback expectation - instance %d", i]];
714730
// Should work when passing nil.
715731
[_configInstances[i] setDefaults:nil];
716732
[_configInstances[i] setDefaults:nil namespace:nil];
@@ -722,20 +738,22 @@ - (void)testSetDefaultsWithNilParams {
722738
namespace:FIRNamespaceGoogleMobilePlatform]
723739
.count,
724740
0);
725-
[expectation fulfill];
741+
[expectations[i] fulfill];
726742
});
727743
}
728744
[self waitForExpectationsWithTimeout:_expectationTimeout handler:nil];
729745
}
730746

731747
- (void)testFetchConfigOverwriteDefaultSet {
748+
NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
749+
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
732750
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
751+
fetchConfigsExpectation[i] = [self
752+
expectationWithDescription:
753+
[NSString stringWithFormat:@"Test fetch configs with defaults set - instance %d", i]];
733754
NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
734755
NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
735756

736-
XCTestExpectation *fetchConfigsExpectation =
737-
[self expectationWithDescription:@"Test fetch configs overwrites defaults set."];
738-
739757
[_configInstances[i] setDefaults:@{key1 : @"default key1"}];
740758

741759
FIRRemoteConfigValue *value = _configInstances[i][key1];
@@ -759,7 +777,7 @@ - (void)testFetchConfigOverwriteDefaultSet {
759777
XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
760778
@"Callback of first successful config "
761779
@"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
762-
[fetchConfigsExpectation fulfill];
780+
[fetchConfigsExpectation[i] fulfill];
763781
};
764782

765783
[_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
@@ -771,11 +789,14 @@ - (void)testFetchConfigOverwriteDefaultSet {
771789
}
772790

773791
- (void)testGetConfigValueBySource {
792+
NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
793+
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
774794
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
795+
fetchConfigsExpectation[i] =
796+
[self expectationWithDescription:
797+
[NSString stringWithFormat:@"Test get config value by source - instance %d", i]];
775798
NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
776799
NSString *value1 = [NSString stringWithFormat:@"value1-%d", i];
777-
XCTestExpectation *fetchConfigsExpectation =
778-
[self expectationWithDescription:@"Test get config value by source."];
779800

780801
NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default value1"};
781802
[_configInstances[i] setDefaults:defaults];
@@ -816,7 +837,7 @@ - (void)testGetConfigValueBySource {
816837
XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess,
817838
@"Callback of first successful config "
818839
@"fetch. Status must equal to FIRRemoteConfigFetchStatusSuccess.");
819-
[fetchConfigsExpectation fulfill];
840+
[fetchConfigsExpectation[i] fulfill];
820841
};
821842
[_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
822843
}
@@ -942,9 +963,12 @@ - (void)testSetDeveloperMode {
942963
}
943964

944965
- (void)testAllKeysFromSource {
966+
NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
967+
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
945968
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
946-
XCTestExpectation *fetchConfigsExpectation =
947-
[self expectationWithDescription:@"Test allKeys methods."];
969+
fetchConfigsExpectation[i] = [self
970+
expectationWithDescription:[NSString
971+
stringWithFormat:@"Test allKeys methods - instance %d", i]];
948972
NSString *key1 = [NSString stringWithFormat:@"key1-%d", i];
949973
NSString *key0 = [NSString stringWithFormat:@"key0-%d", i];
950974
NSDictionary<NSString *, NSString *> *defaults = @{key1 : @"default key1", key0 : @"value0-0"};
@@ -995,7 +1019,7 @@ - (void)testAllKeysFromSource {
9951019
[_configInstances[i] allKeysFromSource:FIRRemoteConfigSourceDefault namespace:nil].count,
9961020
0);
9971021

998-
[fetchConfigsExpectation fulfill];
1022+
[fetchConfigsExpectation[i] fulfill];
9991023
};
10001024
[_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
10011025
}
@@ -1006,10 +1030,12 @@ - (void)testAllKeysFromSource {
10061030
}
10071031

10081032
- (void)testAllKeysWithPrefix {
1033+
NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
1034+
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
10091035
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
1010-
XCTestExpectation *fetchConfigsExpectation =
1011-
[self expectationWithDescription:@"Test allKeys methods."];
1012-
1036+
fetchConfigsExpectation[i] = [self
1037+
expectationWithDescription:[NSString
1038+
stringWithFormat:@"Test allKeys methods - instance %d", i]];
10131039
FIRRemoteConfigFetchCompletion fetchCompletion =
10141040
^void(FIRRemoteConfigFetchStatus status, NSError *error) {
10151041
XCTAssertEqual(status, FIRRemoteConfigFetchStatusSuccess);
@@ -1046,7 +1072,7 @@ - (void)testAllKeysWithPrefix {
10461072
XCTAssertEqual([_configInstances[i] keysWithPrefix:nil].count, 100);
10471073
XCTAssertEqual([_configInstances[i] keysWithPrefix:@""].count, 100);
10481074

1049-
[fetchConfigsExpectation fulfill];
1075+
[fetchConfigsExpectation[i] fulfill];
10501076
};
10511077
[_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
10521078
}
@@ -1074,10 +1100,12 @@ - (void)testSetDeveloperModeConfigSetting {
10741100

10751101
/// Test the minimum fetch interval is applied and read back correctly.
10761102
- (void)testSetMinimumFetchIntervalConfigSetting {
1103+
NSMutableArray<XCTestExpectation *> *fetchConfigsExpectation =
1104+
[[NSMutableArray alloc] initWithCapacity:RCNTestRCNumTotalInstances];
10771105
for (int i = 0; i < RCNTestRCNumTotalInstances; i++) {
1078-
XCTestExpectation *fetchConfigsExpectation =
1079-
[self expectationWithDescription:@"test minimumFetchInterval expectation."];
1080-
1106+
fetchConfigsExpectation[i] = [self
1107+
expectationWithDescription:
1108+
[NSString stringWithFormat:@"Test minimumFetchInterval expectation - instance %d", i]];
10811109
FIRRemoteConfigSettings *settings = [[FIRRemoteConfigSettings alloc] init];
10821110
settings.minimumFetchInterval = 123;
10831111
[_configInstances[i] setConfigSettings:settings];
@@ -1093,7 +1121,7 @@ - (void)testSetMinimumFetchIntervalConfigSetting {
10931121
[_configInstances[i] setConfigSettings:settings];
10941122
XCTAssertEqual([_configInstances[i] configSettings].minimumFetchInterval, 0);
10951123
XCTAssertTrue([_configInstances[i].settings hasMinimumFetchIntervalElapsed:0]);
1096-
[fetchConfigsExpectation fulfill];
1124+
[fetchConfigsExpectation[i] fulfill];
10971125
};
10981126
[_configInstances[i] fetchWithExpirationDuration:43200 completionHandler:fetchCompletion];
10991127
}

0 commit comments

Comments
 (0)