FSTDatastoreTests.mm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <FirebaseFirestore/FIRTimestamp.h>
  18. #import <GRPCClient/GRPCCall+ChannelCredentials.h>
  19. #import <GRPCClient/GRPCCall+Tests.h>
  20. #import <XCTest/XCTest.h>
  21. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  22. #import "Firestore/Source/API/FSTUserDataConverter.h"
  23. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  24. #import "Firestore/Source/Core/FSTQuery.h"
  25. #import "Firestore/Source/Local/FSTQueryData.h"
  26. #import "Firestore/Source/Model/FSTDocumentKey.h"
  27. #import "Firestore/Source/Model/FSTFieldValue.h"
  28. #import "Firestore/Source/Model/FSTMutation.h"
  29. #import "Firestore/Source/Model/FSTMutationBatch.h"
  30. #import "Firestore/Source/Remote/FSTDatastore.h"
  31. #import "Firestore/Source/Remote/FSTRemoteEvent.h"
  32. #import "Firestore/Source/Remote/FSTRemoteStore.h"
  33. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  34. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  35. #include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
  36. #include "Firestore/core/src/firebase/firestore/core/database_info.h"
  37. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  38. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  39. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  40. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  41. namespace util = firebase::firestore::util;
  42. using firebase::firestore::auth::EmptyCredentialsProvider;
  43. using firebase::firestore::core::DatabaseInfo;
  44. using firebase::firestore::model::BatchId;
  45. using firebase::firestore::model::DatabaseId;
  46. using firebase::firestore::model::DocumentKeySet;
  47. using firebase::firestore::model::Precondition;
  48. using firebase::firestore::model::TargetId;
  49. NS_ASSUME_NONNULL_BEGIN
  50. @interface FSTRemoteStore (Tests)
  51. - (void)addBatchToWritePipeline:(FSTMutationBatch *)batch;
  52. @end
  53. #pragma mark - FSTRemoteStoreEventCapture
  54. @interface FSTRemoteStoreEventCapture : NSObject <FSTRemoteSyncer>
  55. - (instancetype)init __attribute__((unavailable("Use initWithTestCase:")));
  56. - (instancetype)initWithTestCase:(XCTestCase *_Nullable)testCase NS_DESIGNATED_INITIALIZER;
  57. - (void)expectWriteEventWithDescription:(NSString *)description;
  58. - (void)expectListenEventWithDescription:(NSString *)description;
  59. @property(nonatomic, weak, nullable) XCTestCase *testCase;
  60. @property(nonatomic, strong) NSMutableArray<NSObject *> *writeEvents;
  61. @property(nonatomic, strong) NSMutableArray<NSObject *> *listenEvents;
  62. @property(nonatomic, strong) NSMutableArray<XCTestExpectation *> *writeEventExpectations;
  63. @property(nonatomic, strong) NSMutableArray<XCTestExpectation *> *listenEventExpectations;
  64. @end
  65. @implementation FSTRemoteStoreEventCapture
  66. - (instancetype)initWithTestCase:(XCTestCase *_Nullable)testCase {
  67. if (self = [super init]) {
  68. _writeEvents = [NSMutableArray array];
  69. _listenEvents = [NSMutableArray array];
  70. _testCase = testCase;
  71. _writeEventExpectations = [NSMutableArray array];
  72. _listenEventExpectations = [NSMutableArray array];
  73. }
  74. return self;
  75. }
  76. - (void)expectWriteEventWithDescription:(NSString *)description {
  77. [self.writeEventExpectations
  78. addObject:[self.testCase
  79. expectationWithDescription:[NSString
  80. stringWithFormat:@"write event %lu: %@",
  81. (unsigned long)
  82. self.writeEventExpectations
  83. .count,
  84. description]]];
  85. }
  86. - (void)expectListenEventWithDescription:(NSString *)description {
  87. [self.listenEventExpectations
  88. addObject:[self.testCase
  89. expectationWithDescription:[NSString
  90. stringWithFormat:@"listen event %lu: %@",
  91. (unsigned long)
  92. self.listenEventExpectations
  93. .count,
  94. description]]];
  95. }
  96. - (void)applySuccessfulWriteWithResult:(FSTMutationBatchResult *)batchResult {
  97. [self.writeEvents addObject:batchResult];
  98. XCTestExpectation *expectation = [self.writeEventExpectations objectAtIndex:0];
  99. [self.writeEventExpectations removeObjectAtIndex:0];
  100. [expectation fulfill];
  101. }
  102. - (void)rejectFailedWriteWithBatchID:(BatchId)batchID error:(NSError *)error {
  103. HARD_FAIL("Not implemented");
  104. }
  105. - (DocumentKeySet)remoteKeysForTarget:(FSTBoxedTargetID *)targetId {
  106. return DocumentKeySet{};
  107. }
  108. - (void)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent {
  109. [self.listenEvents addObject:remoteEvent];
  110. XCTestExpectation *expectation = [self.listenEventExpectations objectAtIndex:0];
  111. [self.listenEventExpectations removeObjectAtIndex:0];
  112. [expectation fulfill];
  113. }
  114. - (void)rejectListenWithTargetID:(const TargetId)targetID error:(NSError *)error {
  115. HARD_FAIL("Not implemented");
  116. }
  117. @end
  118. #pragma mark - FSTDatastoreTests
  119. @interface FSTDatastoreTests : XCTestCase
  120. @end
  121. @implementation FSTDatastoreTests {
  122. FSTDispatchQueue *_testWorkerQueue;
  123. FSTLocalStore *_localStore;
  124. EmptyCredentialsProvider _credentials;
  125. DatabaseInfo _databaseInfo;
  126. FSTDatastore *_datastore;
  127. FSTRemoteStore *_remoteStore;
  128. }
  129. - (void)setUp {
  130. [super setUp];
  131. NSString *projectID = [FSTIntegrationTestCase projectID];
  132. FIRFirestoreSettings *settings = [FSTIntegrationTestCase settings];
  133. if (!settings.sslEnabled) {
  134. [GRPCCall useInsecureConnectionsForHost:settings.host];
  135. }
  136. DatabaseId database_id(util::MakeString(projectID), DatabaseId::kDefault);
  137. _databaseInfo =
  138. DatabaseInfo(database_id, "test-key", util::MakeString(settings.host), settings.sslEnabled);
  139. _testWorkerQueue = [FSTDispatchQueue
  140. queueWith:dispatch_queue_create("com.google.firestore.FSTDatastoreTestsWorkerQueue",
  141. DISPATCH_QUEUE_SERIAL)];
  142. _datastore = [FSTDatastore datastoreWithDatabase:&_databaseInfo
  143. workerDispatchQueue:_testWorkerQueue
  144. credentials:&_credentials];
  145. _remoteStore = [[FSTRemoteStore alloc] initWithLocalStore:_localStore
  146. datastore:_datastore
  147. workerDispatchQueue:_testWorkerQueue];
  148. [_testWorkerQueue dispatchAsync:^() {
  149. [_remoteStore start];
  150. }];
  151. }
  152. - (void)tearDown {
  153. XCTestExpectation *completion = [self expectationWithDescription:@"shutdown"];
  154. [_testWorkerQueue dispatchAsync:^{
  155. [_remoteStore shutdown];
  156. [completion fulfill];
  157. }];
  158. [self awaitExpectations];
  159. [super tearDown];
  160. }
  161. - (void)testCommit {
  162. XCTestExpectation *expectation = [self expectationWithDescription:@"commitWithCompletion"];
  163. [_datastore commitMutations:@[]
  164. completion:^(NSError *_Nullable error) {
  165. XCTAssertNil(error, @"Failed to commit");
  166. [expectation fulfill];
  167. }];
  168. [self awaitExpectations];
  169. }
  170. - (void)testStreamingWrite {
  171. FSTRemoteStoreEventCapture *capture = [[FSTRemoteStoreEventCapture alloc] initWithTestCase:self];
  172. [capture expectWriteEventWithDescription:@"write mutations"];
  173. _remoteStore.syncEngine = capture;
  174. FSTSetMutation *mutation = [self setMutation];
  175. FSTMutationBatch *batch = [[FSTMutationBatch alloc] initWithBatchID:23
  176. localWriteTime:[FIRTimestamp timestamp]
  177. mutations:@[ mutation ]];
  178. [_testWorkerQueue dispatchAsync:^{
  179. [_remoteStore addBatchToWritePipeline:batch];
  180. }];
  181. [self awaitExpectations];
  182. }
  183. - (void)awaitExpectations {
  184. [self waitForExpectationsWithTimeout:4.0
  185. handler:^(NSError *_Nullable expectationError) {
  186. if (expectationError) {
  187. XCTFail(@"Error waiting for timeout: %@", expectationError);
  188. }
  189. }];
  190. }
  191. - (FSTSetMutation *)setMutation {
  192. return [[FSTSetMutation alloc]
  193. initWithKey:[FSTDocumentKey keyWithPathString:@"rooms/eros"]
  194. value:[[FSTObjectValue alloc]
  195. initWithDictionary:@{@"name" : [FSTStringValue stringValue:@"Eros"]}]
  196. precondition:Precondition::None()];
  197. }
  198. @end
  199. NS_ASSUME_NONNULL_END