FTestHelpers.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "FConstants.h"
  18. #import <FirebaseCore/FIRApp.h>
  19. #import <FirebaseCore/FIROptions.h>
  20. #import "FIRDatabaseConfig_Private.h"
  21. #import "FTestAuthTokenGenerator.h"
  22. @implementation FTestHelpers
  23. + (NSTimeInterval) waitUntil:(BOOL (^)())predicate timeout:(NSTimeInterval)seconds {
  24. NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
  25. NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
  26. NSTimeInterval timeoutTime = [timeoutDate timeIntervalSinceReferenceDate];
  27. NSTimeInterval currentTime;
  28. for (currentTime = [NSDate timeIntervalSinceReferenceDate];
  29. !predicate() && currentTime < timeoutTime;
  30. currentTime = [NSDate timeIntervalSinceReferenceDate]) {
  31. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
  32. }
  33. NSTimeInterval finish = [NSDate timeIntervalSinceReferenceDate];
  34. NSAssert(currentTime <= timeoutTime, @"Timed out");
  35. return (finish - start);
  36. }
  37. + (NSArray*) getRandomNodes:(int)num persistence:(BOOL)persistence {
  38. static dispatch_once_t pred = 0;
  39. static NSMutableArray *persistenceRefs = nil;
  40. static NSMutableArray *noPersistenceRefs = nil;
  41. dispatch_once(&pred, ^{
  42. persistenceRefs = [[NSMutableArray alloc] init];
  43. noPersistenceRefs = [[NSMutableArray alloc] init];
  44. // Uncomment the following line to run tests against a background thread
  45. //[Firebase setDispatchQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
  46. });
  47. NSMutableArray *refs = (persistence) ? persistenceRefs : noPersistenceRefs;
  48. id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider authTokenProviderForApp:[FIRApp defaultApp]];
  49. while (num > refs.count) {
  50. NSString *sessionIdentifier = [NSString stringWithFormat:@"test-config-%@persistence-%lu", (persistence) ? @"" : @"no-", refs.count];
  51. FIRDatabaseConfig *config = [[FIRDatabaseConfig alloc] initWithSessionIdentifier:sessionIdentifier authTokenProvider:authTokenProvider];
  52. config.persistenceEnabled = persistence;
  53. FIRDatabaseReference * ref = [[FIRDatabaseReference alloc] initWithConfig:config];
  54. [refs addObject:ref];
  55. }
  56. NSMutableArray* results = [[NSMutableArray alloc] init];
  57. NSString* name = nil;
  58. for (int i = 0; i < num; ++i) {
  59. FIRDatabaseReference * ref = [refs objectAtIndex:i];
  60. if (!name) {
  61. name = [ref childByAutoId].key;
  62. }
  63. [results addObject:[ref child:name]];
  64. }
  65. return results;
  66. }
  67. // Helpers
  68. + (FIRDatabaseReference *) getRandomNode {
  69. NSArray* refs = [self getRandomNodes:1 persistence:YES];
  70. return [refs objectAtIndex:0];
  71. }
  72. + (FIRDatabaseReference *) getRandomNodeWithoutPersistence {
  73. NSArray* refs = [self getRandomNodes:1 persistence:NO];
  74. return refs[0];
  75. }
  76. + (FTupleFirebase *) getRandomNodePair {
  77. NSArray* refs = [self getRandomNodes:2 persistence:YES];
  78. FTupleFirebase* tuple = [[FTupleFirebase alloc] init];
  79. tuple.one = [refs objectAtIndex:0];
  80. tuple.two = [refs objectAtIndex:1];
  81. return tuple;
  82. }
  83. + (FTupleFirebase *) getRandomNodePairWithoutPersistence {
  84. NSArray* refs = [self getRandomNodes:2 persistence:NO];
  85. FTupleFirebase* tuple = [[FTupleFirebase alloc] init];
  86. tuple.one = refs[0];
  87. tuple.two = refs[1];
  88. return tuple;
  89. }
  90. + (FTupleFirebase *) getRandomNodeTriple {
  91. NSArray* refs = [self getRandomNodes:3 persistence:YES];
  92. FTupleFirebase* triple = [[FTupleFirebase alloc] init];
  93. triple.one = [refs objectAtIndex:0];
  94. triple.two = [refs objectAtIndex:1];
  95. triple.three = [refs objectAtIndex:2];
  96. return triple;
  97. }
  98. + (id<FNode>)leafNodeOfSize:(NSUInteger)size {
  99. NSMutableString *string = [NSMutableString string];
  100. NSString *pattern = @"abdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  101. for (NSUInteger i = 0; i < size - pattern.length; i = i + pattern.length) {
  102. [string appendString:pattern];
  103. }
  104. NSUInteger remainingLength = size - string.length;
  105. [string appendString:[pattern substringToIndex:remainingLength]];
  106. return [FSnapshotUtilities nodeFrom:string];
  107. }
  108. @end