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

/driver/mina/src/test/java/com/starlight/intrepid/ConnectionListenerTest.java

https://bitbucket.org/robeden/intrepid
Java | 329 lines | 227 code | 58 blank | 44 comment | 4 complexity | 64e800f7a135fbb78051720beaa2f71e MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. // Copyright (c) 2010 Rob Eden.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are met:
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above copyright
  9. // notice, this list of conditions and the following disclaimer in the
  10. // documentation and/or other materials provided with the distribution.
  11. // * Neither the name of Intrepid nor the
  12. // names of its contributors may be used to endorse or promote products
  13. // derived from this software without specific prior written permission.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. // DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  19. // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. package com.starlight.intrepid;
  26. import com.starlight.intrepid.auth.SimpleUserContextInfo;
  27. import javax.annotation.Nonnull;
  28. import javax.annotation.Nullable;
  29. import com.starlight.intrepid.auth.ConnectionArgs;
  30. import com.starlight.intrepid.auth.UserContextInfo;
  31. import junit.framework.TestCase;
  32. import java.net.InetAddress;
  33. import java.util.concurrent.BlockingQueue;
  34. import java.util.concurrent.LinkedBlockingQueue;
  35. import java.util.concurrent.TimeUnit;
  36. /**
  37. *
  38. */
  39. public class ConnectionListenerTest extends TestCase {
  40. private Intrepid client_instance = null;
  41. private Intrepid server_instance = null;
  42. @Override
  43. protected void tearDown() throws Exception {
  44. // Re-enable
  45. IntrepidTesting.setInterInstanceBridgeDisabled( false );
  46. if ( client_instance != null ) client_instance.close();
  47. if ( server_instance != null ) server_instance.close();
  48. }
  49. public void testSystemProperty() {
  50. assertEquals( "System property 'intrepid.req_invoke_ack_rate_sec' must be set " +
  51. "to '1' when running unit tests.", "1",
  52. System.getProperty( "intrepid.req_invoke_ack_rate_sec" ) );
  53. }
  54. public void testListener() throws Exception {
  55. TestConnectionListener s_listener = new TestConnectionListener( "Server" );
  56. TestConnectionListener c_listener = new TestConnectionListener( "Client" );
  57. server_instance = Intrepid.create( new IntrepidSetup()
  58. .vmidHint( "server" )
  59. .serverPort( 11751 )
  60. .openServer()
  61. .connectionListener( s_listener ) );
  62. client_instance = Intrepid.create( new IntrepidSetup().vmidHint( "client" )
  63. .connectionListener( c_listener ) );
  64. assertTrue( s_listener.event_queue.isEmpty() );
  65. assertTrue( c_listener.event_queue.isEmpty() );
  66. final InetAddress localhost = InetAddress.getByName( "127.0.0.1" );
  67. // Connect to the server
  68. String client_attachment = "My Client Attachment";
  69. VMID server_vmid =
  70. client_instance.connect( localhost, 11751, null, client_attachment );
  71. assertNotNull( server_vmid );
  72. // Make sure both listeners show a connection
  73. ConnectionEventInfo info = s_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  74. assertNotNull( info );
  75. assertEquals( EventType.OPENED, info.type );
  76. assertEquals( client_instance.getLocalVMID(), info.vmid );
  77. assertNull( info.attachment );
  78. assertNull( info.user_context );
  79. assertEquals( localhost, info.host );
  80. assertEquals( 1, info.ack_rate_sec ); // specified in property
  81. System.out.println( "client port: " + info.port );
  82. // OPENING
  83. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  84. assertNotNull( info );
  85. assertEquals( EventType.OPENING, info.type );
  86. assertEquals( localhost, info.host );
  87. assertEquals( 11751, info.port );
  88. // OPENED
  89. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  90. assertNotNull( info );
  91. assertEquals( EventType.OPENED, info.type );
  92. assertEquals( server_instance.getLocalVMID(), info.vmid );
  93. assertEquals( client_attachment, info.attachment );
  94. assertNull( info.user_context );
  95. assertEquals( localhost, info.host );
  96. assertEquals( 1, info.ack_rate_sec );
  97. assertEquals( 11751, info.port );
  98. // Make sure there are no more events
  99. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  100. assertNull( info );
  101. info = s_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  102. assertNull( info );
  103. // Disconnect from server
  104. client_instance.disconnect( server_vmid );
  105. // Make sure both listeners show the disconnect
  106. info = s_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  107. assertNotNull( info );
  108. assertEquals( EventType.CLOSED, info.type );
  109. assertEquals( client_instance.getLocalVMID(), info.vmid );
  110. assertNull( info.attachment );
  111. assertNull( info.user_context );
  112. assertEquals( localhost, info.host );
  113. System.out.println( "client port: " + info.port );
  114. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  115. assertNotNull( info );
  116. assertEquals( EventType.CLOSED, info.type );
  117. assertEquals( server_instance.getLocalVMID(), info.vmid );
  118. assertEquals( client_attachment, info.attachment );
  119. assertNull( info.user_context );
  120. assertEquals( localhost, info.host );
  121. assertEquals( 11751, info.port );
  122. // Make sure there are no more events
  123. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  124. assertNull( info );
  125. info = s_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  126. assertNull( info );
  127. }
  128. public void testListenerWithUserConnection() throws Exception {
  129. TestConnectionListener s_listener = new TestConnectionListener( "Server" );
  130. TestConnectionListener c_listener = new TestConnectionListener( "Client" );
  131. UserContextInfo user_context = new SimpleUserContextInfo( "test_user" );
  132. server_instance = Intrepid.create( new IntrepidSetup()
  133. .vmidHint( "server" )
  134. .serverPort( 11751 )
  135. .connectionListener( s_listener )
  136. .authHandler(
  137. ( connection_args, remote_address, session_source ) -> user_context ) );
  138. client_instance = Intrepid.create( new IntrepidSetup().vmidHint( "client" )
  139. .connectionListener( c_listener ) );
  140. assertTrue( s_listener.event_queue.isEmpty() );
  141. assertTrue( c_listener.event_queue.isEmpty() );
  142. final InetAddress localhost = InetAddress.getByName( "127.0.0.1" );
  143. // Connect to the server
  144. String client_attachment = "My Client Attachment";
  145. VMID server_vmid = client_instance.connect( InetAddress.getByName( "127.0.0.1" ),
  146. 11751, null, client_attachment );
  147. assertNotNull( server_vmid );
  148. // Make sure both listeners show a connection
  149. ConnectionEventInfo info = s_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  150. assertNotNull( info );
  151. assertEquals( EventType.OPENED, info.type );
  152. assertEquals( client_instance.getLocalVMID(), info.vmid );
  153. assertNull( info.attachment );
  154. assertEquals( user_context, info.user_context ); // server will have context
  155. // OPENING
  156. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  157. assertNotNull( info );
  158. assertEquals( EventType.OPENING, info.type );
  159. assertEquals( localhost, info.host );
  160. assertEquals( 11751, info.port );
  161. // OPENED
  162. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  163. assertNotNull( info );
  164. assertEquals( EventType.OPENED, info.type );
  165. assertEquals( server_instance.getLocalVMID(), info.vmid );
  166. assertEquals( client_attachment, info.attachment );
  167. assertNull( info.user_context ); // client will not have user context
  168. // Make sure there are no more events
  169. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  170. assertNull( info );
  171. info = s_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  172. assertNull( info );
  173. // Disconnect from server
  174. client_instance.disconnect( server_vmid );
  175. // Make sure both listeners show the disconnect
  176. info = s_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  177. assertNotNull( info );
  178. assertEquals( EventType.CLOSED, info.type );
  179. assertEquals( client_instance.getLocalVMID(), info.vmid );
  180. assertNull( info.attachment );
  181. assertEquals( user_context, info.user_context ); // server will have context
  182. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  183. assertNotNull( info );
  184. assertEquals( EventType.CLOSED, info.type );
  185. assertEquals( server_instance.getLocalVMID(), info.vmid );
  186. assertEquals( client_attachment, info.attachment );
  187. assertNull( info.user_context ); // client will not have user context
  188. // Make sure there are no more events
  189. info = c_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  190. assertNull( info );
  191. info = s_listener.event_queue.poll( 2, TimeUnit.SECONDS );
  192. assertNull( info );
  193. }
  194. private class TestConnectionListener implements ConnectionListener {
  195. private final BlockingQueue<ConnectionEventInfo> event_queue =
  196. new LinkedBlockingQueue<>();
  197. private final String id;
  198. TestConnectionListener( String id ) {
  199. this.id = id;
  200. }
  201. @Override
  202. public void connectionOpened( @Nonnull InetAddress host, int port,
  203. Object attachment, @Nonnull VMID source_vmid, @Nonnull VMID vmid,
  204. UserContextInfo user_context, VMID previous_vmid,
  205. @Nonnull Object connection_type_description, byte ack_rate_sec ) {
  206. System.out.println( id + " connection opened: " + vmid );
  207. event_queue.add( new ConnectionEventInfo( EventType.OPENED, vmid, attachment,
  208. false, user_context, host, port, ack_rate_sec ) );
  209. }
  210. @Override
  211. public void connectionClosed( @Nonnull InetAddress host, int port,
  212. @Nonnull VMID source_vmid, @Nullable VMID vmid, @Nullable Object attachment,
  213. boolean will_attempt_reconnect, @Nullable UserContextInfo user_context ) {
  214. System.out.println( id + " connection closed: " + vmid );
  215. event_queue.add( new ConnectionEventInfo( EventType.CLOSED, vmid, attachment,
  216. will_attempt_reconnect, user_context, host, port, ( byte ) -1 ) );
  217. }
  218. @Override
  219. public void connectionOpenFailed( @Nonnull InetAddress host, int port,
  220. Object attachment, Exception error, boolean will_retry ) {
  221. System.out.println( id + " connection open failed: " + host.getHostAddress() +
  222. ":" + port + " - " + error );
  223. event_queue.add( new ConnectionEventInfo( EventType.OPEN_FAILED, null,
  224. attachment, will_retry, null, host, port, ( byte ) -1 ) );
  225. }
  226. @Override
  227. public void connectionOpening( @Nonnull InetAddress host, int port,
  228. Object attachment, ConnectionArgs args,
  229. @Nonnull Object connection_type_description ) {
  230. System.out.println( id + " connection opening: " + host.getHostAddress() +
  231. ":" + port );
  232. event_queue.add( new ConnectionEventInfo( EventType.OPENING, null,
  233. attachment, false, null, host, port, ( byte ) -1 ) );
  234. }
  235. }
  236. private enum EventType {
  237. OPENED,
  238. CLOSED,
  239. OPENING,
  240. OPEN_FAILED
  241. }
  242. private class ConnectionEventInfo {
  243. private final EventType type;
  244. private final VMID vmid;
  245. private final Object attachment;
  246. private final boolean will_reconnect;
  247. private final UserContextInfo user_context;
  248. private final InetAddress host;
  249. private final int port;
  250. private final byte ack_rate_sec;
  251. ConnectionEventInfo( EventType type, VMID vmid, Object attachment,
  252. boolean will_reconnect, UserContextInfo user_context, InetAddress host,
  253. int port, byte ack_rate_sec ) {
  254. this.type = type;
  255. this.vmid = vmid;
  256. this.attachment = attachment;
  257. this.will_reconnect = will_reconnect;
  258. this.user_context = user_context;
  259. this.host = host;
  260. this.port = port;
  261. this.ack_rate_sec = ack_rate_sec;
  262. }
  263. @Override
  264. public String toString() {
  265. return "ConnectionEventInfo" + "{attachment=" + attachment + ", type=" +
  266. type + ", vmid=" + vmid + ", will_reconnect=" + will_reconnect +
  267. ", user_context=" + user_context + ", host=" + host + ", port=" + port +
  268. '}';
  269. }
  270. }
  271. }