FTestHelpers.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseDatabase/Tests/Helpers/FTestHelpers.h"
  17. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  18. #import "Interop/Auth/Public/FIRAuthInterop.h"
  19. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabase_Private.h"
  20. #import "FirebaseDatabase/Sources/Constants/FConstants.h"
  21. #import "FirebaseDatabase/Sources/FIRDatabaseConfig_Private.h"
  22. #import "FirebaseDatabase/Tests/Helpers/FTestAuthTokenGenerator.h"
  23. #import "SharedTestUtilities/AppCheckFake/FIRAppCheckFake.h"
  24. #import "SharedTestUtilities/AppCheckFake/FIRAppCheckTokenResultFake.h"
  25. #import "SharedTestUtilities/FIRAuthInteropFake.h"
  26. @implementation FTestHelpers
  27. + (NSTimeInterval)waitUntil:(BOOL (^)(void))predicate timeout:(NSTimeInterval)seconds {
  28. NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
  29. NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
  30. NSTimeInterval timeoutTime = [timeoutDate timeIntervalSinceReferenceDate];
  31. NSTimeInterval currentTime;
  32. for (currentTime = [NSDate timeIntervalSinceReferenceDate];
  33. !predicate() && currentTime < timeoutTime;
  34. currentTime = [NSDate timeIntervalSinceReferenceDate]) {
  35. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  36. beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
  37. }
  38. NSTimeInterval finish = [NSDate timeIntervalSinceReferenceDate];
  39. NSAssert(currentTime <= timeoutTime, @"Timed out");
  40. return (finish - start);
  41. }
  42. + (FIRDatabaseConfig *)defaultConfig {
  43. return [self configForName:@"default"];
  44. }
  45. + (FIRDatabaseConfig *)configForName:(NSString *)name {
  46. id<FIRAuthInterop> auth = [[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil];
  47. id<FIRAppCheckInterop> appCheck = [[FIRAppCheckFake alloc] init];
  48. id<FIRDatabaseConnectionContextProvider> contextProvider =
  49. [FIRDatabaseConnectionContextProvider contextProviderWithAuth:auth appCheck:appCheck];
  50. return [[FIRDatabaseConfig alloc] initWithSessionIdentifier:name
  51. googleAppID:@"fake-app-id"
  52. contextProvider:contextProvider];
  53. }
  54. + (NSString *)databaseURL {
  55. FIROptions *options = [FIROptions defaultOptions];
  56. if (options && ![options.databaseURL isEqualToString:@"https://abc-xyz-123.firebaseio.com"]) {
  57. return options.databaseURL;
  58. }
  59. // If no custom GoogleServices.plist is provided, we default to the Emulator URL.
  60. return @"http://localhost:9000";
  61. }
  62. + (FIRDatabase *)databaseForConfig:(FIRDatabaseConfig *)config {
  63. FParsedUrl *parsedUrl = [FUtilities parseUrl:[FTestHelpers databaseURL]];
  64. return [FIRDatabase createDatabaseForTests:parsedUrl.repoInfo config:config];
  65. }
  66. + (FIRDatabase *)defaultDatabase {
  67. static FIRDatabase *database = nil;
  68. if (database == nil) {
  69. database = [FTestHelpers databaseForConfig:[self defaultConfig]];
  70. }
  71. return database;
  72. }
  73. + (NSArray *)getRandomNodes:(int)num persistence:(BOOL)persistence {
  74. static dispatch_once_t pred = 0;
  75. static NSMutableArray *persistenceRefs = nil;
  76. static NSMutableArray *noPersistenceRefs = nil;
  77. dispatch_once(&pred, ^{
  78. persistenceRefs = [[NSMutableArray alloc] init];
  79. noPersistenceRefs = [[NSMutableArray alloc] init];
  80. // Uncomment the following line to run tests against a background thread
  81. //[Firebase setDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
  82. });
  83. NSMutableArray *refs = (persistence) ? persistenceRefs : noPersistenceRefs;
  84. while (num > refs.count) {
  85. NSString *sessionIdentifier =
  86. [NSString stringWithFormat:@"test-config-%@persistence-%lu", (persistence) ? @"" : @"no-",
  87. (unsigned long)refs.count];
  88. FIRDatabaseConfig *config = [self configForName:sessionIdentifier];
  89. config.persistenceEnabled = persistence;
  90. FIRDatabaseReference *ref = [[self databaseForConfig:config] reference];
  91. [refs addObject:ref];
  92. }
  93. NSMutableArray *results = [[NSMutableArray alloc] init];
  94. NSString *name = nil;
  95. for (int i = 0; i < num; ++i) {
  96. FIRDatabaseReference *ref = [refs objectAtIndex:i];
  97. if (!name) {
  98. name = [ref childByAutoId].key;
  99. }
  100. [results addObject:[ref child:name]];
  101. }
  102. return results;
  103. }
  104. // Helpers
  105. + (FIRDatabaseReference *)getRandomNode {
  106. NSArray *refs = [self getRandomNodes:1 persistence:YES];
  107. return [refs objectAtIndex:0];
  108. }
  109. + (FIRDatabaseReference *)getRandomNodeWithoutPersistence {
  110. NSArray *refs = [self getRandomNodes:1 persistence:NO];
  111. return refs[0];
  112. }
  113. + (FTupleFirebase *)getRandomNodePair {
  114. NSArray *refs = [self getRandomNodes:2 persistence:YES];
  115. FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
  116. tuple.one = [refs objectAtIndex:0];
  117. tuple.two = [refs objectAtIndex:1];
  118. return tuple;
  119. }
  120. + (FTupleFirebase *)getRandomNodePairWithoutPersistence {
  121. NSArray *refs = [self getRandomNodes:2 persistence:NO];
  122. FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
  123. tuple.one = refs[0];
  124. tuple.two = refs[1];
  125. return tuple;
  126. }
  127. + (FTupleFirebase *)getRandomNodeTriple {
  128. NSArray *refs = [self getRandomNodes:3 persistence:YES];
  129. FTupleFirebase *triple = [[FTupleFirebase alloc] init];
  130. triple.one = [refs objectAtIndex:0];
  131. triple.two = [refs objectAtIndex:1];
  132. triple.three = [refs objectAtIndex:2];
  133. return triple;
  134. }
  135. + (id<FNode>)leafNodeOfSize:(NSUInteger)size {
  136. NSMutableString *string = [NSMutableString string];
  137. NSString *pattern = @"abdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  138. for (NSUInteger i = 0; i < size - pattern.length; i = i + pattern.length) {
  139. [string appendString:pattern];
  140. }
  141. NSUInteger remainingLength = size - string.length;
  142. [string appendString:[pattern substringToIndex:remainingLength]];
  143. return [FSnapshotUtilities nodeFrom:string];
  144. }
  145. @end