1+ package com .baeldung .rxjava ;
2+
3+ import io .reactivex .BackpressureStrategy ;
4+ import io .reactivex .Flowable ;
5+ import io .reactivex .FlowableOnSubscribe ;
6+ import io .reactivex .Observable ;
7+ import io .reactivex .exceptions .MissingBackpressureException ;
8+ import io .reactivex .exceptions .OnErrorNotImplementedException ;
9+ import io .reactivex .schedulers .Schedulers ;
10+ import io .reactivex .subscribers .TestSubscriber ;
11+ import org .junit .Test ;
12+
13+ import java .util .ArrayList ;
14+ import java .util .List ;
15+ import java .util .stream .Collectors ;
16+ import java .util .stream .IntStream ;
17+
18+ import static org .assertj .core .api .Assertions .assertThat ;
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertNotNull ;
21+
22+ public class FlowableTest {
23+
24+ @ Test public void whenFlowableIsCreated_thenItIsProperlyInitialized () {
25+ Flowable <Integer > integerFlowable = Flowable .just (1 , 2 , 3 , 4 );
26+ assertNotNull (integerFlowable );
27+ }
28+
29+ @ Test public void whenFlowableIsCreatedFromObservable_thenItIsProperlyInitialized () throws InterruptedException {
30+ Observable <Integer > integerObservable = Observable .just (1 , 2 , 3 );
31+ Flowable <Integer > integerFlowable = integerObservable .toFlowable (BackpressureStrategy .BUFFER );
32+ assertNotNull (integerFlowable );
33+
34+ }
35+
36+ @ Test public void whenFlowableIsCreatedFromFlowableOnSubscribe_thenItIsProperlyInitialized () throws InterruptedException {
37+ FlowableOnSubscribe <Integer > flowableOnSubscribe = flowableEmitter -> {
38+ flowableEmitter .onNext (1 );
39+ };
40+ Flowable <Integer > integerFlowable = Flowable .create (flowableOnSubscribe , BackpressureStrategy .BUFFER );
41+ assertNotNull (integerFlowable );
42+ }
43+
44+ @ Test public void givenFlowableWithBufferStrategy_whenSourceEmitsFasterThanConsumerConsumes_thenAllValuesAreBufferedAndReceived () throws InterruptedException {
45+ List testList = IntStream .range (0 , 100000 ).boxed ().collect (Collectors .toList ());
46+ List listToFill = new ArrayList ();
47+
48+ Observable observable = Observable .fromIterable (testList );
49+ observable .toFlowable (BackpressureStrategy .BUFFER ).observeOn (Schedulers .computation ()).subscribe (listToFill ::add );
50+ Thread .sleep (5000 );
51+ assertEquals (testList , listToFill );
52+ }
53+
54+ @ Test public void givenFlowableWithDropStrategy_whenSourceEmitsFasterThanConsumerConsumes_thenNotAllValuesAreReceived () throws InterruptedException {
55+ List testList = IntStream .range (0 , 100000 ).boxed ().collect (Collectors .toList ());
56+ List listToFill = new ArrayList ();
57+
58+ Observable observable = Observable .fromIterable (testList );
59+ observable .toFlowable (BackpressureStrategy .DROP ).observeOn (Schedulers .computation ()).subscribe (listToFill ::add );
60+ Thread .sleep (5000 );
61+ assertThat (listToFill .size () < testList .size ());
62+ assertThat (!listToFill .contains (100000 ));
63+ }
64+
65+ @ Test
66+ public void givenFlowableWithMissingStrategy_whenSourceEmitsFasterThanConsumerConsumes_thenExceptionIsThrown () throws InterruptedException {
67+ Observable observable = Observable .range (1 , 100000 );
68+ TestSubscriber subscriber =observable .toFlowable (BackpressureStrategy .MISSING ).observeOn (Schedulers .computation ()).test ();
69+ subscriber .awaitTerminalEvent ();
70+ subscriber .assertError (MissingBackpressureException .class );
71+ }
72+
73+ @ Test
74+ public void givenFlowableWithErrorStrategy_whenSourceEmitsFasterThanConsumerConsumes_thenExceptionIsThrown () throws InterruptedException {
75+ Observable observable = Observable .range (1 , 100000 );
76+ TestSubscriber subscriber =observable .toFlowable (BackpressureStrategy .ERROR ).observeOn (Schedulers .computation ()).test ();
77+
78+ subscriber .awaitTerminalEvent ();
79+ subscriber .assertError (MissingBackpressureException .class );
80+ }
81+
82+ @ Test
83+ public void givenFlowableWithLatestStrategy_whenSourceEmitsFasterThanConsumerConsumes_thenExceptionIsThrown () throws InterruptedException {
84+ List testList = IntStream .range (0 , 100000 ).boxed ().collect (Collectors .toList ());
85+ List listToFill = new ArrayList ();
86+
87+ Observable observable = Observable .fromIterable (testList );
88+ observable .toFlowable (BackpressureStrategy .LATEST ).observeOn (Schedulers .computation ()).subscribe (listToFill ::add );
89+ Thread .sleep (6000 );
90+ assertThat (listToFill .size () < testList .size ());
91+ assertThat (listToFill .contains (100000 ));
92+ }
93+
94+ }
0 commit comments