Skip to content

Commit 4090195

Browse files
authored
Database Interop (#1865)
* Register Database with Interop + Add AuthInterop dependency + Have Database register with Interop component container + Add weak dependency on Auth ~ Cleaned up imports * Use Interop for Auth token fetching + Use macro to get auth component ~ Change `getTokenForcingRefresh:withCallback:` call to use Interop component version of Auth ~ Clean up imports * Implement necessary protocols + Added FIRComponentRegistrant and FIRDatabaseNilProtocol to FIRDatabase * Squash of my previous local commits for Database Interop Register Database with Interop: + Add AuthInterop dependency + Have Database register with Interop component container + Add weak dependency on Auth ~ Cleaned up imports Use Interop for Auth token fetching: + Use macro to get auth component ~ Change `getTokenForcingRefresh:withCallback:` call to use Interop component version of Auth ~ Clean up imports Implement necessary protocols: + Added FIRComponentRegistrant and FIRDatabaseNilProtocol to FIRDatabase Clean up Database tests: - Removed all unnecessary header files ~ Ran style.sh across all the tests Cleaned Up project file: Some header removals were missed in the last commit. Sorted some test files Fix Database Fake App Tests: + Added container property + Added an Auth fake in the container + Added the Shared utils files to the necessary targets + Added a missing XCTest reference in a test that didn't compile for me * Revert "Squash of my previous local commits for Database Interop" This reverts commit c0d0421. * Cleaned Up project file Some header removals were missed in the last commit. * Sorted some test files * Fix Database Fake App Tests + Added container property + Added an Auth fake in the container + Added the Shared utils files to the necessary targets + Added a missing XCTest reference in a test that didn't compile for me * PR Feedback Changes + Created new FIRDatabaseComponent class to encapsulate instance creation + Updated FAuthTokenProvider to only use an auth instance rather than an app. * PR Feedback Follow-Up - Removed FIRDatabase registration code * pod deintegrate * Move instance management Somes tests may still be out of date. * Fix Auth integration test * pod deintegrate -- again * PR Feedback * PR Feedback Added the FIRComponentLifecycleMaintainer protocol to FIRDatabaseComponent * Update Firebase/Database/Api/FIRDatabaseComponent.m Missed some small changes * Get integration tests compiling * Fix some tests * Fixed more tests Component needs to be cacheable in order to get the appWIllBeDeleted callback. * Update target memberships * Revert project file changes * Add FIRAuthInteropFake for broken targets * PR feedback * Spacing and Style * Moved `instances` to ivar * Simplify FIRDatabaseDictionary It's now keyed on the URL with the app being implied since each app will get its own `FIRDatabaseComponent`
1 parent e0d7975 commit 4090195

26 files changed

+482
-338
lines changed

Example/Database/Tests/Helpers/FDevice.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
@class FIRDatabaseReference;
2020
@class SenTest;
21+
@class XCTest;
2122

2223
@interfaceFDevice : NSObject
2324
- (id)initOnline;

Example/Database/Tests/Helpers/FDevice.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ - (id)initOfflineWithUrl:(NSString *)firebaseUrl {
5858
- (id)initWithUrl:(NSString *)firebaseUrlandOnline:(BOOL)online {
5959
self = [superinit];
6060
if (self) {
61-
config = [FIRDatabaseConfigconfigForName:[NSStringstringWithFormat:@"device-%lu", deviceId++]];
61+
config = [FTestHelpersconfigForName:[NSStringstringWithFormat:@"device-%lu", deviceId++]];
6262
config.persistenceEnabled = YES;
6363
url = firebaseUrl;
6464
isOnline = online;

Example/Database/Tests/Helpers/FIRFakeApp.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#import<Foundation/Foundation.h>
1818

19+
@class FIRComponentContainer;
1920
@class FIRFakeOptions;
2021

2122
@interfaceFIRFakeApp : NSObject
@@ -24,4 +25,5 @@
2425

2526
@property(nonatomic, readonly) FIRFakeOptions *options;
2627
@property(nonatomic, copy, readonly) NSString *name;
28+
@property(nonatomic, readonly) FIRComponentContainer *container;
2729
@end

Example/Database/Tests/Helpers/FIRFakeApp.m

+34-18
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,49 @@
1616

1717
#import"FIRFakeApp.h"
1818

19+
#import"FIRAuthInteropFake.h"
20+
#import"FIRComponentTestUtilities.h"
21+
#import"FIRDatabaseComponent.h"
22+
1923
@interfaceFIRFakeOptions: NSObject
2024
@property(nonatomic, readonly, copy) NSString *databaseURL;
21-
- (instancetype)initWithURL:(NSString *)url;
25+
- (instancetype)initWithURL:(NSString *)url;
2226
@end
2327

2428
@implementationFIRFakeOptions
25-
- (instancetype)initWithURL:(NSString *)url {
26-
self = [superinit];
27-
if (self) {
28-
self->_databaseURL = url;
29-
}
30-
return self;
29+
- (instancetype)initWithURL:(NSString *)url {
30+
self = [superinit];
31+
if (self) {
32+
_databaseURL = url;
33+
}
34+
return self;
3135
}
3236
@end
3337

34-
@implementationFIRFakeApp
38+
@interfaceFIRDatabaseComponent (Internal)
39+
- (instancetype)initWithApp:(FIRApp *)app;
40+
@end
3541

36-
- (instancetype) initWithName:(NSString *)nameURL:(NSString *)url {
37-
self = [superinit];
38-
if (self) {
39-
self->_name = name;
40-
self->_options = [[FIRFakeOptions alloc] initWithURL:url];
41-
}
42-
return self;
43-
}
42+
@implementationFIRFakeApp
4443

45-
- (void)getTokenForcingRefresh:(BOOL)forceRefreshwithCallback:(void (^)(NSString *_Nullable token, NSError *_Nullable error))callback {
46-
callback(nil, nil);
44+
- (instancetype)initWithName:(NSString *)nameURL:(NSString *)url {
45+
self = [superinit];
46+
if (self) {
47+
_name = name;
48+
_options = [[FIRFakeOptions alloc] initWithURL:url];
49+
_Nullable id (^authBlock)(FIRComponentContainer *, BOOL *) =
50+
^(FIRComponentContainer *container, BOOL *isCacheable) {
51+
return [[FIRAuthInteropFake alloc] initWithToken:niluserID:nilerror:nil];
52+
};
53+
FIRComponentCreationBlock databaseBlock =
54+
^id_Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
55+
return [[FIRDatabaseComponent alloc] initWithApp:container.app];
56+
};
57+
NSDictionary<NSString *, FIRComponentCreationBlock> *components =
58+
@{NSStringFromProtocol(@protocol(FIRAuthInterop)) : authBlock,
59+
NSStringFromProtocol(@protocol(FIRDatabaseProvider)) : databaseBlock};
60+
_container = [[FIRComponentContainer alloc] initWithApp:(FIRApp *)selfcomponents:components];
61+
}
62+
return self;
4763
}
4864
@end

Example/Database/Tests/Helpers/FTestHelpers.h

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#definePATH(__path) [FPath pathWithString:(__path)]
2929

3030
@interfaceFTestHelpers : XCTestCase
31+
+ (FIRDatabaseConfig *) defaultConfig;
32+
+ (FIRDatabaseConfig *) configForName:(NSString *)name;
3133
+ (FIRDatabaseReference *) getRandomNode;
3234
+ (FIRDatabaseReference *) getRandomNodeWithoutPersistence;
3335
+ (FTupleFirebase *) getRandomNodePair;

Example/Database/Tests/Helpers/FTestHelpers.m

+102-79
Original file line numberDiff line numberDiff line change
@@ -15,118 +15,141 @@
1515
*/
1616

1717
#import"FTestHelpers.h"
18-
#import"FConstants.h"
19-
#import<FirebaseCore/FIRApp.h>
18+
19+
#import<FirebaseAuthInterop/FIRAuthInterop.h>
20+
#import<FirebaseCore/FIRAppInternal.h>
21+
#import<FirebaseCore/FIRComponent.h>
22+
#import<FirebaseCore/FIRComponentContainer.h>
2023
#import<FirebaseCore/FIROptions.h>
24+
25+
#import"FConstants.h"
26+
#import"FIRAuthInteropFake.h"
2127
#import"FIRDatabaseConfig_Private.h"
2228
#import"FTestAuthTokenGenerator.h"
2329

2430
@implementationFTestHelpers
2531

26-
+ (NSTimeInterval)waitUntil:(BOOL (^)())predicatetimeout:(NSTimeInterval)seconds {
27-
NSTimeInterval start = [NSDatetimeIntervalSinceReferenceDate];
28-
NSDate *timeoutDate = [NSDatedateWithTimeIntervalSinceNow:seconds];
29-
NSTimeInterval timeoutTime = [timeoutDate timeIntervalSinceReferenceDate];
30-
NSTimeInterval currentTime;
32+
+ (NSTimeInterval)waitUntil:(BOOL (^)())predicatetimeout:(NSTimeInterval)seconds {
33+
NSTimeInterval start = [NSDatetimeIntervalSinceReferenceDate];
34+
NSDate *timeoutDate = [NSDatedateWithTimeIntervalSinceNow:seconds];
35+
NSTimeInterval timeoutTime = [timeoutDate timeIntervalSinceReferenceDate];
36+
NSTimeInterval currentTime;
3137

32-
for (currentTime = [NSDatetimeIntervalSinceReferenceDate];
33-
!predicate() && currentTime < timeoutTime;
34-
currentTime = [NSDatetimeIntervalSinceReferenceDate]) {
35-
[[NSRunLoopcurrentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDatedateWithTimeIntervalSinceNow:0.25]];
36-
}
38+
for (currentTime = [NSDatetimeIntervalSinceReferenceDate];
39+
!predicate() && currentTime < timeoutTime;
40+
currentTime = [NSDatetimeIntervalSinceReferenceDate]) {
41+
[[NSRunLoopcurrentRunLoop] runMode:NSDefaultRunLoopMode
42+
beforeDate:[NSDatedateWithTimeIntervalSinceNow:0.25]];
43+
}
3744

38-
NSTimeInterval finish = [NSDatetimeIntervalSinceReferenceDate];
45+
NSTimeInterval finish = [NSDatetimeIntervalSinceReferenceDate];
3946

40-
NSAssert(currentTime <= timeoutTime, @"Timed out");
47+
NSAssert(currentTime <= timeoutTime, @"Timed out");
4148

42-
return (finish - start);
49+
return (finish - start);
4350
}
4451

45-
+ (NSArray*) getRandomNodes:(int)numpersistence:(BOOL)persistence {
46-
staticdispatch_once_t pred = 0;
47-
staticNSMutableArray *persistenceRefs = nil;
48-
staticNSMutableArray *noPersistenceRefs = nil;
49-
dispatch_once(&pred, ^{
50-
persistenceRefs = [[NSMutableArrayalloc] init];
51-
noPersistenceRefs = [[NSMutableArrayalloc] init];
52-
// Uncomment the following line to run tests against a background thread
53-
//[Firebase setDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
54-
});
55-
56-
NSMutableArray *refs = (persistence) ? persistenceRefs : noPersistenceRefs;
57-
58-
id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderForApp:[FIRApp defaultApp]];
59-
60-
while (num > refs.count) {
61-
NSString *sessionIdentifier = [NSStringstringWithFormat:@"test-config-%@persistence-%lu", (persistence) ? @"" : @"no-", refs.count];
62-
FIRDatabaseConfig *config = [[FIRDatabaseConfig alloc] initWithSessionIdentifier:sessionIdentifier authTokenProvider:authTokenProvider];
63-
config.persistenceEnabled = persistence;
64-
FIRDatabaseReference * ref = [[FIRDatabaseReference alloc] initWithConfig:config];
65-
[refs addObject:ref];
66-
}
52+
+ (FIRDatabaseConfig *)defaultConfig {
53+
return [selfconfigForName:@"default"];
54+
}
6755

68-
NSMutableArray* results = [[NSMutableArrayalloc] init];
69-
NSString* name = nil;
70-
for (int i = 0; i < num; ++i) {
71-
FIRDatabaseReference * ref = [refs objectAtIndex:i];
72-
if (!name) {
73-
name = [ref childByAutoId].key;
74-
}
75-
[results addObject:[ref child:name]];
56+
+ (FIRDatabaseConfig *)configForName:(NSString *)name {
57+
id<FIRAuthInterop> auth = [[FIRAuthInteropFake alloc] initWithToken:niluserID:nilerror:nil];
58+
id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderWithAuth:auth];
59+
return [[FIRDatabaseConfig alloc] initWithSessionIdentifier:name
60+
authTokenProvider:authTokenProvider];
61+
}
62+
63+
+ (NSArray *)getRandomNodes:(int)numpersistence:(BOOL)persistence {
64+
staticdispatch_once_t pred = 0;
65+
staticNSMutableArray *persistenceRefs = nil;
66+
staticNSMutableArray *noPersistenceRefs = nil;
67+
dispatch_once(&pred, ^{
68+
persistenceRefs = [[NSMutableArrayalloc] init];
69+
noPersistenceRefs = [[NSMutableArrayalloc] init];
70+
// Uncomment the following line to run tests against a background thread
71+
//[Firebase setDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
72+
});
73+
74+
NSMutableArray *refs = (persistence) ? persistenceRefs : noPersistenceRefs;
75+
76+
id<FIRAuthInterop> auth = [[FIRAuthInteropFake alloc] initWithToken:niluserID:nilerror:nil];
77+
id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderWithAuth:auth];
78+
79+
while (num > refs.count) {
80+
NSString *sessionIdentifier =
81+
[NSStringstringWithFormat:@"test-config-%@persistence-%lu", (persistence) ? @"" : @"no-",
82+
refs.count];
83+
FIRDatabaseConfig *config =
84+
[[FIRDatabaseConfig alloc] initWithSessionIdentifier:sessionIdentifier
85+
authTokenProvider:authTokenProvider];
86+
config.persistenceEnabled = persistence;
87+
FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithConfig:config];
88+
[refs addObject:ref];
89+
}
90+
91+
NSMutableArray *results = [[NSMutableArrayalloc] init];
92+
NSString *name = nil;
93+
for (int i = 0; i < num; ++i) {
94+
FIRDatabaseReference *ref = [refs objectAtIndex:i];
95+
if (!name) {
96+
name = [ref childByAutoId].key;
7697
}
77-
return results;
98+
[results addObject:[ref child:name]];
99+
}
100+
return results;
78101
}
79102

80103
// Helpers
81-
+ (FIRDatabaseReference *)getRandomNode {
82-
NSArray* refs = [selfgetRandomNodes:1persistence:YES];
83-
return [refs objectAtIndex:0];
104+
+ (FIRDatabaseReference *)getRandomNode {
105+
NSArray *refs = [selfgetRandomNodes:1persistence:YES];
106+
return [refs objectAtIndex:0];
84107
}
85108

86-
+ (FIRDatabaseReference *)getRandomNodeWithoutPersistence {
87-
NSArray* refs = [selfgetRandomNodes:1persistence:NO];
88-
return refs[0];
109+
+ (FIRDatabaseReference *)getRandomNodeWithoutPersistence {
110+
NSArray *refs = [selfgetRandomNodes:1persistence:NO];
111+
return refs[0];
89112
}
90113

91-
+ (FTupleFirebase *)getRandomNodePair {
92-
NSArray* refs = [selfgetRandomNodes:2persistence:YES];
114+
+ (FTupleFirebase *)getRandomNodePair {
115+
NSArray *refs = [selfgetRandomNodes:2persistence:YES];
93116

94-
FTupleFirebase* tuple = [[FTupleFirebase alloc] init];
95-
tuple.one = [refs objectAtIndex:0];
96-
tuple.two = [refs objectAtIndex:1];
117+
FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
118+
tuple.one = [refs objectAtIndex:0];
119+
tuple.two = [refs objectAtIndex:1];
97120

98-
return tuple;
121+
return tuple;
99122
}
100123

101-
+ (FTupleFirebase *)getRandomNodePairWithoutPersistence {
102-
NSArray* refs = [selfgetRandomNodes:2persistence:NO];
124+
+ (FTupleFirebase *)getRandomNodePairWithoutPersistence {
125+
NSArray *refs = [selfgetRandomNodes:2persistence:NO];
103126

104-
FTupleFirebase* tuple = [[FTupleFirebase alloc] init];
105-
tuple.one = refs[0];
106-
tuple.two = refs[1];
127+
FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
128+
tuple.one = refs[0];
129+
tuple.two = refs[1];
107130

108-
return tuple;
131+
return tuple;
109132
}
110133

111-
+ (FTupleFirebase *)getRandomNodeTriple {
112-
NSArray* refs = [selfgetRandomNodes:3persistence:YES];
113-
FTupleFirebase* triple = [[FTupleFirebase alloc] init];
114-
triple.one = [refs objectAtIndex:0];
115-
triple.two = [refs objectAtIndex:1];
116-
triple.three = [refs objectAtIndex:2];
134+
+ (FTupleFirebase *)getRandomNodeTriple {
135+
NSArray *refs = [selfgetRandomNodes:3persistence:YES];
136+
FTupleFirebase *triple = [[FTupleFirebase alloc] init];
137+
triple.one = [refs objectAtIndex:0];
138+
triple.two = [refs objectAtIndex:1];
139+
triple.three = [refs objectAtIndex:2];
117140

118-
return triple;
141+
return triple;
119142
}
120143

121144
+ (id<FNode>)leafNodeOfSize:(NSUInteger)size {
122-
NSMutableString *string = [NSMutableStringstring];
123-
NSString *pattern = @"abdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
124-
for (NSUInteger i = 0; i < size - pattern.length; i = i + pattern.length) {
125-
[string appendString:pattern];
126-
}
127-
NSUInteger remainingLength = size - string.length;
128-
[string appendString:[pattern substringToIndex:remainingLength]];
129-
return [FSnapshotUtilities nodeFrom:string];
145+
NSMutableString *string = [NSMutableStringstring];
146+
NSString *pattern = @"abdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
147+
for (NSUInteger i = 0; i < size - pattern.length; i = i + pattern.length) {
148+
[string appendString:pattern];
149+
}
150+
NSUInteger remainingLength = size - string.length;
151+
[string appendString:[pattern substringToIndex:remainingLength]];
152+
return [FSnapshotUtilities nodeFrom:string];
130153
}
131154

132155
@end

Example/Database/Tests/Integration/FData.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -2529,7 +2529,7 @@ - (void) testUpdateAfterChildSet {
25292529

25302530
- (void) testDeltaSyncNoDataUpdatesAfterReconnect {
25312531
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
2532-
FIRDatabaseConfig *cfg = [FIRDatabaseConfigconfigForName:@"test-config"];
2532+
FIRDatabaseConfig *cfg = [FTestHelpersconfigForName:@"test-config"];
25332533
FIRDatabaseReference * ref2 = [[[FIRDatabaseReference alloc] initWithConfig:cfg] child:ref.key];
25342534
__block id data = @{ @"a": @1, @"b": @2, @"c": @{ @".priority": @3, @".value": @3}, @"d": @4 };
25352535
[selfwaitForCompletionOf:ref setValue:data];

Example/Database/Tests/Integration/FDotInfo.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ - (void) testCanWatchInfoConnected {
5151
}
5252

5353
- (void) testInfoConnectedGoesToFalseOnDisconnect {
54-
FIRDatabaseConfig *cfg = [FIRDatabaseConfigconfigForName:@"test-config"];
54+
FIRDatabaseConfig *cfg = [FTestHelpersconfigForName:@"test-config"];
5555
FIRDatabaseReference * rootRef = [[FIRDatabaseReference alloc] initWithConfig:cfg];
5656
__block BOOL everConnected = NO;
5757
__block NSMutableString *connectedHistory = [[NSMutableStringalloc] init];
@@ -78,7 +78,7 @@ - (void) testInfoConnectedGoesToFalseOnDisconnect {
7878
}
7979

8080
- (void) testInfoServerTimeOffset {
81-
FIRDatabaseConfig *cfg = [FIRDatabaseConfigconfigForName:@"test-config"];
81+
FIRDatabaseConfig *cfg = [FTestHelpersconfigForName:@"test-config"];
8282
FIRDatabaseReference * ref = [[FIRDatabaseReference alloc] initWithConfig:cfg];
8383

8484
// make sure childByAutoId works
@@ -103,8 +103,8 @@ - (void) testInfoServerTimeOffset {
103103
}
104104

105105
- (void) testManualConnectionManagement {
106-
FIRDatabaseConfig *cfg = [FIRDatabaseConfigconfigForName:@"test-config"];
107-
FIRDatabaseConfig *altCfg = [FIRDatabaseConfigconfigForName:@"alt-config"];
106+
FIRDatabaseConfig *cfg = [FTestHelpersconfigForName:@"test-config"];
107+
FIRDatabaseConfig *altCfg = [FTestHelpersconfigForName:@"alt-config"];
108108

109109
FIRDatabaseReference * ref = [[FIRDatabaseReference alloc] initWithConfig:cfg];
110110
FIRDatabaseReference * refAlt = [[FIRDatabaseReference alloc] initWithConfig:altCfg];

0 commit comments

Comments
 (0)
close