https://kotlinlang.org logo
#rx
Title
# rx
f

Fredrik Larsen

09/04/2019, 12:00 PM
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

kioba

09/04/2019, 12:04 PM
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

Fredrik Larsen

09/04/2019, 12:05 PM
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

kioba

09/04/2019, 12:06 PM
yeah :S that means you have to
f

Fredrik Larsen

09/04/2019, 12:06 PM
Fair enough. Thank you again!
👍 1
2 Views