Skip to content

Commit 98270a5

Browse files
committed
Restore thread safety to unit tests
1 parent b9d329e commit 98270a5

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

Example/Core/Tests/FIRAppTest.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#import "FIRTestCase.h"
16+
#import "FIRTestComponents.h"
1617

1718
#import <FirebaseCore/FIRAnalyticsConfiguration+Internal.h>
1819
#import <FirebaseCore/FIRAppInternal.h>
@@ -798,6 +799,17 @@ - (void)testMultipleLibraries {
798799
XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"LegalName/1.0.0 LegalName2/2.0.0"]);
799800
}
800801

802+
- (void)testRegisteringConformingLibrary {
803+
Class testClass = [FIRTestClass class];
804+
[FIRApp registerInternalLibrary:testClass withName:@"LegalName" withVersion:@"1.0.0"];
805+
XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"LegalName/1.0.0"]);
806+
}
807+
808+
- (void)testRegisteringNonConformingLibrary {
809+
XCTAssertThrows([FIRApp registerInternalLibrary:[NSString class] withName:@"IllegalLibrary" withVersion:@"1.0.0"]);
810+
XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"LegalName/1.0.0"]);
811+
}
812+
801813
#pragma mark - private
802814

803815
- (void)expectNotificationForObserver:(id)observer

Example/Core/Tests/FIRComponentContainerTest.m

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ @interface FIRComponentContainer (TestInternal)
2626
@property(nonatomic, strong) NSMutableDictionary<NSString *, FIRComponentCreationBlock> *components;
2727
@property(nonatomic, strong) NSMutableDictionary<NSString *, id> *cachedInstances;
2828

29-
+ (void)registerAsComponentRegistrant:(Class)klass inSet:(NSMutableSet<Class> *)allRegistrants;
29+
+ (void)registerAsComponentRegistrant:(Class<FIRLibrary>)klass
30+
inSet:(NSMutableSet<Class> *)allRegistrants;
3031
- (instancetype)initWithApp:(FIRApp *)app registrants:(NSMutableSet<Class> *)allRegistrants;
31-
+ (void)setRegistrantSet:(NSMutableSet<Class> *)testInput;
32-
+ (NSMutableSet<Class> *)registrantSet;
3332
@end
3433

3534
@interface FIRComponentContainer (TestInternalImplementations)
@@ -59,17 +58,10 @@ @implementation FIRComponentContainerTest
5958
#pragma mark - Registration Tests
6059

6160
- (void)testRegisteringConformingClass {
62-
[FIRComponentContainer setRegistrantSet:[NSMutableSet<Class> set]];
63-
Class testClass = [FIRTestClass class];
64-
[FIRComponentContainer registerAsComponentRegistrant:testClass];
65-
XCTAssertTrue([[FIRComponentContainer registrantSet] containsObject:testClass]);
66-
}
67-
68-
- (void)testRegisteringNonConformingClass {
6961
NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
70-
XCTAssertThrows(
71-
[FIRComponentContainer registerAsComponentRegistrant:[NSString class] inSet:allRegistrants]);
72-
XCTAssertTrue(allRegistrants.count == 0);
62+
Class testClass = [FIRTestClass class];
63+
[FIRComponentContainer registerAsComponentRegistrant:testClass inSet:allRegistrants];
64+
XCTAssertTrue([allRegistrants containsObject:testClass]);
7365
}
7466

7567
- (void)testComponentsPopulatedOnInit {
@@ -161,15 +153,13 @@ - (void)testProtocolAlreadyRegistered {
161153
/// Create a container that has registered the test class.
162154
- (FIRComponentContainer *)containerWithRegistrants:(NSArray<Class> *)registrants {
163155
id appMock = OCMClassMock([FIRApp class]);
164-
[FIRComponentContainer setRegistrantSet:[NSMutableSet<Class> set]];
156+
NSMutableSet<Class> *allRegistrants = [NSMutableSet<Class> set];
165157

166158
// Initialize the container with the test classes.
167159
for (Class c in registrants) {
168-
[FIRComponentContainer registerAsComponentRegistrant:c];
160+
[FIRComponentContainer registerAsComponentRegistrant:c inSet:allRegistrants];
169161
}
170-
171-
return [[FIRComponentContainer alloc] initWithApp:appMock
172-
registrants:[FIRComponentContainer registrantSet]];
162+
return [[FIRComponentContainer alloc] initWithApp:appMock registrants:allRegistrants];
173163
}
174164

175165
@end

Firebase/Core/FIRComponentContainer.m

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ + (void)registerAsComponentRegistrant:(Class<FIRLibrary>)klass {
4747
sFIRComponentRegistrants = [[NSMutableSet<Class> alloc] init];
4848
});
4949

50-
[sFIRComponentRegistrants addObject:klass];
50+
[self registerAsComponentRegistrant:klass inSet:sFIRComponentRegistrants];
51+
}
52+
53+
+ (void)registerAsComponentRegistrant:(Class<FIRLibrary>)klass
54+
inSet:(NSMutableSet<Class> *)allRegistrants {
55+
[allRegistrants addObject:klass];
5156
}
5257

5358
#pragma mark - Internal Initialization
@@ -166,16 +171,6 @@ - (void)removeAllCachedInstances {
166171
[self.cachedInstances removeAllObjects];
167172
}
168173

169-
#pragma mark - Testing Accessors
170-
171-
+ (void)setRegistrantSet:(NSMutableSet<Class> *)testInput {
172-
sFIRComponentRegistrants = testInput;
173-
}
174-
175-
+ (NSMutableSet<Class> *)registrantSet {
176-
return sFIRComponentRegistrants;
177-
}
178-
179174
@end
180175

181176
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)