Skip to content
This repository has been archived by the owner on Jun 5, 2018. It is now read-only.

Commit

Permalink
Run for interval (Robert Palmer)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Sep 1, 2010
1 parent 727fb28 commit cac475f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Classes/GHAsyncTestCase.h
Expand Up @@ -136,4 +136,11 @@ enum {
*/
- (void)notify:(NSInteger)status;

/*!
Run the run loops for the specified interval.
@param interval
@author Adapted from Robert Palmer, pauseForTimeout
*/
- (void)runForInterval:(NSTimeInterval)interval;

@end
24 changes: 24 additions & 0 deletions Classes/GHAsyncTestCase.m
Expand Up @@ -139,6 +139,30 @@ - (void)waitForTimeout:(NSTimeInterval)timeout {
}
}

// Similar to _waitFor:timeout: but just runs the loops
// From Robert Palmer, pauseForTimeout
- (void)runForInterval:(NSTimeInterval)interval {
NSTimeInterval checkEveryInterval = 0.05;
NSDate *runUntilDate = [NSDate dateWithTimeIntervalSinceNow:interval];

if (!_runLoopModes)
_runLoopModes = [[NSArray arrayWithObjects:NSDefaultRunLoopMode, NSRunLoopCommonModes, nil] retain];

NSInteger runIndex = 0;

while ([runUntilDate compare:[NSDate date]] == NSOrderedDescending) {
NSString *mode = [_runLoopModes objectAtIndex:(runIndex++ % [_runLoopModes count])];

[lock_ unlock];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (!mode || ![[NSRunLoop currentRunLoop] runMode:mode beforeDate:[NSDate dateWithTimeIntervalSinceNow:checkEveryInterval]])
// If there were no run loop sources or timers then we should sleep for the interval
[NSThread sleepForTimeInterval:checkEveryInterval];
[pool release];
[lock_ lock];
}
}

- (void)notify:(NSInteger)status {
[self notify:status forSelector:NULL];
}
Expand Down

0 comments on commit cac475f

Please sign in to comment.