PageRenderTime 31ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/DebugUtils/GTMDebugThreadValidationTest.m

http://macfuse.googlecode.com/
Objective C | 110 lines | 74 code | 15 blank | 21 comment | 2 complexity | c3cc47ad9f45c3124a14fadbd429f22e MD5 | raw file
  1//
  2//  GTMDebugThreadValidationTest.m
  3//  Copyright 2008 Google Inc.
  4//
  5//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6//  use this file except in compliance with the License.  You may obtain a copy
  7//  of the License at
  8// 
  9//  http://www.apache.org/licenses/LICENSE-2.0
 10// 
 11//  Unless required by applicable law or agreed to in writing, software
 12//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 13//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 14//  License for the specific language governing permissions and limitations under
 15//  the License.
 16//
 17
 18#import "GTMSenTestCase.h"
 19#import "GTMDebugThreadValidation.h"
 20
 21// GTMDebugThreadValidation only happens on debug builds
 22#if DEBUG
 23
 24@interface GTMDebugThreadValidationTest : GTMTestCase
 25@end
 26
 27// A cheap flag for knowing when our thread has run
 28
 29static volatile BOOL gGTMDebugThreadValidationTestDone = NO;
 30
 31// This is an assertion handler that just records that an assertion has fired.
 32@interface GTMDebugThreadValidationCheckAssertionHandler : NSAssertionHandler {
 33 @private
 34  BOOL handledAssertion_;
 35}
 36- (void)handleFailureInMethod:(SEL)selector 
 37                       object:(id)object 
 38                         file:(NSString *)fileName 
 39                   lineNumber:(NSInteger)line 
 40                  description:(NSString *)format,...;
 41
 42- (void)handleFailureInFunction:(NSString *)functionName 
 43                           file:(NSString *)fileName 
 44                     lineNumber:(NSInteger)line 
 45                    description:(NSString *)format,...;
 46- (BOOL)didHandleAssertion;
 47@end
 48
 49@implementation GTMDebugThreadValidationTest
 50- (void)testOnMainThread {
 51  STAssertNoThrow(GTMAssertRunningOnMainThread(), nil);
 52}
 53
 54- (void)threadFunc:(NSMutableString *)result {
 55  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 56  // We'll insert our own assertion handler that will get called on the assert
 57  // so that we don't have to worry about the log, and exception being thrown.
 58  GTMDebugThreadValidationCheckAssertionHandler *handler = 
 59    [[[GTMDebugThreadValidationCheckAssertionHandler alloc] init] autorelease];
 60  NSMutableDictionary *threadDictionary 
 61    = [[NSThread currentThread] threadDictionary];
 62  [threadDictionary setObject:handler forKey:@"NSAssertionHandler"];
 63  GTMAssertRunningOnMainThread();
 64  if ([handler didHandleAssertion]) {
 65    [result setString:@"ASSERTED"];
 66  }
 67  [threadDictionary removeObjectForKey:@"NSAssertionHandler"];
 68  gGTMDebugThreadValidationTestDone = YES;
 69  [pool release];
 70}
 71
 72- (void)testOnOtherThread {
 73  NSMutableString *result = [NSMutableString string];
 74  gGTMDebugThreadValidationTestDone = NO;
 75  [NSThread detachNewThreadSelector:@selector(threadFunc:)
 76                           toTarget:self
 77                         withObject:result];
 78  NSRunLoop *loop = [NSRunLoop currentRunLoop];
 79  
 80  while (!gGTMDebugThreadValidationTestDone) {
 81    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0.01];
 82    [loop runUntilDate:date];
 83  }
 84  STAssertEqualStrings(result, @"ASSERTED", @"GTMAssertRunningOnMainThread did "
 85                       @"not assert while running on another thread");
 86}
 87@end
 88
 89@implementation GTMDebugThreadValidationCheckAssertionHandler
 90
 91- (void)handleFailureInMethod:(SEL)selector 
 92                       object:(id)object 
 93                         file:(NSString *)fileName 
 94                   lineNumber:(NSInteger)line 
 95                  description:(NSString *)format,... {
 96  handledAssertion_ = YES;
 97}
 98
 99- (void)handleFailureInFunction:(NSString *)functionName 
100                           file:(NSString *)fileName 
101                     lineNumber:(NSInteger)line 
102                    description:(NSString *)format,... {
103  handledAssertion_ = YES;
104}
105
106- (BOOL)didHandleAssertion {
107  return handledAssertion_;
108}
109@end
110#endif  // DEBUG