Swiftgram/AsyncDisplayKitTests/ASDispatchTests.m
Adlai Holler 6a482dc153 [ASDataController] Use 2 Threads Per CPU When Measuring Nodes (#2145)
* [ASDataController] Use custom apply function to control thread count

* Relax the test for stupid Travis CI

* Remove unneeded import
2016-08-26 10:54:55 -07:00

39 lines
1.0 KiB
Objective-C

//
// ASDispatchTests.m
// AsyncDisplayKit
//
// Created by Adlai Holler on 8/25/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "ASDispatch.h"
@interface ASDispatchTests : XCTestCase
@end
@implementation ASDispatchTests
- (void)testDispatchApply
{
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSInteger expectedThreadCount = [NSProcessInfo processInfo].activeProcessorCount * 2;
NSLock *lock = [NSLock new];
NSMutableSet *threads = [NSMutableSet set];
NSMutableIndexSet *indices = [NSMutableIndexSet indexSet];
size_t const iterations = 1E5;
ASDispatchApply(iterations, q, 0, ^(size_t i) {
[lock lock];
[threads addObject:[NSThread currentThread]];
XCTAssertFalse([indices containsIndex:i]);
[indices addIndex:i];
[lock unlock];
});
XCTAssertLessThanOrEqual(threads.count, expectedThreadCount);
XCTAssertEqualObjects(indices, [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, iterations)]);
}
@end