Skip to content

Commit 32d465b

Browse files
committed
Restore FIRCLSSettingsTests.m
1 parent 3883034 commit 32d465b

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

Crashlytics/UnitTests/FIRCLSSettingsTests.m

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,33 +127,27 @@ - (BOOL)writeSettings:(const NSString *)settings
127127
- (void)cacheSettingsWithGoogleAppID:(NSString *)googleAppID
128128
currentTimestamp:(NSTimeInterval)currentTimestamp
129129
expectedRemoveCount:(NSInteger)expectedRemoveCount {
130+
self.fileManager.removeExpectation = [[XCTestExpectation alloc]
131+
initWithDescription:@"FIRCLSMockFileManager.removeExpectation.cache"];
130132
self.fileManager.removeCount = 0;
131-
132-
XCTestExpectation *expectation =
133-
[self expectationForNotification:FIRCLSMockFileManagerDidRemoveItemNotification
134-
object:nil
135-
handler:nil];
136-
expectation.expectedFulfillmentCount = expectedRemoveCount;
133+
self.fileManager.expectedRemoveCount = expectedRemoveCount;
137134

138135
[self.settings cacheSettingsWithGoogleAppID:googleAppID currentTimestamp:currentTimestamp];
139136

140-
[self waitForExpectations:@[ expectation ] timeout:16.0];
137+
[self waitForExpectations:@[ self.fileManager.removeExpectation ] timeout:1];
141138
}
142139

143140
- (void)reloadFromCacheWithGoogleAppID:(NSString *)googleAppID
144141
currentTimestamp:(NSTimeInterval)currentTimestamp
145142
expectedRemoveCount:(NSInteger)expectedRemoveCount {
143+
self.fileManager.removeExpectation = [[XCTestExpectation alloc]
144+
initWithDescription:@"FIRCLSMockFileManager.removeExpectation.reload"];
146145
self.fileManager.removeCount = 0;
147-
148-
XCTestExpectation *expectation =
149-
[self expectationForNotification:FIRCLSMockFileManagerDidRemoveItemNotification
150-
object:nil
151-
handler:nil];
152-
expectation.expectedFulfillmentCount = expectedRemoveCount;
146+
self.fileManager.expectedRemoveCount = expectedRemoveCount;
153147

154148
[self.settings reloadFromCacheWithGoogleAppID:googleAppID currentTimestamp:currentTimestamp];
155149

156-
[self waitForExpectations:@[ expectation ] timeout:14.0];
150+
[self waitForExpectations:@[ self.fileManager.removeExpectation ] timeout:5.0];
157151
}
158152

159153
- (void)testActivatedSettingsCached {
@@ -211,6 +205,10 @@ - (void)testCacheExpiredFromTTL {
211205
[self writeSettings:FIRCLSTestSettingsActivated error:&error];
212206
XCTAssertNil(error, "%@", error);
213207

208+
// 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
209+
// cache and cache key
210+
self.fileManager.expectedRemoveCount = 3;
211+
214212
NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
215213
[self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
216214

@@ -240,6 +238,10 @@ - (void)testCacheExpiredFromBuildInstanceID {
240238
[self writeSettings:FIRCLSTestSettingsActivated error:&error];
241239
XCTAssertNil(error, "%@", error);
242240

241+
// 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
242+
// cache and cache key
243+
self.fileManager.expectedRemoveCount = 3;
244+
243245
NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
244246
[self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
245247

@@ -270,6 +272,10 @@ - (void)testCacheExpiredFromAppVersion {
270272
[self writeSettings:FIRCLSTestSettingsActivated error:&error];
271273
XCTAssertNil(error, "%@", error);
272274

275+
// 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
276+
// cache and cache key
277+
self.fileManager.expectedRemoveCount = 3;
278+
273279
NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
274280
[self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
275281

@@ -296,7 +302,6 @@ - (void)testCacheExpiredFromAppVersion {
296302
XCTAssertEqual(self.settings.errorLogBufferSize, 128000);
297303
}
298304

299-
#ifdef FLAKY_TEST
300305
- (void)testGoogleAppIDChanged {
301306
NSError *error = nil;
302307
[self writeSettings:FIRCLSTestSettingsInverse error:&error];
@@ -382,7 +387,7 @@ - (void)testCorruptCache {
382387
// Cache them, and reload. Since it's corrupted we should delete it all
383388
[self cacheSettingsWithGoogleAppID:TestGoogleAppID
384389
currentTimestamp:currentTimestamp
385-
expectedRemoveCount:3];
390+
expectedRemoveCount:2];
386391

387392
// Should have default values because we deleted the cache and settingsDictionary
388393
XCTAssertEqual(self.settings.isCacheExpired, YES);
@@ -424,7 +429,6 @@ - (void)testCorruptCacheKey {
424429
XCTAssertEqual(self.settings.onDemandBackoffBase, 1.5);
425430
XCTAssertEqual(self.settings.onDemandBackoffStepDuration, 6);
426431
}
427-
#endif // FLAKY_TEST
428432

429433
- (void)testNewReportEndpointSettings {
430434
NSString *settingsJSON =

Crashlytics/UnitTests/Mocks/FIRCLSMockFileManager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ extern NSNotificationName const FIRCLSMockFileManagerDidRemoveItemNotification;
2424
// Incremented when a remove happens with removeItemAtPath
2525
@property(nonatomic) NSInteger removeCount;
2626

27+
// Number of calls to removeItemAtPath are expected for the unit test
28+
@property(nonatomic) NSInteger expectedRemoveCount;
29+
30+
// Will be fulfilled when the expected number of removes have happened
31+
// using removeItemAtPath
32+
//
33+
// Users should initialize this in their test.
34+
@property(nonatomic, strong) XCTestExpectation *removeExpectation;
35+
2736
@end

Crashlytics/UnitTests/Mocks/FIRCLSMockFileManager.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ - (BOOL)removeItemAtPath:(NSString *)path {
4040
@synchronized(self) {
4141
[self.fileSystemDict removeObjectForKey:path];
4242
self.removeCount += 1;
43+
44+
// If we set up the expectation, and we went over the expected count or removes, fulfill the
45+
// expectation
46+
if (self.removeExpectation && self.removeCount >= self.expectedRemoveCount) {
47+
[self.removeExpectation fulfill];
48+
}
4349
}
4450

4551
[[NSNotificationCenter defaultCenter]

0 commit comments

Comments
 (0)