FDevice.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "FDevice.h"
  17. #import <XCTest/XCTest.h>
  18. #import <FirebaseDatabase/FIRDatabaseReference.h>
  19. #import "FIRDatabaseConfig_Private.h"
  20. #import "FIRDatabaseReference_Private.h"
  21. #import "FRepoManager.h"
  22. #import "FTestHelpers.h"
  23. #import "SenTest+FWaiter.h"
  24. @interface FDevice () {
  25. FIRDatabaseConfig *config;
  26. NSString *url;
  27. BOOL isOnline;
  28. BOOL disposed;
  29. }
  30. @end
  31. @implementation FDevice
  32. - (id)initOnline {
  33. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  34. return [self initOnlineWithUrl:[ref description]];
  35. }
  36. - (id)initOffline {
  37. FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
  38. return [self initOfflineWithUrl:[ref description]];
  39. }
  40. - (id)initOnlineWithUrl:(NSString *)firebaseUrl {
  41. return [self initWithUrl:firebaseUrl andOnline:YES];
  42. }
  43. - (id)initOfflineWithUrl:(NSString *)firebaseUrl {
  44. return [self initWithUrl:firebaseUrl andOnline:NO];
  45. }
  46. static NSUInteger deviceId = 0;
  47. - (id)initWithUrl:(NSString *)firebaseUrl andOnline:(BOOL)online {
  48. self = [super init];
  49. if (self) {
  50. config = [FTestHelpers
  51. configForName:[NSString stringWithFormat:@"device-%lu", (unsigned long)deviceId++]];
  52. config.persistenceEnabled = YES;
  53. url = firebaseUrl;
  54. isOnline = online;
  55. }
  56. return self;
  57. }
  58. - (void)dealloc {
  59. if (!self->disposed) {
  60. [NSException raise:NSInternalInconsistencyException format:@"Forgot to dispose device"];
  61. }
  62. }
  63. - (void)dispose {
  64. // TODO: clear persistence
  65. [FRepoManager disposeRepos:self->config];
  66. self->disposed = YES;
  67. }
  68. - (void)goOffline {
  69. isOnline = NO;
  70. [FRepoManager interrupt:config];
  71. }
  72. - (void)goOnline {
  73. isOnline = YES;
  74. [FRepoManager resume:config];
  75. }
  76. - (void)restartOnline {
  77. @autoreleasepool {
  78. [FRepoManager disposeRepos:config];
  79. isOnline = YES;
  80. }
  81. }
  82. - (void)restartOffline {
  83. @autoreleasepool {
  84. [FRepoManager disposeRepos:config];
  85. isOnline = NO;
  86. }
  87. }
  88. // Waits for us to connect and then does an extra round-trip to make sure all initial state
  89. // restoration is completely done.
  90. - (void)waitForIdleUsingWaiter:(XCTest *)waiter {
  91. [self do:^(FIRDatabaseReference *ref) {
  92. __block BOOL connected = NO;
  93. FIRDatabaseHandle handle =
  94. [[ref.root child:@".info/connected"] observeEventType:FIRDataEventTypeValue
  95. withBlock:^(FIRDataSnapshot *snapshot) {
  96. connected = [snapshot.value boolValue];
  97. }];
  98. [waiter waitUntil:^BOOL {
  99. return connected;
  100. }];
  101. [ref.root removeObserverWithHandle:handle];
  102. // HACK: Do a deep setPriority (which we expect to fail because there's no data there) to do a
  103. // no-op roundtrip.
  104. __block BOOL done = NO;
  105. [[ref.root child:@"ENTOHTNUHOE/ONTEHNUHTOE"]
  106. setPriority:@"blah"
  107. withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  108. done = YES;
  109. }];
  110. [waiter waitUntil:^BOOL {
  111. return done;
  112. }];
  113. }];
  114. }
  115. - (void)do:(void (^)(FIRDatabaseReference *))action {
  116. @autoreleasepool {
  117. FIRDatabaseReference *ref = [[[[FIRDatabaseReference alloc] initWithConfig:self->config]
  118. database] referenceFromURL:self->url];
  119. if (!isOnline) {
  120. [FRepoManager interrupt:config];
  121. }
  122. action(ref);
  123. }
  124. }
  125. @end