FTestHelpers.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = [NSString stringWithFormat:@"test-config-%@persistence-%lu",
  66. (persistence) ? @"" : @"no-", (unsigned long)refs.count];
  67. FIRDatabaseConfig *config =
  68. [[FIRDatabaseConfig alloc] initWithSessionIdentifier:sessionIdentifier
  69. authTokenProvider:authTokenProvider];
  70. config.persistenceEnabled = persistence;
  71. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithConfig:config];
  72. [refs addObject:ref];
  73. }
  74. NSMutableArray *results = [[NSMutableArray alloc] init];
  75. NSString *name = nil;
  76. for (int i = 0; i < num; ++i) {
  77. FIRDatabaseReference *ref = [refs objectAtIndex:i];
  78. if (!name) {
  79. name = [ref childByAutoId].key;
  80. }
  81. [results addObject:[ref child:name]];
  82. }
  83. return results;
  84. }
  85. // Helpers
  86. + (FIRDatabaseReference *)getRandomNode {
  87. NSArray *refs = [self getRandomNodes:1 persistence:YES];
  88. return [refs objectAtIndex:0];
  89. }
  90. + (FIRDatabaseReference *)getRandomNodeWithoutPersistence {
  91. NSArray *refs = [self getRandomNodes:1 persistence:NO];
  92. return refs[0];
  93. }
  94. + (FTupleFirebase *)getRandomNodePair {
  95. NSArray *refs = [self getRandomNodes:2 persistence:YES];
  96. FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
  97. tuple.one = [refs objectAtIndex:0];
  98. tuple.two = [refs objectAtIndex:1];
  99. return tuple;
  100. }
  101. + (FTupleFirebase *)getRandomNodePairWithoutPersistence {
  102. NSArray *refs = [self getRandomNodes:2 persistence:NO];
  103. FTupleFirebase *tuple = [[FTupleFirebase alloc] init];
  104. tuple.one = refs[0];
  105. tuple.two = refs[1];
  106. return tuple;
  107. }
  108. + (FTupleFirebase *)getRandomNodeTriple {
  109. NSArray *refs = [self getRandomNodes:3 persistence:YES];
  110. FTupleFirebase *triple = [[FTupleFirebase alloc] init];
  111. triple.one = [refs objectAtIndex:0];
  112. triple.two = [refs objectAtIndex:1];
  113. triple.three = [refs objectAtIndex:2];
  114. return triple;
  115. }
  116. + (id<FNode>)leafNodeOfSize:(NSUInteger)size {
  117. NSMutableString *string = [NSMutableString string];
  118. NSString *pattern = @"abdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  119. for (NSUInteger i = 0; i < size - pattern.length; i = i + pattern.length) {
  120. [string appendString:pattern];
  121. }
  122. NSUInteger remainingLength = size - string.length;
  123. [string appendString:[pattern substringToIndex:remainingLength]];
  124. return [FSnapshotUtilities nodeFrom:string];
  125. }
  126. @end