}
public void testDisconnectOnUnbind() throws Exception {
final IoServiceListenerSupport support = new IoServiceListenerSupport();
MockControl acceptorControl = MockControl
.createStrictControl(IoAcceptor.class);
IoAcceptor acceptor = (IoAcceptor) acceptorControl.getMock();
final TestSession session = new TestSession(acceptor, ADDRESS);
MockControl configControl = MockControl
.createStrictControl(IoAcceptorConfig.class);
IoAcceptorConfig config = (IoAcceptorConfig) configControl.getMock();
MockControl chainControl = MockControl
.createStrictControl(IoFilterChain.class);
IoFilterChain chain = (IoFilterChain) chainControl.getMock();
session.setFilterChain(chain);
MockControl listenerControl = MockControl
.createStrictControl(IoServiceListener.class);
IoServiceListener listener = (IoServiceListener) listenerControl
.getMock();
// Activate a service and create a session.
listener.serviceActivated(acceptor, ADDRESS, null, config);
listener.sessionCreated(session);
chain.fireSessionCreated(session);
chain.fireSessionOpened(session);
listenerControl.replay();
chainControl.replay();
support.add(listener);
support.fireServiceActivated(acceptor, ADDRESS, null, config);
support.fireSessionCreated(session);
listenerControl.verify();
chainControl.verify();
// Deactivate a service and make sure the session is closed & destroyed.
listenerControl.reset();
chainControl.reset();
listener.serviceDeactivated(acceptor, ADDRESS, null, config);
configControl.expectAndReturn(config.isDisconnectOnUnbind(), true);
listener.sessionDestroyed(session);
chain.fireSessionClosed(session);
listenerControl.replay();
configControl.replay();
chainControl.replay();
new Thread() {
// Emulate I/O service
@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
support.fireSessionDestroyed(session);
}
}.start();
support.fireServiceDeactivated(acceptor, ADDRESS, null, config);
Thread.sleep(1000);
listenerControl.verify();
configControl.verify();
chainControl.verify();
Assert.assertTrue(session.isClosing());
Assert.assertEquals(0, support.getManagedSessions(ADDRESS).size());