Hmm, that possibility did occur to me just after I sent it, thanks for pointing it out.

The tests are pasted inline below.

MINA 1.1 test (passes)
------------------------------------------------------

import static java.lang.System.currentTimeMillis;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.net.InetSocketAddress;

import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketConnector;
import org.junit.Test;

public class JUTestSessionClose
{
  private static final int TIMEOUT = 5000;

  @Test
  public void sessionClose ()
    throws Exception
  {
InetSocketAddress address = new InetSocketAddress ("127.0.0.1", 29170);

    SocketAcceptor acceptor = new SocketAcceptor ();

    // close session as soon as it's opened
    IoHandler acceptorHandler = new IoHandlerAdapter ()
    {
      @Override
      public void sessionOpened (IoSession session)
        throws Exception
      {
        session.close ();
      }
    };

    acceptor.bind (address, acceptorHandler);

    SocketConnector connector = new SocketConnector ();

    final Object lock = new Object ();

    // call notify on lock when session is closed
    IoHandler connectorHandler = new IoHandlerAdapter ()
    {
      @Override
      public void sessionClosed (IoSession session)
        throws Exception
      {
        synchronized (lock)
        {
          lock.notify ();
        }
      }
    };

ConnectFuture connectFuture = connector.connect (address, connectorHandler);

    assertTrue (connectFuture.join (TIMEOUT));

    long startedAt = currentTimeMillis ();

    // wait for connector to receive sessionClosed
    synchronized (lock)
    {
      lock.wait (TIMEOUT);
    }

    assertFalse ("Did not receive close",
                 connectFuture.getSession ().isConnected ());

    assertTrue (currentTimeMillis () - startedAt < TIMEOUT);

    connectFuture.getSession().close();
    acceptor.unbindAll();
  }
}

------------------------------------------------------

MINA 2.0 test (fails against 2.0 trunk)
------------------------------------------------------

import java.net.InetSocketAddress;

import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.junit.Test;

import static java.lang.System.currentTimeMillis;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class JUTestSessionClose
{
  private static final int TIMEOUT = 5000;

  @Test
  public void sessionClose ()
    throws Exception
  {
InetSocketAddress address = new InetSocketAddress ("127.0.0.1", 29170);

    SocketAcceptor acceptor = new NioSocketAcceptor ();

    // close session as soon as it's opened
    acceptor.setHandler (new IoHandlerAdapter ()
    {
      @Override
      public void sessionOpened (IoSession session)
        throws Exception
      {
        session.close ();
      }
    });

    acceptor.bind (address);

    SocketConnector connector = new NioSocketConnector (1);

    final Object lock = new Object ();

    // call notify on lock when session is closed
    connector.setHandler (new IoHandlerAdapter ()
    {
      @Override
      public void sessionClosed (IoSession session)
        throws Exception
      {
        synchronized (lock)
        {
          lock.notify ();
        }
      }
    });

    ConnectFuture connectFuture = connector.connect (address);

    assertTrue (connectFuture.await (TIMEOUT));

    long startedAt = currentTimeMillis ();

    // wait for connector to receive sessionClosed
    synchronized (lock)
    {
      lock.wait (TIMEOUT);
    }

    assertFalse ("Did not receive close",
                 connectFuture.getSession ().isConnected ());

    assertTrue (currentTimeMillis () - startedAt < TIMEOUT);

    connector.dispose ();
    acceptor.dispose ();
  }
}

------------------------------------------------------

On 15/02/2008, at 12:13 PM, Mark Webb wrote:

your attachment did not come through.  AFAIK, this mailing list does
not support attachments...



On Thu, Feb 14, 2008 at 6:22 PM, Matthew Phillips <[EMAIL PROTECTED]> wrote:
While porting to MINA 2.0 I've had a few tests fail that depend on the
IoHandler.sessionClosed callback. It appears that if the acceptor
closes a session in its IoHandler, the connector's IoHandler does not
get a session close event.

The attached tests demonstrate this: the one ending in MINA 1.1 works
fine under MINA 1.1.5, the one ending in MINA 2.0 (which is a direct
port of the 1.1 test to the MINA 2.0 API) does not: the sessionClosed
method is not called when the acceptor closes its end.

Hope someone can either explain why my understanding of how this is
supposed to work is wrong or have a look at how MINA 2.0 handles this.

Cheers,

Matthew.





--
--------------------------------
Talent hits a target no one else can hit; Genius hits a target no one
else can see.

Reply via email to