Skip to content

Instantly share code, notes, and snippets.

@McGalanes
Created June 2, 2017 11:27
Show Gist options
  • Save McGalanes/b54d730bda3a17a38ede7c263e36dbdb to your computer and use it in GitHub Desktop.
Save McGalanes/b54d730bda3a17a38ede7c263e36dbdb to your computer and use it in GitHub Desktop.
Needed for Tests with RxJava Schedulers
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;
public 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