Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Using CACurrentMediaTime for GHRunForInterval so as to not interfere …

…with swizzled NSDate methods.
  • Loading branch information...
commit 8ed3a7637d04b76bda55b27a7f863c85c07c613d 1 parent e66825b
John Boiles authored
View
4 Classes/GHTestUtils.h
@@ -38,7 +38,7 @@
@param interval Interval for the main loop to run
*/
-void GHRunForInterval(NSTimeInterval interval);
+void GHRunForInterval(CFTimeInterval interval);
/*!
Keep running the main runloop until whileBlock returns NO or timeout is reached.
@@ -48,4 +48,4 @@ void GHRunForInterval(NSTimeInterval interval);
@param timeout Maximum time to run the main loop before giving up
@param whileBlock Block that returns YES if the main runloop should keep running
*/
-void GHRunUntilTimeoutWhileBlock(NSTimeInterval timeout, BOOL(^whileBlock)());
+void GHRunUntilTimeoutWhileBlock(CFTimeInterval timeout, BOOL(^whileBlock)());
View
26 Classes/GHTestUtils.m
@@ -28,25 +28,31 @@
//
#import "GHTestUtils.h"
+#import <QuartzCore/CoreAnimation.h>
-void GHRunForInterval(NSTimeInterval interval) {
- NSTimeInterval checkEveryInterval = 0.01;
- NSTimeInterval runUntilTime = [NSDate timeIntervalSinceReferenceDate] + interval;
- NSArray *runLoopModes = [NSArray arrayWithObjects:NSDefaultRunLoopMode, NSRunLoopCommonModes, nil];
+void GHRunForInterval(CFTimeInterval interval) {
+ CFTimeInterval checkEveryInterval = 0.01;
+ // We use CACurrentMediaTime() instead of [NSDate timeIntervalSinceReferenceDate] here for two reasons
+ // 1. One testing strategy for time-dependent code is to mock NSDate methods using swizzling.
+ // 2. CACurrentMediaTime() is better for relative timing since it's not subject to network time synchronization.
+ CFTimeInterval runUntilTime = CACurrentMediaTime() + interval;
+ NSArray *runLoopModes = @[(NSString *)kCFRunLoopDefaultMode, (NSString *)kCFRunLoopCommonModes];
NSInteger runIndex = 0;
- while ([NSDate timeIntervalSinceReferenceDate] < runUntilTime) {
+ while (CACurrentMediaTime() < runUntilTime) {
NSString *mode = [runLoopModes objectAtIndex:(runIndex++ % [runLoopModes count])];
@autoreleasepool {
- if (!mode || ![[NSRunLoop currentRunLoop] runMode:mode beforeDate:[NSDate dateWithTimeIntervalSinceNow:checkEveryInterval]])
+ SInt32 runLoopStatus = CFRunLoopRunInMode((__bridge CFStringRef)mode, checkEveryInterval, false);
+ if (!mode || (runLoopStatus == kCFRunLoopRunFinished)) {
// If there were no run loop sources or timers then we should sleep for the interval
- [NSThread sleepForTimeInterval:checkEveryInterval];
+ usleep(checkEveryInterval * USEC_PER_SEC);
+ }
}
}
}
-void GHRunUntilTimeoutWhileBlock(NSTimeInterval timeout, BOOL(^whileBlock)()) {
- NSTimeInterval endTime = [NSDate timeIntervalSinceReferenceDate] + timeout;
- while (whileBlock() && ([NSDate timeIntervalSinceReferenceDate] < endTime)) {
+void GHRunUntilTimeoutWhileBlock(CFTimeInterval timeout, BOOL(^whileBlock)()) {
+ CFTimeInterval endTime = CACurrentMediaTime() + timeout;
+ while (whileBlock() && (CACurrentMediaTime() < endTime)) {
GHRunForInterval(0.1);
}
}
View
4 Project-iOS/GHUnitIOS.xcodeproj/project.pbxproj
@@ -163,6 +163,7 @@
42A274AC16361D9500BA4C74 /* GHTestUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 42A274A916361D9500BA4C74 /* GHTestUtils.m */; };
42A274AD16361D9500BA4C74 /* GHTestUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 42A274A916361D9500BA4C74 /* GHTestUtils.m */; };
42A274B216361F4200BA4C74 /* GHTestUtilsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 42A274B116361F4200BA4C74 /* GHTestUtilsTest.m */; };
+ 42A274F61637535900BA4C74 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 42A678EE1401423D0060D1B5 /* QuartzCore.framework */; };
42A678EF1401423D0060D1B5 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 42A678EE1401423D0060D1B5 /* QuartzCore.framework */; };
/* End PBXBuildFile section */
@@ -315,6 +316,7 @@
0097C4AB13BA7FC800D3BEF0 /* Foundation.framework in Frameworks */,
0097C4AC13BA7FC800D3BEF0 /* CoreGraphics.framework in Frameworks */,
0012D1EC140EFD960093EC59 /* libGHUnitIOSSimulator.a in Frameworks */,
+ 42A274F61637535900BA4C74 /* QuartzCore.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1014,6 +1016,7 @@
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)\"",
+ "\"$(SYSTEM_APPS_DIR)/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks\"",
);
GCC_DYNAMIC_NO_PIC = NO;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
@@ -1040,6 +1043,7 @@
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)\"",
+ "\"$(SYSTEM_APPS_DIR)/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks\"",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Tests/Tests-Prefix.pch";
View
BIN  Project-iOS/TestImages/GHUnitIOSTestViewTest-testNoSavedImage-1-0-GHUnitIOSTestView.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

1 comment on commit 8ed3a76

@gabriel
Collaborator

lg2m

Please sign in to comment.
Something went wrong with that request. Please try again.