FDotInfo.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "FDotInfo.h"
  17. #import "FIRDatabaseConfig_Private.h"
  18. #import "FTestHelpers.h"
  19. @implementation FDotInfo
  20. - (void)testCanGetReferenceToInfoNodes {
  21. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  22. [ref.root child:@".info"];
  23. [ref.root child:@".info/foo"];
  24. }
  25. - (void)testCantWriteToInfo {
  26. FIRDatabaseReference *ref = [[FTestHelpers getRandomNode].root child:@".info"];
  27. XCTAssertThrows([ref setValue:@"hi"], @"Cannot write to path at /.info");
  28. XCTAssertThrows([ref setValue:@"hi" andPriority:@5], @"Cannot write to path at /.info");
  29. XCTAssertThrows([ref setPriority:@"hi"], @"Cannot write to path at /.info");
  30. XCTAssertThrows([ref runTransactionBlock:^FIRTransactionResult *(FIRMutableData *currentData) {
  31. return [FIRTransactionResult successWithValue:currentData];
  32. }],
  33. @"Cannot write to path at /.info");
  34. XCTAssertThrows([ref removeValue], @"Cannot write to path at /.info");
  35. XCTAssertThrows([[ref child:@"test"] setValue:@"hi"], @"Cannot write to path at /.info");
  36. }
  37. - (void)testCanWatchInfoConnected {
  38. FIRDatabaseReference *rootRef = [FTestHelpers getRandomNode].root;
  39. __block BOOL done = NO;
  40. [[rootRef child:@".info/connected"] observeEventType:FIRDataEventTypeValue
  41. withBlock:^(FIRDataSnapshot *snapshot) {
  42. if ([[snapshot value] boolValue]) {
  43. done = YES;
  44. }
  45. }];
  46. [self waitUntil:^{
  47. return done;
  48. }];
  49. }
  50. - (void)testInfoConnectedGoesToFalseOnDisconnect {
  51. FIRDatabaseConfig *cfg = [FTestHelpers configForName:@"test-config"];
  52. FIRDatabaseReference *rootRef = [[FIRDatabaseReference alloc] initWithConfig:cfg];
  53. __block BOOL everConnected = NO;
  54. __block NSMutableString *connectedHistory = [[NSMutableString alloc] init];
  55. [[rootRef child:@".info/connected"]
  56. observeEventType:FIRDataEventTypeValue
  57. withBlock:^(FIRDataSnapshot *snapshot) {
  58. if ([[snapshot value] boolValue]) {
  59. everConnected = YES;
  60. }
  61. if (everConnected) {
  62. [connectedHistory appendString:([[snapshot value] boolValue] ? @"YES," : @"NO,")];
  63. }
  64. }];
  65. [self waitUntil:^{
  66. return everConnected;
  67. }];
  68. [FRepoManager interrupt:cfg];
  69. [FRepoManager resume:cfg];
  70. [self waitUntil:^BOOL {
  71. return [connectedHistory isEqualToString:@"YES,NO,YES,"];
  72. }];
  73. [FRepoManager interrupt:cfg];
  74. [FRepoManager disposeRepos:cfg];
  75. }
  76. - (void)testInfoServerTimeOffset {
  77. FIRDatabaseConfig *cfg = [FTestHelpers configForName:@"test-config"];
  78. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithConfig:cfg];
  79. // make sure childByAutoId works
  80. [ref childByAutoId];
  81. NSMutableArray *offsets = [[NSMutableArray alloc] init];
  82. [[ref child:@".info/serverTimeOffset"] observeEventType:FIRDataEventTypeValue
  83. withBlock:^(FIRDataSnapshot *snapshot) {
  84. NSLog(@"got value: %@", snapshot.value);
  85. [offsets addObject:snapshot.value];
  86. }];
  87. WAIT_FOR(offsets.count == 1);
  88. XCTAssertTrue([[offsets objectAtIndex:0] isKindOfClass:[NSNumber class]],
  89. @"Second element should be a number, in milliseconds");
  90. // make sure childByAutoId still works
  91. [ref childByAutoId];
  92. [FRepoManager interrupt:cfg];
  93. [FRepoManager disposeRepos:cfg];
  94. }
  95. - (void)testManualConnectionManagement {
  96. FIRDatabaseConfig *cfg = [FTestHelpers configForName:@"test-config"];
  97. FIRDatabaseConfig *altCfg = [FTestHelpers configForName:@"alt-config"];
  98. FIRDatabaseReference *ref = [[FIRDatabaseReference alloc] initWithConfig:cfg];
  99. FIRDatabaseReference *refAlt = [[FIRDatabaseReference alloc] initWithConfig:altCfg];
  100. // Wait until we're connected to both Firebases
  101. __block BOOL ready = NO;
  102. [[ref child:@".info/connected"] observeEventType:FIRDataEventTypeValue
  103. withBlock:^(FIRDataSnapshot *snapshot) {
  104. ready = [[snapshot value] boolValue];
  105. }];
  106. [self waitUntil:^{
  107. return ready;
  108. }];
  109. [[ref child:@".info/connected"] removeAllObservers];
  110. ready = NO;
  111. [[refAlt child:@".info/connected"] observeEventType:FIRDataEventTypeValue
  112. withBlock:^(FIRDataSnapshot *snapshot) {
  113. ready = [[snapshot value] boolValue];
  114. }];
  115. [self waitUntil:^{
  116. return ready;
  117. }];
  118. [[refAlt child:@".info/connected"] removeAllObservers];
  119. [FIRDatabaseReference goOffline];
  120. // Ensure we're disconnected from both Firebases
  121. ready = NO;
  122. [[ref child:@".info/connected"]
  123. observeSingleEventOfType:FIRDataEventTypeValue
  124. withBlock:^(FIRDataSnapshot *snapshot) {
  125. XCTAssertFalse([[snapshot value] boolValue],
  126. @".info/connected should be false");
  127. ready = YES;
  128. }];
  129. [self waitUntil:^{
  130. return ready;
  131. }];
  132. ready = NO;
  133. [[refAlt child:@".info/connected"]
  134. observeSingleEventOfType:FIRDataEventTypeValue
  135. withBlock:^(FIRDataSnapshot *snapshot) {
  136. XCTAssertFalse([[snapshot value] boolValue],
  137. @".info/connected should be false");
  138. ready = YES;
  139. }];
  140. [self waitUntil:^{
  141. return ready;
  142. }];
  143. // Ensure that we don't automatically reconnect upon new Firebase creation
  144. FIRDatabaseReference *refDup = [[FIRDatabaseReference alloc] initWithConfig:altCfg];
  145. [[refDup child:@".info/connected"] observeEventType:FIRDataEventTypeValue
  146. withBlock:^(FIRDataSnapshot *snapshot) {
  147. if ([[snapshot value] boolValue]) {
  148. XCTFail(@".info/connected should remain false");
  149. }
  150. }];
  151. // Wait for 1.5 seconds to make sure connected remains false
  152. [NSThread sleepForTimeInterval:1.5];
  153. [[refDup child:@".info/connected"] removeAllObservers];
  154. [FIRDatabaseReference goOnline];
  155. // Ensure we're reconnected to both Firebases
  156. ready = NO;
  157. [[ref child:@".info/connected"] observeEventType:FIRDataEventTypeValue
  158. withBlock:^(FIRDataSnapshot *snapshot) {
  159. ready = [[snapshot value] boolValue];
  160. }];
  161. [self waitUntil:^{
  162. return ready;
  163. }];
  164. [[ref child:@".info/connected"] removeAllObservers];
  165. ready = NO;
  166. [[refAlt child:@".info/connected"] observeEventType:FIRDataEventTypeValue
  167. withBlock:^(FIRDataSnapshot *snapshot) {
  168. ready = [[snapshot value] boolValue];
  169. }];
  170. [self waitUntil:^{
  171. return ready;
  172. }];
  173. [[refAlt child:@".info/connected"] removeAllObservers];
  174. }
  175. @end