3131import quickfix .field .MsgType ;
3232import quickfix .mina .ProtocolFactory ;
3333import quickfix .mina .SingleThreadedEventHandlingStrategy ;
34+ import quickfix .mina .initiator .ConnectException ;
3435import quickfix .mina .ssl .SSLSupport ;
3536import quickfix .test .acceptance .ATServer ;
3637import quickfix .test .util .StackTraceUtil ;
4142import java .lang .management .ManagementFactory ;
4243import java .lang .management .ThreadInfo ;
4344import java .lang .management .ThreadMXBean ;
45+ import java .net .InetSocketAddress ;
4446import java .net .ServerSocket ;
4547import java .net .Socket ;
48+ import java .net .SocketAddress ;
4649import java .util .HashMap ;
4750import java .util .concurrent .CountDownLatch ;
4851import java .util .concurrent .ExecutorService ;
4952import java .util .concurrent .Executors ;
5053import java .util .concurrent .TimeUnit ;
5154import java .util .concurrent .atomic .AtomicBoolean ;
5255import java .util .concurrent .atomic .AtomicInteger ;
56+ import java .util .concurrent .atomic .AtomicReference ;
5357import java .util .logging .Level ;
5458
5559import static junit .framework .TestCase .assertNotNull ;
@@ -233,22 +237,25 @@ public void testDoubleStartOfInitiator() throws Exception {
233237 }
234238
235239 @ Test
236- public void testInitiatorConnectionException () throws Exception {
240+ public void testInitiatorConnectException () throws Exception {
237241 // use a free port to make sure nothing is listening
238242 int freePort = AvailablePortFinder .getNextAvailable ();
239243 Initiator initiator = null ;
244+ String host = "localhost" ;
240245 AtomicBoolean onConnectExceptionWasCalled = new AtomicBoolean (false );
246+ AtomicReference <SocketAddress > socketAddress = new AtomicReference <>(null );
241247 try {
242248 SessionID clientSessionID = new SessionID (FixVersions .BEGINSTRING_FIX42 , "TW" , "ISLD" );
243249 SessionSettings settings = getClientSessionSettings (clientSessionID , freePort );
244250 settings .setString (clientSessionID , "ReconnectInterval" , "1" );
245- settings .setString (clientSessionID , "SocketConnectHost" , "localhost" );
246- settings .setString (clientSessionID , "SocketConnectProtocol" , ProtocolFactory .getTypeString (ProtocolFactory .VM_PIPE ));
251+ settings .setString (clientSessionID , "SocketConnectHost" , host );
252+ settings .setString (clientSessionID , "SocketConnectProtocol" , ProtocolFactory .getTypeString (ProtocolFactory .SOCKET ));
247253
248254 SessionStateListener sessionStateListener = new SessionStateListener () {
249255 @ Override
250256 public void onConnectException (SessionID sessionID , Exception e ) {
251257 onConnectExceptionWasCalled .set (true );
258+ socketAddress .set (((ConnectException ) e ).getSocketAddress ());
252259 }
253260 };
254261 // add state listener on creation of Session
@@ -261,12 +268,16 @@ public void onCreate(SessionID sessionId) {
261268 DefaultSessionFactory sessionFactory = new DefaultSessionFactory (clientApplication , new MemoryStoreFactory (), new ScreenLogFactory (settings ), new DefaultMessageFactory ());
262269 initiator = new SocketInitiator (sessionFactory , settings , 10000 );
263270 initiator .start ();
264- Thread .sleep (3000 ); // make sure we try to connect
271+ Thread .sleep (5000 ); // make sure we try to connect
265272 } finally {
266273 if (initiator != null ) {
267274 initiator .stop (true );
268275 }
269276 assertTrue (onConnectExceptionWasCalled .get ());
277+ InetSocketAddress inetSocketAddress = (InetSocketAddress ) socketAddress .get ();
278+ assertNotNull (inetSocketAddress );
279+ assertEquals (host , inetSocketAddress .getHostName ());
280+ assertEquals (freePort , inetSocketAddress .getPort ());
270281 }
271282 }
272283
0 commit comments