aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMTransientRootPortProxyTest.m
blob: a85498f80eee2ad11b6c8ea1573588c9b7e46241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
//  GTMTransientRootPortProxyTest.m
//
//  Copyright 2006-2009 Google Inc.
//
//  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
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMSenTestCase.h"
#import "GTMTransientRootPortProxy.h"

#define kDefaultTimeout 5.0

enum {
  kGTMTransientThreadConditionStarting = 777,
  kGTMTransientThreadConditionStarted,
  kGTMTransientThreadConditionQuitting,
  kGTMTransientThreadConditionQuitted
};

// === Start off declaring some auxillary data structures ===

// The @protocol that we'll use for testing with.
@protocol DOPortTestProtocol
- (oneway void)doOneWayVoid;
- (bycopy NSString *)doReturnStringBycopy;
@end

// The "server" we'll use to test the DO connection.  This server will implement
// our test protocol, and it will run in a separate thread from the main
// unit testing thread, so the DO requests can be serviced.
@interface DOPortTestServer : NSObject <DOPortTestProtocol> {
 @private
  NSPort *clientSendPort_;
  NSPort *clientReceivePort_;
}
- (void)runThread:(NSConditionLock *)lock;
- (NSPort *)clientSendPort;
- (NSPort *)clientReceivePort;
@end

@implementation DOPortTestServer

- (void)runThread:(NSConditionLock *)lock {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  NSDate *future = [NSDate dateWithTimeIntervalSinceNow:kDefaultTimeout];
  if(![lock lockWhenCondition:kGTMTransientThreadConditionStarting
                   beforeDate:future]) {
    _GTMDevLog(@"Unable to acquire lock in runThread! This is BAD!");
    [pool drain];
    [NSThread exit];
  }

  clientSendPort_ = [NSPort port];
  clientReceivePort_ = [NSPort port];

  NSConnection *conn
    = [[NSConnection alloc] initWithReceivePort:clientSendPort_
                                       sendPort:clientReceivePort_];
  [conn setRootObject:self];
  [lock unlockWithCondition:kGTMTransientThreadConditionStarted];
  while (![lock tryLockWhenCondition:kGTMTransientThreadConditionQuitting]) {
    NSDate *runUntil = [NSDate dateWithTimeIntervalSinceNow:0.1];
    [[NSRunLoop currentRunLoop] runUntilDate:runUntil];
  }
  [conn setRootObject:nil];
  [clientSendPort_ invalidate];
  [clientReceivePort_ invalidate];
  [conn release];
  [pool drain];
  [lock unlockWithCondition:kGTMTransientThreadConditionQuitted];
}

- (NSPort *)clientSendPort {
  return clientSendPort_;
}

- (NSPort *)clientReceivePort {
  return clientReceivePort_;
}

- (oneway void)doOneWayVoid {
  // Do nothing
}
- (bycopy NSString *)doReturnStringBycopy {
  return @"TestString";
}

@end

// === Done with auxillary data structures, now for the main test class ===

@interface GTMTransientRootPortProxyTest : GTMTestCase {
  DOPortTestServer *server_;
  NSConditionLock *syncLock_;
}

@end

@implementation GTMTransientRootPortProxyTest

- (void)testTransientRootPortProxy {
  syncLock_ = [[[NSConditionLock alloc]
                initWithCondition:kGTMTransientThreadConditionStarting]
               autorelease];

  // Setup our server.
  server_ = [[[DOPortTestServer alloc] init] autorelease];
  [NSThread detachNewThreadSelector:@selector(runThread:)
                           toTarget:server_
                         withObject:syncLock_];
  NSDate *future = [NSDate dateWithTimeIntervalSinceNow:kDefaultTimeout];
  STAssertTrue([syncLock_ lockWhenCondition:kGTMTransientThreadConditionStarted
                                 beforeDate:future],
               @"Unable to start thread");
  [syncLock_ unlockWithCondition:kGTMTransientThreadConditionStarted];

  NSPort *receivePort = [server_ clientReceivePort];
  NSPort *sendPort = [server_ clientSendPort];

  GTMTransientRootPortProxy<DOPortTestProtocol> *failProxy =
    [GTMTransientRootPortProxy rootProxyWithReceivePort:nil
                                               sendPort:nil
                                               protocol:@protocol(DOPortTestProtocol)
                                         requestTimeout:kDefaultTimeout
                                           replyTimeout:kDefaultTimeout];
  STAssertNil(failProxy, @"should have failed w/o a port");
  failProxy =
    [GTMTransientRootPortProxy rootProxyWithReceivePort:receivePort
                                               sendPort:sendPort
                                               protocol:nil
                                         requestTimeout:kDefaultTimeout
                                           replyTimeout:kDefaultTimeout];
  STAssertNil(failProxy, @"should have failed w/o a protocol");

  GTMTransientRootPortProxy<DOPortTestProtocol> *proxy =
    [GTMTransientRootPortProxy rootProxyWithReceivePort:receivePort
                                               sendPort:sendPort
                                               protocol:@protocol(DOPortTestProtocol)
                                         requestTimeout:kDefaultTimeout
                                           replyTimeout:kDefaultTimeout];

  STAssertEqualObjects([proxy doReturnStringBycopy],
                       @"TestString", @"proxy should have returned "
                       @"'TestString'");

  // Redo the *exact* same test to make sure we can have multiple instances
  // in the same app.
  proxy =
    [GTMTransientRootPortProxy rootProxyWithReceivePort:receivePort
                                               sendPort:sendPort
                                               protocol:@protocol(DOPortTestProtocol)
                                         requestTimeout:kDefaultTimeout
                                           replyTimeout:kDefaultTimeout];

  STAssertEqualObjects([proxy doReturnStringBycopy],
                       @"TestString", @"proxy should have returned "
                       @"'TestString'");
  [syncLock_ tryLockWhenCondition:kGTMTransientThreadConditionStarted];
  [syncLock_ unlockWithCondition:kGTMTransientThreadConditionQuitting];

  // Wait for the server to shutdown so we clean up nicely.
  // The max amount of time we will wait until we abort this test.
  NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:kDefaultTimeout];
  // The server did not shutdown and we want to capture this as an error
  STAssertTrue([syncLock_ lockWhenCondition:kGTMTransientThreadConditionQuitted
                                 beforeDate:timeout],
               @"The server did not shutdown gracefully before the timeout.");
  [syncLock_ unlockWithCondition:kGTMTransientThreadConditionQuitted];
}

@end