FSTSpecTests.m 26 KB

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