FSTDatastoreTests.mm 9.4 KB

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