Created
February 10, 2016 09:50
-
-
Save WassimBenltaief/3c3ee917388e6f51704f to your computer and use it in GitHub Desktop.
RxAndroidTestSchedulerHook and RxJavaTestSchedulerHook as TestRule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RxSchedulersOverrideRule implements TestRule { | |
private final RxJavaSchedulersHook mRxJavaSchedulersHook = new RxJavaSchedulersHook() { | |
@Override | |
public Scheduler getIOScheduler() { | |
return Schedulers.immediate(); | |
} | |
@Override | |
public Scheduler getNewThreadScheduler() { | |
return Schedulers.immediate(); | |
} | |
}; | |
private final RxAndroidSchedulersHook mRxAndroidSchedulersHook = new RxAndroidSchedulersHook() { | |
@Override | |
public Scheduler getMainThreadScheduler() { | |
return Schedulers.immediate(); | |
} | |
}; | |
// Hack to get around RxJavaPlugins.reset() not being public | |
// See https://github.com/ReactiveX/RxJava/issues/2297 | |
// Hopefully the method will be public in new releases of RxAndroid and we can remove the hack. | |
private void callResetViaReflectionIn(RxJavaPlugins rxJavaPlugins) | |
throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { | |
Method method = rxJavaPlugins.getClass().getDeclaredMethod("reset"); | |
method.setAccessible(true); | |
method.invoke(rxJavaPlugins); | |
} | |
@Override | |
public Statement apply(final Statement base, Description description) { | |
return new Statement() { | |
@Override | |
public void evaluate() throws Throwable { | |
RxAndroidPlugins.getInstance().reset(); | |
RxAndroidPlugins.getInstance().registerSchedulersHook(mRxAndroidSchedulersHook); | |
callResetViaReflectionIn(RxJavaPlugins.getInstance()); | |
RxJavaPlugins.getInstance().registerSchedulersHook(mRxJavaSchedulersHook); | |
base.evaluate(); | |
RxAndroidPlugins.getInstance().reset(); | |
callResetViaReflectionIn(RxJavaPlugins.getInstance()); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment