我在我的应用程序中使用Android-DDP库。该库实现了Distributed Data Protocol
。
我想实现一些集成测试,其中我不想模拟DDP-client
。我在同一台机器上设置了后端实例,所以我不担心网络问题。
我开始使用这样的测试结构:
@Inject
Meteor meteor;
@Test
public void testSendMessage() throws Exception {
final Object[] params = new Object[]{"example params"};//some params are build here.
final Result result = Result.empty(); //my class for tests;
final CountDownLatch latch = new CountDownLatch(1);
meteor.call("send_message", params, new ResultListener() {
@Override
public void onSuccess(String result) {
result.setSucess(result);
latch.countDown();
}
@Override
public void onError(String error, String reason, String details) {
result.setError(error, reason, details);
latch.countDown();
}
});
latch.await(); // here we block the main thread, and callback will not be called because of it.
//here goes some assertions, but they will never call.
}
但回调从未被调用过。经过小规模的调查后,我发现, ddp-client
使用AsyncTask
来执行后台操作并向客户端代码发送回调。
由于始终在AsyncTask.onPostExecute
上调用 main thread
,我无法通过回调接听电话:测试阻止main thread
直到调用回调,但永远不会调用回调,因为在AsyncTask.onPostExecute
上调用main thread
(被测试阻止)
因此,为了解决这个问题,我需要在不同的线程中运行tests
或AsyncTask.onPostExecute
,或者以某种方式不阻止线程来测试结果。这可能吗?
答案 0 :(得分:0)