I try to run an Android JUnit test of a multi-threaded code. I used a CountDownLatch in order to assert from the Handler. The problem is that I never reach the timeout of the await() method. If I type Thread.sleep(3000) in the Thread for instance, and await() for 50 milliseconds, it'll still wait 3 seconds.
This is the code example:
private static boolean called;
private static Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
called = true;
};
};
public final void testSuccessfulFetch() throws Throwable {
final CountDownLatch signal = new CountDownLatch(1);
runTestOnUiThread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
Message msg = handler.obtainMessage();
handler.sendMessage(msg);
signal.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
signal.await(50, TimeUnit.MILLISECONDS);
assertTrue("handler wasn't called", called);
}
What am I missing?
Thanks!
Aucun commentaire:
Enregistrer un commentaire