FSTDatastoreTests.mm 9.0 KB

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