In my test I have the following code:
/* two users written to realm here, verified */
TestSubscriber<RealmResults<User>> testSubscriber = new TestSubscriber<>();
usersByQuery(getRealm().where(User.class))
.filter(RealmResults::isLoaded)
.subscribe(testSubscriber);
/*
.subscribe(users1 -> {
assertTrue("Users are not two, found instead: " + users1.size(), users1.size() == 2);
Log.d("Another test", "" + users1.size() + " users on thread: " + Thread.currentThread().getName());
});
*/
try {
Log.d("Test", "Sleeping on " + Thread.currentThread().getName() + "\n");
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
testSubscriber.assertValueCount(1);
The test will fail with:
java.lang.AssertionError: Number of onNext events differ; expected: 1, actual: 0
If I replace the testSubscriber with the commented subscribe and remove the test subscriber assertion, it will pass and output:
D/Test: Sleeping on mainD/Another test: 2 users on thread: main
I do not understand why the TestSubscribers OnNext
will not be called when the other one will. I'd like to use the test subscriber so I can replace the Thread.sleep
block with an awaitValueCount
.