Skip to content

Instantly share code, notes, and snippets.

@rsaunders100
Created December 4, 2013 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsaunders100/7786566 to your computer and use it in GitHub Desktop.
Save rsaunders100/7786566 to your computer and use it in GitHub Desktop.
How to unit an objective C class with an async callback.
- (void)testAsyncExample
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block BOOL hasRunBlock = NO;
[MyClass asyncCallback:^{
hasRunBlock = YES;
dispatch_semaphore_signal(semaphore);
}];
double timeoutDelayInSeconds = 1.0;
dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(timeoutDelayInSeconds * NSEC_PER_SEC));
long result = dispatch_semaphore_wait(semaphore, timeout);
XCTAssertTrue(result == 0, @"Timed out waiting for semaphore");
XCTAssertTrue(hasRunBlock, @"");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment