public void testUnregisterReregisterForReads() throws Exception {
    Class c = Class.forName(getChannelImplName());

    client1.bind(loopBackAnyPort);
    client1.oldConnect(svrAddr);

    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);
    client1.registerForReads((DataListener) mockHandler);

    ByteBuffer b = verifyDataPassing(svrChan);

    client1.unregisterForReads();
    b.rewind();
    svrChan.oldWrite(b);
    Thread.sleep(5000);
    mockHandler.expect(MockObject.NONE);

    client1.registerForReads((DataListener) mockHandler);
    CalledMethod m = mockHandler.expect(MockNIOServer.INCOMING_DATA);
    ByteBuffer actualBuf = (ByteBuffer) m.getAllParams()[1];
    String result = helper.readString(actualBuf, actualBuf.remaining());
    assertEquals("de", result);

    verifyTearDown();
  }
  /**
   * Order between TCPChannel.connect and TCPChannel.registerForRead results in different code
   * paths...this is one of the tests.
   *
   * @throws Exception
   */
  public void testRegisterForReadsAfterConnect() throws Exception {
    // make sure we are testing the right one....
    Class c = Class.forName(getChannelImplName());
    assertEquals("should be instance of secure channel", c, client1.getClass());

    // no bind, just do connect to test port is not zero
    client1.oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    mockConnect.expect("connected");
    log.info("connected");

    boolean isConnected = client1.isConnected();
    assertTrue("Client should be connected", isConnected);
    InetSocketAddress localAddr = client1.getLocalAddress();
    assertTrue("Port should not be 0", localAddr.getPort() != 0);

    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);

    client1.registerForReads((DataListener) mockHandler);

    log.info("data passing");
    verifyDataPassing(svrChan);
    log.info("teardown");
    verifyTearDown();
    log.info("done");
  }
  /**
   * This cannot pass on linux right now as warnings end up in the log from reads firing even though
   * there should be none if not connected. We can fix this later if it is needed.
   *
   * <p>Order between TCPChannel.connect and TCPChannel.registerForRead results in different code
   * paths...this is one of the tests.
   *
   * @throws Exception
   */
  public void xtestRegisterForReadsBeforeConnect() throws Exception {
    // make sure we are testing the right one....
    Class c = Class.forName(getChannelImplName());
    assertEquals("should be instance of correct channel type", c, client1.getClass());

    client1.bind(loopBackAnyPort);
    client1.registerForReads((DataListener) mockHandler);
    client1.oldConnect(svrAddr, (ConnectionCallback) mockConnect);
    mockConnect.expect("connected");

    boolean isConnected = client1.isConnected();
    assertTrue("Client should be connected", isConnected);

    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);

    verifyDataPassing(svrChan);
    verifyTearDown();
  }
  /**
   * Test closing socket before ChannelManager shutdown works.
   *
   * @throws Exception
   */
  public void testCloseSvrSocketBeforeChannelMgrShutdown() throws Exception {
    Class c = Class.forName(getChannelImplName());
    client1.bind(loopBackAnyPort);
    client1.oldConnect(svrAddr);

    boolean isConnected = client1.isConnected();
    assertTrue("Client should be connected", isConnected);

    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);
    client1.registerForReads((DataListener) mockHandler);

    verifyDataPassing(svrChan);

    svrChan.oldClose();

    // shutdown channel manager first
    mockServer.stop();

    mockHandler.expect(MockNIOServer.FAR_END_CLOSED);
  }
  /**
   * There was a bug where calling ChannelManager.shutdown before closing any sockets registered
   * with that ChannelManager cannot be closed...well, at least this test proves when we close the
   * test, the other side should receive that -1 indicating the far end closed the socket.
   *
   * @throws Exception
   */
  public void testCloseSocketAfterChannelMgrShutdown() throws Exception {
    Class c = Class.forName(getChannelImplName());

    client1.bind(loopBackAnyPort);
    client1.oldConnect(svrAddr);
    TCPChannel svrChan = ZNioFailureSuperclass.expectServerChannel(mockServer, c);

    client1.registerForReads((DataListener) mockHandler);

    verifyDataPassing(svrChan);

    // shutdown channel manager first....should all sockets be closed?  Right now
    // someone has to manually close all accepted sockets...ie. client responsibility.
    svrChan.oldClose();

    mockServer.stop();

    // notice the Channelmanager on the client side has not shut down so we should
    // see a close event....
    mockHandler.expect(MockNIOServer.FAR_END_CLOSED);
  }