Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing for code like dispatch_async.

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];
    });
like image 433
jianhua Avatar asked Dec 13 '25 06:12

jianhua


1 Answers

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();

}

like image 109
jianhua Avatar answered Dec 15 '25 22:12

jianhua