Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lazy man's RxJava Espresso Scheduler Hooks
public class RxSchedulerHook {
private ISRxSchedulerHook() {
// no instances
}
/**
* this makes sure that when we run the tests all of RxJava
* operates on a single thread (Scheduler.immediate)
*/
public static void registerHooksForTesting() {
RxJavaPlugins.getInstance().registerSchedulersHook(new RxJavaTestSchedulerHook());
//RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidTestSchedulerHook());
}
static class RxAndroidTestSchedulerHook
extends RxAndroidSchedulersHook {
// we use the async task scheduler because Espresso already knows how to handle this executor :D
@Override
public Scheduler getMainThreadScheduler() {
return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
static class RxJavaTestSchedulerHook
extends RxJavaSchedulersHook {
// we use the async task scheduler because Espresso already knows how to handle this executor :D
@Override
public Scheduler getComputationScheduler() {
return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public Scheduler getIOScheduler() {
return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public Scheduler getNewThreadScheduler() {
return Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.