Skip to content

Instantly share code, notes, and snippets.

@vl4dimir
Created November 20, 2010 14:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vl4dimir/707869 to your computer and use it in GitHub Desktop.
Save vl4dimir/707869 to your computer and use it in GitHub Desktop.
Example asynchronous GHUnit test.
#import <SenTestingKit/SenTestingKit.h>
@interface AsyncTests : SenTestCase {
NSCondition* condition;
BOOL operationSucceeded;
}
@end
#import "AsyncTests.h"
@implementation AsyncTests
- (void) setUp
{
condition = [[NSCondition alloc] init];
operationSucceeded = NO;
}
- (void) tearDown
{
[condition release];
}
- (void) testAsynchronousMethodCall
{
// Create and start an asynchronous operation
NSOperationQueue* queue = [[[NSOperationQueue alloc] init] autorelease];
MyOperation* operation = [[[MyOperation alloc] init] autorelease];
[queue addOperation:operation];
// Block and wait for one of the delegate methods to be called
[condition lock];
[condition wait];
[condition unlock];
// Our operation has completed at this point, so we can check if it was successful
STAssertTrue(operationSucceeded, @"");
}
- (void) myOperationDidFinish
{
operationSucceeded = YES;
[condition lock];
[condition signal];
[condition unlock];
}
- (void) myOperationDidFailWithError:(NSError*)error
{
operationSucceeded = NO;
[condition lock];
[condition signal];
[condition unlock];
}
@end
Copy link

ghost commented May 18, 2011

Do you need the [connection release] lines in your callbacks - you didn't retain it in the testAsynchronousMethodCall?

@vl4dimir
Copy link
Author

What [connection release] line? The only thing I'm releasing is the condition object in the tearDown method.

Copy link

ghost commented May 20, 2011

Ok, this is embarrassing, I seem to have imagined things. You're absolutely right!

@vl4dimir
Copy link
Author

No problem, it happens to everyone ;)

@nheagy
Copy link

nheagy commented Jul 1, 2011

What happens if operation fails so badly that neither delegate method is called?

@vl4dimir
Copy link
Author

vl4dimir commented Jul 1, 2011

Good point. I guess you could set a timer to go off after some delay, and in its target method you could check if the asynchronous operation is still in progress. If so, you can kill it and signal the condition without setting the operationSucceeded flag, making the test fail.

@jmoody
Copy link

jmoody commented Apr 13, 2012

To solve the problem where neither delegate method is called, you could replace -wait with -waitUntilDate:

NSDate *date = [[NSDate date] dateByAddingTimeInterval:10];
[condition lock];
BOOL timedOut = ![self.condition waitUntilDate:date];
[self.condition unlock];

if (timedOut) {
  // do something
} else {
    GHAssertTrue(self.operationSucceeded, nil);
}

I encountered this problem when testing async code on the command line. Specifically, I was testing the outcome of an ASIHTTRequest. On the command line, my asynchronous tests were hanging so I implemented the 10 sec timeout with -waitUntilDate:. Unfortunately, I have not discovered the root of the problem: why are the async tests hanging on the command line?

@chris-hatton
Copy link

OCUnit / SenTesting framework does not run within the normal RunLoop; which NSURLConnection relies on to execute. This is why the request never starts and none of the delegate methods are ever called. I tried to make this work in OCUnit for some time (including manually 'spinning' the RunLoop) with no success. The simplest/best alternative is using another test framework which does run tests in full application mode. I recommend GHUnit for ease of use; it's quick to set up and there are plenty of examples online.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment