FSTDatastoreTests.m 8.7 KB

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