|
16 | 16 | package rx.lang.scala.examples
|
17 | 17 |
|
18 | 18 | import java.io.IOException
|
| 19 | +import java.util.concurrent.CountDownLatch |
| 20 | +import java.util.concurrent.TimeUnit |
19 | 21 |
|
20 | 22 | import scala.concurrent.duration.Duration
|
21 | 23 | import scala.concurrent.duration.DurationInt
|
@@ -142,6 +144,12 @@ class RxScalaDemo extends JUnitSuite {
|
142 | 144 | o.buffer(5).subscribe((l: Seq[Int]) => println(l.mkString("[", ", ", "]")))
|
143 | 145 | }
|
144 | 146 |
|
| 147 | + @Test def bufferExample() { |
| 148 | + val o = Observable.from(1 to 18).zip(Observable.interval(100 millis)).map(_._1) |
| 149 | + val boundary = Observable.interval(500 millis) |
| 150 | + o.buffer(boundary).toBlockingObservable.foreach((l: Seq[Int]) => println(l.mkString("[", ", ", "]"))) |
| 151 | + } |
| 152 | + |
145 | 153 | @Test def windowExample() {
|
146 | 154 | (for ((o, i) <- Observable.from(1 to 18).window(5).zipWithIndex; n <- o)
|
147 | 155 | yield s"Observable#$i emits $n"
|
@@ -682,6 +690,21 @@ class RxScalaDemo extends JUnitSuite {
|
682 | 690 | println(result)
|
683 | 691 | }
|
684 | 692 |
|
| 693 | + @Test def delayExample3(): Unit = { |
| 694 | + val o = List(100, 500, 200).toObservable.delay( |
| 695 | + (i: Int) => Observable.items(i).delay(i millis) |
| 696 | + ) |
| 697 | + o.toBlockingObservable.foreach(println(_)) |
| 698 | + } |
| 699 | + |
| 700 | + @Test def delayExample4(): Unit = { |
| 701 | + val o = List(100, 500, 200).toObservable.delay( |
| 702 | + () => Observable.interval(500 millis).take(1), |
| 703 | + (i: Int) => Observable.items(i).delay(i millis) |
| 704 | + ) |
| 705 | + o.toBlockingObservable.foreach(println(_)) |
| 706 | + } |
| 707 | + |
685 | 708 | @Test def delaySubscriptionExample(): Unit = {
|
686 | 709 | val o = List(100L, 200L, 300L).toObservable.delaySubscription(2 seconds)
|
687 | 710 | val result = o.toBlockingObservable.toList
|
@@ -844,4 +867,96 @@ class RxScalaDemo extends JUnitSuite {
|
844 | 867 | val o = List(1, 2).toObservable :+ 3 :+ 4
|
845 | 868 | assertEquals(List(1, 2, 3, 4), o.toBlockingObservable.toList)
|
846 | 869 | }
|
| 870 | + |
| 871 | + @Test def sequenceEqualExampe(): Unit = { |
| 872 | + val o1 = List(1, 2, 3).toObservable |
| 873 | + val o2 = List(1, 2, 3).toObservable |
| 874 | + val o3 = List(1, 2).toObservable |
| 875 | + val o4 = List(1.0, 2.0, 3.0).toObservable |
| 876 | + assertTrue(o1.sequenceEqual(o2).toBlockingObservable.single) |
| 877 | + assertFalse(o1.sequenceEqual(o3).toBlockingObservable.single) |
| 878 | + assertTrue(o1.sequenceEqual(o4).toBlockingObservable.single) |
| 879 | + } |
| 880 | + |
| 881 | + @Test def takeExample(): Unit = { |
| 882 | + val o = (1 to 20).toObservable |
| 883 | + .zip(Observable.interval(300 millis)) |
| 884 | + .map(_._1) |
| 885 | + .take(2 seconds) |
| 886 | + println(o.toBlockingObservable.toList) |
| 887 | + } |
| 888 | + |
| 889 | + @Test def takeRightExample(): Unit = { |
| 890 | + val o = (1 to 6).toObservable.takeRight(3) |
| 891 | + assertEquals(List(4, 5, 6), o.toBlockingObservable.toList) |
| 892 | + } |
| 893 | + |
| 894 | + @Test def takeRightExample2(): Unit = { |
| 895 | + val o = (1 to 10).toObservable |
| 896 | + .zip(Observable.interval(100 millis)) |
| 897 | + .map(_._1) |
| 898 | + .takeRight(300 millis) |
| 899 | + println(o.toBlockingObservable.toList) |
| 900 | + } |
| 901 | + |
| 902 | + @Test def takeRightExample3(): Unit = { |
| 903 | + val o = (1 to 10).toObservable |
| 904 | + .zip(Observable.interval(100 millis)) |
| 905 | + .map(_._1) |
| 906 | + .takeRight(2, 300 millis) |
| 907 | + println(o.toBlockingObservable.toList) |
| 908 | + } |
| 909 | + |
| 910 | + @Test def timeIntervalExample(): Unit = { |
| 911 | + val o = (1 to 10).toObservable |
| 912 | + .zip(Observable.interval(100 millis)) |
| 913 | + .map(_._1) |
| 914 | + .timeInterval |
| 915 | + println(o.toBlockingObservable.toList) |
| 916 | + } |
| 917 | + |
| 918 | + @Test def schedulerExample1(): Unit = { |
| 919 | + val latch = new CountDownLatch(1) |
| 920 | + val worker = IOScheduler().createWorker |
| 921 | + worker.schedule { |
| 922 | + println("Hello from Scheduler") |
| 923 | + latch.countDown() |
| 924 | + } |
| 925 | + latch.await(5, TimeUnit.SECONDS) |
| 926 | + } |
| 927 | + |
| 928 | + @Test def schedulerExample2(): Unit = { |
| 929 | + val latch = new CountDownLatch(1) |
| 930 | + val worker = IOScheduler().createWorker |
| 931 | + worker.schedule(1 seconds) { |
| 932 | + println("Hello from Scheduler after 1 second") |
| 933 | + latch.countDown() |
| 934 | + } |
| 935 | + latch.await(5, TimeUnit.SECONDS) |
| 936 | + } |
| 937 | + |
| 938 | + @Test def schedulerExample3(): Unit = { |
| 939 | + val worker = IOScheduler().createWorker |
| 940 | + var no = 1 |
| 941 | + val subscription = worker.schedulePeriodically(initialDelay = 1 seconds, period = 100 millis) { |
| 942 | + println(s"Hello(${no}) from Scheduler") |
| 943 | + no += 1 |
| 944 | + } |
| 945 | + TimeUnit.SECONDS.sleep(2) |
| 946 | + subscription.unsubscribe() |
| 947 | + } |
| 948 | + |
| 949 | + @Test def schedulerExample4(): Unit = { |
| 950 | + val worker = IOScheduler().createWorker |
| 951 | + var no = 1 |
| 952 | + def hello: Unit = { |
| 953 | + println(s"Hello(${no}) from Scheduler") |
| 954 | + no += 1 |
| 955 | + worker.schedule(100 millis)(hello) |
| 956 | + } |
| 957 | + val subscription = worker.schedule(1 seconds)(hello) |
| 958 | + TimeUnit.SECONDS.sleep(2) |
| 959 | + subscription.unsubscribe() |
| 960 | + } |
| 961 | + |
847 | 962 | }
|
0 commit comments