/**
   * Test session open session cleanup (bug #474936)
   *
   * @throws Exception on test failure
   */
  @Test
  public void testOpenSessionCleanup() throws Exception {
    int iterationCount = 100;

    StdErrLog.getLogger(FastFailSocket.class).setLevel(StdErrLog.LEVEL_OFF);

    StdErrLog sessLog = StdErrLog.getLogger(WebSocketSession.class);
    int oldLevel = sessLog.getLevel();
    sessLog.setLevel(StdErrLog.LEVEL_OFF);

    for (int requests = 0; requests < iterationCount; requests++) {
      fastFail();
      fastClose();
      dropConnection();
    }

    sessLog.setLevel(oldLevel);

    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
      client.setProtocols("container");
      client.setTimeout(1, TimeUnit.SECONDS);
      client.connect();
      client.sendStandardRequest();
      client.expectUpgradeResponse();

      client.write(new TextFrame().setPayload("calls"));
      client.write(new TextFrame().setPayload("openSessions"));

      EventQueue<WebSocketFrame> frames = client.readFrames(3, 6, TimeUnit.SECONDS);
      WebSocketFrame frame;
      String resp;

      frame = frames.poll();
      assertThat("frames[0].opcode", frame.getOpCode(), is(OpCode.TEXT));
      resp = frame.getPayloadAsUTF8();
      assertThat(
          "Should only have 1 open session",
          resp,
          containsString("calls=" + ((iterationCount * 2) + 1)));

      frame = frames.poll();
      assertThat("frames[1].opcode", frame.getOpCode(), is(OpCode.TEXT));
      resp = frame.getPayloadAsUTF8();
      assertThat("Should only have 1 open session", resp, containsString("openSessions.size=1\n"));

      frame = frames.poll();
      assertThat("frames[2].opcode", frame.getOpCode(), is(OpCode.CLOSE));
      CloseInfo close = new CloseInfo(frame);
      assertThat("Close Status Code", close.getStatusCode(), is(StatusCode.NORMAL));
      client.write(close.asFrame()); // respond with close

      // ensure server socket got close event
      assertThat(
          "Open Sessions Latch", closeSocket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
      assertThat("Open Sessions.statusCode", closeSocket.closeStatusCode, is(StatusCode.NORMAL));
      assertThat("Open Sessions.errors", closeSocket.errors.size(), is(0));
    }
  }
 private void dropConnection() throws Exception {
   try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
     client.setProtocols("container");
     client.setTimeout(1, TimeUnit.SECONDS);
     try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
       client.connect();
       client.sendStandardRequest();
       client.expectUpgradeResponse();
       client.disconnect();
     }
   }
 }
 /**
  * Test the requirement of responding with an http 400 when using a Sec-WebSocket-Version that is
  * unsupported.
  */
 @Test
 public void testRequestVersion29() throws Exception {
   @SuppressWarnings("resource")
   BlockheadClient client = new BlockheadClient(server.getServerUri());
   client.setVersion(29); // intentionally bad version
   try {
     client.connect();
     client.sendStandardRequest();
     HttpResponse response = client.readResponseHeader();
     Assert.assertThat("Response Status Code", response.getStatusCode(), is(400));
     Assert.assertThat(
         "Response Status Reason",
         response.getStatusReason(),
         containsString("Unsupported websocket version specification"));
     Assert.assertThat("Response Versions", response.getHeader("Sec-WebSocket-Version"), is("13"));
   } finally {
     client.disconnect();
   }
 }
  private void fastFail() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
      client.setProtocols("fastfail");
      client.setTimeout(1, TimeUnit.SECONDS);
      try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();

        // client.readFrames(1,2,TimeUnit.SECONDS);

        CloseInfo close = new CloseInfo(StatusCode.NORMAL, "Normal");
        client.write(close.asFrame()); // respond with close

        // ensure server socket got close event
        assertThat("Fast Fail Latch", closeSocket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
        assertThat(
            "Fast Fail.statusCode", closeSocket.closeStatusCode, is(StatusCode.SERVER_ERROR));
        assertThat("Fast Fail.errors", closeSocket.errors.size(), is(1));
      }
    }
  }