FTestBase.m 6.7 KB

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