2

使用调度程序进行测试的 RxJava 规则是

import io.reactivex.Scheduler
import io.reactivex.android.plugins.RxAndroidPlugins
import io.reactivex.internal.schedulers.ExecutorScheduler
import io.reactivex.plugins.RxJavaPlugins
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import java.util.concurrent.Executor

class RxImmediateSchedulerRule : TestRule {

    private val immediate = object : Scheduler() {

        override fun createWorker(): Worker {
            return ExecutorScheduler.ExecutorWorker(Executor { it.run() }, true)
        }
    }

//    private val immediate = Schedulers.trampoline()

    override fun apply(base: Statement, description: Description): Statement {
        return object : Statement() {
            @Throws(Throwable::class)
            override fun evaluate() {
                RxJavaPlugins.setInitIoSchedulerHandler { immediate }
                RxJavaPlugins.setInitComputationSchedulerHandler { immediate }
                RxJavaPlugins.setInitNewThreadSchedulerHandler { immediate }
                RxJavaPlugins.setInitSingleSchedulerHandler { immediate }
                RxAndroidPlugins.setInitMainThreadSchedulerHandler { immediate }

                try {
                    base.evaluate()
                } finally {
                    RxJavaPlugins.reset()
                    RxAndroidPlugins.reset()
                }
            }
        }
    }
}

现在使用 RxJava3

ExecutorScheduler.ExecutorWorker(Executor { it.run() }, true, true)

这里调用了第二个参数,fair但类中没有 JavaDoc 或该参数的解释ExecutorScheduler。公平参数的用途是什么,应该在什么时候使用?

4

1 回答 1

1

ExecutorScheduler 类中没有此参数的 JavaDoc 或解释

那是因为ExecutorScheduler是一个内部类,并不意味着或期望直接使用。

公平性在Schedulers.from().

于 2020-08-28T13:46:43.197 回答