FSTDatastoreTests.mm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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::DatabaseId;
  45. using firebase::firestore::model::DocumentKeySet;
  46. using firebase::firestore::model::Precondition;
  47. using firebase::firestore::model::TargetId;
  48. NS_ASSUME_NONNULL_BEGIN
  49. @interface FSTRemoteStore (Tests)
  50. - (void)commitBatch:(FSTMutationBatch *)batch;
  51. @end
  52. #pragma mark - FSTRemoteStoreEventCapture
  53. @interface FSTRemoteStoreEventCapture : NSObject <FSTRemoteSyncer>
  54. - (instancetype)init __attribute__((unavailable("Use initWithTestCase:")));
  55. - (instancetype)initWithTestCase:(XCTestCase *_Nullable)testCase NS_DESIGNATED_INITIALIZER;
  56. - (void)expectWriteEventWithDescription:(NSString *)description;
  57. - (void)expectListenEventWithDescription:(NSString *)description;
  58. @property(nonatomic, weak, nullable) XCTestCase *testCase;
  59. @property(nonatomic, strong) NSMutableArray<NSObject *> *writeEvents;
  60. @property(nonatomic, strong) NSMutableArray<NSObject *> *listenEvents;
  61. @property(nonatomic, strong) NSMutableArray<XCTestExpectation *> *writeEventExpectations;
  62. @property(nonatomic, strong) NSMutableArray<XCTestExpectation *> *listenEventExpectations;
  63. @end
  64. @implementation FSTRemoteStoreEventCapture
  65. - (instancetype)initWithTestCase:(XCTestCase *_Nullable)testCase {
  66. if (self = [super init]) {
  67. _writeEvents = [NSMutableArray array];
  68. _listenEvents = [NSMutableArray array];
  69. _testCase = testCase;
  70. _writeEventExpectations = [NSMutableArray array];
  71. _listenEventExpectations = [NSMutableArray array];
  72. }
  73. return self;
  74. }
  75. - (void)expectWriteEventWithDescription:(NSString *)description {
  76. [self.writeEventExpectations
  77. addObject:[self.testCase
  78. expectationWithDescription:[NSString
  79. stringWithFormat:@"write event %lu: %@",
  80. (unsigned long)
  81. self.writeEventExpectations
  82. .count,
  83. description]]];
  84. }
  85. - (void)expectListenEventWithDescription:(NSString *)description {
  86. [self.listenEventExpectations
  87. addObject:[self.testCase
  88. expectationWithDescription:[NSString
  89. stringWithFormat:@"listen event %lu: %@",
  90. (unsigned long)
  91. self.listenEventExpectations
  92. .count,
  93. description]]];
  94. }
  95. - (void)applySuccessfulWriteWithResult:(FSTMutationBatchResult *)batchResult {
  96. [self.writeEvents addObject:batchResult];
  97. XCTestExpectation *expectation = [self.writeEventExpectations objectAtIndex:0];
  98. [self.writeEventExpectations removeObjectAtIndex:0];
  99. [expectation fulfill];
  100. }
  101. - (void)rejectFailedWriteWithBatchID:(FSTBatchID)batchID error:(NSError *)error {
  102. HARD_FAIL("Not implemented");
  103. }
  104. - (DocumentKeySet)remoteKeysForTarget:(FSTBoxedTargetID *)targetId {
  105. return DocumentKeySet{};
  106. }
  107. - (void)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent {
  108. [self.listenEvents addObject:remoteEvent];
  109. XCTestExpectation *expectation = [self.listenEventExpectations objectAtIndex:0];
  110. [self.listenEventExpectations removeObjectAtIndex:0];
  111. [expectation fulfill];
  112. }
  113. - (void)rejectListenWithTargetID:(const TargetId)targetID error:(NSError *)error {
  114. HARD_FAIL("Not implemented");
  115. }
  116. @end
  117. #pragma mark - FSTDatastoreTests
  118. @interface FSTDatastoreTests : XCTestCase
  119. @end
  120. @implementation FSTDatastoreTests {
  121. FSTDispatchQueue *_testWorkerQueue;
  122. FSTLocalStore *_localStore;
  123. EmptyCredentialsProvider _credentials;
  124. DatabaseInfo _databaseInfo;
  125. FSTDatastore *_datastore;
  126. FSTRemoteStore *_remoteStore;
  127. }
  128. - (void)setUp {
  129. [super setUp];
  130. NSString *projectID = [[NSProcessInfo processInfo] environment][@"PROJECT_ID"];
  131. if (!projectID) {
  132. projectID = @"test-db";
  133. }
  134. FIRFirestoreSettings *settings = [FSTIntegrationTestCase settings];
  135. if (!settings.sslEnabled) {
  136. [GRPCCall useInsecureConnectionsForHost:settings.host];
  137. }
  138. DatabaseId database_id(util::MakeStringView(projectID), DatabaseId::kDefault);
  139. _databaseInfo = DatabaseInfo(database_id, "test-key", util::MakeStringView(settings.host),
  140. settings.sslEnabled);
  141. _testWorkerQueue = [FSTDispatchQueue
  142. queueWith:dispatch_queue_create("com.google.firestore.FSTDatastoreTestsWorkerQueue",
  143. DISPATCH_QUEUE_SERIAL)];
  144. _datastore = [FSTDatastore datastoreWithDatabase:&_databaseInfo
  145. workerDispatchQueue:_testWorkerQueue
  146. credentials:&_credentials];
  147. _remoteStore = [[FSTRemoteStore alloc] initWithLocalStore:_localStore
  148. datastore:_datastore
  149. workerDispatchQueue:_testWorkerQueue];
  150. [_testWorkerQueue dispatchAsync:^() {
  151. [_remoteStore start];
  152. }];
  153. }
  154. - (void)tearDown {
  155. XCTestExpectation *completion = [self expectationWithDescription:@"shutdown"];
  156. [_testWorkerQueue dispatchAsync:^{
  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 dispatchAsync:^{
  181. [_remoteStore commitBatch:batch];
  182. }];
  183. [self awaitExpectations];
  184. }
  185. - (void)awaitExpectations {
  186. [self waitForExpectationsWithTimeout:4.0
  187. handler:^(NSError *_Nullable expectationError) {
  188. if (expectationError) {
  189. XCTFail(@"Error waiting for timeout: %@", expectationError);
  190. }
  191. }];
  192. }
  193. - (FSTSetMutation *)setMutation {
  194. return [[FSTSetMutation alloc]
  195. initWithKey:[FSTDocumentKey keyWithPathString:@"rooms/eros"]
  196. value:[[FSTObjectValue alloc]
  197. initWithDictionary:@{@"name" : [FSTStringValue stringValue:@"Eros"]}]
  198. precondition:Precondition::None()];
  199. }
  200. @end
  201. NS_ASSUME_NONNULL_END