FDevice.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <XCTest/XCTest.h>
  17. #import "FDevice.h"
  18. #import <FirebaseDatabase/FIRDatabaseReference.h>
  19. #import "FRepoManager.h"
  20. #import "FIRDatabaseReference_Private.h"
  21. #import "FIRDatabaseConfig_Private.h"
  22. #import "SenTest+FWaiter.h"
  23. #import "FTestHelpers.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 = [FIRDatabaseConfig configForName:[NSString stringWithFormat:@"device-%lu", deviceId++]];
  51. config.persistenceEnabled = YES;
  52. url = firebaseUrl;
  53. isOnline = online;
  54. }
  55. return self;
  56. }
  57. - (void) dealloc
  58. {
  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 restoration is completely done.
  89. - (void)waitForIdleUsingWaiter:(XCTest*)waiter {
  90. [self do:^(FIRDatabaseReference *ref) {
  91. __block BOOL connected = NO;
  92. FIRDatabaseHandle handle = [[ref.root child:@".info/connected"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  93. connected = [snapshot.value boolValue];
  94. }];
  95. [waiter waitUntil:^BOOL { return connected; }];
  96. [ref.root removeObserverWithHandle:handle];
  97. // HACK: Do a deep setPriority (which we expect to fail because there's no data there) to do a no-op roundtrip.
  98. __block BOOL done = NO;
  99. [[ref.root child:@"ENTOHTNUHOE/ONTEHNUHTOE"] setPriority:@"blah" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  100. done = YES;
  101. }];
  102. [waiter waitUntil:^BOOL { return done; }];
  103. }];
  104. }
  105. - (void)do:(void (^)(FIRDatabaseReference *))action {
  106. @autoreleasepool {
  107. FIRDatabaseReference *ref = [[[[FIRDatabaseReference alloc] initWithConfig:self->config] database] referenceFromURL:self->url];
  108. if (!isOnline) {
  109. [FRepoManager interrupt:config];
  110. }
  111. action(ref);
  112. }
  113. }
  114. @end