FSTSyncEngineTestDriver.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 "Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h"
  17. #include <unordered_map>
  18. #import <FirebaseFirestore/FIRFirestoreErrors.h>
  19. #import <GRPCClient/GRPCCall.h>
  20. #import "Firestore/Source/Core/FSTEventManager.h"
  21. #import "Firestore/Source/Core/FSTQuery.h"
  22. #import "Firestore/Source/Core/FSTSnapshotVersion.h"
  23. #import "Firestore/Source/Core/FSTSyncEngine.h"
  24. #import "Firestore/Source/Local/FSTLocalStore.h"
  25. #import "Firestore/Source/Local/FSTPersistence.h"
  26. #import "Firestore/Source/Model/FSTMutation.h"
  27. #import "Firestore/Source/Remote/FSTDatastore.h"
  28. #import "Firestore/Source/Remote/FSTWatchChange.h"
  29. #import "Firestore/Source/Util/FSTAssert.h"
  30. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  31. #import "Firestore/Source/Util/FSTLogger.h"
  32. #import "Firestore/Example/Tests/Core/FSTSyncEngine+Testing.h"
  33. #import "Firestore/Example/Tests/SpecTests/FSTMockDatastore.h"
  34. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  35. using firebase::firestore::auth::HashUser;
  36. using firebase::firestore::auth::User;
  37. NS_ASSUME_NONNULL_BEGIN
  38. @implementation FSTQueryEvent
  39. - (NSString *)description {
  40. // The Query is also included in the view, so we skip it.
  41. return [NSString stringWithFormat:@"<FSTQueryEvent: viewSnapshot=%@, error=%@>",
  42. self.viewSnapshot, self.error];
  43. }
  44. @end
  45. @implementation FSTOutstandingWrite
  46. @end
  47. @interface FSTSyncEngineTestDriver ()
  48. #pragma mark - Parts of the Firestore system that the spec tests need to control.
  49. @property(nonatomic, strong, readonly) FSTMockDatastore *datastore;
  50. @property(nonatomic, strong, readonly) FSTEventManager *eventManager;
  51. @property(nonatomic, strong, readonly) FSTRemoteStore *remoteStore;
  52. @property(nonatomic, strong, readonly) FSTLocalStore *localStore;
  53. @property(nonatomic, strong, readonly) FSTSyncEngine *syncEngine;
  54. #pragma mark - Data structures for holding events sent by the watch stream.
  55. /** A block for the FSTEventAggregator to use to report events to the test. */
  56. @property(nonatomic, strong, readonly) void (^eventHandler)(FSTQueryEvent *);
  57. /** The events received by our eventHandler and not yet retrieved via capturedEventsSinceLastCall */
  58. @property(nonatomic, strong, readonly) NSMutableArray<FSTQueryEvent *> *events;
  59. /** A dictionary for tracking the listens on queries. */
  60. @property(nonatomic, strong, readonly)
  61. NSMutableDictionary<FSTQuery *, FSTQueryListener *> *queryListeners;
  62. @end
  63. @implementation FSTSyncEngineTestDriver {
  64. // ivar is declared as mutable.
  65. std::unordered_map<User, NSMutableArray<FSTOutstandingWrite *> *, HashUser> _outstandingWrites;
  66. User _currentUser;
  67. }
  68. - (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
  69. garbageCollector:(id<FSTGarbageCollector>)garbageCollector {
  70. return [self initWithPersistence:persistence
  71. garbageCollector:garbageCollector
  72. initialUser:User::Unauthenticated()
  73. outstandingWrites:{}];
  74. }
  75. - (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
  76. garbageCollector:(id<FSTGarbageCollector>)garbageCollector
  77. initialUser:(const User &)initialUser
  78. outstandingWrites:(const FSTOutstandingWriteQueues &)outstandingWrites {
  79. if (self = [super init]) {
  80. // Do a deep copy.
  81. for (const auto &pair : outstandingWrites) {
  82. _outstandingWrites[pair.first] = [pair.second mutableCopy];
  83. }
  84. _events = [NSMutableArray array];
  85. // Set up the sync engine and various stores.
  86. dispatch_queue_t mainQueue = dispatch_get_main_queue();
  87. FSTDispatchQueue *dispatchQueue = [FSTDispatchQueue queueWith:mainQueue];
  88. _localStore = [[FSTLocalStore alloc] initWithPersistence:persistence
  89. garbageCollector:garbageCollector
  90. initialUser:initialUser];
  91. _datastore = [FSTMockDatastore mockDatastoreWithWorkerDispatchQueue:dispatchQueue];
  92. _remoteStore = [FSTRemoteStore remoteStoreWithLocalStore:_localStore datastore:_datastore];
  93. _syncEngine = [[FSTSyncEngine alloc] initWithLocalStore:_localStore
  94. remoteStore:_remoteStore
  95. initialUser:initialUser];
  96. _remoteStore.syncEngine = _syncEngine;
  97. _eventManager = [FSTEventManager eventManagerWithSyncEngine:_syncEngine];
  98. _remoteStore.onlineStateDelegate = self;
  99. // Set up internal event tracking for the spec tests.
  100. NSMutableArray<FSTQueryEvent *> *events = [NSMutableArray array];
  101. _eventHandler = ^(FSTQueryEvent *e) {
  102. [events addObject:e];
  103. };
  104. _events = events;
  105. _queryListeners = [NSMutableDictionary dictionary];
  106. _expectedLimboDocuments = [NSSet set];
  107. _expectedActiveTargets = [NSDictionary dictionary];
  108. _currentUser = initialUser;
  109. }
  110. return self;
  111. }
  112. - (const FSTOutstandingWriteQueues &)outstandingWrites {
  113. return _outstandingWrites;
  114. }
  115. - (const User &)currentUser {
  116. return _currentUser;
  117. }
  118. - (void)applyChangedOnlineState:(FSTOnlineState)onlineState {
  119. [self.syncEngine applyChangedOnlineState:onlineState];
  120. [self.eventManager applyChangedOnlineState:onlineState];
  121. }
  122. - (void)start {
  123. [self.localStore start];
  124. [self.remoteStore start];
  125. }
  126. - (void)validateUsage {
  127. // We could relax this if we found a reason to.
  128. FSTAssert(self.events.count == 0,
  129. @"You must clear all pending events by calling"
  130. " capturedEventsSinceLastCall before calling shutdown.");
  131. }
  132. - (void)shutdown {
  133. [self.remoteStore shutdown];
  134. [self.localStore shutdown];
  135. }
  136. - (void)validateNextWriteSent:(FSTMutation *)expectedWrite {
  137. NSArray<FSTMutation *> *request = [self.datastore nextSentWrite];
  138. // Make sure the write went through the pipe like we expected it to.
  139. FSTAssert(request.count == 1, @"Only single mutation requests are supported at the moment");
  140. FSTMutation *actualWrite = request[0];
  141. FSTAssert([actualWrite isEqual:expectedWrite],
  142. @"Mock datastore received write %@ but first outstanding mutation was %@", actualWrite,
  143. expectedWrite);
  144. FSTLog(@"A write was sent: %@", actualWrite);
  145. }
  146. - (int)sentWritesCount {
  147. return [self.datastore writesSent];
  148. }
  149. - (int)writeStreamRequestCount {
  150. return [self.datastore writeStreamRequestCount];
  151. }
  152. - (int)watchStreamRequestCount {
  153. return [self.datastore watchStreamRequestCount];
  154. }
  155. - (void)disableNetwork {
  156. // Make sure to execute all writes that are currently queued. This allows us
  157. // to assert on the total number of requests sent before shutdown.
  158. [self.remoteStore fillWritePipeline];
  159. [self.remoteStore disableNetwork];
  160. }
  161. - (void)enableNetwork {
  162. [self.remoteStore enableNetwork];
  163. }
  164. - (void)changeUser:(const User &)user {
  165. _currentUser = user;
  166. [self.syncEngine userDidChange:user];
  167. }
  168. - (FSTOutstandingWrite *)receiveWriteAckWithVersion:(FSTSnapshotVersion *)commitVersion
  169. mutationResults:
  170. (NSArray<FSTMutationResult *> *)mutationResults {
  171. FSTOutstandingWrite *write = [self currentOutstandingWrites].firstObject;
  172. [[self currentOutstandingWrites] removeObjectAtIndex:0];
  173. [self validateNextWriteSent:write.write];
  174. [self.datastore ackWriteWithVersion:commitVersion mutationResults:mutationResults];
  175. return write;
  176. }
  177. - (FSTOutstandingWrite *)receiveWriteError:(int)errorCode
  178. userInfo:(NSDictionary<NSString *, id> *)userInfo {
  179. NSError *error =
  180. [NSError errorWithDomain:FIRFirestoreErrorDomain code:errorCode userInfo:userInfo];
  181. FSTOutstandingWrite *write = [self currentOutstandingWrites].firstObject;
  182. [self validateNextWriteSent:write.write];
  183. // If this is a permanent error, the mutation is not expected to be sent again so we remove it
  184. // from currentOutstandingWrites.
  185. if ([FSTDatastore isPermanentWriteError:error]) {
  186. [[self currentOutstandingWrites] removeObjectAtIndex:0];
  187. }
  188. FSTLog(@"Failing a write.");
  189. [self.datastore failWriteWithError:error];
  190. return write;
  191. }
  192. - (NSArray<FSTQueryEvent *> *)capturedEventsSinceLastCall {
  193. NSArray<FSTQueryEvent *> *result = [self.events copy];
  194. [self.events removeAllObjects];
  195. return result;
  196. }
  197. - (FSTTargetID)addUserListenerWithQuery:(FSTQuery *)query {
  198. // TODO(dimond): Allow customizing listen options in spec tests
  199. // TODO(dimond): Change spec tests to verify isFromCache on snapshots
  200. FSTListenOptions *options = [[FSTListenOptions alloc] initWithIncludeQueryMetadataChanges:YES
  201. includeDocumentMetadataChanges:YES
  202. waitForSyncWhenOnline:NO];
  203. FSTQueryListener *listener = [[FSTQueryListener alloc]
  204. initWithQuery:query
  205. options:options
  206. viewSnapshotHandler:^(FSTViewSnapshot *_Nullable snapshot, NSError *_Nullable error) {
  207. FSTQueryEvent *event = [[FSTQueryEvent alloc] init];
  208. event.query = query;
  209. event.viewSnapshot = snapshot;
  210. event.error = error;
  211. [self.events addObject:event];
  212. }];
  213. self.queryListeners[query] = listener;
  214. return [self.eventManager addListener:listener];
  215. }
  216. - (void)removeUserListenerWithQuery:(FSTQuery *)query {
  217. FSTQueryListener *listener = self.queryListeners[query];
  218. [self.queryListeners removeObjectForKey:query];
  219. [self.eventManager removeListener:listener];
  220. }
  221. - (void)writeUserMutation:(FSTMutation *)mutation {
  222. FSTOutstandingWrite *write = [[FSTOutstandingWrite alloc] init];
  223. write.write = mutation;
  224. [[self currentOutstandingWrites] addObject:write];
  225. FSTLog(@"sending a user write.");
  226. [self.syncEngine writeMutations:@[ mutation ]
  227. completion:^(NSError *_Nullable error) {
  228. FSTLog(@"A callback was called with error: %@", error);
  229. write.done = YES;
  230. write.error = error;
  231. }];
  232. }
  233. - (void)receiveWatchChange:(FSTWatchChange *)change
  234. snapshotVersion:(FSTSnapshotVersion *_Nullable)snapshot {
  235. [self.datastore writeWatchChange:change snapshotVersion:snapshot];
  236. }
  237. - (void)receiveWatchStreamError:(int)errorCode userInfo:(NSDictionary<NSString *, id> *)userInfo {
  238. NSError *error =
  239. [NSError errorWithDomain:FIRFirestoreErrorDomain code:errorCode userInfo:userInfo];
  240. [self.datastore failWatchStreamWithError:error];
  241. // Unlike web, stream should re-open synchronously (if we have any listeners)
  242. if (self.queryListeners.count > 0) {
  243. FSTAssert(self.datastore.isWatchStreamOpen, @"Watch stream is open");
  244. }
  245. }
  246. - (NSDictionary<FSTDocumentKey *, FSTBoxedTargetID *> *)currentLimboDocuments {
  247. return [self.syncEngine currentLimboDocuments];
  248. }
  249. - (NSDictionary<FSTBoxedTargetID *, FSTQueryData *> *)activeTargets {
  250. return [[self.datastore activeTargets] copy];
  251. }
  252. #pragma mark - Helper Methods
  253. - (NSMutableArray<FSTOutstandingWrite *> *)currentOutstandingWrites {
  254. NSMutableArray<FSTOutstandingWrite *> *writes = _outstandingWrites[_currentUser];
  255. if (!writes) {
  256. writes = [NSMutableArray array];
  257. _outstandingWrites[_currentUser] = writes;
  258. }
  259. return writes;
  260. }
  261. @end
  262. NS_ASSUME_NONNULL_END