示例#1
0
  /**
   * In a situation where the upgrade/connection is successfull, and there is no activity for a
   * while, the idle timeout triggers on the client side and automatically initiates a close
   * handshake.
   */
  @Test
  public void testIdleDetectedByClient() throws Exception {
    TrackingSocket wsocket = new TrackingSocket();

    WebSocketClient client = factory.newWebSocketClient(wsocket);

    URI wsUri = server.getWsUri();
    Future<UpgradeResponse> future = client.connect(wsUri);

    ServerConnection ssocket = server.accept();
    ssocket.upgrade();

    // Validate that connect occurred
    future.get(500, TimeUnit.MILLISECONDS);
    wsocket.waitForConnected(500, TimeUnit.MILLISECONDS);

    // Wait for inactivity idle timeout.
    long start = System.currentTimeMillis();
    wsocket.waitForClose(10, TimeUnit.SECONDS);
    long end = System.currentTimeMillis();
    long dur = (end - start);
    // Make sure idle timeout takes less than 5 total seconds
    Assert.assertThat("Idle Timeout", dur, lessThanOrEqualTo(5000L));

    // Client should see a close event, with status NO_CLOSE
    wsocket.assertCloseCode(StatusCode.NORMAL);
  }
  protected void initJavaScript() throws Exception {
    // Initializes the thread model
    org.mozilla.javascript.Context jsContext = org.mozilla.javascript.Context.enter();
    try {
      ScriptableObject rootScope = jsContext.initStandardObjects();

      // TODO: not needed now that HttpClient supports cookies
      ScriptableObject.defineClass(rootScope, HttpCookieStore.class);
      jsContext.evaluateString(
          rootScope, "var cookies = new HttpCookieStore();", "cookies", 1, null);
      HttpCookieStore cookies = (HttpCookieStore) rootScope.get("cookies", rootScope);
      cookies.putAll(this.cookies);
      this.cookies = cookies;

      ScriptableObject.defineClass(rootScope, JavaScriptThreadModel.class);
      jsContext.evaluateString(
          rootScope, "var threadModel = new JavaScriptThreadModel(this);", "threadModel", 1, null);
      threadModel = (ThreadModel) rootScope.get("threadModel", rootScope);
      threadModel.init();

      ScriptableObject.defineClass(rootScope, XMLHttpRequestClient.class);
      ScriptableObject.defineClass(rootScope, XMLHttpRequestExchange.class);
      jsContext.evaluateString(
          rootScope, "var xhrClient = new XMLHttpRequestClient(2);", "xhrClient", 1, null);
      xhrClient = (XMLHttpRequestClient) rootScope.get("xhrClient", rootScope);
      xhrClient.start();

      ScriptableObject.defineClass(rootScope, WebSocketClientFactory.class);
      ScriptableObject.defineClass(rootScope, WebSocketClient.class);
      jsContext.evaluateString(
          rootScope,
          "var wsClientFactory = new WebSocketClientFactory();",
          "wsClientFactory",
          1,
          null);
      wsClientFactory = (WebSocketClientFactory) rootScope.get("wsClientFactory", rootScope);
      wsClientFactory.start();
    } finally {
      org.mozilla.javascript.Context.exit();
    }
  }
 protected void destroyJavaScript() throws Exception {
   wsClientFactory.stop();
   xhrClient.stop();
   threadModel.destroy();
 }
示例#4
0
 @After
 public void stopFactory() throws Exception {
   factory.stop();
 }
示例#5
0
 @Before
 public void startFactory() throws Exception {
   factory = new WebSocketClientFactory();
   factory.getPolicy().setIdleTimeout(250); // idle timeout (for all tests here)
   factory.start();
 }