FDevice.m 4.2 KB

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