Example #1
0
 public SafariD00(URI uri) {
   if (LOG instanceof StdErrLog) {
     ((StdErrLog) LOG).setLevel(StdErrLog.LEVEL_DEBUG);
   }
   this.uri = uri;
   this.endpoint = new InetSocketAddress(uri.getHost(), uri.getPort());
 }
  /**
   * 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));
    }
  }
  @Test
  public void testStart() throws Exception {
    TestLifeCycle lifecycle = new TestLifeCycle();
    TestListener listener = new TestListener();
    lifecycle.addLifeCycleListener(listener);

    lifecycle.setCause(cause);

    try {
      StdErrLog.getLogger(AbstractLifeCycle.class).setHideStacks(true);
      lifecycle.start();
      assertTrue(false);
    } catch (Exception e) {
      assertEquals(cause, e);
      assertEquals(cause, listener.getCause());
    } finally {
      StdErrLog.getLogger(AbstractLifeCycle.class).setHideStacks(false);
    }
    lifecycle.setCause(null);

    lifecycle.start();

    // check that the starting event has been thrown
    assertTrue("The staring event didn't occur", listener.starting);

    // check that the started event has been thrown
    assertTrue("The started event didn't occur", listener.started);

    // check that the starting event occurs before the started event
    assertTrue(
        "The starting event must occur before the started event",
        listener.startingTime <= listener.startedTime);

    // check that the lifecycle's state is started
    assertTrue("The lifecycle state is not started", lifecycle.isStarted());
  }