Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RxJava Merge Lists Test in Kotlin
import io.reactivex.Observable
import io.reactivex.schedulers.TestScheduler
import org.junit.Test
import java.util.concurrent.TimeUnit
class MergeUnitTest {
@Test
@Throws(Exception::class)
fun mergeSimple() {
val scheduler = TestScheduler()
val observable = Observable.fromIterable(listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "", ""))
val observable2 = Observable.fromIterable(listOf("b", "", "c", "e", "f", "a", "d"))
observable2.mergeWith(observable)
.doOnNext {
print("mergeSimple: ${it} \n")
}
.subscribe()
scheduler.advanceTimeBy(200, TimeUnit.SECONDS)
}//Result: Will emit all item in their order
@Test
@Throws(Exception::class)
fun mergeSorted() {
val scheduler = TestScheduler()
val observable = Observable.fromIterable(listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "", ""))
val observable2 = Observable.fromIterable(listOf("b", "", "c", "e", "f", "a", "d"))
observable2.mergeWith(observable)
.sorted()
.filter { it.isNotBlank() }
.doOnNext {
print("mergeSorted: ${it} \n")
}
.subscribe()
scheduler.advanceTimeBy(200, TimeUnit.SECONDS)
}//Result: Will emit all item in sorted order with filtering empty strings
@Test
@Throws(Exception::class)
fun mergeDistinctSorted() {
val scheduler = TestScheduler()
val observable = Observable.fromIterable(listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "", ""))
val observable2 = Observable.fromIterable(listOf("b", "", "c", "e", "f", "a", "d"))
observable2.mergeWith(observable)
.sorted()
.distinct()
.filter { it.isNotBlank() }
.doOnNext {
print("mergeDistinctSorted: ${it} \n")
}
.subscribe()
scheduler.advanceTimeBy(200, TimeUnit.SECONDS)
}//Result: Will emit all distinct item in sorted order with filtering empty strings
@Test
@Throws(Exception::class)
fun zip() {
val data1 = Observable.fromIterable(listOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "", ""))
val data2 = Observable.fromIterable(listOf("b", "", "c", "e", "f", "a", "d"))
val iter = Arrays.asList(data1, data2)
Observable.zip<String, Array<Any>>(iter) { args1 -> args1 }
.subscribe({
it.forEach {
print("${it} ")
}
})
}//Result: Will merge both lists and emit all item in their order
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.