FTestBase.m 5.8 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 <FirebaseCore/FIRApp.h>
  17. #import <FirebaseCore/FIROptions.h>
  18. #import "FTestBase.h"
  19. #import "FTestAuthTokenGenerator.h"
  20. #import "FIRDatabaseQuery_Private.h"
  21. #import "FIRTestAuthTokenProvider.h"
  22. @implementation FTestBase
  23. + (void)setUp
  24. {
  25. static dispatch_once_t once;
  26. dispatch_once(&once, ^ {
  27. [FIRApp configure];
  28. });
  29. }
  30. - (void)setUp
  31. {
  32. [super setUp];
  33. [FIRDatabase setLoggingEnabled:YES];
  34. _databaseURL = [[FIRApp defaultApp] options].databaseURL;
  35. // Disabled normally since they slow down the tests and don't actually assert anything (they just NSLog timings).
  36. runPerfTests = NO;
  37. }
  38. - (void)snapWaiter:(FIRDatabaseReference *)path withBlock:(fbt_void_datasnapshot)fn {
  39. __block BOOL done = NO;
  40. [path observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snap) {
  41. fn(snap);
  42. done = YES;
  43. }];
  44. NSTimeInterval timeTaken = [self waitUntil:^BOOL{
  45. return done;
  46. } timeout:kFirebaseTestWaitUntilTimeout];
  47. NSLog(@"snapWaiter:withBlock: timeTaken:%f", timeTaken);
  48. XCTAssertTrue(done, @"Properly finished.");
  49. }
  50. - (void) waitUntilConnected:(FIRDatabaseReference *)ref {
  51. __block BOOL connected = NO;
  52. FIRDatabaseHandle handle = [[ref.root child:@".info/connected"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  53. connected = [snapshot.value boolValue];
  54. }];
  55. WAIT_FOR(connected);
  56. [ref.root removeObserverWithHandle:handle];
  57. }
  58. - (void) waitForRoundTrip:(FIRDatabaseReference *)ref {
  59. // HACK: Do a deep setPriority (which we expect to fail because there's no data there) to do a no-op roundtrip.
  60. __block BOOL done = NO;
  61. [[ref.root child:@"ENTOHTNUHOE/ONTEHNUHTOE"] setPriority:@"blah" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  62. done = YES;
  63. }];
  64. WAIT_FOR(done);
  65. }
  66. - (void) waitForQueue:(FIRDatabaseReference *)ref {
  67. dispatch_sync([FIRDatabaseQuery sharedQueue], ^{});
  68. }
  69. - (void) waitForEvents:(FIRDatabaseReference *)ref {
  70. [self waitForQueue:ref];
  71. __block BOOL done = NO;
  72. dispatch_async(dispatch_get_main_queue(), ^{
  73. done = YES;
  74. });
  75. WAIT_FOR(done);
  76. }
  77. - (void)waitForValueOf:(FIRDatabaseQuery *)ref toBe:(id)expected {
  78. __block id value;
  79. FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  80. value = snapshot.value;
  81. }];
  82. @try {
  83. [self waitUntil:^BOOL {
  84. return [value isEqual:expected];
  85. }];
  86. } @catch (NSException *exception) {
  87. @throw [NSException exceptionWithName:@"DidNotGetValue" reason:@"Did not get expected value"
  88. userInfo:@{ @"expected": (!expected ? @"nil" : expected),
  89. @"actual": (!value ? @"nil" : value) }];
  90. } @finally {
  91. [ref removeObserverWithHandle:handle];
  92. }
  93. }
  94. - (void)waitForExportValueOf:(FIRDatabaseQuery *)ref toBe:(id)expected {
  95. __block id value;
  96. FIRDatabaseHandle handle = [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  97. value = snapshot.valueInExportFormat;
  98. }];
  99. @try {
  100. [self waitUntil:^BOOL {
  101. return [value isEqual:expected];
  102. }];
  103. } @catch (NSException *exception) {
  104. if ([exception.name isEqualToString:@"Timed out"]) {
  105. @throw [NSException exceptionWithName:@"DidNotGetValue" reason:@"Did not get expected value"
  106. userInfo:@{ @"expected": (!expected ? @"nil" : expected),
  107. @"actual": (!value ? @"nil" : value) }]; } else {
  108. @throw exception;
  109. }
  110. } @finally {
  111. [ref removeObserverWithHandle:handle];
  112. }
  113. }
  114. - (void)waitForCompletionOf:(FIRDatabaseReference *)ref setValue:(id)value {
  115. [self waitForCompletionOf:ref setValue:value andPriority:nil];
  116. }
  117. - (void)waitForCompletionOf:(FIRDatabaseReference *)ref setValue:(id)value andPriority:(id)priority {
  118. __block BOOL done = NO;
  119. [ref setValue:value andPriority:priority withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  120. done = YES;
  121. }];
  122. @try {
  123. WAIT_FOR(done);
  124. } @catch (NSException *exception) {
  125. @throw [NSException exceptionWithName:@"DidNotSetValue" reason:@"Did not complete setting value"
  126. userInfo:@{ @"ref": [ref description],
  127. @"done": done ? @"true" : @"false",
  128. @"value": (!value ? @"nil" : value),
  129. @"priority": (!priority ? @"nil" : priority) }];
  130. }
  131. }
  132. - (void)waitForCompletionOf:(FIRDatabaseReference *)ref updateChildValues:(NSDictionary *)values {
  133. __block BOOL done = NO;
  134. [ref updateChildValues:values withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  135. done = YES;
  136. }];
  137. @try {
  138. WAIT_FOR(done);
  139. } @catch (NSException *exception) {
  140. @throw [NSException exceptionWithName:@"DidNotUpdateChildValues" reason:@"Could not finish updating child values"
  141. userInfo:@{ @"ref": [ref description],
  142. @"values": (!values ? @"nil" : values)}];
  143. }
  144. }
  145. @end