Esempio n. 1
0
  @Test
  public void testProxy_timeout() {
    // 初始化config
    AsyncLoadConfig config = new AsyncLoadConfig(3 * 100l); // 设置超时时间为300ms
    // 初始化executor
    AsyncLoadExecutor executor = new AsyncLoadExecutor(10, 100);
    executor.initital();
    // 初始化proxy
    AsyncLoadEnhanceProxy<AsyncLoadTestService> proxy =
        new AsyncLoadEnhanceProxy<AsyncLoadTestService>();
    proxy.setService(asyncLoadTestService);
    proxy.setConfig(config);
    proxy.setExecutor(executor);

    AsyncLoadTestService service = proxy.getProxy();
    AsyncLoadTestModel model1 = service.getRemoteModel("first", 1000); // 每个请求sleep 1000ms
    AsyncLoadTestModel model2 = service.getRemoteModel("two", 200); // 每个请求sleep 1000ms

    long start = 0, end = 0;
    start = System.currentTimeMillis();
    try {
      System.out.println(model1.getDetail());
      Assert.fail(); // 不会走到这一步
    } catch (Exception e) { // TimeoutException异常
      System.out.println(e);
    }
    end = System.currentTimeMillis();
    Assert.assertTrue((end - start) < 500l); // 会超时

    start = System.currentTimeMillis();
    try {
      System.out.println(model2.getDetail());
    } catch (Exception e) {
      Assert.fail(); // 不会走到这一步
    }
    end = System.currentTimeMillis();
    Assert.assertTrue((end - start) < 500l); // 不会超时
  }
Esempio n. 2
0
  @Test
  public void testProxy() {
    // 初始化config
    AsyncLoadConfig config = new AsyncLoadConfig(3 * 1000l);
    // 初始化executor
    AsyncLoadExecutor executor = new AsyncLoadExecutor(10, 100);
    executor.initital();
    // 初始化proxy
    AsyncLoadEnhanceProxy<AsyncLoadTestService> proxy =
        new AsyncLoadEnhanceProxy<AsyncLoadTestService>();
    proxy.setService(asyncLoadTestService);
    proxy.setConfig(config);
    proxy.setExecutor(executor);
    // 执行测试
    AsyncLoadTestService service = proxy.getProxy();
    AsyncLoadTestModel model1 = service.getRemoteModel("first", 1000); // 每个请求sleep 1000ms
    AsyncLoadTestModel model2 = service.getRemoteModel("two", 1000); // 每个请求sleep 1000ms
    AsyncLoadTestModel model3 = service.getRemoteModel("three", 1000); // 每个请求sleep 1000ms

    long start = 0, end = 0;
    start = System.currentTimeMillis();
    System.out.println(model1.getDetail());
    end = System.currentTimeMillis();
    Assert.assertTrue((end - start) > 500l); // 第一次会阻塞, 响应时间会在1000ms左右

    start = System.currentTimeMillis();
    System.out.println(model2.getDetail());
    end = System.currentTimeMillis();
    Assert.assertTrue((end - start) < 500l); // 第二次不会阻塞,因为第一个已经阻塞了1000ms

    start = System.currentTimeMillis();
    System.out.println(model3.getDetail());
    end = System.currentTimeMillis();
    Assert.assertTrue((end - start) < 500l); // 第三次不会阻塞,因为第一个已经阻塞了1000ms

    // 销毁executor
    executor.destory();
  }