FSTSpecTests.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 <GRPCClient/GRPCCall.h>
  18. #import "FirebaseFirestore/FIRFirestoreErrors.h"
  19. #import "Firestore/Source/Auth/FSTUser.h"
  20. #import "Firestore/Source/Core/FSTEventManager.h"
  21. #import "Firestore/Source/Core/FSTQuery.h"
  22. #import "Firestore/Source/Core/FSTSnapshotVersion.h"
  23. #import "Firestore/Source/Core/FSTViewSnapshot.h"
  24. #import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
  25. #import "Firestore/Source/Local/FSTNoOpGarbageCollector.h"
  26. #import "Firestore/Source/Local/FSTPersistence.h"
  27. #import "Firestore/Source/Local/FSTQueryData.h"
  28. #import "Firestore/Source/Model/FSTDocument.h"
  29. #import "Firestore/Source/Model/FSTDocumentKey.h"
  30. #import "Firestore/Source/Model/FSTFieldValue.h"
  31. #import "Firestore/Source/Model/FSTMutation.h"
  32. #import "Firestore/Source/Model/FSTPath.h"
  33. #import "Firestore/Source/Remote/FSTExistenceFilter.h"
  34. #import "Firestore/Source/Remote/FSTWatchChange.h"
  35. #import "Firestore/Source/Util/FSTAssert.h"
  36. #import "Firestore/Source/Util/FSTClasses.h"
  37. #import "Firestore/Source/Util/FSTLogger.h"
  38. #import "Firestore/Example/Tests/Remote/FSTWatchChange+Testing.h"
  39. #import "Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h"
  40. #import "Firestore/Example/Tests/Util/FSTHelpers.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)doDisableNetwork {
  280. [self.driver disableNetwork];
  281. }
  282. - (void)doEnableNetwork {
  283. [self.driver enableNetwork];
  284. }
  285. - (void)doChangeUser:(id)UID {
  286. FSTUser *user = [UID isEqual:[NSNull null]] ? [FSTUser unauthenticatedUser]
  287. : [[FSTUser alloc] initWithUID:UID];
  288. [self.driver changeUser:user];
  289. }
  290. - (void)doRestart {
  291. // Any outstanding user writes should be automatically re-sent, so we want to preserve them
  292. // when re-creating the driver.
  293. FSTOutstandingWriteQueues *outstandingWrites = self.driver.outstandingWrites;
  294. [self.driver shutdown];
  295. // NOTE: We intentionally don't shutdown / re-create driverPersistence, since we want to
  296. // preserve the persisted state. This is a bit of a cheat since it means we're not exercising
  297. // the initialization / start logic that would normally be hit, but simplifies the plumbing and
  298. // allows us to run these tests against FSTMemoryPersistence as well (there would be no way to
  299. // re-create FSTMemoryPersistence without losing all persisted state).
  300. self.driver = [[FSTSyncEngineTestDriver alloc] initWithPersistence:self.driverPersistence
  301. garbageCollector:self.garbageCollector
  302. initialUser:self.driver.currentUser
  303. outstandingWrites:outstandingWrites];
  304. [self.driver start];
  305. }
  306. - (void)doStep:(NSDictionary *)step {
  307. if (step[@"userListen"]) {
  308. [self doListen:step[@"userListen"]];
  309. } else if (step[@"userUnlisten"]) {
  310. [self doUnlisten:step[@"userUnlisten"]];
  311. } else if (step[@"userSet"]) {
  312. [self doSet:step[@"userSet"]];
  313. } else if (step[@"userPatch"]) {
  314. [self doPatch:step[@"userPatch"]];
  315. } else if (step[@"userDelete"]) {
  316. [self doDelete:step[@"userDelete"]];
  317. } else if (step[@"watchAck"]) {
  318. [self doWatchAck:step[@"watchAck"] snapshot:step[@"watchSnapshot"]];
  319. } else if (step[@"watchCurrent"]) {
  320. [self doWatchCurrent:step[@"watchCurrent"] snapshot:step[@"watchSnapshot"]];
  321. } else if (step[@"watchRemove"]) {
  322. [self doWatchRemove:step[@"watchRemove"] snapshot:step[@"watchSnapshot"]];
  323. } else if (step[@"watchEntity"]) {
  324. [self doWatchEntity:step[@"watchEntity"] snapshot:step[@"watchSnapshot"]];
  325. } else if (step[@"watchFilter"]) {
  326. [self doWatchFilter:step[@"watchFilter"] snapshot:step[@"watchSnapshot"]];
  327. } else if (step[@"watchReset"]) {
  328. [self doWatchReset:step[@"watchReset"] snapshot:step[@"watchSnapshot"]];
  329. } else if (step[@"watchStreamClose"]) {
  330. [self doWatchStreamClose:step[@"watchStreamClose"]];
  331. } else if (step[@"watchProto"]) {
  332. // watchProto isn't yet used, and it's unclear how to create arbitrary protos from JSON.
  333. FSTFail(@"watchProto is not yet supported.");
  334. } else if (step[@"writeAck"]) {
  335. [self doWriteAck:step[@"writeAck"]];
  336. } else if (step[@"failWrite"]) {
  337. [self doFailWrite:step[@"failWrite"]];
  338. } else if (step[@"enableNetwork"]) {
  339. if ([step[@"enableNetwork"] boolValue]) {
  340. [self doEnableNetwork];
  341. } else {
  342. [self doDisableNetwork];
  343. }
  344. } else if (step[@"changeUser"]) {
  345. [self doChangeUser:step[@"changeUser"]];
  346. } else if (step[@"restart"]) {
  347. [self doRestart];
  348. } else {
  349. XCTFail(@"Unknown step: %@", step);
  350. }
  351. }
  352. - (void)validateEvent:(FSTQueryEvent *)actual matches:(NSDictionary *)expected {
  353. FSTQuery *expectedQuery = [self parseQuery:expected[@"query"]];
  354. XCTAssertEqualObjects(actual.query, expectedQuery);
  355. if ([expected[@"errorCode"] integerValue] != 0) {
  356. XCTAssertNotNil(actual.error);
  357. XCTAssertEqual(actual.error.code, [expected[@"errorCode"] integerValue]);
  358. } else {
  359. NSMutableArray *expectedChanges = [NSMutableArray array];
  360. NSMutableArray *removed = expected[@"removed"];
  361. for (NSArray *changeSpec in removed) {
  362. [expectedChanges
  363. addObject:[self parseChange:changeSpec ofType:FSTDocumentViewChangeTypeRemoved]];
  364. }
  365. NSMutableArray *added = expected[@"added"];
  366. for (NSArray *changeSpec in added) {
  367. [expectedChanges
  368. addObject:[self parseChange:changeSpec ofType:FSTDocumentViewChangeTypeAdded]];
  369. }
  370. NSMutableArray *modified = expected[@"modified"];
  371. for (NSArray *changeSpec in modified) {
  372. [expectedChanges
  373. addObject:[self parseChange:changeSpec ofType:FSTDocumentViewChangeTypeModified]];
  374. }
  375. NSMutableArray *metadata = expected[@"metadata"];
  376. for (NSArray *changeSpec in metadata) {
  377. [expectedChanges
  378. addObject:[self parseChange:changeSpec ofType:FSTDocumentViewChangeTypeMetadata]];
  379. }
  380. XCTAssertEqualObjects(actual.viewSnapshot.documentChanges, expectedChanges);
  381. BOOL expectedHasPendingWrites =
  382. expected[@"hasPendingWrites"] ? [expected[@"hasPendingWrites"] boolValue] : NO;
  383. BOOL expectedIsFromCache = expected[@"fromCache"] ? [expected[@"fromCache"] boolValue] : NO;
  384. XCTAssertEqual(actual.viewSnapshot.hasPendingWrites, expectedHasPendingWrites,
  385. @"hasPendingWrites");
  386. XCTAssertEqual(actual.viewSnapshot.isFromCache, expectedIsFromCache, @"isFromCache");
  387. }
  388. }
  389. - (void)validateStepExpectations:(NSMutableArray *_Nullable)stepExpectations {
  390. NSArray<FSTQueryEvent *> *events = self.driver.capturedEventsSinceLastCall;
  391. if (!stepExpectations) {
  392. XCTAssertEqual(events.count, 0);
  393. for (FSTQueryEvent *event in events) {
  394. XCTFail(@"Unexpected event: %@", event);
  395. }
  396. return;
  397. }
  398. events =
  399. [events sortedArrayUsingComparator:^NSComparisonResult(FSTQueryEvent *q1, FSTQueryEvent *q2) {
  400. return [q1.query.canonicalID compare:q2.query.canonicalID];
  401. }];
  402. XCTAssertEqual(events.count, stepExpectations.count);
  403. NSUInteger i = 0;
  404. for (; i < stepExpectations.count && i < events.count; ++i) {
  405. [self validateEvent:events[i] matches:stepExpectations[i]];
  406. }
  407. for (; i < stepExpectations.count; ++i) {
  408. XCTFail(@"Missing event: %@", stepExpectations[i]);
  409. }
  410. for (; i < events.count; ++i) {
  411. XCTFail(@"Unexpected event: %@", events[i]);
  412. }
  413. }
  414. - (void)validateStateExpectations:(nullable NSDictionary *)expected {
  415. if (expected) {
  416. if (expected[@"numOutstandingWrites"]) {
  417. XCTAssertEqual([self.driver sentWritesCount], [expected[@"numOutstandingWrites"] intValue]);
  418. }
  419. if (expected[@"writeStreamRequestCount"]) {
  420. XCTAssertEqual([self.driver writeStreamRequestCount],
  421. [expected[@"writeStreamRequestCount"] intValue]);
  422. }
  423. if (expected[@"watchStreamRequestCount"]) {
  424. XCTAssertEqual([self.driver watchStreamRequestCount],
  425. [expected[@"watchStreamRequestCount"] intValue]);
  426. }
  427. if (expected[@"limboDocs"]) {
  428. NSMutableSet<FSTDocumentKey *> *expectedLimboDocuments = [NSMutableSet set];
  429. NSArray *docNames = expected[@"limboDocs"];
  430. for (NSString *name in docNames) {
  431. [expectedLimboDocuments addObject:FSTTestDocKey(name)];
  432. }
  433. // Update the expected limbo documents
  434. self.driver.expectedLimboDocuments = expectedLimboDocuments;
  435. }
  436. if (expected[@"activeTargets"]) {
  437. NSMutableDictionary *expectedActiveTargets = [NSMutableDictionary dictionary];
  438. [expected[@"activeTargets"] enumerateKeysAndObjectsUsingBlock:^(NSString *targetIDString,
  439. NSDictionary *queryData,
  440. BOOL *stop) {
  441. FSTTargetID targetID = [targetIDString intValue];
  442. FSTQuery *query = [self parseQuery:queryData[@"query"]];
  443. NSData *resumeToken = [queryData[@"resumeToken"] dataUsingEncoding:NSUTF8StringEncoding];
  444. // TODO(mcg): populate the purpose of the target once it's possible to encode that in the
  445. // spec tests. For now, hard-code that it's a listen despite the fact that it's not always
  446. // the right value.
  447. expectedActiveTargets[@(targetID)] =
  448. [[FSTQueryData alloc] initWithQuery:query
  449. targetID:targetID
  450. purpose:FSTQueryPurposeListen
  451. snapshotVersion:[FSTSnapshotVersion noVersion]
  452. resumeToken:resumeToken];
  453. }];
  454. self.driver.expectedActiveTargets = expectedActiveTargets;
  455. }
  456. }
  457. // Always validate that the expected limbo docs match the actual limbo docs.
  458. [self validateLimboDocuments];
  459. // Always validate that the expected active targets match the actual active targets.
  460. [self validateActiveTargets];
  461. }
  462. - (void)validateLimboDocuments {
  463. // Make a copy so it can modified while checking against the expected limbo docs.
  464. NSMutableDictionary<FSTDocumentKey *, FSTBoxedTargetID *> *actualLimboDocs =
  465. [NSMutableDictionary dictionaryWithDictionary:self.driver.currentLimboDocuments];
  466. // Validate that each limbo doc has an expected active target
  467. [actualLimboDocs enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key,
  468. FSTBoxedTargetID *targetID, BOOL *stop) {
  469. XCTAssertNotNil(self.driver.expectedActiveTargets[targetID],
  470. @"Found limbo doc without an expected active target");
  471. }];
  472. for (FSTDocumentKey *expectedLimboDoc in self.driver.expectedLimboDocuments) {
  473. XCTAssertNotNil(actualLimboDocs[expectedLimboDoc],
  474. @"Expected doc to be in limbo, but was not: %@", expectedLimboDoc);
  475. [actualLimboDocs removeObjectForKey:expectedLimboDoc];
  476. }
  477. XCTAssertTrue(actualLimboDocs.count == 0, "Unexpected docs in limbo: %@", actualLimboDocs);
  478. }
  479. - (void)validateActiveTargets {
  480. // Create a copy so we can modify it in tests
  481. NSMutableDictionary<FSTBoxedTargetID *, FSTQueryData *> *actualTargets =
  482. [NSMutableDictionary dictionaryWithDictionary:self.driver.activeTargets];
  483. [self.driver.expectedActiveTargets enumerateKeysAndObjectsUsingBlock:^(FSTBoxedTargetID *targetID,
  484. FSTQueryData *queryData,
  485. BOOL *stop) {
  486. XCTAssertNotNil(actualTargets[targetID], @"Expected active target not found: %@", queryData);
  487. // TODO(mcg): validate the purpose of the target once it's possible to encode that in the
  488. // spec tests. For now, only validate properties that can be validated.
  489. // XCTAssertEqualObjects(actualTargets[targetID], queryData);
  490. FSTQueryData *actual = actualTargets[targetID];
  491. XCTAssertEqualObjects(actual.query, queryData.query);
  492. XCTAssertEqual(actual.targetID, queryData.targetID);
  493. XCTAssertEqualObjects(actual.snapshotVersion, queryData.snapshotVersion);
  494. XCTAssertEqualObjects(actual.resumeToken, queryData.resumeToken);
  495. [actualTargets removeObjectForKey:targetID];
  496. }];
  497. XCTAssertTrue(actualTargets.count == 0, "Unexpected active targets: %@", actualTargets);
  498. }
  499. - (void)runSpecTestSteps:(NSArray *)steps config:(NSDictionary *)config {
  500. @try {
  501. [self setUpForSpecWithConfig:config];
  502. for (NSDictionary *step in steps) {
  503. FSTLog(@"Doing step %@", step);
  504. [self doStep:step];
  505. [self validateStepExpectations:step[@"expect"]];
  506. [self validateStateExpectations:step[@"stateExpect"]];
  507. }
  508. [self.driver validateUsage];
  509. } @finally {
  510. // Ensure that the driver is torn down even if the test is failing due to a thrown exception so
  511. // that any resources held by the driver are released. This is important when the driver is
  512. // backed by LevelDB because LevelDB locks its database. If -tearDownForSpec were not called
  513. // after an exception then subsequent attempts to open the LevelDB will fail, making it harder
  514. // to zero in on the spec tests as a culprit.
  515. [self tearDownForSpec];
  516. }
  517. }
  518. #pragma mark - The actual test methods.
  519. - (void)testSpecTests {
  520. if ([self isTestBaseClass]) return;
  521. // Enumerate the .json files containing the spec tests.
  522. NSMutableArray<NSString *> *specFiles = [NSMutableArray array];
  523. NSMutableArray<NSDictionary *> *parsedSpecs = [NSMutableArray array];
  524. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  525. NSFileManager *fs = [NSFileManager defaultManager];
  526. BOOL exclusiveMode = NO;
  527. for (NSString *file in [fs enumeratorAtPath:[bundle bundlePath]]) {
  528. if (![@"json" isEqual:[file pathExtension]]) {
  529. continue;
  530. }
  531. // Read and parse the JSON from the file.
  532. NSString *fileName = [file stringByDeletingPathExtension];
  533. NSString *path = [bundle pathForResource:fileName ofType:@"json"];
  534. NSData *json = [NSData dataWithContentsOfFile:path];
  535. XCTAssertNotNil(json);
  536. NSError *error = nil;
  537. id _Nullable parsed = [NSJSONSerialization JSONObjectWithData:json options:0 error:&error];
  538. XCTAssertNil(error, @"%@", error);
  539. XCTAssertTrue([parsed isKindOfClass:[NSDictionary class]]);
  540. NSDictionary *testDict = (NSDictionary *)parsed;
  541. exclusiveMode = exclusiveMode || [self anyTestsAreMarkedExclusive:testDict];
  542. [specFiles addObject:fileName];
  543. [parsedSpecs addObject:testDict];
  544. }
  545. // Now iterate over them and run them.
  546. __block bool ranAtLeastOneTest = NO;
  547. for (NSUInteger i = 0; i < specFiles.count; i++) {
  548. NSLog(@"Spec test file: %@", specFiles[i]);
  549. // Iterate over the tests in the file and run them.
  550. [parsedSpecs[i] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  551. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  552. NSDictionary *testDescription = (NSDictionary *)obj;
  553. NSString *describeName = testDescription[@"describeName"];
  554. NSString *itName = testDescription[@"itName"];
  555. NSString *name = [NSString stringWithFormat:@"%@ %@", describeName, itName];
  556. NSDictionary *config = testDescription[@"config"];
  557. NSArray *steps = testDescription[@"steps"];
  558. NSArray<NSString *> *tags = testDescription[@"tags"];
  559. BOOL runTest = !exclusiveMode || [tags indexOfObject:kExclusiveTag] != NSNotFound;
  560. if ([tags indexOfObject:kNoIOSTag] != NSNotFound) {
  561. runTest = NO;
  562. }
  563. if (runTest) {
  564. NSLog(@" Spec test: %@", name);
  565. [self runSpecTestSteps:steps config:config];
  566. ranAtLeastOneTest = YES;
  567. } else {
  568. NSLog(@" [SKIPPED] Spec test: %@", name);
  569. }
  570. }];
  571. }
  572. XCTAssertTrue(ranAtLeastOneTest);
  573. }
  574. - (BOOL)anyTestsAreMarkedExclusive:(NSDictionary *)tests {
  575. __block BOOL found = NO;
  576. [tests enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  577. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  578. NSDictionary *testDescription = (NSDictionary *)obj;
  579. NSArray<NSString *> *tags = testDescription[@"tags"];
  580. if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  581. found = YES;
  582. *stop = YES;
  583. }
  584. }];
  585. return found;
  586. }
  587. @end
  588. NS_ASSUME_NONNULL_END