FTestHelpers.m 5.8 KB

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