FSTSpecTests.mm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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/Example/Tests/SpecTests/FSTSpecTests.h"
  17. #import <FirebaseFirestore/FIRFirestoreErrors.h>
  18. #import <GRPCClient/GRPCCall.h>
  19. #include <map>
  20. #include <utility>
  21. #import "Firestore/Source/Core/FSTEventManager.h"
  22. #import "Firestore/Source/Core/FSTQuery.h"
  23. #import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
  24. #import "Firestore/Source/Local/FSTNoOpGarbageCollector.h"
  25. #import "Firestore/Source/Local/FSTPersistence.h"
  26. #import "Firestore/Source/Local/FSTQueryData.h"
  27. #import "Firestore/Source/Model/FSTDocument.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/Remote/FSTExistenceFilter.h"
  32. #import "Firestore/Source/Remote/FSTWatchChange.h"
  33. #import "Firestore/Source/Util/FSTClasses.h"
  34. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  35. #import "Firestore/Example/Tests/Remote/FSTWatchChange+Testing.h"
  36. #import "Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h"
  37. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  38. #include "Firestore/core/src/firebase/firestore/auth/user.h"
  39. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  40. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  41. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  42. #include "Firestore/core/src/firebase/firestore/util/log.h"
  43. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  44. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  45. namespace testutil = firebase::firestore::testutil;
  46. namespace util = firebase::firestore::util;
  47. using firebase::firestore::auth::User;
  48. using firebase::firestore::model::DocumentKey;
  49. using firebase::firestore::model::SnapshotVersion;
  50. using firebase::firestore::model::TargetId;
  51. NS_ASSUME_NONNULL_BEGIN
  52. // Disables all other tests; useful for debugging. Multiple tests can have this tag and they'll all
  53. // be run (but all others won't).
  54. static NSString *const kExclusiveTag = @"exclusive";
  55. // A tag for tests that should be excluded from execution (on iOS), useful to allow the platforms
  56. // to temporarily diverge.
  57. static NSString *const kNoIOSTag = @"no-ios";
  58. @interface FSTSpecTests ()
  59. @property(nonatomic, strong) FSTSyncEngineTestDriver *driver;
  60. // Some config info for the currently running spec; used when restarting the driver (for doRestart).
  61. @property(nonatomic, assign) BOOL GCEnabled;
  62. @property(nonatomic, strong) id<FSTPersistence> driverPersistence;
  63. @end
  64. @implementation FSTSpecTests
  65. - (id<FSTPersistence>)persistence {
  66. @throw FSTAbstractMethodException(); // NOLINT
  67. }
  68. - (void)setUpForSpecWithConfig:(NSDictionary *)config {
  69. // Store persistence / GCEnabled so we can re-use it in doRestart.
  70. self.driverPersistence = [self persistence];
  71. NSNumber *GCEnabled = config[@"useGarbageCollection"];
  72. self.GCEnabled = [GCEnabled boolValue];
  73. self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:self.driverPersistence
  74. garbageCollector:self.garbageCollector];
  75. [self.driver start];
  76. }
  77. - (void)tearDownForSpec {
  78. [self.driver shutdown];
  79. [self.driverPersistence shutdown];
  80. }
  81. /**
  82. * Creates the appropriate garbage collector for the test configuration: an eager collector if
  83. * GC is enabled or a no-op collector otherwise.
  84. */
  85. - (id<FSTGarbageCollector>)garbageCollector {
  86. return self.GCEnabled ? [[FSTEagerGarbageCollector alloc] init]
  87. : [[FSTNoOpGarbageCollector alloc] init];
  88. }
  89. /**
  90. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  91. * FSTSpecTests since it is incomplete without the implementations supplied by its subclasses.
  92. */
  93. - (BOOL)isTestBaseClass {
  94. return [self class] == [FSTSpecTests class];
  95. }
  96. #pragma mark - Methods for constructing objects from specs.
  97. - (nullable FSTQuery *)parseQuery:(id)querySpec {
  98. if ([querySpec isKindOfClass:[NSString class]]) {
  99. return FSTTestQuery(util::MakeStringView((NSString *)querySpec));
  100. } else if ([querySpec isKindOfClass:[NSDictionary class]]) {
  101. NSDictionary *queryDict = (NSDictionary *)querySpec;
  102. NSString *path = queryDict[@"path"];
  103. __block FSTQuery *query = FSTTestQuery(util::MakeStringView(path));
  104. if (queryDict[@"limit"]) {
  105. NSNumber *limit = queryDict[@"limit"];
  106. query = [query queryBySettingLimit:limit.integerValue];
  107. }
  108. if (queryDict[@"filters"]) {
  109. NSArray *filters = queryDict[@"filters"];
  110. [filters enumerateObjectsUsingBlock:^(NSArray *_Nonnull filter, NSUInteger idx,
  111. BOOL *_Nonnull stop) {
  112. query = [query queryByAddingFilter:FSTTestFilter(util::MakeStringView(filter[0]), filter[1],
  113. filter[2])];
  114. }];
  115. }
  116. if (queryDict[@"orderBys"]) {
  117. NSArray *orderBys = queryDict[@"orderBys"];
  118. [orderBys enumerateObjectsUsingBlock:^(NSArray *_Nonnull orderBy, NSUInteger idx,
  119. BOOL *_Nonnull stop) {
  120. query = [query
  121. queryByAddingSortOrder:FSTTestOrderBy(util::MakeStringView(orderBy[0]), orderBy[1])];
  122. }];
  123. }
  124. return query;
  125. } else {
  126. XCTFail(@"Invalid query: %@", querySpec);
  127. return nil;
  128. }
  129. }
  130. - (SnapshotVersion)parseVersion:(NSNumber *_Nullable)version {
  131. return testutil::Version(version.longLongValue);
  132. }
  133. - (FSTDocumentViewChange *)parseChange:(NSArray *)change ofType:(FSTDocumentViewChangeType)type {
  134. BOOL hasMutations = NO;
  135. for (NSUInteger i = 3; i < change.count; ++i) {
  136. if ([change[i] isEqual:@"local"]) {
  137. hasMutations = YES;
  138. }
  139. }
  140. NSNumber *version = change[1];
  141. XCTAssert([change[0] isKindOfClass:[NSString class]]);
  142. FSTDocument *doc = FSTTestDoc(util::MakeStringView((NSString *)change[0]), version.longLongValue,
  143. change[2], hasMutations);
  144. return [FSTDocumentViewChange changeWithDocument:doc type:type];
  145. }
  146. #pragma mark - Methods for doing the steps of the spec test.
  147. - (void)doListen:(NSArray *)listenSpec {
  148. FSTQuery *query = [self parseQuery:listenSpec[1]];
  149. FSTTargetID actualID = [self.driver addUserListenerWithQuery:query];
  150. FSTTargetID expectedID = [listenSpec[0] intValue];
  151. XCTAssertEqual(actualID, expectedID, @"targetID assigned to listen");
  152. }
  153. - (void)doUnlisten:(NSArray *)unlistenSpec {
  154. FSTQuery *query = [self parseQuery:unlistenSpec[1]];
  155. [self.driver removeUserListenerWithQuery:query];
  156. }
  157. - (void)doSet:(NSArray *)setSpec {
  158. [self.driver writeUserMutation:FSTTestSetMutation(setSpec[0], setSpec[1])];
  159. }
  160. - (void)doPatch:(NSArray *)patchSpec {
  161. [self.driver
  162. writeUserMutation:FSTTestPatchMutation(util::MakeStringView(patchSpec[0]), patchSpec[1], {})];
  163. }
  164. - (void)doDelete:(NSString *)key {
  165. [self.driver writeUserMutation:FSTTestDeleteMutation(key)];
  166. }
  167. - (void)doWatchAck:(NSArray<NSNumber *> *)ackedTargets snapshot:(NSNumber *)watchSnapshot {
  168. FSTWatchTargetChange *change =
  169. [FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateAdded
  170. targetIDs:ackedTargets
  171. cause:nil];
  172. [self.driver receiveWatchChange:change snapshotVersion:[self parseVersion:watchSnapshot]];
  173. }
  174. - (void)doWatchCurrent:(NSArray<id> *)currentSpec snapshot:(NSNumber *)watchSnapshot {
  175. NSArray<NSNumber *> *currentTargets = currentSpec[0];
  176. NSData *resumeToken = [currentSpec[1] dataUsingEncoding:NSUTF8StringEncoding];
  177. FSTWatchTargetChange *change =
  178. [FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateCurrent
  179. targetIDs:currentTargets
  180. resumeToken:resumeToken];
  181. [self.driver receiveWatchChange:change snapshotVersion:[self parseVersion:watchSnapshot]];
  182. }
  183. - (void)doWatchRemove:(NSDictionary *)watchRemoveSpec snapshot:(NSNumber *)watchSnapshot {
  184. NSError *error = nil;
  185. NSDictionary *cause = watchRemoveSpec[@"cause"];
  186. if (cause) {
  187. int code = ((NSNumber *)cause[@"code"]).intValue;
  188. NSDictionary *userInfo = @{
  189. NSLocalizedDescriptionKey : @"Error from watchRemove.",
  190. };
  191. error = [NSError errorWithDomain:FIRFirestoreErrorDomain code:code userInfo:userInfo];
  192. }
  193. FSTWatchTargetChange *change =
  194. [FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateRemoved
  195. targetIDs:watchRemoveSpec[@"targetIds"]
  196. cause:error];
  197. [self.driver receiveWatchChange:change snapshotVersion:[self parseVersion:watchSnapshot]];
  198. // Unlike web, the FSTMockDatastore detects a watch removal with cause and will remove active
  199. // targets
  200. }
  201. - (void)doWatchEntity:(NSDictionary *)watchEntity snapshot:(NSNumber *_Nullable)watchSnapshot {
  202. if (watchEntity[@"docs"]) {
  203. HARD_ASSERT(!watchEntity[@"doc"], "Exactly one of |doc| or |docs| needs to be set.");
  204. int count = 0;
  205. NSArray *docs = watchEntity[@"docs"];
  206. for (NSDictionary *doc in docs) {
  207. count++;
  208. bool isLast = (count == docs.count);
  209. NSMutableDictionary *watchSpec = [NSMutableDictionary dictionary];
  210. watchSpec[@"doc"] = doc;
  211. if (watchEntity[@"targets"]) {
  212. watchSpec[@"targets"] = watchEntity[@"targets"];
  213. }
  214. if (watchEntity[@"removedTargets"]) {
  215. watchSpec[@"removedTargets"] = watchEntity[@"removedTargets"];
  216. }
  217. NSNumber *_Nullable version = nil;
  218. if (isLast) {
  219. version = watchSnapshot;
  220. }
  221. [self doWatchEntity:watchSpec snapshot:version];
  222. }
  223. } else if (watchEntity[@"doc"]) {
  224. NSArray *docSpec = watchEntity[@"doc"];
  225. FSTDocumentKey *key = FSTTestDocKey(docSpec[0]);
  226. FSTObjectValue *value = FSTTestObjectValue(docSpec[2]);
  227. SnapshotVersion version = [self parseVersion:docSpec[1]];
  228. FSTMaybeDocument *doc = [FSTDocument documentWithData:value
  229. key:key
  230. version:std::move(version)
  231. hasLocalMutations:NO];
  232. FSTWatchChange *change =
  233. [[FSTDocumentWatchChange alloc] initWithUpdatedTargetIDs:watchEntity[@"targets"]
  234. removedTargetIDs:watchEntity[@"removedTargets"]
  235. documentKey:doc.key
  236. document:doc];
  237. [self.driver receiveWatchChange:change snapshotVersion:[self parseVersion:watchSnapshot]];
  238. } else if (watchEntity[@"key"]) {
  239. FSTDocumentKey *docKey = FSTTestDocKey(watchEntity[@"key"]);
  240. FSTWatchChange *change =
  241. [[FSTDocumentWatchChange alloc] initWithUpdatedTargetIDs:@[]
  242. removedTargetIDs:watchEntity[@"removedTargets"]
  243. documentKey:docKey
  244. document:nil];
  245. [self.driver receiveWatchChange:change snapshotVersion:[self parseVersion:watchSnapshot]];
  246. } else {
  247. HARD_FAIL("Either key, doc or docs must be set.");
  248. }
  249. }
  250. - (void)doWatchFilter:(NSArray *)watchFilter snapshot:(NSNumber *_Nullable)watchSnapshot {
  251. NSArray<NSNumber *> *targets = watchFilter[0];
  252. HARD_ASSERT(targets.count == 1, "ExistenceFilters currently support exactly one target only.");
  253. int keyCount = watchFilter.count == 0 ? 0 : (int)watchFilter.count - 1;
  254. // TODO(dimond): extend this with different existence filters over time.
  255. FSTExistenceFilter *filter = [FSTExistenceFilter filterWithCount:keyCount];
  256. FSTExistenceFilterWatchChange *change =
  257. [FSTExistenceFilterWatchChange changeWithFilter:filter targetID:targets[0].intValue];
  258. [self.driver receiveWatchChange:change snapshotVersion:[self parseVersion:watchSnapshot]];
  259. }
  260. - (void)doWatchReset:(NSArray<NSNumber *> *)watchReset snapshot:(NSNumber *_Nullable)watchSnapshot {
  261. FSTWatchTargetChange *change =
  262. [FSTWatchTargetChange changeWithState:FSTWatchTargetChangeStateReset
  263. targetIDs:watchReset
  264. cause:nil];
  265. [self.driver receiveWatchChange:change snapshotVersion:[self parseVersion:watchSnapshot]];
  266. }
  267. - (void)doWatchStreamClose:(NSDictionary *)closeSpec {
  268. NSDictionary *errorSpec = closeSpec[@"error"];
  269. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  270. NSNumber *runBackoffTimer = closeSpec[@"runBackoffTimer"];
  271. // TODO(b/72313632): Incorporate backoff in iOS Spec Tests.
  272. HARD_ASSERT(runBackoffTimer.boolValue, "iOS Spec Tests don't support backoff.");
  273. [self.driver receiveWatchStreamError:code userInfo:errorSpec];
  274. }
  275. - (void)doWriteAck:(NSDictionary *)spec {
  276. SnapshotVersion version = [self parseVersion:spec[@"version"]];
  277. NSNumber *expectUserCallback = spec[@"expectUserCallback"];
  278. FSTMutationResult *mutationResult =
  279. [[FSTMutationResult alloc] initWithVersion:version transformResults:nil];
  280. FSTOutstandingWrite *write =
  281. [self.driver receiveWriteAckWithVersion:version mutationResults:@[ mutationResult ]];
  282. if (expectUserCallback.boolValue) {
  283. HARD_ASSERT(write.done, "Write should be done");
  284. HARD_ASSERT(!write.error, "Ack should not fail");
  285. }
  286. }
  287. - (void)doFailWrite:(NSDictionary *)spec {
  288. NSDictionary *errorSpec = spec[@"error"];
  289. NSNumber *expectUserCallback = spec[@"expectUserCallback"];
  290. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  291. FSTOutstandingWrite *write = [self.driver receiveWriteError:code userInfo:errorSpec];
  292. if (expectUserCallback.boolValue) {
  293. HARD_ASSERT(write.done, "Write should be done");
  294. XCTAssertNotNil(write.error, @"Write should have failed");
  295. XCTAssertEqualObjects(write.error.domain, FIRFirestoreErrorDomain);
  296. XCTAssertEqual(write.error.code, code);
  297. }
  298. }
  299. - (void)doRunTimer:(NSString *)timer {
  300. FSTTimerID timerID;
  301. if ([timer isEqualToString:@"all"]) {
  302. timerID = FSTTimerIDAll;
  303. } else if ([timer isEqualToString:@"listen_stream_idle"]) {
  304. timerID = FSTTimerIDListenStreamIdle;
  305. } else if ([timer isEqualToString:@"listen_stream_connection_backoff"]) {
  306. timerID = FSTTimerIDListenStreamConnectionBackoff;
  307. } else if ([timer isEqualToString:@"write_stream_idle"]) {
  308. timerID = FSTTimerIDWriteStreamIdle;
  309. } else if ([timer isEqualToString:@"write_stream_connection_backoff"]) {
  310. timerID = FSTTimerIDWriteStreamConnectionBackoff;
  311. } else if ([timer isEqualToString:@"online_state_timeout"]) {
  312. timerID = FSTTimerIDOnlineStateTimeout;
  313. } else {
  314. HARD_FAIL("runTimer spec step specified unknown timer: %s", timer);
  315. }
  316. [self.driver runTimer:timerID];
  317. }
  318. - (void)doDisableNetwork {
  319. [self.driver disableNetwork];
  320. }
  321. - (void)doEnableNetwork {
  322. [self.driver enableNetwork];
  323. }
  324. - (void)doChangeUser:(id)UID {
  325. if ([UID isEqual:[NSNull null]]) {
  326. UID = nil;
  327. }
  328. [self.driver changeUser:User::FromUid(UID)];
  329. }
  330. - (void)doRestart {
  331. // Any outstanding user writes should be automatically re-sent, so we want to preserve them
  332. // when re-creating the driver.
  333. FSTOutstandingWriteQueues outstandingWrites = self.driver.outstandingWrites;
  334. User currentUser = self.driver.currentUser;
  335. [self.driver shutdown];
  336. // NOTE: We intentionally don't shutdown / re-create driverPersistence, since we want to
  337. // preserve the persisted state. This is a bit of a cheat since it means we're not exercising
  338. // the initialization / start logic that would normally be hit, but simplifies the plumbing and
  339. // allows us to run these tests against FSTMemoryPersistence as well (there would be no way to
  340. // re-create FSTMemoryPersistence without losing all persisted state).
  341. self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:self.driverPersistence
  342. garbageCollector:self.garbageCollector
  343. initialUser:currentUser
  344. outstandingWrites:outstandingWrites];
  345. [self.driver start];
  346. }
  347. - (void)doStep:(NSDictionary *)step {
  348. if (step[@"userListen"]) {
  349. [self doListen:step[@"userListen"]];
  350. } else if (step[@"userUnlisten"]) {
  351. [self doUnlisten:step[@"userUnlisten"]];
  352. } else if (step[@"userSet"]) {
  353. [self doSet:step[@"userSet"]];
  354. } else if (step[@"userPatch"]) {
  355. [self doPatch:step[@"userPatch"]];
  356. } else if (step[@"userDelete"]) {
  357. [self doDelete:step[@"userDelete"]];
  358. } else if (step[@"watchAck"]) {
  359. [self doWatchAck:step[@"watchAck"] snapshot:step[@"watchSnapshot"]];
  360. } else if (step[@"watchCurrent"]) {
  361. [self doWatchCurrent:step[@"watchCurrent"] snapshot:step[@"watchSnapshot"]];
  362. } else if (step[@"watchRemove"]) {
  363. [self doWatchRemove:step[@"watchRemove"] snapshot:step[@"watchSnapshot"]];
  364. } else if (step[@"watchEntity"]) {
  365. [self doWatchEntity:step[@"watchEntity"] snapshot:step[@"watchSnapshot"]];
  366. } else if (step[@"watchFilter"]) {
  367. [self doWatchFilter:step[@"watchFilter"] snapshot:step[@"watchSnapshot"]];
  368. } else if (step[@"watchReset"]) {
  369. [self doWatchReset:step[@"watchReset"] snapshot:step[@"watchSnapshot"]];
  370. } else if (step[@"watchStreamClose"]) {
  371. [self doWatchStreamClose:step[@"watchStreamClose"]];
  372. } else if (step[@"watchProto"]) {
  373. // watchProto isn't yet used, and it's unclear how to create arbitrary protos from JSON.
  374. HARD_FAIL("watchProto is not yet supported.");
  375. } else if (step[@"writeAck"]) {
  376. [self doWriteAck:step[@"writeAck"]];
  377. } else if (step[@"failWrite"]) {
  378. [self doFailWrite:step[@"failWrite"]];
  379. } else if (step[@"runTimer"]) {
  380. [self doRunTimer:step[@"runTimer"]];
  381. } else if (step[@"enableNetwork"]) {
  382. if ([step[@"enableNetwork"] boolValue]) {
  383. [self doEnableNetwork];
  384. } else {
  385. [self doDisableNetwork];
  386. }
  387. } else if (step[@"changeUser"]) {
  388. [self doChangeUser:step[@"changeUser"]];
  389. } else if (step[@"restart"]) {
  390. [self doRestart];
  391. } else {
  392. XCTFail(@"Unknown step: %@", step);
  393. }
  394. }
  395. - (void)validateEvent:(FSTQueryEvent *)actual matches:(NSDictionary *)expected {
  396. FSTQuery *expectedQuery = [self parseQuery:expected[@"query"]];
  397. XCTAssertEqualObjects(actual.query, expectedQuery);
  398. if ([expected[@"errorCode"] integerValue] != 0) {
  399. XCTAssertNotNil(actual.error);
  400. XCTAssertEqual(actual.error.code, [expected[@"errorCode"] integerValue]);
  401. } else {
  402. NSMutableArray *expectedChanges = [NSMutableArray array];
  403. NSMutableArray *removed = expected[@"removed"];
  404. for (NSArray *changeSpec in removed) {
  405. [expectedChanges
  406. addObject:[self parseChange:changeSpec ofType:FSTDocumentViewChangeTypeRemoved]];
  407. }
  408. NSMutableArray *added = expected[@"added"];
  409. for (NSArray *changeSpec in added) {
  410. [expectedChanges
  411. addObject:[self parseChange:changeSpec ofType:FSTDocumentViewChangeTypeAdded]];
  412. }
  413. NSMutableArray *modified = expected[@"modified"];
  414. for (NSArray *changeSpec in modified) {
  415. [expectedChanges
  416. addObject:[self parseChange:changeSpec ofType:FSTDocumentViewChangeTypeModified]];
  417. }
  418. NSMutableArray *metadata = expected[@"metadata"];
  419. for (NSArray *changeSpec in metadata) {
  420. [expectedChanges
  421. addObject:[self parseChange:changeSpec ofType:FSTDocumentViewChangeTypeMetadata]];
  422. }
  423. XCTAssertEqualObjects(actual.viewSnapshot.documentChanges, expectedChanges);
  424. BOOL expectedHasPendingWrites =
  425. expected[@"hasPendingWrites"] ? [expected[@"hasPendingWrites"] boolValue] : NO;
  426. BOOL expectedIsFromCache = expected[@"fromCache"] ? [expected[@"fromCache"] boolValue] : NO;
  427. XCTAssertEqual(actual.viewSnapshot.hasPendingWrites, expectedHasPendingWrites,
  428. @"hasPendingWrites");
  429. XCTAssertEqual(actual.viewSnapshot.isFromCache, expectedIsFromCache, @"isFromCache");
  430. }
  431. }
  432. - (void)validateStepExpectations:(NSMutableArray *_Nullable)stepExpectations {
  433. NSArray<FSTQueryEvent *> *events = self.driver.capturedEventsSinceLastCall;
  434. if (!stepExpectations) {
  435. XCTAssertEqual(events.count, 0);
  436. for (FSTQueryEvent *event in events) {
  437. XCTFail(@"Unexpected event: %@", event);
  438. }
  439. return;
  440. }
  441. events =
  442. [events sortedArrayUsingComparator:^NSComparisonResult(FSTQueryEvent *q1, FSTQueryEvent *q2) {
  443. return [q1.query.canonicalID compare:q2.query.canonicalID];
  444. }];
  445. XCTAssertEqual(events.count, stepExpectations.count);
  446. NSUInteger i = 0;
  447. for (; i < stepExpectations.count && i < events.count; ++i) {
  448. [self validateEvent:events[i] matches:stepExpectations[i]];
  449. }
  450. for (; i < stepExpectations.count; ++i) {
  451. XCTFail(@"Missing event: %@", stepExpectations[i]);
  452. }
  453. for (; i < events.count; ++i) {
  454. XCTFail(@"Unexpected event: %@", events[i]);
  455. }
  456. }
  457. - (void)validateStateExpectations:(nullable NSDictionary *)expected {
  458. if (expected) {
  459. if (expected[@"numOutstandingWrites"]) {
  460. XCTAssertEqual([self.driver sentWritesCount], [expected[@"numOutstandingWrites"] intValue]);
  461. }
  462. if (expected[@"writeStreamRequestCount"]) {
  463. XCTAssertEqual([self.driver writeStreamRequestCount],
  464. [expected[@"writeStreamRequestCount"] intValue]);
  465. }
  466. if (expected[@"watchStreamRequestCount"]) {
  467. XCTAssertEqual([self.driver watchStreamRequestCount],
  468. [expected[@"watchStreamRequestCount"] intValue]);
  469. }
  470. if (expected[@"limboDocs"]) {
  471. NSMutableSet<FSTDocumentKey *> *expectedLimboDocuments = [NSMutableSet set];
  472. NSArray *docNames = expected[@"limboDocs"];
  473. for (NSString *name in docNames) {
  474. [expectedLimboDocuments addObject:FSTTestDocKey(name)];
  475. }
  476. // Update the expected limbo documents
  477. self.driver.expectedLimboDocuments = expectedLimboDocuments;
  478. }
  479. if (expected[@"activeTargets"]) {
  480. NSMutableDictionary *expectedActiveTargets = [NSMutableDictionary dictionary];
  481. [expected[@"activeTargets"] enumerateKeysAndObjectsUsingBlock:^(NSString *targetIDString,
  482. NSDictionary *queryData,
  483. BOOL *stop) {
  484. FSTTargetID targetID = [targetIDString intValue];
  485. FSTQuery *query = [self parseQuery:queryData[@"query"]];
  486. NSData *resumeToken = [queryData[@"resumeToken"] dataUsingEncoding:NSUTF8StringEncoding];
  487. // TODO(mcg): populate the purpose of the target once it's possible to encode that in the
  488. // spec tests. For now, hard-code that it's a listen despite the fact that it's not always
  489. // the right value.
  490. expectedActiveTargets[@(targetID)] =
  491. [[FSTQueryData alloc] initWithQuery:query
  492. targetID:targetID
  493. listenSequenceNumber:0
  494. purpose:FSTQueryPurposeListen
  495. snapshotVersion:SnapshotVersion::None()
  496. resumeToken:resumeToken];
  497. }];
  498. self.driver.expectedActiveTargets = expectedActiveTargets;
  499. }
  500. }
  501. // Always validate that the expected limbo docs match the actual limbo docs.
  502. [self validateLimboDocuments];
  503. // Always validate that the expected active targets match the actual active targets.
  504. [self validateActiveTargets];
  505. }
  506. - (void)validateLimboDocuments {
  507. // Make a copy so it can modified while checking against the expected limbo docs.
  508. std::map<DocumentKey, TargetId> actualLimboDocs = self.driver.currentLimboDocuments;
  509. // Validate that each limbo doc has an expected active target
  510. for (const auto &kv : actualLimboDocs) {
  511. XCTAssertNotNil(self.driver.expectedActiveTargets[@(kv.second)],
  512. @"Found limbo doc without an expected active target");
  513. }
  514. for (FSTDocumentKey *expectedLimboDoc in self.driver.expectedLimboDocuments) {
  515. XCTAssert(actualLimboDocs.find(expectedLimboDoc) != actualLimboDocs.end(),
  516. @"Expected doc to be in limbo, but was not: %@", expectedLimboDoc);
  517. actualLimboDocs.erase(expectedLimboDoc);
  518. }
  519. XCTAssertTrue(actualLimboDocs.empty(), "%lu Unexpected docs in limbo, the first one is <%s, %d>",
  520. actualLimboDocs.size(), actualLimboDocs.begin()->first.ToString().c_str(),
  521. actualLimboDocs.begin()->second);
  522. }
  523. - (void)validateActiveTargets {
  524. // Create a copy so we can modify it in tests
  525. NSMutableDictionary<FSTBoxedTargetID *, FSTQueryData *> *actualTargets =
  526. [NSMutableDictionary dictionaryWithDictionary:self.driver.activeTargets];
  527. [self.driver.expectedActiveTargets enumerateKeysAndObjectsUsingBlock:^(FSTBoxedTargetID *targetID,
  528. FSTQueryData *queryData,
  529. BOOL *stop) {
  530. XCTAssertNotNil(actualTargets[targetID], @"Expected active target not found: %@", queryData);
  531. // TODO(mcg): validate the purpose of the target once it's possible to encode that in the
  532. // spec tests. For now, only validate properties that can be validated.
  533. // XCTAssertEqualObjects(actualTargets[targetID], queryData);
  534. FSTQueryData *actual = actualTargets[targetID];
  535. XCTAssertNotNil(actual);
  536. if (actual) {
  537. XCTAssertEqualObjects(actual.query, queryData.query);
  538. XCTAssertEqual(actual.targetID, queryData.targetID);
  539. XCTAssertEqual(actual.snapshotVersion, queryData.snapshotVersion);
  540. XCTAssertEqualObjects(actual.resumeToken, queryData.resumeToken);
  541. }
  542. [actualTargets removeObjectForKey:targetID];
  543. }];
  544. XCTAssertTrue(actualTargets.count == 0, "Unexpected active targets: %@", actualTargets);
  545. }
  546. - (void)runSpecTestSteps:(NSArray *)steps config:(NSDictionary *)config {
  547. @try {
  548. [self setUpForSpecWithConfig:config];
  549. for (NSDictionary *step in steps) {
  550. LOG_DEBUG("Doing step %s", step);
  551. [self doStep:step];
  552. [self validateStepExpectations:step[@"expect"]];
  553. [self validateStateExpectations:step[@"stateExpect"]];
  554. }
  555. [self.driver validateUsage];
  556. } @finally {
  557. // Ensure that the driver is torn down even if the test is failing due to a thrown exception so
  558. // that any resources held by the driver are released. This is important when the driver is
  559. // backed by LevelDB because LevelDB locks its database. If -tearDownForSpec were not called
  560. // after an exception then subsequent attempts to open the LevelDB will fail, making it harder
  561. // to zero in on the spec tests as a culprit.
  562. [self tearDownForSpec];
  563. }
  564. }
  565. #pragma mark - The actual test methods.
  566. - (void)testSpecTests {
  567. if ([self isTestBaseClass]) return;
  568. // Enumerate the .json files containing the spec tests.
  569. NSMutableArray<NSString *> *specFiles = [NSMutableArray array];
  570. NSMutableArray<NSDictionary *> *parsedSpecs = [NSMutableArray array];
  571. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  572. NSFileManager *fs = [NSFileManager defaultManager];
  573. BOOL exclusiveMode = NO;
  574. for (NSString *file in [fs enumeratorAtPath:[bundle bundlePath]]) {
  575. if (![@"json" isEqual:[file pathExtension]]) {
  576. continue;
  577. }
  578. // Read and parse the JSON from the file.
  579. NSString *fileName = [file stringByDeletingPathExtension];
  580. NSString *path = [bundle pathForResource:fileName ofType:@"json"];
  581. NSData *json = [NSData dataWithContentsOfFile:path];
  582. XCTAssertNotNil(json);
  583. NSError *error = nil;
  584. id _Nullable parsed = [NSJSONSerialization JSONObjectWithData:json options:0 error:&error];
  585. XCTAssertNil(error, @"%@", error);
  586. XCTAssertTrue([parsed isKindOfClass:[NSDictionary class]]);
  587. NSDictionary *testDict = (NSDictionary *)parsed;
  588. exclusiveMode = exclusiveMode || [self anyTestsAreMarkedExclusive:testDict];
  589. [specFiles addObject:fileName];
  590. [parsedSpecs addObject:testDict];
  591. }
  592. // Now iterate over them and run them.
  593. __block bool ranAtLeastOneTest = NO;
  594. for (NSUInteger i = 0; i < specFiles.count; i++) {
  595. NSLog(@"Spec test file: %@", specFiles[i]);
  596. // Iterate over the tests in the file and run them.
  597. [parsedSpecs[i] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  598. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  599. NSDictionary *testDescription = (NSDictionary *)obj;
  600. NSString *describeName = testDescription[@"describeName"];
  601. NSString *itName = testDescription[@"itName"];
  602. NSString *name = [NSString stringWithFormat:@"%@ %@", describeName, itName];
  603. NSDictionary *config = testDescription[@"config"];
  604. NSArray *steps = testDescription[@"steps"];
  605. NSArray<NSString *> *tags = testDescription[@"tags"];
  606. BOOL runTest = !exclusiveMode || [tags indexOfObject:kExclusiveTag] != NSNotFound;
  607. if ([tags indexOfObject:kNoIOSTag] != NSNotFound) {
  608. runTest = NO;
  609. }
  610. if (runTest) {
  611. NSLog(@" Spec test: %@", name);
  612. [self runSpecTestSteps:steps config:config];
  613. ranAtLeastOneTest = YES;
  614. } else {
  615. NSLog(@" [SKIPPED] Spec test: %@", name);
  616. }
  617. }];
  618. }
  619. XCTAssertTrue(ranAtLeastOneTest);
  620. }
  621. - (BOOL)anyTestsAreMarkedExclusive:(NSDictionary *)tests {
  622. __block BOOL found = NO;
  623. [tests enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  624. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  625. NSDictionary *testDescription = (NSDictionary *)obj;
  626. NSArray<NSString *> *tags = testDescription[@"tags"];
  627. if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  628. found = YES;
  629. *stop = YES;
  630. }
  631. }];
  632. return found;
  633. }
  634. @end
  635. NS_ASSUME_NONNULL_END