diff options
-rw-r--r-- | UnitTesting/GTMFoundationUnitTestingUtilities.h | 21 | ||||
-rw-r--r-- | UnitTesting/GTMFoundationUnitTestingUtilities.m | 26 | ||||
-rw-r--r-- | UnitTesting/GTMUIUnitTestingHarness/main.m | 18 |
3 files changed, 52 insertions, 13 deletions
diff --git a/UnitTesting/GTMFoundationUnitTestingUtilities.h b/UnitTesting/GTMFoundationUnitTestingUtilities.h index 7d0792e..efbfba5 100644 --- a/UnitTesting/GTMFoundationUnitTestingUtilities.h +++ b/UnitTesting/GTMFoundationUnitTestingUtilities.h @@ -6,9 +6,9 @@ // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy // of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -24,13 +24,13 @@ // NSDate* next = [NSDate dateWithTimeIntervalSinceNow:resolution]; // [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode // beforeDate:next]; -// where |resolution| is a guess at how long it will take for the event to +// where |resolution| is a guess at how long it will take for the event to // happen. There are two major problems with this approach: // a) By guessing we force the test to take at least |resolution| time. -// b) It makes for flaky tests in that sometimes this guess isn't good, and the +// b) It makes for flaky tests in that sometimes this guess isn't good, and the // test takes slightly longer than |resolution| time causing the test to post // a possibly false-negative failure. -// To make timing callback tests easier use this class and the +// To make timing callback tests easier use this class and the // GTMUnitTestingAdditions additions to NSRunLoop and NSApplication. // Your call would look something like this: // id<GTMUnitTestingRunLoopContext> context = [self getMeAContext]; @@ -52,6 +52,15 @@ // Returns YES if we are currently being unittested. + (BOOL)areWeBeingUnitTested; + +// Installs a timer to quit the process after the given time, as a catch all for +// something not working. There is a problem that of the testing bundle fails +// to load when is is being hosted in a custom app, the app will remain running +// until the user quits it. This provides a way out of that. When the timer +// fires, a message is logged, and the process is directly exited, no clean +// shutdown. This requires a runloop be running. ++ (void)installTestingTimeout:(NSTimeInterval)maxRunInterval; + @end // An implementation of the GTMUnitTestingRunLoopContext that is a simple @@ -78,7 +87,7 @@ context:(id<GTMUnitTestingRunLoopContext>)context; // Calls -gtm_runUntilDate:mode:context: with mode set to NSDefaultRunLoopMode. -- (BOOL)gtm_runUntilDate:(NSDate *)date +- (BOOL)gtm_runUntilDate:(NSDate *)date context:(id<GTMUnitTestingRunLoopContext>)context; // Calls -gtm_runUntilDate:mode:context: with mode set to NSDefaultRunLoopMode, diff --git a/UnitTesting/GTMFoundationUnitTestingUtilities.m b/UnitTesting/GTMFoundationUnitTestingUtilities.m index ab1f29b..5304990 100644 --- a/UnitTesting/GTMFoundationUnitTestingUtilities.m +++ b/UnitTesting/GTMFoundationUnitTestingUtilities.m @@ -6,9 +6,9 @@ // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy // of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -16,6 +16,8 @@ // the License. // +#import <unistd.h> + #import "GTMFoundationUnitTestingUtilities.h" @implementation GTMFoundationUnitTestingUtilities @@ -23,7 +25,7 @@ // Returns YES if we are currently being unittested. + (BOOL)areWeBeingUnitTested { BOOL answer = NO; - + // Check to see if the SenTestProbe class is linked in before we call it. Class SenTestProbeClass = NSClassFromString(@"SenTestProbe"); if (SenTestProbeClass != Nil) { @@ -39,6 +41,22 @@ return answer; } ++ (void)maxRuntimeTimer:(NSTimer*)timer { + // Directly use fprintf so the message always shows up. + fprintf(stderr, "%s:%d: error: %s : Hit app testing timeout!\n", + __FILE__, __LINE__, __func__); + fflush(stderr); + exit(1); +} + ++ (void)installTestingTimeout:(NSTimeInterval)maxRunInterval { + [NSTimer scheduledTimerWithTimeInterval:maxRunInterval + target:self + selector:@selector(maxRuntimeTimer:) + userInfo:nil + repeats:NO]; +} + @end @implementation GTMUnitTestingBooleanRunLoopContext @@ -64,7 +82,7 @@ context:context]; } -- (BOOL)gtm_runUntilDate:(NSDate *)date +- (BOOL)gtm_runUntilDate:(NSDate *)date context:(id<GTMUnitTestingRunLoopContext>)context { return [self gtm_runUntilDate:date mode:NSDefaultRunLoopMode context:context]; } diff --git a/UnitTesting/GTMUIUnitTestingHarness/main.m b/UnitTesting/GTMUIUnitTestingHarness/main.m index 57ef9c2..7438499 100644 --- a/UnitTesting/GTMUIUnitTestingHarness/main.m +++ b/UnitTesting/GTMUIUnitTestingHarness/main.m @@ -7,9 +7,9 @@ // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy // of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -20,8 +20,20 @@ #import <Cocoa/Cocoa.h> #import "GTMAppKitUnitTestingUtilities.h" +#import "GTMFoundationUnitTestingUtilities.h" int main(int argc, char *argv[]) { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [GTMAppKitUnitTestingUtilities setUpForUIUnitTestsIfBeingTested]; - return NSApplicationMain(argc, (const char **) argv); + + // Give ourselves a max of 10 minutes for the tests. Sometimes (in automated + // builds) the unittesting bundle fails to load which causes the app to keep + // running forever. This will force it to exit after a certain amount of time + // instead of hanging running forever. + [GTMFoundationUnitTestingUtilities installTestingTimeout:10*60.0]; + + int result = NSApplicationMain(argc, (const char **) argv); + [pool drain]; + return result; } |