FTestBase.m 6.6 KB

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