Last active
December 11, 2015 05:09
-
-
Save mikekatz/4550441 to your computer and use it in GitHub Desktop.
Test asynchronous code with OCUnit, this time with a the polling code in a macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define POLL_INTERVAL 0.05 //50ms | |
#define N_SEC_TO_POLL 1.0 //poll for 1s | |
#define MAX_POLL_COUNT N_SEC_TO_POLL / POLL_INTERVAL | |
#define CAT(x, y) x ## y | |
#define TOKCAT(x, y) CAT(x, y) | |
#define __pollCountVar TOKCAT(__pollCount,__LINE__) | |
#define POLL(__done) \ | |
NSUInteger __pollCountVar = 0; \ | |
while (__done == NO && __pollCountVar < MAX_POLL_COUNT) { \ | |
NSLog(@"polling... %i", __pollCountVar ); \ | |
NSDate* untilDate = [NSDate dateWithTimeIntervalSinceNow:POLL_INTERVAL]; \ | |
[[NSRunLoop currentRunLoop] runUntilDate:untilDate]; \ | |
__pollCountVar ++; \ | |
} \ | |
if (__pollCountVar == MAX_POLL_COUNT) { \ | |
STFail(@"polling timed out"); \ | |
} | |
- (void) testAnAsynchronousFunction | |
{ | |
__block BOOL done = NO; | |
//do async on a delay | |
float delayInSeconds = 5; | |
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); | |
dispatch_after(popTime, dispatch_get_current_queue(), ^(void){ | |
BOOL result = dosomething(); //do the actual logic | |
STAssertTrue(result, @"this would be whatever needed testing"); | |
done = YES; | |
}); | |
NSLog(@"started poll"); | |
POLL(done); | |
NSLog(@"done polling"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment