FSTSyncEngineTestDriver.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #import "Firestore/Source/Core/FSTTypes.h"
  18. @class FSTDocumentKey;
  19. @class FSTMutation;
  20. @class FSTMutationResult;
  21. @class FSTQuery;
  22. @class FSTQueryData;
  23. @class FSTSnapshotVersion;
  24. @class FSTUser;
  25. @class FSTViewSnapshot;
  26. @class FSTWatchChange;
  27. @protocol FSTGarbageCollector;
  28. @protocol FSTPersistence;
  29. NS_ASSUME_NONNULL_BEGIN
  30. /**
  31. * Interface used for object that contain exactly one of either a view snapshot or an error for the
  32. * given query.
  33. */
  34. @interface FSTQueryEvent : NSObject
  35. @property(nonatomic, strong) FSTQuery *query;
  36. @property(nonatomic, strong, nullable) FSTViewSnapshot *viewSnapshot;
  37. @property(nonatomic, strong, nullable) NSError *error;
  38. @end
  39. /** Holds an outstanding write and its result. */
  40. @interface FSTOutstandingWrite : NSObject
  41. /** The write that is outstanding. */
  42. @property(nonatomic, strong, readwrite) FSTMutation *write;
  43. /** Whether this write is done (regardless of whether it was successful or not). */
  44. @property(nonatomic, assign, readwrite) BOOL done;
  45. /** The error - if any - of this write. */
  46. @property(nonatomic, strong, nullable, readwrite) NSError *error;
  47. @end
  48. /** Mapping of user => array of FSTMutations for that user. */
  49. typedef NSDictionary<FSTUser *, NSArray<FSTOutstandingWrite *> *> FSTOutstandingWriteQueues;
  50. /**
  51. * A test driver for FSTSyncEngine that allows simulated event delivery and capture. As much as
  52. * possible, all sources of nondeterminism are removed so that test execution is consistent and
  53. * reliable.
  54. *
  55. * FSTSyncEngineTestDriver:
  56. *
  57. * + constructs an FSTSyncEngine using a mocked FSTDatastore for the backend;
  58. * + allows the caller to trigger events (user API calls and incoming FSTDatastore messages);
  59. * + performs sequencing validation internally (e.g. that when a user mutation is initiated, the
  60. * FSTSyncEngine correctly sends it to the remote store); and
  61. * + exposes the set of FSTQueryEvents generated for the caller to verify.
  62. *
  63. * Events come in three major flavors:
  64. *
  65. * + user events: simulate user API calls
  66. * + watch events: simulate RPC interactions with the Watch backend
  67. * + write events: simulate RPC interactions with the Streaming Write backend
  68. *
  69. * Each method on the driver injects a different event into the system.
  70. */
  71. @interface FSTSyncEngineTestDriver : NSObject
  72. /**
  73. * Initializes the underlying FSTSyncEngine with the given local persistence implementation and
  74. * garbage collection policy.
  75. */
  76. - (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
  77. garbageCollector:(id<FSTGarbageCollector>)garbageCollector;
  78. /**
  79. * Initializes the underlying FSTSyncEngine with the given local persistence implementation and
  80. * a set of existing outstandingWrites (useful when your FSTPersistence object has
  81. * persisted mutation queues).
  82. */
  83. - (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
  84. garbageCollector:(id<FSTGarbageCollector>)garbageCollector
  85. initialUser:(FSTUser *)initialUser
  86. outstandingWrites:(FSTOutstandingWriteQueues *)outstandingWrites
  87. NS_DESIGNATED_INITIALIZER;
  88. - (instancetype)init NS_UNAVAILABLE;
  89. /** Starts the FSTSyncEngine and its underlying components. */
  90. - (void)start;
  91. /** Validates that the API has been used correctly after a test is complete. */
  92. - (void)validateUsage;
  93. /** Shuts the FSTSyncEngine down. */
  94. - (void)shutdown;
  95. /**
  96. * Adds a listener to the FSTSyncEngine as if the user had initiated a new listen for the given
  97. * query.
  98. *
  99. * Resulting events are captured and made available via the capturedEventsSinceLastCall method.
  100. *
  101. * @param query A valid query to execute against the backend.
  102. * @return The target ID assigned by the system to track the query.
  103. */
  104. - (FSTTargetID)addUserListenerWithQuery:(FSTQuery *)query;
  105. /**
  106. * Removes a listener from the FSTSyncEngine as if the user had removed a listener corresponding
  107. * to the given query.
  108. *
  109. * Resulting events are captured and made available via the capturedEventsSinceLastCall method.
  110. *
  111. * @param query An identical query corresponding to one passed to -addUserListenerWithQuery.
  112. */
  113. - (void)removeUserListenerWithQuery:(FSTQuery *)query;
  114. /**
  115. * Delivers a WatchChange RPC to the FSTSyncEngine as if it were received from the backend watch
  116. * service, either in response to addUserListener: or removeUserListener calls or because the
  117. * simulated backend has new data.
  118. *
  119. * Resulting events are captured and made available via the capturedEventsSinceLastCall method.
  120. *
  121. * @param change Any type of watch change
  122. * @param snapshot A snapshot version to attach, if applicable. This should be sent when
  123. * simulating the server having sent a complete snapshot.
  124. */
  125. - (void)receiveWatchChange:(FSTWatchChange *)change
  126. snapshotVersion:(FSTSnapshotVersion *_Nullable)snapshot;
  127. /**
  128. * Delivers a watch stream error as if the Streaming Watch backend has generated some kind of error.
  129. *
  130. * @param errorCode A FIRFirestoreErrorCode value, from FIRFirestoreErrors.h
  131. * @param userInfo Any additional details that the server might have sent along with the error.
  132. * For the moment this is effectively unused, but is logged.
  133. */
  134. - (void)receiveWatchStreamError:(int)errorCode userInfo:(NSDictionary<NSString *, id> *)userInfo;
  135. /**
  136. * Performs a mutation against the FSTSyncEngine as if the user had written the mutation through
  137. * the API.
  138. *
  139. * Also retains the mutation so that the driver can validate that the sync engine sent the mutation
  140. * to the remote store before receiveWatchChange:snapshotVersion: and receiveWriteError:userInfo:
  141. * events are processed.
  142. *
  143. * @param mutation Any type of valid mutation.
  144. */
  145. - (void)writeUserMutation:(FSTMutation *)mutation;
  146. /**
  147. * Delivers a write error as if the Streaming Write backend has generated some kind of error.
  148. *
  149. * For the moment write errors are usually must be in response to a mutation that has been written
  150. * with writeUserMutation:. Spontaneously errors due to idle timeout, server restart, or credential
  151. * expiration aren't yet supported.
  152. *
  153. * @param errorCode A FIRFirestoreErrorCode value, from FIRFirestoreErrors.h
  154. * @param userInfo Any additional details that the server might have sent along with the error.
  155. * For the moment this is effectively unused, but is logged.
  156. */
  157. - (FSTOutstandingWrite *)receiveWriteError:(int)errorCode
  158. userInfo:(NSDictionary<NSString *, id> *)userInfo;
  159. /**
  160. * Delivers a write acknowledgement as if the Streaming Write backend has acknowledged a write with
  161. * the snapshot version at which the write was committed.
  162. *
  163. * @param commitVersion The snapshot version at which the simulated server has committed
  164. * the mutation. Snapshot versions must be monotonically increasing.
  165. * @param mutationResults The mutation results for the write that is being acked.
  166. */
  167. - (FSTOutstandingWrite *)receiveWriteAckWithVersion:(FSTSnapshotVersion *)commitVersion
  168. mutationResults:(NSArray<FSTMutationResult *> *)mutationResults;
  169. /**
  170. * A count of the mutations written to the write stream by the FSTSyncEngine, but not yet
  171. * acknowledged via receiveWriteError: or receiveWriteAckWithVersion:mutationResults.
  172. */
  173. @property(nonatomic, readonly) int sentWritesCount;
  174. /**
  175. * A count of the total number of requests sent to the write stream since the beginning of the test
  176. * case.
  177. */
  178. @property(nonatomic, readonly) int writeStreamRequestCount;
  179. /**
  180. * A count of the total number of requests sent to the watch stream since the beginning of the test
  181. * case.
  182. */
  183. @property(nonatomic, readonly) int watchStreamRequestCount;
  184. /**
  185. * Disables RemoteStore's network connection and shuts down all streams.
  186. */
  187. - (void)disableNetwork;
  188. /**
  189. * Enables RemoteStore's network connection.
  190. */
  191. - (void)enableNetwork;
  192. /**
  193. * Switches the FSTSyncEngine to a new user. The test driver tracks the outstanding mutations for
  194. * each user, so future receiveWriteAck/Error operations will validate the write sent to the mock
  195. * datastore matches the next outstanding write for that user.
  196. */
  197. - (void)changeUser:(FSTUser *)user;
  198. /**
  199. * Returns all query events generated by the FSTSyncEngine in response to the event injection
  200. * methods called previously. The events are cleared after each invocation of this method.
  201. */
  202. - (NSArray<FSTQueryEvent *> *)capturedEventsSinceLastCall;
  203. /**
  204. * The writes that have been sent to the FSTSyncEngine via writeUserMutation: but not yet
  205. * acknowledged by calling receiveWriteAck/Error:. They are tracked per-user.
  206. *
  207. * It is mostly an implementation detail used internally to validate that the writes sent to the
  208. * mock backend by the FSTSyncEngine match the user mutations that initiated them.
  209. *
  210. * It is exposed specifically for use with the
  211. * initWithPersistence:GCEnabled:outstandingWrites: initializer to test persistence
  212. * scenarios where the FSTSyncEngine is restarted while the FSTPersistence implementation still has
  213. * outstanding persisted mutations.
  214. *
  215. * Note: The size of the list for the current user will generally be the same as
  216. * sentWritesCount, but not necessarily, since the FSTRemoteStore limits the number of
  217. * outstanding writes to the backend at a given time.
  218. */
  219. @property(nonatomic, strong, readonly) FSTOutstandingWriteQueues *outstandingWrites;
  220. /** The current user for the FSTSyncEngine; determines which mutation queue is active. */
  221. @property(nonatomic, strong, readonly) FSTUser *currentUser;
  222. /** The current set of documents in limbo. */
  223. @property(nonatomic, strong, readonly)
  224. NSDictionary<FSTDocumentKey *, FSTBoxedTargetID *> *currentLimboDocuments;
  225. /** The expected set of documents in limbo. */
  226. @property(nonatomic, strong, readwrite) NSSet<FSTDocumentKey *> *expectedLimboDocuments;
  227. /** The set of active targets as observed on the watch stream. */
  228. @property(nonatomic, strong, readonly)
  229. NSDictionary<FSTBoxedTargetID *, FSTQueryData *> *activeTargets;
  230. /** The expected set of active targets, keyed by target ID. */
  231. @property(nonatomic, strong, readwrite)
  232. NSDictionary<FSTBoxedTargetID *, FSTQueryData *> *expectedActiveTargets;
  233. @end
  234. NS_ASSUME_NONNULL_END