PageRenderTime 62ms CodeModel.GetById 16ms app.highlight 42ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 19#import "GTMSenTestCase.h"
 20#import "GTMSignalHandler.h"
 21#import "GTMUnitTestDevLog.h"
 22#import "GTMFoundationUnitTestingUtilities.h"
 23
 24@interface GTMSignalHandlerTest : GTMTestCase
 25@end
 26
 27@interface SignalCounter : NSObject<GTMUnitTestingRunLoopContext> {
 28 @public
 29  int signalCount_;
 30  int lastSeenSignal_;
 31  BOOL shouldStop_;
 32}
 33- (int)count;
 34- (int)lastSeen;
 35- (void)countSignal:(int)signo;
 36+ (id)signalCounter;
 37- (void)resetShouldStop;
 38@end // SignalCounter
 39
 40@implementation SignalCounter
 41+ (id)signalCounter {
 42  return [[[self alloc] init] autorelease];
 43}
 44- (int)count {
 45  return signalCount_;
 46}
 47- (int)lastSeen {
 48  return lastSeenSignal_;
 49}
 50// Count the number of times this signal handler has fired.
 51- (void)countSignal:(int)signo {
 52  signalCount_++;
 53  lastSeenSignal_ = signo;
 54  shouldStop_ = YES;
 55}
 56
 57- (BOOL)shouldStop {
 58  return shouldStop_;
 59}
 60
 61- (void)resetShouldStop {
 62  shouldStop_ = NO;
 63}
 64
 65@end
 66
 67@implementation GTMSignalHandlerTest
 68- (void)nomnomnom:(int)blah {
 69  STFail(@"Should never be called!");
 70}
 71
 72- (void)testNillage {
 73  GTMSignalHandler *handler;
 74
 75  // Just an init should return nil.
 76  [GTMUnitTestDevLog expectString:@"Don't call init, use "
 77                                  @"initWithSignal:target:action:"];
 78  handler = [[[GTMSignalHandler alloc] init] autorelease];
 79  STAssertNil(handler, nil);
 80
 81  // Zero signal should return nil as well.
 82  handler = [[[GTMSignalHandler alloc]
 83              initWithSignal:0
 84                      target:self
 85                      action:@selector(nomnomnom:)] autorelease];
 86  STAssertNil(handler, nil);
 87
 88}
 89
 90- (void)testSingleHandler {
 91  SignalCounter *counter = [SignalCounter signalCounter];
 92  STAssertNotNil(counter, nil);
 93
 94  GTMSignalHandler *handler = [[[GTMSignalHandler alloc]
 95                                 initWithSignal:SIGWINCH
 96                                         target:counter
 97                                         action:@selector(countSignal:)]
 98                               autorelease];
 99  STAssertNotNil(handler, nil);
100  raise(SIGWINCH);
101
102  NSRunLoop *rl = [NSRunLoop currentRunLoop];
103  [rl gtm_runUpToSixtySecondsWithContext:counter];
104
105  STAssertEquals([counter count], 1, nil);
106  STAssertEquals([counter lastSeen], SIGWINCH, nil);
107  [counter resetShouldStop];
108
109  raise(SIGWINCH);
110  [rl gtm_runUpToSixtySecondsWithContext:counter];
111
112  STAssertEquals([counter count], 2, nil);
113  STAssertEquals([counter lastSeen], SIGWINCH, nil);
114  [counter resetShouldStop];
115
116  // create a second one to make sure we're seding data where we want
117  SignalCounter *counter2 = [SignalCounter signalCounter];
118  STAssertNotNil(counter2, nil);
119  [[[GTMSignalHandler alloc] initWithSignal:SIGUSR1
120                                     target:counter2
121                                     action:@selector(countSignal:)] autorelease];
122
123  raise(SIGUSR1);
124  [rl gtm_runUpToSixtySecondsWithContext:counter2];
125
126  STAssertEquals([counter count], 2, nil);
127  STAssertEquals([counter lastSeen], SIGWINCH, nil);
128  STAssertEquals([counter2 count], 1, nil);
129  STAssertEquals([counter2 lastSeen], SIGUSR1, nil);
130
131  [handler invalidate];
132
133  // The signal is still ignored (so we shouldn't die), but the
134  // the handler method should not get called.
135  raise(SIGWINCH);
136  [rl runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.2]];
137
138  STAssertEquals([counter count], 2, nil);
139  STAssertEquals([counter lastSeen], SIGWINCH, nil);
140  STAssertEquals([counter2 count], 1, nil);
141  STAssertEquals([counter2 lastSeen], SIGUSR1, nil);
142
143}
144
145- (void)testIgnore {
146  SignalCounter *counter = [SignalCounter signalCounter];
147  STAssertNotNil(counter, nil);
148
149  [[[GTMSignalHandler alloc] initWithSignal:SIGUSR1
150                                     target:counter
151                                     action:NULL] autorelease];
152
153  raise(SIGUSR1);
154  NSRunLoop *rl = [NSRunLoop currentRunLoop];
155  [rl runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.2]];
156  STAssertEquals([counter count], 0, nil);
157
158}
159
160@end