Skip to content

Instantly share code, notes, and snippets.

@timrijckaert
Created March 5, 2017 22:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timrijckaert/1b07748bf4757fd401493af750925fbc to your computer and use it in GitHub Desktop.
Save timrijckaert/1b07748bf4757fd401493af750925fbc to your computer and use it in GitHub Desktop.
RxJava2 Rule to change the Schedulers to immediate one. (https://www.infoq.com/articles/Testing-RxJava2)
private static class ImmediateSchedulersRule implements TestRule {
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
RxJavaPlugins.setIoSchedulerHandler(scheduler ->
Schedulers.trampoline());
RxJavaPlugins.setComputationSchedulerHandler(scheduler ->
Schedulers.trampoline());
RxJavaPlugins.setNewThreadSchedulerHandler(scheduler ->
Schedulers.trampoline());
try {
base.evaluate();
} finally {
RxJavaPlugins.reset();
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment