FDotInfo.m 6.7 KB

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