Skip to content

Commit 045d4f9

Browse files
tweak RxScalaDemo (and Olympics example)
1 parent a1f7c02 commit 045d4f9

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ object Olympics {
2222
case class Medal(val year: Int, val games: String, val discipline: String, val medal: String, val athlete: String, val country: String)
2323

2424
def mountainBikeMedals: Observable[Medal] = Observable.items(
25+
duration(100 millis), // a short delay because medals are only awarded some time after the Games began
2526
Observable.items(
2627
Medal(1996, "Atlanta 1996", "cross-country men", "Gold", "Bart BRENTJENS", "Netherlands"),
2728
Medal(1996, "Atlanta 1996", "cross-country women", "Gold", "Paola PEZZO", "Italy"),
@@ -69,18 +70,33 @@ object Olympics {
6970
).concat
7071

7172
// speed it up :D
72-
val fourYears = 4000.millis
73+
val oneYear = 1000.millis
7374

74-
val neverUsedDummyMedal = Medal(3333, "?", "?", "?", "?", "?")
75+
//val neverUsedDummyMedal = Medal(3333, "?", "?", "?", "?", "?")
7576

76-
def fourYearsEmpty: Observable[Medal] = {
77+
/** runs an infinite loop, and returns Bottom type (Nothing) */
78+
def getNothing: Nothing = {
79+
println("You shouldn't have called this method ;-)")
80+
getNothing
81+
}
82+
83+
/** returns an Observable which emits no elements and completes after a duration of d */
84+
def duration(d: Duration): Observable[Nothing] = Observable.interval(d).take(1).filter(_ => false).map(_ => getNothing)
85+
86+
def fourYearsEmpty: Observable[Medal] = duration(4*oneYear)
87+
88+
def yearTicks: Observable[Int] =
89+
(Observable.from(1996 to 2014) zip (Observable.items(-1) ++ Observable.interval(oneYear))).map(_._1)
90+
91+
/*
92+
def fourYearsEmptyOld: Observable[Medal] = {
7793
// TODO this should return an observable which emits nothing during fourYears and then completes
7894
// Because of https://github.com/Netflix/RxJava/issues/388, we get non-terminating tests
7995
// And this https://github.com/Netflix/RxJava/pull/289#issuecomment-24738668 also causes problems
8096
// So we don't use this:
81-
// Observable.interval(fourYears).take(1).map(i => neverUsedDummyMedal).filter(m => false)
97+
Observable.interval(fourYears).take(1).map(i => neverUsedDummyMedal).filter(m => false)
8298
// But we just return empty, which completes immediately
83-
Observable.empty
84-
}
99+
// Observable.empty
100+
}*/
85101

86102
}

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ 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+
//@Ignore // Since this doesn't do automatic testing, don't increase build time unnecessarily
3636
class RxScalaDemo extends JUnitSuite {
3737

3838
@Test def intervalExample() {
@@ -78,13 +78,9 @@ class RxScalaDemo extends JUnitSuite {
7878
val first = Observable.from(List(10, 11, 12))
7979
val second = Observable.from(List(10, 11, 12))
8080

81-
val b1 = (first zip second) map (p => p._1 == p._2) forall (b => b)
81+
val b = (first zip second) forall { case (a, b) => a == b }
8282

83-
val equality = (a: Any, b: Any) => a == b
84-
val b2 = (first zip second) map (p => equality(p._1, p._2)) forall (b => b)
85-
86-
assertTrue(b1.toBlockingObservable.single)
87-
assertTrue(b2.toBlockingObservable.single)
83+
assertTrue(b.toBlockingObservable.single)
8884
}
8985

9086
@Test def testObservableComparisonWithForComprehension() {
@@ -93,7 +89,7 @@ class RxScalaDemo extends JUnitSuite {
9389

9490
val booleans = for ((n1, n2) <- (first zip second)) yield (n1 == n2)
9591

96-
val b1 = booleans.forall(_ == true) // without `== true`, b1 is assigned the forall function
92+
val b1 = booleans.forall(identity)
9793

9894
assertTrue(b1.toBlockingObservable.single)
9995
}
@@ -216,18 +212,21 @@ class RxScalaDemo extends JUnitSuite {
216212
}).flatten.toBlockingObservable.foreach(println(_))
217213
}
218214

219-
@Ignore // TODO something's bad here
220215
@Test def timingTest1() {
221216
val numbersByModulo3 = Observable.interval(1000 millis).take(9).groupBy(_ % 3)
222217

223218
val t0 = System.currentTimeMillis
224219

225220
(for ((modulo, numbers) <- numbersByModulo3) yield {
226221
println("Observable for modulo" + modulo + " started at t = " + (System.currentTimeMillis - t0))
227-
numbers.take(1) // <- TODO very unexpected
228-
//numbers
222+
numbers.map(n => s"${n} is in the modulo-$modulo group")
229223
}).flatten.toBlockingObservable.foreach(println(_))
230224
}
225+
226+
@Test def testOlympicYearTicks() {
227+
Olympics.yearTicks.subscribe(println(_))
228+
waitFor(Olympics.yearTicks)
229+
}
231230

232231
@Test def groupByExample() {
233232
val medalsByCountry = Olympics.mountainBikeMedals.groupBy(medal => medal.country)
@@ -238,8 +237,10 @@ class RxScalaDemo extends JUnitSuite {
238237
firstMedalOfEachCountry.subscribe(medal => {
239238
println(s"${medal.country} wins its first medal in ${medal.year}")
240239
})
240+
241+
Olympics.yearTicks.subscribe(year => println(s"\nYear $year starts."))
241242

242-
waitFor(firstMedalOfEachCountry)
243+
waitFor(Olympics.yearTicks)
243244
}
244245

245246
@Test def groupByUntilExample() {
@@ -469,7 +470,10 @@ class RxScalaDemo extends JUnitSuite {
469470

470471
def output(s: String): Unit = println(s)
471472

472-
// blocks until obs has completed
473+
/** Subscribes to obs and waits until obs has completed. Note that if you subscribe to
474+
* obs yourself and also call waitFor(obs), all side-effects of subscribing to obs
475+
* will happen twice.
476+
*/
473477
def waitFor[T](obs: Observable[T]): Unit = {
474478
obs.toBlockingObservable.toIterable.last
475479
}

0 commit comments

Comments
 (0)