Files
Texture/Tests/ASCollectionViewThrashTests.mm
Huy Nguyen 6e46d9bcd5 [ASDataController] Add an experiment that avoids flushing editing queue before starting the data pipeline pipeline (#1564)
* [ASDataController] Avoid flushing editing queue before starting data controller pipeline

Step 1 of the pipeline is on main thread and needs pendingMap which was updated by the end of step 1 of the run. And step 1 operates on data source index space. So it should be ok to start it ASAP -- without waiting for any running work on the background editing queue.

One potential race condition after this change is that it's possible for main thread to query the data source (step 1) while the editing queue consumes the last change set (step 3). However, as long as the client's each nodeBlock and the node's layout code capture/reference an individual model object (as opposed to the whole data set) -- which is what clients are supposed to do anyways, then everything should be fine.

The benefit of this diff is that the pipeline will be able to accept many more change sets within a short time window, for example when clients submit a burst of separate small updates.

I tested this diff against our test suite several times and smoke tested it in our code base without any issues.

* Wrap in an experiment

* Enable experiment in tests

* Minor change
2019-06-30 21:08:06 -07:00

228 lines
7.7 KiB
Plaintext

//
// ASCollectionViewThrashTests.mm
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <XCTest/XCTest.h>
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import <AsyncDisplayKit/ASCollectionView.h>
#import <stdatomic.h>
#import "ASTestCase.h"
#import "ASThrashUtility.h"
@interface ASCollectionViewThrashTests : ASTestCase
@end
@implementation ASCollectionViewThrashTests
{
// The current update, which will be logged in case of a failure.
ASThrashUpdate *_update;
BOOL _failed;
}
- (void)setUp
{
[super setUp];
ASConfiguration *config = [ASConfiguration new];
config.experimentalFeatures = ASExperimentalOptimizeDataControllerPipeline;
[ASConfigurationManager test_resetWithConfiguration:config];
}
- (void)tearDown
{
[super tearDown];
if (_failed && _update != nil) {
NSLog(@"Failed update %@: %@", _update, _update.logFriendlyBase64Representation);
}
_failed = NO;
_update = nil;
}
// NOTE: Despite the documentation, this is not always called if an exception is caught.
- (void)recordFailureWithDescription:(NSString *)description inFile:(NSString *)filePath atLine:(NSUInteger)lineNumber expected:(BOOL)expected
{
_failed = YES;
[super recordFailureWithDescription:description inFile:filePath atLine:lineNumber expected:expected];
}
- (void)verifyDataSource:(ASThrashDataSource *)ds
{
CollectionView *collectionView = ds.collectionView;
NSArray <ASThrashTestSection *> *data = [ds data];
for (NSInteger i = 0; i < collectionView.numberOfSections; i++) {
XCTAssertEqual([collectionView numberOfItemsInSection:i], data[i].items.count);
for (NSInteger j = 0; j < [collectionView numberOfItemsInSection:i]; j++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
ASThrashTestItem *item = data[i].items[j];
ASThrashTestNode *node = (ASThrashTestNode *)[collectionView nodeForItemAtIndexPath:indexPath];
XCTAssertEqualObjects(node.item, item, @"Wrong node at index path %@", indexPath);
}
}
}
#pragma mark Test Methods
- (void)testInitialDataRead
{
ASThrashDataSource *ds = [[ASThrashDataSource alloc] initCollectionViewDataSourceWithData:[ASThrashTestSection sectionsWithCount:kInitialSectionCount]];
[self verifyDataSource:ds];
}
/// Replays the Base64 representation of an ASThrashUpdate from "ASThrashTestRecordedCase" file
- (void)testRecordedThrashCase
{
NSURL *caseURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"ASThrashTestRecordedCase" withExtension:nil subdirectory:@"TestResources"];
NSString *base64 = [NSString stringWithContentsOfURL:caseURL encoding:NSUTF8StringEncoding error:NULL];
_update = [ASThrashUpdate thrashUpdateWithBase64String:base64];
if (_update == nil) {
return;
}
ASThrashDataSource *ds = [[ASThrashDataSource alloc] initCollectionViewDataSourceWithData:_update.oldData];
[self applyUpdateUsingBatchUpdates:_update
toDataSource:ds
animated:NO
useXCTestWait:YES];
[self verifyDataSource:ds];
}
- (void)testThrashingWildly
{
for (NSInteger i = 0; i < kThrashingIterationCount; i++) {
[self setUp];
@autoreleasepool {
NSArray *sections = [ASThrashTestSection sectionsWithCount:kInitialSectionCount];
_update = [[ASThrashUpdate alloc] initWithData:sections];
ASThrashDataSource *ds = [[ASThrashDataSource alloc] initCollectionViewDataSourceWithData:sections];
[self applyUpdateUsingBatchUpdates:_update
toDataSource:ds
animated:NO
useXCTestWait:NO];
[self verifyDataSource:ds];
[self expectationForPredicate:[ds predicateForDeallocatedHierarchy] evaluatedWithObject:(id)kCFNull handler:nil];
}
[self waitForExpectationsWithTimeout:3 handler:nil];
[self tearDown];
}
}
- (void)testThrashingWildlyOnSameCollectionView
{
XCTestExpectation *expectation = [self expectationWithDescription:@"last test ran"];
ASThrashDataSource *ds = [[ASThrashDataSource alloc] initCollectionViewDataSourceWithData:nil];
for (NSInteger i = 0; i < 1000; i++) {
[self setUp];
@autoreleasepool {
NSArray *sections = [ASThrashTestSection sectionsWithCount:kInitialSectionCount];
_update = [[ASThrashUpdate alloc] initWithData:sections];
[ds setData:sections];
[ds.collectionView reloadData];
[self applyUpdateUsingBatchUpdates:_update
toDataSource:ds
animated:NO
useXCTestWait:NO];
[self verifyDataSource:ds];
if (i == 999) {
[expectation fulfill];
}
}
[self tearDown];
}
[self waitForExpectationsWithTimeout:3 handler:nil];
}
- (void)testThrashingWildlyDispatchWildly
{
XCTestExpectation *expectation = [self expectationWithDescription:@"last test ran"];
for (NSInteger i = 0; i < kThrashingIterationCount; i++) {
[self setUp];
@autoreleasepool {
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *sections = [ASThrashTestSection sectionsWithCount:kInitialSectionCount];
_update = [[ASThrashUpdate alloc] initWithData:sections];
ASThrashDataSource *ds = [[ASThrashDataSource alloc] initCollectionViewDataSourceWithData:sections];
[self applyUpdateUsingBatchUpdates:_update
toDataSource:ds
animated:NO
useXCTestWait:NO];
[self verifyDataSource:ds];
if (i == kThrashingIterationCount-1) {
[expectation fulfill];
}
});
}
[self tearDown];
}
[self waitForExpectationsWithTimeout:100 handler:nil];
}
#pragma mark Helpers
- (void)applyUpdateUsingBatchUpdates:(ASThrashUpdate *)update
toDataSource:(ASThrashDataSource *)dataSource animated:(BOOL)animated
useXCTestWait:(BOOL)wait
{
CollectionView *collectionView = dataSource.collectionView;
XCTestExpectation *expectation;
if (wait) {
expectation = [self expectationWithDescription:@"Wait for collection view to update"];
}
void (^updateBlock)() = ^ void (){
dataSource.data = update.data;
[collectionView insertSections:update.insertedSectionIndexes];
[collectionView deleteSections:update.deletedSectionIndexes];
[collectionView reloadSections:update.replacedSectionIndexes];
[update.insertedItemIndexes enumerateObjectsUsingBlock:^(NSMutableIndexSet * _Nonnull indexes, NSUInteger idx, BOOL * _Nonnull stop) {
NSArray *indexPaths = [indexes indexPathsInSection:idx];
[collectionView insertItemsAtIndexPaths:indexPaths];
}];
[update.deletedItemIndexes enumerateObjectsUsingBlock:^(NSMutableIndexSet * _Nonnull indexes, NSUInteger idx, BOOL * _Nonnull stop) {
NSArray *indexPaths = [indexes indexPathsInSection:idx];
[collectionView deleteItemsAtIndexPaths:indexPaths];
}];
[update.replacedItemIndexes enumerateObjectsUsingBlock:^(NSMutableIndexSet * _Nonnull indexes, NSUInteger idx, BOOL * _Nonnull stop) {
NSArray *indexPaths = [indexes indexPathsInSection:idx];
[collectionView reloadItemsAtIndexPaths:indexPaths];
}];
};
@try {
[collectionView performBatchAnimated:animated
updates:updateBlock
completion:^(BOOL finished) {
[expectation fulfill];
}];
} @catch (NSException *exception) {
_failed = YES;
XCTFail("TEST FAILED");
@throw exception;
}
if (wait) {
[self waitForExpectationsWithTimeout:1 handler:nil];
}
}
@end