@@ -68,36 +68,39 @@ public void testConnector() throws Exception {
6868
6969 connector .addPropertyChangeListener (new SessionConnectorListener ());
7070
71- Session session = connector .createSession (sessionID );
72- assertNotNull (session );
73-
74- Map <SessionID , Session > sessions = Collections .singletonMap (session .getSessionID (), session );
75- connector .setSessions (sessions );
76-
77- assertEquals (1 , propertyChangeEvents .size ());
78-
79- assertEquals (1 , connector .getManagedSessions ().size ());
80- assertEquals (session , connector .getManagedSessions ().get (0 ));
81-
82- assertFalse (connector .isLoggedOn ());
83-
84- Field stateField = session .getClass ().getDeclaredField ("state" );
85- stateField .setAccessible (true );
86- SessionState state = (SessionState ) stateField .get (session );
87-
88- state .setLogonSent (true );
89- state .setLogonReceived (true );
90- assertTrue (connector .isLoggedOn ());
91-
92- assertTrue (session .isEnabled ());
93- connector .logoutAllSessions (true );
94- // Acceptors should get re-enabled after Logout
95- assertTrue (session .isEnabled ());
96-
97- assertEquals (9999 , connector .getIntSetting (Acceptor .SETTING_SOCKET_ACCEPT_PORT ));
98-
99- assertNotNull (connector .getScheduledExecutorService ());
100- assertEquals (settings , connector .getSettings ());
71+ try (Session session = connector .createSession (sessionID )) {
72+ assertNotNull (session );
73+
74+ Map <SessionID , Session > sessions = Collections .singletonMap (session .getSessionID (), session );
75+ connector .setSessions (sessions );
76+
77+ assertEquals (1 , propertyChangeEvents .size ());
78+
79+ assertEquals (1 , connector .getManagedSessions ().size ());
80+ assertEquals (session , connector .getManagedSessions ().get (0 ));
81+
82+ assertFalse (connector .isLoggedOn ());
83+
84+ Field stateField = session .getClass ().getDeclaredField ("state" );
85+ stateField .setAccessible (true );
86+ SessionState state = (SessionState ) stateField .get (session );
87+
88+ state .setLogonSent (true );
89+ state .setLogonReceived (true );
90+ assertTrue (connector .isLoggedOn ());
91+
92+ assertTrue (session .isEnabled ());
93+ connector .logoutAllSessions (true );
94+ // Acceptors should get re-enabled after Logout
95+ assertTrue (session .isEnabled ());
96+
97+ assertEquals (9999 , connector .getIntSetting (Acceptor .SETTING_SOCKET_ACCEPT_PORT ));
98+
99+ assertNotNull (connector .getScheduledExecutorService ());
100+ assertEquals (settings , connector .getSettings ());
101+ } finally {
102+ connector .stop (true );
103+ }
101104 }
102105
103106 @ Test
@@ -145,7 +148,10 @@ public void testOneSessionLoggedOnOneSessionNotLoggedOne() throws Exception {
145148 assertFalse (connector .isLoggedOn ());
146149 assertTrue (connector .anyLoggedOn ());
147150 }
151+ } finally {
152+ connector .stop (true );
148153 }
154+
149155 }
150156
151157 /**
@@ -161,37 +167,40 @@ public void testAddingRemovingDynamicSessions() throws Exception {
161167
162168 SessionConnector connector = new SessionConnectorUnderTest (settings , sessionFactory );
163169 connector .setSessions (new HashMap <>());
164- Session session = connector .createSession (sessionID );
170+ try (Session session = connector .createSession (sessionID )) {
171+ // one-time use connector to create a slightly different session
172+ SessionSettings settings2 = setUpSessionSettings (sessionID2 );
173+ SessionConnector connector2 = new SessionConnectorUnderTest (settings2 , sessionFactory );
174+ connector .setSessions (new HashMap <>());
175+ try (Session session2 = connector2 .createSession (sessionID2 )) {
176+
177+ assertNotNull (session );
178+ assertNotNull (session2 );
165179
166- // one-time use connector to create a slightly different session
167- SessionSettings settings2 = setUpSessionSettings (sessionID2 );
168- SessionConnector connector2 = new SessionConnectorUnderTest (settings2 , sessionFactory );
169- connector .setSessions (new HashMap <>());
170- Session session2 = connector2 .createSession (sessionID2 );
171- assertNotNull (session );
172- assertNotNull (session2 );
173-
174- assertEquals (0 , connector .getManagedSessions ().size ());
175- connector .addDynamicSession (session );
176- assertEquals (1 , connector .getManagedSessions ().size ());
177- connector .addDynamicSession (session2 );
178- assertEquals (2 , connector .getManagedSessions ().size ());
179- // the list can be in arbitrary order so let's make sure that we get both
180- HashMap <SessionID , Session > map = new HashMap <>();
181- for (Session s : connector .getManagedSessions ()) {
182- map .put (s .getSessionID (), s );
180+ assertEquals (0 , connector .getManagedSessions ().size ());
181+ connector .addDynamicSession (session );
182+ assertEquals (1 , connector .getManagedSessions ().size ());
183+ connector .addDynamicSession (session2 );
184+ assertEquals (2 , connector .getManagedSessions ().size ());
185+ // the list can be in arbitrary order so let's make sure that we get both
186+ HashMap <SessionID , Session > map = new HashMap <>();
187+ for (Session s : connector .getManagedSessions ()) {
188+ map .put (s .getSessionID (), s );
189+ }
190+ assertEquals (session , map .get (session .getSessionID ()));
191+ assertEquals (session2 , map .get (session2 .getSessionID ()));
192+
193+ connector .removeDynamicSession (session .getSessionID ());
194+ assertEquals (1 , connector .getManagedSessions ().size ());
195+ assertEquals (session2 , connector .getManagedSessions ().get (0 ));
196+ connector .removeDynamicSession (session2 .getSessionID ());
197+ assertEquals (0 , connector .getManagedSessions ().size ());
198+ } finally {
199+ connector2 .stop ();
200+ }
201+ } finally {
202+ connector .stop (true );
183203 }
184- assertEquals (session , map .get (session .getSessionID ()));
185- assertEquals (session2 , map .get (session2 .getSessionID ()));
186-
187- connector .removeDynamicSession (session .getSessionID ());
188- assertEquals (1 , connector .getManagedSessions ().size ());
189- assertEquals (session2 , connector .getManagedSessions ().get (0 ));
190- connector .removeDynamicSession (session2 .getSessionID ());
191- assertEquals (0 , connector .getManagedSessions ().size ());
192-
193- session .close ();
194- session2 .close ();
195204 }
196205
197206 /**
@@ -242,6 +251,7 @@ public void testDynamicInitiatorSession() throws Exception {
242251 for (Session s :sessions ){
243252 s .close ();
244253 }
254+ connector .stop ();
245255 }
246256
247257 private SessionSettings setUpSessionSettings (SessionID sessionID ) {
@@ -274,9 +284,6 @@ private SessionSettings setUpInitiatorSessionSettings(SessionID sessionID) {
274284 settings .setString (Initiator .SETTING_PROXY_DOMAIN ,"Test Proxy Domain" );
275285 settings .setString (Initiator .SETTING_PROXY_HOST ,"Test Proxy Host" );
276286 settings .setString (Initiator .SETTING_PROXY_PORT ,"888" );
277-
278-
279-
280287 settings .setBool (Initiator .SETTING_DYNAMIC_SESSION ,false );
281288 settings .setString (sessionID , SessionFactory .SETTING_CONNECTION_TYPE ,
282289 SessionFactory .INITIATOR_CONNECTION_TYPE );
@@ -301,14 +308,14 @@ public void start() throws ConfigError, RuntimeError {
301308 }
302309
303310 public void stop () {
311+ super .stopSessionTimer ();
304312 }
305313
306314 public void stop (boolean force ) {
307- }
308-
309- public void block () throws ConfigError , RuntimeError {
315+ super .stopSessionTimer ();
310316 }
311317 }
318+
312319 private static class AbstractSocketInitiatorUnderTest extends AbstractSocketInitiator {
313320
314321 public AbstractSocketInitiatorUnderTest (SessionSettings settings , SessionFactory sessionFactory ) throws ConfigError {
@@ -317,19 +324,18 @@ public AbstractSocketInitiatorUnderTest(SessionSettings settings, SessionFactory
317324
318325 public void start () throws ConfigError , RuntimeError {
319326 }
320- public void createDynamicSession (SessionID sessionID ) throws ConfigError {
321- super .createDynamicSession (sessionID );
322- }
327+
323328 public void stop () {
329+ clearConnectorSessions ();
324330 }
331+
325332 public void stopInitiators (){
326333 super .stopInitiators ();
327334 }
335+
328336 public void stop (boolean force ) {
329337 }
330338
331- public void block () throws ConfigError , RuntimeError {
332- }
333339 @ Override
334340 protected void createSessionInitiators () throws ConfigError {
335341 super .createSessionInitiators ();
0 commit comments