I'm struggling with the TestScheduler and hoping t...
# rx
f
I'm struggling with the TestScheduler and hoping that someone can render some insight. I've boiled the troublesome code down to this example:
Copy code
@Test
    fun `validate test`() {
        val scheduler = TestScheduler()

        val upstream = Flowable.fromCallable { "foo" }
            .subscribeOn(scheduler)
            .observeOn(scheduler)
            .delay(2, TimeUnit.SECONDS)

        val implementation = upstream
            .map { true }
            .startWith(false)
            .subscribeOn(scheduler)
            .observeOn(scheduler)
            .doOnEach { println(it) }
            .test()

        implementation
            .assertNotTerminated()
            .assertNoValues()

        scheduler.advanceTimeBy(1, TimeUnit.SECONDS)

        implementation
            .assertNotTerminated()
            .assertValues(false)

        scheduler.advanceTimeBy(2, TimeUnit.SECONDS)

        implementation
            .assertNotComplete()
            .assertValues(false, true) /* assertion fails*/

        /*
        * Output:
        * 
        * OnNextNotification[false]
        * 
        * java.lang.AssertionError: Value count differs; expected: 2 [false, true] but was: 1 [false] (latch = 1, values = 1, errors = 0, completions = 0)
        *   Expected :2 [false, true] 
        *   Actual   :1 [false] (latch = 1, values = 1, errors = 0, completions = 0)
        */
    }
k
you might want to pass the scheduler to the delay as well.
delay has it's own schedulers:
Copy code
public final Single delay(long time, TimeUnit unit) {
    return delay(time, unit, Schedulers.computation(), false);
}
f
oh my. That's exactly it. But that means that I need to explicitly set the scheduler for all calls to delay?
Thank you
k
yeah :S that means you have to
f
Fair enough. Thank you again!
👍 1