FTestHelpers.m 6.0 KB

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