java - Odd TestObserver behaviour when subscribing to a Subject -


consider following rxjava 2 snippet in kotlin:

// 1. create subject val subject = publishsubject.create<int>()  // 2. observable val observable = subject.subscribeon(schedulers.io())  // 3. subscribe val observer = observable.test()  // 4. trigger next subject.onnext(42)  // 5. await observer.awaitcount(1)  // 6. assert value observer.assertvalue(42) 

from understanding, observer should able receive 42 after waiting on statement 5 , assertion on statement 6 should succeed.

however, happens is: 5 blocks until times out because no value received , assertion on 6 fails.

also, if put breakpoint on 3 , resume execution after pauses, work. looks threading problem.

i'm missing here. correct way consume hot observable?

by unsing subscribeon, put actual subscription subject on thread may take bit longer subject.onnext(42) still finds unsubscribed subject.

apart fact using subscribeon on publishsubject has no practical use, await subscription looping bit:

while (!subject.hasobservers()) { thread.sleep(1); } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -