FSTDatastoreTests.mm 9.5 KB

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