Unit testing for code like dispatch_async.
-
20-06-2021 - |
题
Need to do unit testing for the following code, dispatch_async means code won't be executed by app logic sequence, any idea on how to make it run timely?
Thank you.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[AdTracker dosomething];
});
解决方案
See http://www.mikeash.com/pyblog/friday-qa-2011-07-22-writing-unit-tests.html
+ (BOOL)waitFor2:(finishBlock)block {
NSTimeInterval timeoutInSeconds = 10.0;
NSDate* giveUpDate = [NSDate dateWithTimeIntervalSinceNow:timeoutInSeconds];
while (!block() && ([giveUpDate timeIntervalSinceNow] > 0)) {
NSDate *stopDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
[[NSRunLoop currentRunLoop] runUntilDate:stopDate]; // un-blocking.
DLog(@"+++++ %@", [NSDate date]);
}
return block();
}
不隶属于 StackOverflow