FSTDatastoreTests.m 8.4 KB

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