Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/kotlin/rx/lang/kotlin/observables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ public inline fun <T> Observable<T>.subscribeWith( body : FunctionSubscriberModi
modifier.body()
return subscribe(modifier.subscriber)
}

public fun <T> Observable<Observable<T>>.switchOnNext(): Observable<T> = Observable.switchOnNext(this)
41 changes: 38 additions & 3 deletions src/test/kotlin/rx/lang/kotlin/ExtensionTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package rx.lang.kotlin

import rx.Observable
import org.junit.Test
import org.mockito.Mockito.*
import org.mockito.Matchers.*
import org.junit.Assert.*
import rx.Notification
import kotlin.concurrent.thread
import rx.Subscriber
import org.funktionale.partials.*
import rx.*
import rx.schedulers.TestScheduler
import java.util.concurrent.TimeUnit

/**
* This class contains tests using the extension functions provided by the language adaptor.
Expand Down Expand Up @@ -236,6 +236,41 @@ public class ExtensionTests : KotlinTests() {
assertEquals(listOf(3, 6, 9), values[2])
}

@Test
public fun testSwitchOnNext() {
val testScheduler = TestScheduler()
val worker = testScheduler.createWorker()

val observable = observable<Observable<Long>> { s ->
fun at(delay: Long, func : () -> Unit){
worker.schedule({
func()
}, delay, TimeUnit.MILLISECONDS)
}

val first = Observable.interval(5, TimeUnit.MILLISECONDS, testScheduler).take(3)
at(0, { s.onNext(first) })

val second = Observable.interval(5, TimeUnit.MILLISECONDS, testScheduler).take(3)
at(11, { s.onNext(second) })

at(40, { s.onCompleted() })
}

observable.switchOnNext().subscribe(received)

val inOrder = inOrder(a)
testScheduler.advanceTimeTo(10, TimeUnit.MILLISECONDS)
inOrder.verify(a, times(1)).received(0L)
inOrder.verify(a, times(1)).received(1L)

testScheduler.advanceTimeTo(40, TimeUnit.MILLISECONDS)
inOrder.verify(a, times(1)).received(0L)
inOrder.verify(a, times(1)).received(1L)
inOrder.verify(a, times(1)).received(2L)
inOrder.verifyNoMoreInteractions()
}

val funOnSubscribe: (Int, Subscriber<in String>) -> Unit = { counter, subscriber ->
subscriber.onNext("hello_$counter")
subscriber.onCompleted()
Expand Down