Skip to content

Commit 61c2218

Browse files
add usability tests
1 parent 0566f9c commit 61c2218

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scalaimpl/BlockingObservable.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,9 @@ class BlockingObservable[+T](val asJava: rx.observables.BlockingObservable[_ <:
3636
def toIterable: Iterable[T] = {
3737
asJava.toIterable().asScala
3838
}
39+
40+
def toList: List[T] = {
41+
asJava.toIterable().asScala.toList
42+
}
3943

4044
}

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scalatests/RxScalaDemo.scala

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,93 @@ package rx.lang.scalatests
22

33
import org.scalatest.junit.JUnitSuite
44

5+
import scala.language.postfixOps
56
import rx.lang.scala._
67
import scala.concurrent.duration._
78
import org.junit.{Before, Test, Ignore}
89

910
import org.junit.Assert._
1011

12+
@Ignore // Since this doesn't do automatic testing.
1113
class RxScalaDemo extends JUnitSuite {
12-
13-
@Test def testSwitchNoTiming() {
14-
val o1: Observable[Observable[Int]] = Observable(Observable(1, 2), Observable(3, 4))
15-
val o2: Observable[Int] = o1.switch
1614

15+
def output(s: String): Unit = println(s)
16+
17+
def sleep(ms: Long): Unit = Thread.sleep(ms)
18+
19+
@Test def intervalExample() {
20+
println("hello")
21+
Observable.interval(200 millis).take(5).subscribe((n: Long) => println("n = " + n))
22+
// need to sleep here because otherwise JUnit kills the thread created by interval()
23+
sleep(1200)
24+
}
25+
26+
def msTicks(start: Long, step: Long): Observable[Long] = {
27+
// will be easier once we have Observable.generate method
28+
Observable.interval(step millis) map (_ * step + start)
29+
}
30+
31+
def prefixedTicks(start: Long, step: Long, prefix: String): Observable[String] = {
32+
msTicks(start, step).map(prefix + _)
33+
}
34+
35+
@Test def testTicks() {
36+
prefixedTicks(5000, 500, "t = ").take(5).subscribe(output(_))
37+
sleep(3000)
38+
}
39+
40+
@Test def testSwitch() {
41+
// We do not have ultimate precision: Sometimes, 747 gets through, sometimes not
42+
val o = Observable.interval(1000 millis).map(n => prefixedTicks(0, 249, s"Observable#$n: "))
43+
o.switch.take(16).subscribe(output(_))
44+
sleep(5000)
45+
}
46+
47+
@Test def testSwitchOnObservableOfInt() {
1748
// Correctly rejected with error
1849
// "Cannot prove that Observable[Int] <:< Observable[Observable[U]]"
19-
// val o3 = Observable(1, 2).switch
50+
// val o = Observable(1, 2).switch
51+
}
52+
53+
@Test def testMyOwnSequenceEqual() {
54+
// the sequenceEqual operation can be obtained like this:
2055

21-
println(o2.toBlockingObservable.toIterable.toList)
56+
val first = Observable(10, 11, 12)
57+
val second = Observable(10, 11, 12)
58+
59+
val b1 = (first zip second) map (p => p._1 == p._2) forall (b => b)
60+
61+
val equality = (a: Any, b: Any) => a == b
62+
val b2 = (first zip second) map (p => equality(p._1, p._2)) forall (b => b)
63+
64+
assertTrue(b1.toBlockingObservable.single)
65+
assertTrue(b2.toBlockingObservable.single)
2266
}
2367

24-
@Test def testSequenceEqualIsUnnecessary() {
25-
// the sequenceEqual is unnecessary
68+
@Test def testMyOwnSequenceEqualWithForComprehension() {
69+
// the sequenceEqual operation can be obtained like this:
70+
2671
val first = Observable(10, 11, 12)
2772
val second = Observable(10, 11, 12)
2873

29-
val b1 = (first zip second) map (p => p._1 == p._2)
74+
val booleans = for ((n1, n2) <- (first zip second)) yield (n1 == n2)
3075

31-
val equality = (a: Any, b: Any) => a == b
76+
val b1 = booleans.forall(_ == true) // without `== true`, b1 is assigned the forall function
3277

33-
val b2 = (first zip second) map (p => equality(p._1, p._2))
78+
assertTrue(b1.toBlockingObservable.single)
3479
}
3580

3681
@Test def testStartWithIsUnnecessary() {
3782
val before = Observable(-2, -1, 0)
3883
val source = Observable(1, 2, 3)
39-
before ++ source
40-
}
41-
42-
@Test def intervalExample() {
43-
println("hello")
44-
Observable.interval(200 millis).take(5).subscribe((n: Long) => println("n = " + n))
45-
// need to sleep here because otherwise JUnit kills the thread created by interval()
46-
Thread.sleep(1200)
84+
println((before ++ source).toBlockingObservable.toList)
4785
}
48-
49-
@Test def forComprehensionExample() {
86+
87+
@Test def mergeExample() {
5088
val slowNumbers = Observable.interval(400 millis).take(5).map("slow " + _)
5189
val fastNumbers = Observable.interval(200 millis).take(10).map("fast " + _)
5290

53-
// it would actually make more sense to use merge instead of for comprehension...
54-
val o1 = Observable(slowNumbers, fastNumbers)
55-
val o2 = {for (o <- o1; str <- o) yield str}
56-
57-
o2.subscribe((s: String) => println(s))
58-
59-
Thread.sleep(2300)
91+
// TODO
6092
}
6193

62-
}
94+
}

0 commit comments

Comments
 (0)