Last active
December 31, 2016 03:52
-
-
Save carloseduardosx/95a1a5841b744acea5954c8fbf544571 to your computer and use it in GitHub Desktop.
Simple JUnit TestRule for do something when Observable schedulers change
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
package com.carloseduardo.rules | |
import org.junit.rules.TestRule | |
import org.junit.runner.Description | |
import org.junit.runners.model.Statement | |
import rx.Scheduler | |
import rx.android.plugins.RxAndroidPlugins | |
import rx.android.plugins.RxAndroidSchedulersHook | |
import rx.plugins.RxJavaHooks | |
import rx.schedulers.Schedulers | |
class RxJavaSchedulersChangedRule : TestRule { | |
override fun apply(base: Statement?, description: Description?): Statement { | |
return object: Statement() { | |
override fun evaluate() { | |
RxJavaHooks.setOnIOScheduler { Schedulers.immediate() } | |
RxJavaHooks.setOnComputationScheduler { Schedulers.immediate() } | |
RxJavaHooks.setOnNewThreadScheduler { Schedulers.immediate() } | |
RxAndroidPlugins.getInstance().reset() | |
RxAndroidPlugins.getInstance().registerSchedulersHook(object: RxAndroidSchedulersHook() { | |
override fun getMainThreadScheduler(): Scheduler { | |
return Schedulers.immediate() | |
} | |
}) | |
try { | |
base?.evaluate() | |
} finally { | |
RxJavaHooks.reset() | |
RxAndroidPlugins.getInstance().reset() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment