package br.com.vitorsalgado.example.utils

import io.reactivex.plugins.RxJavaPlugins
import io.reactivex.schedulers.Schedulers
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

class RxUtils {
  class ImmediateSchedulersRule : TestRule {
    override fun apply(base: Statement, description: Description): Statement {
      return object : Statement() {
        @Throws(Throwable::class)
        override fun evaluate() {
          RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
          RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
          RxJavaPlugins.setNewThreadSchedulerHandler { Schedulers.trampoline() }

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