Skip to content

Instantly share code, notes, and snippets.

@umair13adil
Last active January 19, 2018 12:32
Show Gist options
  • Select an option

  • Save umair13adil/22676d01663ddb60fd226f86090b52af to your computer and use it in GitHub Desktop.

Select an option

Save umair13adil/22676d01663ddb60fd226f86090b52af to your computer and use it in GitHub Desktop.
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