FTestHelpers.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "FirebaseAuth/Interop/FIRAuthInterop.h"
  18. #import "FirebaseCore/Extension/FirebaseCoreInternal.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 "SharedTestUtilities/AppCheckFake/FIRAppCheckFake.h"
  23. #import "SharedTestUtilities/AppCheckFake/FIRAppCheckTokenResultFake.h"
  24. #import "SharedTestUtilities/FIRAuthInteropFake.h"
  25. @implementation FTestHelpers
  26. + (NSTimeInterval)waitUntil:(BOOL (^)(void))predicate timeout:(NSTimeInterval)seconds {
  27. NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
  28. NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
  29. NSTimeInterval timeoutTime = [timeoutDate timeIntervalSinceReferenceDate];
  30. NSTimeInterval currentTime;
  31. for (currentTime = [NSDate timeIntervalSinceReferenceDate];
  32. !predicate() && currentTime < timeoutTime;
  33. currentTime = [NSDate timeIntervalSinceReferenceDate]) {
  34. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  35. beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
  36. }
  37. NSTimeInterval finish = [NSDate timeIntervalSinceReferenceDate];
  38. NSAssert(currentTime <= timeoutTime, @"Timed out");
  39. return (finish - start);
  40. }
  41. + (FIRDatabaseConfig *)defaultConfig {
  42. return [self configForName:@"default"];
  43. }
  44. + (FIRDatabaseConfig *)configForName:(NSString *)name {
  45. id<FIRAuthInterop> auth = [[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil];
  46. id<FIRAppCheckInterop> appCheck = [[FIRAppCheckFake alloc] init];
  47. id<FIRDatabaseConnectionContextProvider> contextProvider =
  48. [FIRDatabaseConnectionContextProvider contextProviderWithAuth:auth appCheck:appCheck];
  49. return [[FIRDatabaseConfig alloc] initWithSessionIdentifier:name
  50. googleAppID:@"fake-app-id"
  51. contextProvider:contextProvider];
  52. }
  53. + (NSString *)databaseURL {
  54. FIROptions *options = [FIROptions defaultOptions];
  55. if (options && ![options.databaseURL isEqualToString:@"https://abc-xyz-123.firebaseio.com"]) {
  56. return options.databaseURL;
  57. }
  58. // If no custom GoogleServices.plist is provided, we default to the Emulator URL.
  59. return @"http://localhost:9000";
  60. }
  61. + (FIRDatabase *)databaseForConfig:(FIRDatabaseConfig *)config {
  62. FParsedUrl *parsedUrl = [FUtilities parseUrl:[FTestHelpers databaseURL]];
  63. return [FIRDatabase createDatabaseForTests:parsedUrl.repoInfo config:config];
  64. }
  65. + (FIRDatabase *)defaultDatabase {
  66. static FIRDatabase *database = nil;
  67. if (database == nil) {
  68. database = [FTestHelpers databaseForConfig:[self defaultConfig]];
  69. }
  70. return database;
  71. }
  72. + (NSArray *)getRandomNodes:(int)num persistence:(BOOL)persistence {
  73. static dispatch_once_t pred = 0;
  74. static NSMutableArray *persistenceRefs = nil;
  75. static NSMutableArray *noPersistenceRefs = nil;
  76. dispatch_once(&pred, ^{
  77. persistenceRefs = [[NSMutableArray alloc] init];
  78. noPersistenceRefs = [[NSMutableArray alloc] init];
  79. // Uncomment the following line to run tests against a background thread
  80. //[Firebase setDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
  81. });
  82. NSMutableArray *refs = (persistence) ? persistenceRefs : noPersistenceRefs;
  83. while (num > refs.count) {
  84. NSString *sessionIdentifier =
  85. [NSString stringWithFormat:@"test-config-%@persistence-%lu", (persistence) ? @"" : @"no-",
  86. (unsigned long)refs.count];
  87. FIRDatabaseConfig *config = [self configForName:sessionIdentifier];
  88. config.persistenceEnabled = persistence;
  89. FIRDatabaseReference *ref = [[self databaseForConfig:config] reference];
  90. [refs addObject:ref];
  91. }
  92. NSMutableArray *results = [[NSMutableArray alloc] init];
  93. NSString *name = nil;
  94. for (int i = 0; i < num; ++i) {
  95. FIRDatabaseReference *ref = [refs objectAtIndex:i];
  96. if (!name) {
  97. name = [ref childByAutoId].key;
  98. }
  99. [results addObject:[ref child:name]];
  100. }
  101. return results;
  102. }
  103. // Helpers
  104. + (FIRDatabaseReference *)getRandomNode {
  105. NSArray *refs = [self getRandomNodes:1 persistence:YES];
  106. return [refs objectAtIndex:0];
  107. }
  108. + (FIRDatabaseReference *)getRandomNodeWithoutPersistence {
  109. NSArray *refs = [self getRandomNodes:1 persistence:NO];
  110. return refs[0];
  111. }
  112. + (FTupleFirebase *)getRandomNodePair {
  113. NSArray *refs = [self getRandomNodes:2 persistence:YES];
  114. FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
  115. tuple.one = [refs objectAtIndex:0];
  116. tuple.two = [refs objectAtIndex:1];
  117. return tuple;
  118. }
  119. + (FTupleFirebase *)getRandomNodePairWithoutPersistence {
  120. NSArray *refs = [self getRandomNodes:2 persistence:NO];
  121. FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
  122. tuple.one = refs[0];
  123. tuple.two = refs[1];
  124. return tuple;
  125. }
  126. + (FTupleFirebase *)getRandomNodeTriple {
  127. NSArray *refs = [self getRandomNodes:3 persistence:YES];
  128. FTupleFirebase *triple = [[FTupleFirebase alloc] init];
  129. triple.one = [refs objectAtIndex:0];
  130. triple.two = [refs objectAtIndex:1];
  131. triple.three = [refs objectAtIndex:2];
  132. return triple;
  133. }
  134. + (id<FNode>)leafNodeOfSize:(NSUInteger)size {
  135. NSMutableString *string = [NSMutableString string];
  136. NSString *pattern = @"abdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  137. for (NSUInteger i = 0; i < size - pattern.length; i = i + pattern.length) {
  138. [string appendString:pattern];
  139. }
  140. NSUInteger remainingLength = size - string.length;
  141. [string appendString:[pattern substringToIndex:remainingLength]];
  142. return [FSnapshotUtilities nodeFrom:string];
  143. }
  144. @end