FDevice.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 = [FTestHelpers configForName:
  51. [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. {
  60. if (!self->disposed) {
  61. [NSException raise:NSInternalInconsistencyException format:@"Forgot to dispose device"];
  62. }
  63. }
  64. - (void) dispose {
  65. // TODO: clear persistence
  66. [FRepoManager disposeRepos:self->config];
  67. self->disposed = YES;
  68. }
  69. - (void)goOffline {
  70. isOnline = NO;
  71. [FRepoManager interrupt:config];
  72. }
  73. - (void)goOnline {
  74. isOnline = YES;
  75. [FRepoManager resume:config];
  76. }
  77. - (void)restartOnline {
  78. @autoreleasepool {
  79. [FRepoManager disposeRepos:config];
  80. isOnline = YES;
  81. }
  82. }
  83. - (void)restartOffline {
  84. @autoreleasepool {
  85. [FRepoManager disposeRepos:config];
  86. isOnline = NO;
  87. }
  88. }
  89. // Waits for us to connect and then does an extra round-trip to make sure all initial state restoration is completely done.
  90. - (void)waitForIdleUsingWaiter:(XCTest*)waiter {
  91. [self do:^(FIRDatabaseReference *ref) {
  92. __block BOOL connected = NO;
  93. FIRDatabaseHandle handle = [[ref.root child:@".info/connected"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
  94. connected = [snapshot.value boolValue];
  95. }];
  96. [waiter waitUntil:^BOOL { return connected; }];
  97. [ref.root removeObserverWithHandle:handle];
  98. // HACK: Do a deep setPriority (which we expect to fail because there's no data there) to do a no-op roundtrip.
  99. __block BOOL done = NO;
  100. [[ref.root child:@"ENTOHTNUHOE/ONTEHNUHTOE"] setPriority:@"blah" withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
  101. done = YES;
  102. }];
  103. [waiter waitUntil:^BOOL { return done; }];
  104. }];
  105. }
  106. - (void)do:(void (^)(FIRDatabaseReference *))action {
  107. @autoreleasepool {
  108. FIRDatabaseReference *ref = [[[[FIRDatabaseReference alloc] initWithConfig:self->config] database] referenceFromURL:self->url];
  109. if (!isOnline) {
  110. [FRepoManager interrupt:config];
  111. }
  112. action(ref);
  113. }
  114. }
  115. @end