PageRenderTime 32ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/Foundation/GTMTransientRootPortProxyTest.m

http://macfuse.googlecode.com/
Objective C | 182 lines | 124 code | 28 blank | 30 comment | 2 complexity | acc923ff77c16c02b34890386045467c MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  1. //
  2. // GTMTransientRootPortProxyTest.m
  3. //
  4. // Copyright 2006-2009 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 "GTMTransientRootPortProxy.h"
  20. #define kDefaultTimeout 5.0
  21. enum {
  22. kGTMTransientThreadConditionStarting = 777,
  23. kGTMTransientThreadConditionStarted,
  24. kGTMTransientThreadConditionQuitting,
  25. kGTMTransientThreadConditionQuitted
  26. };
  27. // === Start off declaring some auxillary data structures ===
  28. // The @protocol that we'll use for testing with.
  29. @protocol DOPortTestProtocol
  30. - (oneway void)doOneWayVoid;
  31. - (bycopy NSString *)doReturnStringBycopy;
  32. @end
  33. // The "server" we'll use to test the DO connection. This server will implement
  34. // our test protocol, and it will run in a separate thread from the main
  35. // unit testing thread, so the DO requests can be serviced.
  36. @interface DOPortTestServer : NSObject <DOPortTestProtocol> {
  37. @private
  38. NSPort *clientSendPort_;
  39. NSPort *clientReceivePort_;
  40. }
  41. - (void)runThread:(NSConditionLock *)lock;
  42. - (NSPort *)clientSendPort;
  43. - (NSPort *)clientReceivePort;
  44. @end
  45. @implementation DOPortTestServer
  46. - (void)runThread:(NSConditionLock *)lock {
  47. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  48. NSDate *future = [NSDate dateWithTimeIntervalSinceNow:kDefaultTimeout];
  49. if(![lock lockWhenCondition:kGTMTransientThreadConditionStarting
  50. beforeDate:future]) {
  51. _GTMDevLog(@"Unable to acquire lock in runThread! This is BAD!");
  52. [pool drain];
  53. [NSThread exit];
  54. }
  55. clientSendPort_ = [NSPort port];
  56. clientReceivePort_ = [NSPort port];
  57. NSConnection *conn
  58. = [[NSConnection alloc] initWithReceivePort:clientSendPort_
  59. sendPort:clientReceivePort_];
  60. [conn setRootObject:self];
  61. [lock unlockWithCondition:kGTMTransientThreadConditionStarted];
  62. while (![lock tryLockWhenCondition:kGTMTransientThreadConditionQuitting]) {
  63. NSDate *runUntil = [NSDate dateWithTimeIntervalSinceNow:0.1];
  64. [[NSRunLoop currentRunLoop] runUntilDate:runUntil];
  65. }
  66. [conn setRootObject:nil];
  67. [clientSendPort_ invalidate];
  68. [clientReceivePort_ invalidate];
  69. [conn release];
  70. [pool drain];
  71. [lock unlockWithCondition:kGTMTransientThreadConditionQuitted];
  72. }
  73. - (NSPort *)clientSendPort {
  74. return clientSendPort_;
  75. }
  76. - (NSPort *)clientReceivePort {
  77. return clientReceivePort_;
  78. }
  79. - (oneway void)doOneWayVoid {
  80. // Do nothing
  81. }
  82. - (bycopy NSString *)doReturnStringBycopy {
  83. return @"TestString";
  84. }
  85. @end
  86. // === Done with auxillary data structures, now for the main test class ===
  87. @interface GTMTransientRootPortProxyTest : GTMTestCase {
  88. DOPortTestServer *server_;
  89. NSConditionLock *syncLock_;
  90. }
  91. @end
  92. @implementation GTMTransientRootPortProxyTest
  93. - (void)testTransientRootPortProxy {
  94. syncLock_ = [[[NSConditionLock alloc]
  95. initWithCondition:kGTMTransientThreadConditionStarting]
  96. autorelease];
  97. // Setup our server.
  98. server_ = [[[DOPortTestServer alloc] init] autorelease];
  99. [NSThread detachNewThreadSelector:@selector(runThread:)
  100. toTarget:server_
  101. withObject:syncLock_];
  102. NSDate *future = [NSDate dateWithTimeIntervalSinceNow:kDefaultTimeout];
  103. STAssertTrue([syncLock_ lockWhenCondition:kGTMTransientThreadConditionStarted
  104. beforeDate:future],
  105. @"Unable to start thread");
  106. [syncLock_ unlockWithCondition:kGTMTransientThreadConditionStarted];
  107. NSPort *receivePort = [server_ clientReceivePort];
  108. NSPort *sendPort = [server_ clientSendPort];
  109. GTMTransientRootPortProxy<DOPortTestProtocol> *failProxy =
  110. [GTMTransientRootPortProxy rootProxyWithReceivePort:nil
  111. sendPort:nil
  112. protocol:@protocol(DOPortTestProtocol)
  113. requestTimeout:kDefaultTimeout
  114. replyTimeout:kDefaultTimeout];
  115. STAssertNil(failProxy, @"should have failed w/o a port");
  116. failProxy =
  117. [GTMTransientRootPortProxy rootProxyWithReceivePort:receivePort
  118. sendPort:sendPort
  119. protocol:nil
  120. requestTimeout:kDefaultTimeout
  121. replyTimeout:kDefaultTimeout];
  122. STAssertNil(failProxy, @"should have failed w/o a protocol");
  123. GTMTransientRootPortProxy<DOPortTestProtocol> *proxy =
  124. [GTMTransientRootPortProxy rootProxyWithReceivePort:receivePort
  125. sendPort:sendPort
  126. protocol:@protocol(DOPortTestProtocol)
  127. requestTimeout:kDefaultTimeout
  128. replyTimeout:kDefaultTimeout];
  129. STAssertEqualObjects([proxy doReturnStringBycopy],
  130. @"TestString", @"proxy should have returned "
  131. @"'TestString'");
  132. // Redo the *exact* same test to make sure we can have multiple instances
  133. // in the same app.
  134. proxy =
  135. [GTMTransientRootPortProxy rootProxyWithReceivePort:receivePort
  136. sendPort:sendPort
  137. protocol:@protocol(DOPortTestProtocol)
  138. requestTimeout:kDefaultTimeout
  139. replyTimeout:kDefaultTimeout];
  140. STAssertEqualObjects([proxy doReturnStringBycopy],
  141. @"TestString", @"proxy should have returned "
  142. @"'TestString'");
  143. [syncLock_ tryLockWhenCondition:kGTMTransientThreadConditionStarted];
  144. [syncLock_ unlockWithCondition:kGTMTransientThreadConditionQuitting];
  145. // Wait for the server to shutdown so we clean up nicely.
  146. // The max amount of time we will wait until we abort this test.
  147. NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:kDefaultTimeout];
  148. // The server did not shutdown and we want to capture this as an error
  149. STAssertTrue([syncLock_ lockWhenCondition:kGTMTransientThreadConditionQuitted
  150. beforeDate:timeout],
  151. @"The server did not shutdown gracefully before the timeout.");
  152. [syncLock_ unlockWithCondition:kGTMTransientThreadConditionQuitted];
  153. }
  154. @end