44
55import java .util .ArrayList ;
66import java .util .List ;
7- import java .util .concurrent .Callable ;
8- import java .util .concurrent .ExecutionException ;
9- import java .util .concurrent .ExecutorService ;
10- import java .util .concurrent .Executors ;
11- import java .util .concurrent .Future ;
7+ import java .util .concurrent .TimeUnit ;
128
13- import org .junit .AfterClass ;
14- import org .junit .BeforeClass ;
159import org .junit .Test ;
1610
1711import rx .Observable ;
1812import rx .observers .TestSubscriber ;
1913
2014public class ObservableCombineUnitTest {
2115
22- private static ExecutorService executor ;
23-
24- @ BeforeClass
25- public static void setupClass () {
26- executor = Executors .newFixedThreadPool (10 );
27- }
28-
29- @ AfterClass
30- public static void tearDownClass () {
31- executor .shutdown ();
32- }
33-
3416 @ Test
3517 public void givenTwoObservables_whenMerged_shouldEmitCombinedResults () {
36- List <String > results = new ArrayList <>();
18+ TestSubscriber <String > testSubscriber = new TestSubscriber <>();
3719
3820 //@formatter:off
3921 Observable .merge (
4022 Observable .from (new String [] {"Hello" , "World" }),
4123 Observable .from (new String []{ "I love" , "RxJava" })
42- ).subscribe (data -> {
43- results .add (data );
44- });
45- //@formatter:on
46-
47- assertThat (results ).isNotEmpty ();
48- assertThat (results .size ()).isEqualTo (4 );
49- assertThat (results ).contains ("Hello" , "World" , "I love" , "RxJava" );
50- }
51-
52- @ Test
53- public void givenAnObservable_whenStartWith_thenPrependEmittedResults () {
54- StringBuffer buffer = new StringBuffer ();
55-
56- //@formatter:off
57- Observable
58- .from (new String [] { "RxJava" , "Observables" })
59- .startWith ("Buzzwords of Reactive Programming" )
60- .subscribe (data -> {
61- buffer .append (data ).append (" " );
62- });
24+ ).subscribe (testSubscriber );
6325 //@formatter:on
6426
65- assertThat ( buffer . toString (). trim ()). isEqualTo ( "Buzzwords of Reactive Programming RxJava Observables " );
27+ testSubscriber . assertValues ( "Hello" , "World" , "I love" , " RxJava" );
6628 }
6729
6830 @ Test
@@ -73,11 +35,7 @@ public void givenTwoObservables_whenZipped_thenReturnCombinedResults() {
7335 Observable .zip (
7436 Observable .from (new String [] { "Simple" , "Moderate" , "Complex" }),
7537 Observable .from (new String [] { "Solutions" , "Success" , "Heirarchy" }),
76- (str1 , str2 ) -> {
77- return str1 + " " + str2 ;
78- }).subscribe (zipped -> {
79- zippedStrings .add (zipped );
80- });
38+ (str1 , str2 ) -> str1 + " " + str2 ).subscribe (zippedStrings ::add );
8139 //formatter:on
8240
8341 assertThat (zippedStrings ).isNotEmpty ();
@@ -87,35 +45,31 @@ public void givenTwoObservables_whenZipped_thenReturnCombinedResults() {
8745
8846 @ Test
8947 public void givenMutipleObservablesOneThrows_whenMerged_thenCombineBeforePropagatingError () {
90- Future <String > f1 = executor .submit (createCallable ("Hello" ));
91- Future <String > f2 = executor .submit (createCallable ("World" ));
92- Future <String > f3 = executor .submit (createCallable (null ));
93- Future <String > f4 = executor .submit (createCallable ("RxJava" ));
94-
9548 TestSubscriber <String > testSubscriber = new TestSubscriber <>();
9649
97- //@formatter:off
9850 Observable .mergeDelayError (
99- Observable .from (f1 ),
100- Observable .from (f2 ),
101- Observable .from (f3 ),
102- Observable .from (f4 )
51+ Observable .from (new String [] { "hello" , "world" }),
52+ Observable .error (new RuntimeException ("Some exception" )),
53+ Observable .from (new String [] { "rxjava" })
10354 ).subscribe (testSubscriber );
104- //@formatter:on
105-
55+
10656 testSubscriber .assertValues ("hello" , "world" , "rxjava" );
107- testSubscriber .assertError (ExecutionException .class );
57+ testSubscriber .assertError (RuntimeException .class ); ;
10858 }
10959
110- private Callable <String > createCallable (final String data ) {
111- return new Callable <String >() {
112- @ Override
113- public String call () throws Exception {
114- if (data == null ) {
115- throw new IllegalArgumentException ("Data should not be null." );
116- }
117- return data .toLowerCase ();
118- }
119- };
60+ @ Test
61+ public void givenAStream_whenZippedWithInterval_shouldDelayStreamEmmission () {
62+ TestSubscriber <String > testSubscriber = new TestSubscriber <>();
63+
64+ Observable <String > data = Observable .just ("one" , "two" , "three" , "four" , "five" );
65+ Observable <Long > interval = Observable .interval (1L , TimeUnit .SECONDS );
66+
67+ Observable
68+ .zip (data , interval , (strData , tick ) -> String .format ("[%d]=%s" , tick , strData ))
69+ .toBlocking ().subscribe (testSubscriber );
70+
71+ testSubscriber .assertCompleted ();
72+ testSubscriber .assertValueCount (5 );
73+ testSubscriber .assertValues ("[0]=one" , "[1]=two" , "[2]=three" , "[3]=four" , "[4]=five" );
12074 }
12175}
0 commit comments