Skip to content

Commit ed983ee

Browse files
some RxScalaDemo tweaks
1 parent 045d4f9 commit ed983ee

File tree

1 file changed

+27
-21
lines changed
  • language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples

1 file changed

+27
-21
lines changed

language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@ import org.scalatest.junit.JUnitSuite
3232
import rx.lang.scala._
3333
import rx.lang.scala.schedulers._
3434

35-
//@Ignore // Since this doesn't do automatic testing, don't increase build time unnecessarily
35+
/**
36+
* Demo how the different operators can be used. In Eclipse, you can right-click
37+
* a test and choose "Run As" > "Scala JUnit Test".
38+
*
39+
* For each operator added to Observable.java, we add a little usage demo here.
40+
* It does not need to test the functionality (that's already done by the tests in
41+
* RxJava core), but it should demonstrate how it can be used, to make sure that
42+
* the method signature makes sense.
43+
*/
44+
@Ignore // Since this doesn't do automatic testing, don't increase build time unnecessarily
3645
class RxScalaDemo extends JUnitSuite {
3746

3847
@Test def intervalExample() {
@@ -251,30 +260,36 @@ class RxScalaDemo extends JUnitSuite {
251260
}
252261

253262
@Test def combineLatestExample() {
254-
val first_counter = Observable.interval(250 millis)
255-
val second_counter = Observable.interval(550 millis)
256-
val combined_counter = first_counter.combineLatest(second_counter,
263+
val firstCounter = Observable.interval(250 millis)
264+
val secondCounter = Observable.interval(550 millis)
265+
val combinedCounter = firstCounter.combineLatest(secondCounter,
257266
(x: Long, y: Long) => List(x,y)) take 10
258267

259-
combined_counter subscribe {x => println(s"Emitted group: $x")}
268+
combinedCounter subscribe {x => println(s"Emitted group: $x")}
269+
waitFor(combinedCounter)
260270
}
261271

272+
@Test def olympicsExampleWithoutPublish() {
273+
val medals = Olympics.mountainBikeMedals.doOnEach(_ => println("onNext"))
274+
medals.subscribe(println(_)) // triggers an execution of medals Observable
275+
waitFor(medals) // triggers another execution of medals Observable
276+
}
262277

263-
@Test def olympicsExample() {
264-
val medals = Olympics.mountainBikeMedals.publish
265-
medals.subscribe(println(_))
278+
@Test def olympicsExampleWithPublish() {
279+
val medals = Olympics.mountainBikeMedals.doOnEach(_ => println("onNext")).publish
280+
medals.subscribe(println(_)) // triggers an execution of medals Observable
266281
medals.connect
267-
//waitFor(medals)
282+
waitFor(medals) // triggers another execution of medals Observable
268283
}
269284

270285
@Test def exampleWithoutPublish() {
271-
val unshared = List(1 to 4).toObservable
286+
val unshared = Observable.from(1 to 4)
272287
unshared.subscribe(n => println(s"subscriber 1 gets $n"))
273288
unshared.subscribe(n => println(s"subscriber 2 gets $n"))
274289
}
275290

276291
@Test def exampleWithPublish() {
277-
val unshared = List(1 to 4).toObservable
292+
val unshared = Observable.from(1 to 4)
278293
val shared = unshared.publish
279294
shared.subscribe(n => println(s"subscriber 1 gets $n"))
280295
shared.subscribe(n => println(s"subscriber 2 gets $n"))
@@ -403,7 +418,7 @@ class RxScalaDemo extends JUnitSuite {
403418
}
404419

405420
@Test def timestampExample() {
406-
val timestamped = Observable.interval(100 millis).take(3).timestamp.toBlockingObservable
421+
val timestamped = Observable.interval(100 millis).take(6).timestamp.toBlockingObservable
407422
for ((millis, value) <- timestamped if value > 0) {
408423
println(value + " at t = " + millis)
409424
}
@@ -442,15 +457,6 @@ class RxScalaDemo extends JUnitSuite {
442457
val oc3: rx.Notification[_ <: Int] = oc2.asJavaNotification
443458
val oc4: rx.Notification[_ <: Any] = oc2.asJavaNotification
444459
}
445-
446-
@Test def elementAtReplacement() {
447-
assertEquals("b", List("a", "b", "c").toObservable.drop(1).first.toBlockingObservable.single)
448-
}
449-
450-
@Test def elementAtOrDefaultReplacement() {
451-
assertEquals("b", List("a", "b", "c").toObservable.drop(1).firstOrElse("!").toBlockingObservable.single)
452-
assertEquals("!!", List("a", "b", "c").toObservable.drop(10).firstOrElse("!!").toBlockingObservable.single)
453-
}
454460

455461
@Test def takeWhileWithIndexAlternative {
456462
val condition = true

0 commit comments

Comments
 (0)