allending/Kiwi — Issue #89: Async assertions didn't work in OSX
Author: jasperblues
Created: 2012-02-14 11:52:26+00:00
State: open

Async specs fail under Cocoa (OSX) bundle. Looks like something to do with run loop - eg first spec passes, second fails.

SPEC_BEGIN(ExampleAsyncSpec)

const float nanosecondToSeconds = 1e9;

context(@"Fetching service data", ^{
it(@"should pass when spinning a runloop", ^{
__block NSString* fetchedData = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds),
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";
});

float sleepCount = 0;
while (fetchedData == nil && sleepCount < 1) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
usleep(50000);
sleepCount = sleepCount + 0.05;
}
NSString* outcome;
if (fetchedData != nil) {
outcome = @"expression happened before 9 seconds.";
}
else {
outcome = @"didn't happen";
}
[[outcome should] equal:@"expression happened before 9 seconds."]; //passes

});

it(@"should pass when using Kiwi async assertions", ^{
__block NSString* fetchedData = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds),
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";
});

[[theObject(&fetchedData) shouldEventually] equal:@"expected response data"]; //fails

});

});

SPEC_END