FTestBase.m 6.4 KB

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