FSTDatastoreTests.mm 8.7 KB

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