public void testServerExceptionOnDeliver() throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);

    Server server = new Server();
    Connector connector = new SelectChannelConnector();
    server.addConnector(connector);
    server.setHandler(
        new AbstractHandler() {
          public void handle(
              String target,
              Request request,
              HttpServletRequest httpRequest,
              HttpServletResponse httpResponse)
              throws IOException, ServletException {
            request.setHandled(true);
            if (target.endsWith("/connect")) {
              serverLatch.countDown();
              try {
                // Simulate a long poll timeout
                Thread.sleep(10000);
              } catch (InterruptedException x) {
                Thread.currentThread().interrupt();
              }
            } else if (target.endsWith("/deliver")) {
              // Throw an exception on deliver
              throw new TestException();
            }
          }
        });
    server.start();
    try {
      RHTTPClient client = createClient(connector.getLocalPort(), "test4");
      try {
        final CountDownLatch deliverLatch = new CountDownLatch(1);
        client.addClientListener(
            new ClientListener.Adapter() {
              @Override
              public void deliverException(RHTTPResponse response) {
                deliverLatch.countDown();
              }
            });
        client.connect();

        assertTrue(serverLatch.await(1000, TimeUnit.MILLISECONDS));

        client.deliver(
            new RHTTPResponse(1, 200, "OK", new LinkedHashMap<String, String>(), new byte[0]));

        assertTrue(deliverLatch.await(1000, TimeUnit.MILLISECONDS));
      } finally {
        destroyClient(client);
      }
    } finally {
      server.stop();
    }
  }
 public void testConnectNoServer() throws Exception {
   RHTTPClient client = createClient(8080, "test1");
   try {
     client.connect();
     fail();
   } catch (IOException x) {
   } finally {
     destroyClient(client);
   }
 }
  public void testServerShutdownAfterConnect() throws Exception {
    final CountDownLatch connectLatch = new CountDownLatch(1);
    final CountDownLatch stopLatch = new CountDownLatch(1);

    Server server = new Server();
    Connector connector = new SocketConnector();
    server.addConnector(connector);
    server.setHandler(
        new AbstractHandler() {
          public void handle(
              String target,
              Request request,
              HttpServletRequest httpRequest,
              HttpServletResponse httpResponse)
              throws IOException, ServletException {
            request.setHandled(true);
            if (target.endsWith("/connect")) {
              connectLatch.countDown();
              try {
                Thread.sleep(10000);
              } catch (InterruptedException e) {
                stopLatch.countDown();
              }
            }
          }
        });
    server.start();
    try {
      RHTTPClient client = createClient(connector.getLocalPort(), "test5");
      try {
        final CountDownLatch serverLatch = new CountDownLatch(1);
        client.addClientListener(
            new ClientListener.Adapter() {
              @Override
              public void connectClosed() {
                serverLatch.countDown();
              }
            });
        client.connect();

        assertTrue(connectLatch.await(2000, TimeUnit.MILLISECONDS));

        server.stop();
        assertTrue(stopLatch.await(2000, TimeUnit.MILLISECONDS));

        assertTrue(serverLatch.await(2000, TimeUnit.MILLISECONDS));
      } finally {
        destroyClient(client);
      }
    } finally {
      server.stop();
    }
  }
  public void testServerExceptionOnHandshake() throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);

    Server server = new Server();
    Connector connector = new SelectChannelConnector();
    server.addConnector(connector);
    server.setHandler(
        new AbstractHandler() {
          public void handle(
              String target,
              Request request,
              HttpServletRequest httpRequest,
              HttpServletResponse httpResponse)
              throws IOException, ServletException {
            request.setHandled(true);
            if (target.endsWith("/handshake")) {
              serverLatch.countDown();
              throw new TestException();
            }
          }
        });
    server.start();
    try {
      RHTTPClient client = createClient(connector.getLocalPort(), "test2");
      try {
        try {
          client.connect();
          fail();
        } catch (IOException x) {
        }

        assertTrue(serverLatch.await(1000, TimeUnit.MILLISECONDS));
      } finally {
        destroyClient(client);
      }
    } finally {
      server.stop();
    }
  }