FTestBase.m 6.5 KB

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