测试异步 RPC




这里有使用GWT SyncProxy的经验吗?
我尝试测试异步 rpc,但未测试故障和成功下的代码。不幸的是没有错误日志,但也许有人可以帮助我。示例来自此页面:http://code.google.com/p/gwt-syncproxy/

编辑:
我希望测试失败。所以我添加了'assertNull(result);'。奇怪的是,控制台首先给出"异步好",然后给出"异步坏"。所以函数运行了两次?!朱尼特给出的结果是绿色的。

public class Greeet extends TestCase {
@Test
public void testGreetingServiceAsync() throws Exception {
      GreetingServiceAsync rpcServiceAsync = (GreetingServiceAsync) SyncProxy.newProxyInstance(
        GreetingServiceAsync.class, 
        "http://127.0.0.1:8888/greettest/", "greet");
      rpcServiceAsync.greetServer("SyncProxy", new AsyncCallback<String>() {
        public void onFailure(Throwable caught) {
          System.out.println("Async bad " );
        }
        public void onSuccess(String result) {
          System.out.println("Async good " );
          assertNull(result);
        }
      });
      Thread.sleep(100); // configure a sleep time similar to the time spend by the request
}
}

为了使用 gwt-syncproxy 进行测试:

  1. 您必须启动 gwt 服务器:如果您使用的是 maven,则必须启动 'mvn gwt:run',或者在 eclipse 中启动 'project -> run as -> web app'。
  2. 您必须设置服务的网址,通常是"http://127.0.0.1:8888/your_module"。请注意,在您的示例中,您使用的是应用程序 html 的 URL。
  3. 如果你测试异步,你必须等到调用完成,所以在你的情况下,你需要一个 Thread.sleep(sometime)在你的方法结束时。
  4. 如果测试同步,则不需要睡眠。

以下是两个示例测试用例:

同步测试

public void testGreetingServiceSync() throws Exception {
  GreetingService rpcService = (GreetingService)SyncProxy.newProxyInstance(
     GreetingService.class, 
    "http://127.0.0.1:8888/rpcsample/", "greet");
  String s = rpcService.greetServer("SyncProxy");
  System.out.println("Sync good " + s);
}

异步测试

boolean finishOk = false;
public void testGreetingServiceAsync() throws Exception {
  GreetingServiceAsync rpcServiceAsync = (GreetingServiceAsync) SyncProxy.newProxyInstance(
    GreetingServiceAsync.class, 
    "http://127.0.0.1:8888/rpcsample/", "greet");
  finishOk = false;
  rpcServiceAsync.greetServer("SyncProxy", new AsyncCallback<String>() {
    public void onFailure(Throwable caught) {
      caught.printStackTrace();
    }
    public void onSuccess(String result) {
      assertNull(result);
      finishOk = true;
    }
  });
  Thread.sleep(100);
  assertTrue("A timeout or error happenned in onSuccess", finishOk);
}

最新更新