/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMSignalHandlerTest.m

http://macfuse.googlecode.com/ · Objective C · 160 lines · 107 code · 30 blank · 23 comment · 0 complexity · 0ca3ea276cb427744ec7349eff405493 MD5 · raw file

  1. //
  2. // GTMSignalHandlerTest.m
  3. //
  4. // Copyright 2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMSenTestCase.h"
  19. #import "GTMSignalHandler.h"
  20. #import "GTMUnitTestDevLog.h"
  21. #import "GTMFoundationUnitTestingUtilities.h"
  22. @interface GTMSignalHandlerTest : GTMTestCase
  23. @end
  24. @interface SignalCounter : NSObject<GTMUnitTestingRunLoopContext> {
  25. @public
  26. int signalCount_;
  27. int lastSeenSignal_;
  28. BOOL shouldStop_;
  29. }
  30. - (int)count;
  31. - (int)lastSeen;
  32. - (void)countSignal:(int)signo;
  33. + (id)signalCounter;
  34. - (void)resetShouldStop;
  35. @end // SignalCounter
  36. @implementation SignalCounter
  37. + (id)signalCounter {
  38. return [[[self alloc] init] autorelease];
  39. }
  40. - (int)count {
  41. return signalCount_;
  42. }
  43. - (int)lastSeen {
  44. return lastSeenSignal_;
  45. }
  46. // Count the number of times this signal handler has fired.
  47. - (void)countSignal:(int)signo {
  48. signalCount_++;
  49. lastSeenSignal_ = signo;
  50. shouldStop_ = YES;
  51. }
  52. - (BOOL)shouldStop {
  53. return shouldStop_;
  54. }
  55. - (void)resetShouldStop {
  56. shouldStop_ = NO;
  57. }
  58. @end
  59. @implementation GTMSignalHandlerTest
  60. - (void)nomnomnom:(int)blah {
  61. STFail(@"Should never be called!");
  62. }
  63. - (void)testNillage {
  64. GTMSignalHandler *handler;
  65. // Just an init should return nil.
  66. [GTMUnitTestDevLog expectString:@"Don't call init, use "
  67. @"initWithSignal:target:action:"];
  68. handler = [[[GTMSignalHandler alloc] init] autorelease];
  69. STAssertNil(handler, nil);
  70. // Zero signal should return nil as well.
  71. handler = [[[GTMSignalHandler alloc]
  72. initWithSignal:0
  73. target:self
  74. action:@selector(nomnomnom:)] autorelease];
  75. STAssertNil(handler, nil);
  76. }
  77. - (void)testSingleHandler {
  78. SignalCounter *counter = [SignalCounter signalCounter];
  79. STAssertNotNil(counter, nil);
  80. GTMSignalHandler *handler = [[[GTMSignalHandler alloc]
  81. initWithSignal:SIGWINCH
  82. target:counter
  83. action:@selector(countSignal:)]
  84. autorelease];
  85. STAssertNotNil(handler, nil);
  86. raise(SIGWINCH);
  87. NSRunLoop *rl = [NSRunLoop currentRunLoop];
  88. [rl gtm_runUpToSixtySecondsWithContext:counter];
  89. STAssertEquals([counter count], 1, nil);
  90. STAssertEquals([counter lastSeen], SIGWINCH, nil);
  91. [counter resetShouldStop];
  92. raise(SIGWINCH);
  93. [rl gtm_runUpToSixtySecondsWithContext:counter];
  94. STAssertEquals([counter count], 2, nil);
  95. STAssertEquals([counter lastSeen], SIGWINCH, nil);
  96. [counter resetShouldStop];
  97. // create a second one to make sure we're seding data where we want
  98. SignalCounter *counter2 = [SignalCounter signalCounter];
  99. STAssertNotNil(counter2, nil);
  100. [[[GTMSignalHandler alloc] initWithSignal:SIGUSR1
  101. target:counter2
  102. action:@selector(countSignal:)] autorelease];
  103. raise(SIGUSR1);
  104. [rl gtm_runUpToSixtySecondsWithContext:counter2];
  105. STAssertEquals([counter count], 2, nil);
  106. STAssertEquals([counter lastSeen], SIGWINCH, nil);
  107. STAssertEquals([counter2 count], 1, nil);
  108. STAssertEquals([counter2 lastSeen], SIGUSR1, nil);
  109. [handler invalidate];
  110. // The signal is still ignored (so we shouldn't die), but the
  111. // the handler method should not get called.
  112. raise(SIGWINCH);
  113. [rl runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.2]];
  114. STAssertEquals([counter count], 2, nil);
  115. STAssertEquals([counter lastSeen], SIGWINCH, nil);
  116. STAssertEquals([counter2 count], 1, nil);
  117. STAssertEquals([counter2 lastSeen], SIGUSR1, nil);
  118. }
  119. - (void)testIgnore {
  120. SignalCounter *counter = [SignalCounter signalCounter];
  121. STAssertNotNil(counter, nil);
  122. [[[GTMSignalHandler alloc] initWithSignal:SIGUSR1
  123. target:counter
  124. action:NULL] autorelease];
  125. raise(SIGUSR1);
  126. NSRunLoop *rl = [NSRunLoop currentRunLoop];
  127. [rl runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.2]];
  128. STAssertEquals([counter count], 0, nil);
  129. }
  130. @end