FSTDatastoreTests.mm 9.5 KB

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