Thread.sleep(...)
is recommended in testing RxJava code snippets. However, that is not efficient enough.Here is an alternative way for your reference.
AtomicBoolean atomicBoolean = new AtomicBoolean(false);
ExecutorService executorService = Executors.newFixedThreadPool(4);
Observable.timer(10, TimeUnit.MILLISECONDS, Schedulers.from(executorService))
.subscribe(t -> atomicBoolean.set(true));
executorService.awaitTermination(100, TimeUnit.MILLISECONDS);
assertTrue(atomicBoolean.get());
The key is to inject a custom thread pool and wait for that thread pool to complete.
No comments:
Post a Comment