FTestHelpers.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "FTestAuthTokenGenerator.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<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderWithAuth:auth];
  48. return [[FIRDatabaseConfig alloc] initWithSessionIdentifier:name
  49. authTokenProvider:authTokenProvider];
  50. }
  51. + (NSArray *)getRandomNodes:(int)num persistence:(BOOL)persistence {
  52. static dispatch_once_t pred = 0;
  53. static NSMutableArray *persistenceRefs = nil;
  54. static NSMutableArray *noPersistenceRefs = nil;
  55. dispatch_once(&pred, ^{
  56. persistenceRefs = [[NSMutableArray alloc] init];
  57. noPersistenceRefs = [[NSMutableArray alloc] init];
  58. // Uncomment the following line to run tests against a background thread
  59. //[Firebase setDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
  60. });
  61. NSMutableArray *refs = (persistence) ? persistenceRefs : noPersistenceRefs;
  62. id<FIRAuthInterop> auth = [[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil];
  63. id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderWithAuth:auth];
  64. while (num > refs.count) {
  65. NSString *sessionIdentifier =
  66. [NSString stringWithFormat:@"test-config-%@persistence-%lu", (persistence) ? @"" : @"no-",
  67. (unsigned long)refs.count];
  68. FIRDatabaseConfig *config =
  69. [[FIRDatabaseConfig alloc] initWithSessionIdentifier:sessionIdentifier
  70. authTokenProvider:authTokenProvider];
  71. config.persistenceEnabled = persistence;
  72. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithConfig:config];
  73. [refs addObject:ref];
  74. }
  75. NSMutableArray *results = [[NSMutableArray alloc] init];
  76. NSString *name = nil;
  77. for (int i = 0; i < num; ++i) {
  78. FIRDatabaseReference *ref = [refs objectAtIndex:i];
  79. if (!name) {
  80. name = [ref childByAutoId].key;
  81. }
  82. [results addObject:[ref child:name]];
  83. }
  84. return results;
  85. }
  86. // Helpers
  87. + (FIRDatabaseReference *)getRandomNode {
  88. NSArray *refs = [self getRandomNodes:1 persistence:YES];
  89. return [refs objectAtIndex:0];
  90. }
  91. + (FIRDatabaseReference *)getRandomNodeWithoutPersistence {
  92. NSArray *refs = [self getRandomNodes:1 persistence:NO];
  93. return refs[0];
  94. }
  95. + (FTupleFirebase *)getRandomNodePair {
  96. NSArray *refs = [self getRandomNodes:2 persistence:YES];
  97. FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
  98. tuple.one = [refs objectAtIndex:0];
  99. tuple.two = [refs objectAtIndex:1];
  100. return tuple;
  101. }
  102. + (FTupleFirebase *)getRandomNodePairWithoutPersistence {
  103. NSArray *refs = [self getRandomNodes:2 persistence:NO];
  104. FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
  105. tuple.one = refs[0];
  106. tuple.two = refs[1];
  107. return tuple;
  108. }
  109. + (FTupleFirebase *)getRandomNodeTriple {
  110. NSArray *refs = [self getRandomNodes:3 persistence:YES];
  111. FTupleFirebase *triple = [[FTupleFirebase alloc] init];
  112. triple.one = [refs objectAtIndex:0];
  113. triple.two = [refs objectAtIndex:1];
  114. triple.three = [refs objectAtIndex:2];
  115. return triple;
  116. }
  117. + (id<FNode>)leafNodeOfSize:(NSUInteger)size {
  118. NSMutableString *string = [NSMutableString string];
  119. NSString *pattern = @"abdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  120. for (NSUInteger i = 0; i < size - pattern.length; i = i + pattern.length) {
  121. [string appendString:pattern];
  122. }
  123. NSUInteger remainingLength = size - string.length;
  124. [string appendString:[pattern substringToIndex:remainingLength]];
  125. return [FSnapshotUtilities nodeFrom:string];
  126. }
  127. @end