@@ -24,6 +24,8 @@ import org.mockito.Mockito.*
24
24
import rx.Notification
25
25
import rx.Observable
26
26
import rx.Subscriber
27
+ import rx.schedulers.TestScheduler
28
+ import java.util.concurrent.TimeUnit
27
29
import kotlin.concurrent.thread
28
30
29
31
/* *
@@ -236,6 +238,41 @@ public class ExtensionTests : KotlinTests() {
236
238
assertEquals(listOf (3 , 6 , 9 ), values[2 ])
237
239
}
238
240
241
+ @Test
242
+ public fun testSwitchOnNext () {
243
+ val testScheduler = TestScheduler ()
244
+ val worker = testScheduler.createWorker()
245
+
246
+ val observable = observable<Observable <Long >> { s ->
247
+ fun at (delay : Long , func : () -> Unit ){
248
+ worker.schedule({
249
+ func()
250
+ }, delay, TimeUnit .MILLISECONDS )
251
+ }
252
+
253
+ val first = Observable .interval(5 , TimeUnit .MILLISECONDS , testScheduler).take(3 )
254
+ at(0 , { s.onNext(first) })
255
+
256
+ val second = Observable .interval(5 , TimeUnit .MILLISECONDS , testScheduler).take(3 )
257
+ at(11 , { s.onNext(second) })
258
+
259
+ at(40 , { s.onCompleted() })
260
+ }
261
+
262
+ observable.switchOnNext().subscribe(received())
263
+
264
+ val inOrder = inOrder(a)
265
+ testScheduler.advanceTimeTo(10 , TimeUnit .MILLISECONDS )
266
+ inOrder.verify(a, times(1 )).received(0L )
267
+ inOrder.verify(a, times(1 )).received(1L )
268
+
269
+ testScheduler.advanceTimeTo(40 , TimeUnit .MILLISECONDS )
270
+ inOrder.verify(a, times(1 )).received(0L )
271
+ inOrder.verify(a, times(1 )).received(1L )
272
+ inOrder.verify(a, times(1 )).received(2L )
273
+ inOrder.verifyNoMoreInteractions()
274
+ }
275
+
239
276
val funOnSubscribe: (Int , Subscriber <in String >) -> Unit = { counter, subscriber ->
240
277
subscriber.onNext(" hello_$counter " )
241
278
subscriber.onCompleted()
0 commit comments