FSTDatastoreTests.mm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/Core/FSTSnapshotVersion.h"
  26. #import "Firestore/Source/Local/FSTQueryData.h"
  27. #import "Firestore/Source/Model/FSTDocumentKey.h"
  28. #import "Firestore/Source/Model/FSTFieldValue.h"
  29. #import "Firestore/Source/Model/FSTMutation.h"
  30. #import "Firestore/Source/Model/FSTMutationBatch.h"
  31. #import "Firestore/Source/Remote/FSTDatastore.h"
  32. #import "Firestore/Source/Remote/FSTRemoteEvent.h"
  33. #import "Firestore/Source/Remote/FSTRemoteStore.h"
  34. #import "Firestore/Source/Util/FSTAssert.h"
  35. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  36. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  37. #include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
  38. #include "Firestore/core/src/firebase/firestore/core/database_info.h"
  39. #include "Firestore/core/src/firebase/firestore/model/database_id.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::TargetId;
  46. NS_ASSUME_NONNULL_BEGIN
  47. @interface FSTRemoteStore (Tests)
  48. - (void)commitBatch:(FSTMutationBatch *)batch;
  49. @end
  50. #pragma mark - FSTRemoteStoreEventCapture
  51. @interface FSTRemoteStoreEventCapture : NSObject <FSTRemoteSyncer>
  52. - (instancetype)init __attribute__((unavailable("Use initWithTestCase:")));
  53. - (instancetype)initWithTestCase:(XCTestCase *_Nullable)testCase NS_DESIGNATED_INITIALIZER;
  54. - (void)expectWriteEventWithDescription:(NSString *)description;
  55. - (void)expectListenEventWithDescription:(NSString *)description;
  56. @property(nonatomic, weak, nullable) XCTestCase *testCase;
  57. @property(nonatomic, strong) NSMutableArray<NSObject *> *writeEvents;
  58. @property(nonatomic, strong) NSMutableArray<NSObject *> *listenEvents;
  59. @property(nonatomic, strong) NSMutableArray<XCTestExpectation *> *writeEventExpectations;
  60. @property(nonatomic, strong) NSMutableArray<XCTestExpectation *> *listenEventExpectations;
  61. @end
  62. @implementation FSTRemoteStoreEventCapture
  63. - (instancetype)initWithTestCase:(XCTestCase *_Nullable)testCase {
  64. if (self = [super init]) {
  65. _writeEvents = [NSMutableArray array];
  66. _listenEvents = [NSMutableArray array];
  67. _testCase = testCase;
  68. _writeEventExpectations = [NSMutableArray array];
  69. _listenEventExpectations = [NSMutableArray array];
  70. }
  71. return self;
  72. }
  73. - (void)expectWriteEventWithDescription:(NSString *)description {
  74. [self.writeEventExpectations
  75. addObject:[self.testCase
  76. expectationWithDescription:[NSString
  77. stringWithFormat:@"write event %lu: %@",
  78. (unsigned long)
  79. self.writeEventExpectations
  80. .count,
  81. description]]];
  82. }
  83. - (void)expectListenEventWithDescription:(NSString *)description {
  84. [self.listenEventExpectations
  85. addObject:[self.testCase
  86. expectationWithDescription:[NSString
  87. stringWithFormat:@"listen event %lu: %@",
  88. (unsigned long)
  89. self.listenEventExpectations
  90. .count,
  91. description]]];
  92. }
  93. - (void)applySuccessfulWriteWithResult:(FSTMutationBatchResult *)batchResult {
  94. [self.writeEvents addObject:batchResult];
  95. XCTestExpectation *expectation = [self.writeEventExpectations objectAtIndex:0];
  96. [self.writeEventExpectations removeObjectAtIndex:0];
  97. [expectation fulfill];
  98. }
  99. - (void)rejectFailedWriteWithBatchID:(FSTBatchID)batchID error:(NSError *)error {
  100. FSTFail(@"Not implemented");
  101. }
  102. - (void)applyRemoteEvent:(FSTRemoteEvent *)remoteEvent {
  103. [self.listenEvents addObject:remoteEvent];
  104. XCTestExpectation *expectation = [self.listenEventExpectations objectAtIndex:0];
  105. [self.listenEventExpectations removeObjectAtIndex:0];
  106. [expectation fulfill];
  107. }
  108. - (void)rejectListenWithTargetID:(const TargetId)targetID error:(NSError *)error {
  109. FSTFail(@"Not implemented");
  110. }
  111. @end
  112. #pragma mark - FSTDatastoreTests
  113. @interface FSTDatastoreTests : XCTestCase
  114. @end
  115. @implementation FSTDatastoreTests {
  116. FSTDispatchQueue *_testWorkerQueue;
  117. FSTLocalStore *_localStore;
  118. EmptyCredentialsProvider _credentials;
  119. DatabaseInfo _databaseInfo;
  120. FSTDatastore *_datastore;
  121. FSTRemoteStore *_remoteStore;
  122. }
  123. - (void)setUp {
  124. [super setUp];
  125. NSString *projectID = [[NSProcessInfo processInfo] environment][@"PROJECT_ID"];
  126. if (!projectID) {
  127. projectID = @"test-db";
  128. }
  129. FIRFirestoreSettings *settings = [FSTIntegrationTestCase settings];
  130. if (!settings.sslEnabled) {
  131. [GRPCCall useInsecureConnectionsForHost:settings.host];
  132. }
  133. DatabaseId database_id(util::MakeStringView(projectID), DatabaseId::kDefault);
  134. _databaseInfo = DatabaseInfo(database_id, "test-key", util::MakeStringView(settings.host),
  135. settings.sslEnabled);
  136. _testWorkerQueue = [FSTDispatchQueue
  137. queueWith:dispatch_queue_create("com.google.firestore.FSTDatastoreTestsWorkerQueue",
  138. DISPATCH_QUEUE_SERIAL)];
  139. _datastore = [FSTDatastore datastoreWithDatabase:&_databaseInfo
  140. workerDispatchQueue:_testWorkerQueue
  141. credentials:&_credentials];
  142. _remoteStore = [[FSTRemoteStore alloc] initWithLocalStore:_localStore
  143. datastore:_datastore
  144. workerDispatchQueue:_testWorkerQueue];
  145. [_testWorkerQueue dispatchAsync:^() {
  146. [_remoteStore start];
  147. }];
  148. }
  149. - (void)tearDown {
  150. XCTestExpectation *completion = [self expectationWithDescription:@"shutdown"];
  151. [_testWorkerQueue dispatchAsync:^{
  152. [_remoteStore shutdown];
  153. [completion fulfill];
  154. }];
  155. [self awaitExpectations];
  156. [super tearDown];
  157. }
  158. - (void)testCommit {
  159. XCTestExpectation *expectation = [self expectationWithDescription:@"commitWithCompletion"];
  160. [_datastore commitMutations:@[]
  161. completion:^(NSError *_Nullable error) {
  162. XCTAssertNil(error, @"Failed to commit");
  163. [expectation fulfill];
  164. }];
  165. [self awaitExpectations];
  166. }
  167. - (void)testStreamingWrite {
  168. FSTRemoteStoreEventCapture *capture = [[FSTRemoteStoreEventCapture alloc] initWithTestCase:self];
  169. [capture expectWriteEventWithDescription:@"write mutations"];
  170. _remoteStore.syncEngine = capture;
  171. FSTSetMutation *mutation = [self setMutation];
  172. FSTMutationBatch *batch = [[FSTMutationBatch alloc] initWithBatchID:23
  173. localWriteTime:[FIRTimestamp timestamp]
  174. mutations:@[ mutation ]];
  175. [_testWorkerQueue dispatchAsync:^{
  176. [_remoteStore commitBatch:batch];
  177. }];
  178. [self awaitExpectations];
  179. }
  180. - (void)awaitExpectations {
  181. [self waitForExpectationsWithTimeout:4.0
  182. handler:^(NSError *_Nullable expectationError) {
  183. if (expectationError) {
  184. XCTFail(@"Error waiting for timeout: %@", expectationError);
  185. }
  186. }];
  187. }
  188. - (FSTSetMutation *)setMutation {
  189. return [[FSTSetMutation alloc]
  190. initWithKey:[FSTDocumentKey keyWithPathString:@"rooms/eros"]
  191. value:[[FSTObjectValue alloc]
  192. initWithDictionary:@{@"name" : [FSTStringValue stringValue:@"Eros"]}]
  193. precondition:[FSTPrecondition none]];
  194. }
  195. @end
  196. NS_ASSUME_NONNULL_END