FSTDatastoreTests.mm 9.2 KB

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