PageRenderTime 18ms CodeModel.GetById 13ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  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. #import "GTMSenTestCase.h"
  18. #import "GTMDebugThreadValidation.h"
  19. // GTMDebugThreadValidation only happens on debug builds
  20. #if DEBUG
  21. @interface GTMDebugThreadValidationTest : GTMTestCase
  22. @end
  23. // A cheap flag for knowing when our thread has run
  24. static volatile BOOL gGTMDebugThreadValidationTestDone = NO;
  25. // This is an assertion handler that just records that an assertion has fired.
  26. @interface GTMDebugThreadValidationCheckAssertionHandler : NSAssertionHandler {
  27. @private
  28. BOOL handledAssertion_;
  29. }
  30. - (void)handleFailureInMethod:(SEL)selector
  31. object:(id)object
  32. file:(NSString *)fileName
  33. lineNumber:(NSInteger)line
  34. description:(NSString *)format,...;
  35. - (void)handleFailureInFunction:(NSString *)functionName
  36. file:(NSString *)fileName
  37. lineNumber:(NSInteger)line
  38. description:(NSString *)format,...;
  39. - (BOOL)didHandleAssertion;
  40. @end
  41. @implementation GTMDebugThreadValidationTest
  42. - (void)testOnMainThread {
  43. STAssertNoThrow(GTMAssertRunningOnMainThread(), nil);
  44. }
  45. - (void)threadFunc:(NSMutableString *)result {
  46. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  47. // We'll insert our own assertion handler that will get called on the assert
  48. // so that we don't have to worry about the log, and exception being thrown.
  49. GTMDebugThreadValidationCheckAssertionHandler *handler =
  50. [[[GTMDebugThreadValidationCheckAssertionHandler alloc] init] autorelease];
  51. NSMutableDictionary *threadDictionary
  52. = [[NSThread currentThread] threadDictionary];
  53. [threadDictionary setObject:handler forKey:@"NSAssertionHandler"];
  54. GTMAssertRunningOnMainThread();
  55. if ([handler didHandleAssertion]) {
  56. [result setString:@"ASSERTED"];
  57. }
  58. [threadDictionary removeObjectForKey:@"NSAssertionHandler"];
  59. gGTMDebugThreadValidationTestDone = YES;
  60. [pool release];
  61. }
  62. - (void)testOnOtherThread {
  63. NSMutableString *result = [NSMutableString string];
  64. gGTMDebugThreadValidationTestDone = NO;
  65. [NSThread detachNewThreadSelector:@selector(threadFunc:)
  66. toTarget:self
  67. withObject:result];
  68. NSRunLoop *loop = [NSRunLoop currentRunLoop];
  69. while (!gGTMDebugThreadValidationTestDone) {
  70. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0.01];
  71. [loop runUntilDate:date];
  72. }
  73. STAssertEqualStrings(result, @"ASSERTED", @"GTMAssertRunningOnMainThread did "
  74. @"not assert while running on another thread");
  75. }
  76. @end
  77. @implementation GTMDebugThreadValidationCheckAssertionHandler
  78. - (void)handleFailureInMethod:(SEL)selector
  79. object:(id)object
  80. file:(NSString *)fileName
  81. lineNumber:(NSInteger)line
  82. description:(NSString *)format,... {
  83. handledAssertion_ = YES;
  84. }
  85. - (void)handleFailureInFunction:(NSString *)functionName
  86. file:(NSString *)fileName
  87. lineNumber:(NSInteger)line
  88. description:(NSString *)format,... {
  89. handledAssertion_ = YES;
  90. }
  91. - (BOOL)didHandleAssertion {
  92. return handledAssertion_;
  93. }
  94. @end
  95. #endif // DEBUG