FSTSyncEngineTestDriver.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 <Foundation/Foundation.h>
  17. #include <cstddef>
  18. #include <map>
  19. #include <memory>
  20. #include <unordered_map>
  21. #include <utility>
  22. #include <vector>
  23. #include "Firestore/core/src/auth/user.h"
  24. #include "Firestore/core/src/core/query.h"
  25. #include "Firestore/core/src/core/view_snapshot.h"
  26. #include "Firestore/core/src/model/model_fwd.h"
  27. #include "Firestore/core/src/nanopb/byte_string.h"
  28. #include "Firestore/core/src/nanopb/nanopb_util.h"
  29. #include "Firestore/core/src/remote/watch_change.h"
  30. #include "Firestore/core/src/util/async_queue.h"
  31. #include "Firestore/core/src/util/empty.h"
  32. namespace firebase {
  33. namespace firestore {
  34. namespace local {
  35. class Persistence;
  36. class TargetData;
  37. } // namespace local
  38. } // namespace firestore
  39. } // namespace firebase
  40. namespace auth = firebase::firestore::auth;
  41. namespace core = firebase::firestore::core;
  42. namespace local = firebase::firestore::local;
  43. namespace model = firebase::firestore::model;
  44. namespace nanopb = firebase::firestore::nanopb;
  45. namespace remote = firebase::firestore::remote;
  46. namespace util = firebase::firestore::util;
  47. // A map holds expected information about currently active targets. The keys are
  48. // target ID, and the values are a vector of `TargetData`s mapped to the target and
  49. // the target's resume token.
  50. using ActiveTargetMap =
  51. std::unordered_map<model::TargetId,
  52. std::pair<std::vector<local::TargetData>, nanopb::ByteString>>;
  53. NS_ASSUME_NONNULL_BEGIN
  54. /**
  55. * Interface used for object that contain exactly one of either a view snapshot or an error for the
  56. * given query.
  57. */
  58. @interface FSTQueryEvent : NSObject
  59. @property(nonatomic, assign) core::Query query;
  60. @property(nonatomic, strong, nullable) NSError *error;
  61. - (const absl::optional<core::ViewSnapshot> &)viewSnapshot;
  62. - (void)setViewSnapshot:(absl::optional<core::ViewSnapshot>)snapshot;
  63. @end
  64. /** Holds an outstanding write and its result. */
  65. @interface FSTOutstandingWrite : NSObject
  66. /** The write that is outstanding. */
  67. - (const model::Mutation &)write;
  68. - (void)setWrite:(model::Mutation)write;
  69. /** Whether this write is done (regardless of whether it was successful or not). */
  70. @property(nonatomic, assign, readwrite) BOOL done;
  71. /** The error - if any - of this write. */
  72. @property(nonatomic, strong, nullable, readwrite) NSError *error;
  73. @end
  74. /** Mapping of user => array of FSTMutations for that user. */
  75. typedef std::unordered_map<auth::User, NSMutableArray<FSTOutstandingWrite *> *, auth::HashUser>
  76. FSTOutstandingWriteQueues;
  77. /**
  78. * A test driver for FSTSyncEngine that allows simulated event delivery and capture. As much as
  79. * possible, all sources of nondeterminism are removed so that test execution is consistent and
  80. * reliable.
  81. *
  82. * FSTSyncEngineTestDriver:
  83. *
  84. * + constructs an FSTSyncEngine using a mocked Datastore for the backend;
  85. * + allows the caller to trigger events (user API calls and incoming Datastore messages);
  86. * + performs sequencing validation internally (e.g. that when a user mutation is initiated, the
  87. * FSTSyncEngine correctly sends it to the remote store); and
  88. * + exposes the set of FSTQueryEvents generated for the caller to verify.
  89. *
  90. * Events come in three major flavors:
  91. *
  92. * + user events: simulate user API calls
  93. * + watch events: simulate RPC interactions with the Watch backend
  94. * + write events: simulate RPC interactions with the Streaming Write backend
  95. *
  96. * Each method on the driver injects a different event into the system.
  97. */
  98. @interface FSTSyncEngineTestDriver : NSObject
  99. /**
  100. * Initializes the underlying FSTSyncEngine with the given local persistence implementation and
  101. * a set of existing outstandingWrites (useful when your Persistence object has persisted
  102. * mutation queues).
  103. */
  104. - (instancetype)initWithPersistence:(std::unique_ptr<local::Persistence>)persistence
  105. initialUser:(const auth::User &)initialUser
  106. outstandingWrites:(const FSTOutstandingWriteQueues &)outstandingWrites
  107. maxConcurrentLimboResolutions:(size_t)maxConcurrentLimboResolutions NS_DESIGNATED_INITIALIZER;
  108. - (instancetype)init NS_UNAVAILABLE;
  109. /** Starts the FSTSyncEngine and its underlying components. */
  110. - (void)start;
  111. /** Validates that the API has been used correctly after a test is complete. */
  112. - (void)validateUsage;
  113. /** Shuts the FSTSyncEngine down. */
  114. - (void)shutdown;
  115. /**
  116. * Adds a listener to the FSTSyncEngine as if the user had initiated a new listen for the given
  117. * query.
  118. *
  119. * Resulting events are captured and made available via the capturedEventsSinceLastCall method.
  120. *
  121. * @param query A valid query to execute against the backend.
  122. * @return The target ID assigned by the system to track the query.
  123. */
  124. - (model::TargetId)addUserListenerWithQuery:(core::Query)query;
  125. /**
  126. * Removes a listener from the FSTSyncEngine as if the user had removed a listener corresponding
  127. * to the given query.
  128. *
  129. * Resulting events are captured and made available via the capturedEventsSinceLastCall method.
  130. *
  131. * @param query An identical query corresponding to one passed to -addUserListenerWithQuery.
  132. */
  133. - (void)removeUserListenerWithQuery:(const core::Query &)query;
  134. /**
  135. * Delivers a WatchChange RPC to the FSTSyncEngine as if it were received from the backend watch
  136. * service, either in response to addUserListener: or removeUserListener calls or because the
  137. * simulated backend has new data.
  138. *
  139. * Resulting events are captured and made available via the capturedEventsSinceLastCall method.
  140. *
  141. * @param change Any type of watch change
  142. * @param snapshot A snapshot version to attach, if applicable. This should be sent when
  143. * simulating the server having sent a complete snapshot.
  144. */
  145. - (void)receiveWatchChange:(const remote::WatchChange &)change
  146. snapshotVersion:(const model::SnapshotVersion &)snapshot;
  147. /**
  148. * Delivers a watch stream error as if the Streaming Watch backend has generated some kind of error.
  149. *
  150. * @param errorCode A FIRFirestoreErrorCode value, from FIRFirestoreErrors.h
  151. * @param userInfo Any additional details that the server might have sent along with the error.
  152. * For the moment this is effectively unused, but is logged.
  153. */
  154. - (void)receiveWatchStreamError:(int)errorCode userInfo:(NSDictionary<NSString *, id> *)userInfo;
  155. /**
  156. * Performs a mutation against the FSTSyncEngine as if the user had written the mutation through
  157. * the API.
  158. *
  159. * Also retains the mutation so that the driver can validate that the sync engine sent the mutation
  160. * to the remote store before receiveWatchChange:snapshotVersion: and receiveWriteError:userInfo:
  161. * events are processed.
  162. *
  163. * @param mutation Any type of valid mutation.
  164. */
  165. - (void)writeUserMutation:(model::Mutation)mutation;
  166. /**
  167. * Delivers a write error as if the Streaming Write backend has generated some kind of error.
  168. *
  169. * For the moment write errors are usually must be in response to a mutation that has been written
  170. * with writeUserMutation:. Spontaneously errors due to idle timeout, server restart, or credential
  171. * expiration aren't yet supported.
  172. *
  173. * @param errorCode A FIRFirestoreErrorCode value, from FIRFirestoreErrors.h
  174. * @param userInfo Any additional details that the server might have sent along with the error.
  175. * For the moment this is effectively unused, but is logged.
  176. * @param keepInQueue Whether to keep the write in the write queue as it will be retried.
  177. */
  178. - (FSTOutstandingWrite *)receiveWriteError:(int)errorCode
  179. userInfo:(NSDictionary<NSString *, id> *)userInfo
  180. keepInQueue:(BOOL)keepInQueue;
  181. /**
  182. * Delivers a write acknowledgement as if the Streaming Write backend has acknowledged a write with
  183. * the snapshot version at which the write was committed.
  184. *
  185. * @param commitVersion The snapshot version at which the simulated server has committed
  186. * the mutation. Snapshot versions must be monotonically increasing.
  187. * @param mutationResults The mutation results for the write that is being acked.
  188. */
  189. - (FSTOutstandingWrite *)receiveWriteAckWithVersion:(const model::SnapshotVersion &)commitVersion
  190. mutationResults:
  191. (std::vector<model::MutationResult>)mutationResults;
  192. /**
  193. * A count of the mutations written to the write stream by the FSTSyncEngine, but not yet
  194. * acknowledged via receiveWriteError: or receiveWriteAckWithVersion:mutationResults.
  195. */
  196. @property(nonatomic, readonly) int sentWritesCount;
  197. /**
  198. * A count of the total number of requests sent to the write stream since the beginning of the test
  199. * case.
  200. */
  201. @property(nonatomic, readonly) int writeStreamRequestCount;
  202. /**
  203. * A count of the total number of requests sent to the watch stream since the beginning of the test
  204. * case.
  205. */
  206. @property(nonatomic, readonly) int watchStreamRequestCount;
  207. /**
  208. * Disables RemoteStore's network connection and shuts down all streams.
  209. */
  210. - (void)disableNetwork;
  211. /**
  212. * Enables RemoteStore's network connection.
  213. */
  214. - (void)enableNetwork;
  215. /**
  216. * Runs a pending timer callback on the worker queue.
  217. */
  218. - (void)runTimer:(util::TimerId)timerID;
  219. /**
  220. * Switches the FSTSyncEngine to a new user. The test driver tracks the outstanding mutations for
  221. * each user, so future receiveWriteAck/Error operations will validate the write sent to the mock
  222. * datastore matches the next outstanding write for that user.
  223. */
  224. - (void)changeUser:(const auth::User &)user;
  225. /**
  226. * Drains the client's dispatch queue.
  227. */
  228. - (void)drainQueue;
  229. /**
  230. * Returns all query events generated by the FSTSyncEngine in response to the event injection
  231. * methods called previously. The events are cleared after each invocation of this method.
  232. */
  233. - (NSArray<FSTQueryEvent *> *)capturedEventsSinceLastCall;
  234. /**
  235. * Returns the names of the documents that the client acknowledged since the last call to this
  236. * method. The keys are cleared after each invocation of this method.
  237. */
  238. - (NSArray<NSString *> *)capturedAcknowledgedWritesSinceLastCall;
  239. /**
  240. * Returns the names of the documents that the client rejected since the last call to this
  241. * method. The keys are cleared after each invocation of this method.
  242. */
  243. - (NSArray<NSString *> *)capturedRejectedWritesSinceLastCall;
  244. /** The current set of documents in limbo with active targets. */
  245. - (std::map<model::DocumentKey, model::TargetId>)activeLimboDocumentResolutions;
  246. /** The current set of documents in limbo that are enqueued for resolution. */
  247. - (std::vector<model::DocumentKey>)enqueuedLimboDocumentResolutions;
  248. /** The expected set of documents in limbo with an active target. */
  249. - (const model::DocumentKeySet &)expectedActiveLimboDocuments;
  250. /** Sets the expected set of documents in limbo with an active target. */
  251. - (void)setExpectedActiveLimboDocuments:(model::DocumentKeySet)docs;
  252. /** The expected set of documents in limbo that are enqueued for resolution. */
  253. - (const model::DocumentKeySet &)expectedEnqueuedLimboDocuments;
  254. /** Sets the expected set of documents in limbo that are enqueued for resolution. */
  255. - (void)setExpectedEnqueuedLimboDocuments:(model::DocumentKeySet)docs;
  256. /**
  257. * The writes that have been sent to the FSTSyncEngine via writeUserMutation: but not yet
  258. * acknowledged by calling receiveWriteAck/Error:. They are tracked per-user.
  259. *
  260. * It is mostly an implementation detail used internally to validate that the writes sent to the
  261. * mock backend by the FSTSyncEngine match the user mutations that initiated them.
  262. *
  263. * It is exposed specifically for use with the
  264. * initWithPersistence:GCEnabled:outstandingWrites: initializer to test persistence
  265. * scenarios where the FSTSyncEngine is restarted while the Persistence implementation still has
  266. * outstanding persisted mutations.
  267. *
  268. * Note: The size of the list for the current user will generally be the same as
  269. * sentWritesCount, but not necessarily, since the `RemoteStore` limits the number of
  270. * outstanding writes to the backend at a given time.
  271. */
  272. @property(nonatomic, assign, readonly) const FSTOutstandingWriteQueues &outstandingWrites;
  273. /** The current user for the FSTSyncEngine; determines which mutation queue is active. */
  274. @property(nonatomic, assign, readonly) const auth::User &currentUser;
  275. /**
  276. * The number of waitForPendingWrites events that have been received.
  277. */
  278. @property(nonatomic, readonly) int waitForPendingWritesEvents;
  279. - (void)incrementWaitForPendingWritesEvents;
  280. - (void)resetWaitForPendingWritesEvents;
  281. /**
  282. * Register a new waitForPendingWrites() callback.
  283. */
  284. - (void)waitForPendingWrites;
  285. /**
  286. * The number of snapshots-in-sync events that have been received.
  287. */
  288. @property(nonatomic, readonly) int snapshotsInSyncEvents;
  289. - (void)incrementSnapshotsInSyncEvents;
  290. - (void)resetSnapshotsInSyncEvents;
  291. /**
  292. * Adds a snpahots-in-sync listener to the event manager and keeps track of it so that it
  293. * can be easily removed later.
  294. */
  295. - (void)addSnapshotsInSyncListener;
  296. /**
  297. * Removes the snapshots-in-sync listener from the event manager.
  298. */
  299. - (void)removeSnapshotsInSyncListener;
  300. /** The set of active targets as observed on the watch stream. */
  301. - (const std::unordered_map<model::TargetId, local::TargetData> &)activeTargets;
  302. /** The expected set of active targets, keyed by target ID. */
  303. - (const ActiveTargetMap &)expectedActiveTargets;
  304. - (void)setExpectedActiveTargets:(ActiveTargetMap)targets;
  305. @end
  306. NS_ASSUME_NONNULL_END