15
15
16
16
import static org .junit .Assert .assertEquals ;
17
17
18
+ import java .util .Arrays ;
18
19
import java .util .HashMap ;
20
+ import java .util .List ;
19
21
import java .util .concurrent .Callable ;
22
+ import java .util .concurrent .CopyOnWriteArrayList ;
20
23
import java .util .concurrent .atomic .AtomicInteger ;
24
+ import java .util .concurrent .atomic .AtomicReference ;
21
25
22
26
import org .junit .Assert ;
23
27
import org .junit .Test ;
24
28
25
29
import io .reactivex .Flowable ;
26
30
import io .reactivex .flowable .FlowableEventStream .Event ;
27
31
import io .reactivex .functions .*;
32
+ import io .reactivex .plugins .RxJavaPlugins ;
28
33
29
34
public class FlowableScanTests {
30
35
36
+
31
37
@ Test
32
38
public void testUnsubscribeScan () {
33
39
@@ -49,81 +55,155 @@ public void accept(HashMap<String, String> v) {
49
55
}
50
56
51
57
@ Test
52
- public void testFlowableScanSeedDoesNotEmitErrorTwiceIfScanFunctionThrows () {
58
+ public void testScanWithSeedDoesNotEmitErrorTwiceIfScanFunctionThrows () {
59
+ final List <Throwable > list = new CopyOnWriteArrayList <Throwable >();
60
+ Consumer <Throwable > errorConsumer = new Consumer <Throwable >() {
61
+ @ Override
62
+ public void accept (Throwable t ) throws Exception {
63
+ list .add (t );
64
+ }};
65
+ try {
66
+ RxJavaPlugins .setErrorHandler (errorConsumer );
67
+ final RuntimeException e = new RuntimeException ();
68
+ final RuntimeException e2 = new RuntimeException ();
69
+ Burst .items (1 ).error (e2 )
70
+ .scan (0 , throwingBiFunction (e ))
71
+ .test ()
72
+ .assertNoValues ()
73
+ .assertError (e );
74
+ assertEquals (Arrays .asList (e2 ), list );
75
+ } finally {
76
+ RxJavaPlugins .reset ();
77
+ }
78
+ }
79
+
80
+ @ Test
81
+ public void testScanWithSeedDoesNotEmitTerminalEventTwiceIfScanFunctionThrows () {
53
82
final RuntimeException e = new RuntimeException ();
54
- Burst .item (1 ).error (e ).scan (0 , new BiFunction <Integer , Integer , Integer >() {
83
+ Burst .item (1 ).create ()
84
+ .scan (0 , throwingBiFunction (e ))
85
+ .test ()
86
+ .assertNoValues ()
87
+ .assertError (e );
88
+ }
89
+
90
+ @ Test
91
+ public void testScanWithSeedDoesNotProcessOnNextAfterTerminalEventIfScanFunctionThrows () {
92
+ final RuntimeException e = new RuntimeException ();
93
+ final AtomicInteger count = new AtomicInteger ();
94
+ Burst .items (1 , 2 ).create ().scan (0 , new BiFunction <Integer , Integer , Integer >() {
55
95
56
96
@ Override
57
97
public Integer apply (Integer n1 , Integer n2 ) throws Exception {
98
+ count .incrementAndGet ();
58
99
throw e ;
59
100
}})
60
101
.test ()
61
102
.assertNoValues ()
62
103
.assertError (e );
104
+ assertEquals (1 , count .get ());
105
+ }
106
+
107
+ @ Test
108
+ public void testScanWithSeedCompletesNormally () {
109
+ Flowable .just (1 ,2 ,3 ).scan (0 , SUM )
110
+ .test ()
111
+ .assertValues (0 , 1 , 3 , 6 )
112
+ .assertComplete ();
63
113
}
64
114
65
115
@ Test
66
- public void testFlowableScanSeedDoesNotEmitTerminalEventTwiceIfScanFunctionThrows () {
116
+ public void testScanWithSeedWhenScanSeedProviderThrows () {
67
117
final RuntimeException e = new RuntimeException ();
68
- Burst .item (1 ).create ().scan (0 , new BiFunction <Integer , Integer , Integer >() {
118
+ Flowable .just (1 ,2 ,3 ).scanWith (throwingCallable (e ),
119
+ SUM )
120
+ .test ()
121
+ .assertError (e )
122
+ .assertNoValues ();
123
+ }
69
124
125
+ @ Test
126
+ public void testScanNoSeed () {
127
+ Flowable .just (1 , 2 , 3 )
128
+ .scan (SUM )
129
+ .test ()
130
+ .assertValues (1 , 3 , 6 )
131
+ .assertComplete ();
132
+ }
133
+
134
+ @ Test
135
+ public void testScanNoSeedDoesNotEmitErrorTwiceIfScanFunctionThrows () {
136
+ final List <Throwable > list = new CopyOnWriteArrayList <Throwable >();
137
+ Consumer <Throwable > errorConsumer = new Consumer <Throwable >() {
70
138
@ Override
71
- public Integer apply (Integer n1 , Integer n2 ) throws Exception {
72
- throw e ;
73
- }})
139
+ public void accept (Throwable t ) throws Exception {
140
+ list .add (t );
141
+ }};
142
+ try {
143
+ RxJavaPlugins .setErrorHandler (errorConsumer );
144
+ final RuntimeException e = new RuntimeException ();
145
+ final RuntimeException e2 = new RuntimeException ();
146
+ Burst .items (1 , 2 ).error (e2 )
147
+ .scan (throwingBiFunction (e ))
148
+ .test ()
149
+ .assertValue (1 )
150
+ .assertError (e );
151
+ assertEquals (Arrays .asList (e2 ), list );
152
+ } finally {
153
+ RxJavaPlugins .reset ();
154
+ }
155
+ }
156
+
157
+ @ Test
158
+ public void testScanNoSeedDoesNotEmitTerminalEventTwiceIfScanFunctionThrows () {
159
+ final RuntimeException e = new RuntimeException ();
160
+ Burst .items (1 , 2 ).create ()
161
+ .scan (throwingBiFunction (e ))
74
162
.test ()
75
- .assertNoValues ( )
163
+ .assertValue ( 1 )
76
164
.assertError (e );
77
165
}
78
166
79
167
@ Test
80
- public void testFlowableScanSeedDoesNotProcessOnNextAfterTerminalEventIfScanFunctionThrows () {
168
+ public void testScanNoSeedDoesNotProcessOnNextAfterTerminalEventIfScanFunctionThrows () {
81
169
final RuntimeException e = new RuntimeException ();
82
170
final AtomicInteger count = new AtomicInteger ();
83
- Burst .items (1 , 2 ).create ().scan (0 , new BiFunction <Integer , Integer , Integer >() {
171
+ Burst .items (1 , 2 , 3 ).create ().scan (new BiFunction <Integer , Integer , Integer >() {
84
172
85
173
@ Override
86
174
public Integer apply (Integer n1 , Integer n2 ) throws Exception {
87
175
count .incrementAndGet ();
88
176
throw e ;
89
177
}})
90
178
.test ()
91
- .assertNoValues ( )
179
+ .assertValue ( 1 )
92
180
.assertError (e );
93
181
assertEquals (1 , count .get ());
94
182
}
95
183
96
- @ Test
97
- public void testFlowableScanSeedCompletesNormally () {
98
- Flowable .just (1 ,2 ,3 ).scan (0 , new BiFunction <Integer , Integer , Integer >() {
99
-
184
+ private static BiFunction <Integer ,Integer , Integer > throwingBiFunction (final RuntimeException e ) {
185
+ return new BiFunction <Integer , Integer , Integer >() {
100
186
@ Override
101
- public Integer apply (Integer t1 , Integer t2 ) throws Exception {
102
- return t1 + t2 ;
103
- }})
104
- .test ()
105
- .assertValues (0 , 1 , 3 , 6 )
106
- .assertComplete ();
187
+ public Integer apply (Integer n1 , Integer n2 ) throws Exception {
188
+ throw e ;
189
+ }
190
+ };
107
191
}
192
+
193
+ private static final BiFunction <Integer , Integer , Integer > SUM = new BiFunction <Integer , Integer , Integer >() {
194
+
195
+ @ Override
196
+ public Integer apply (Integer t1 , Integer t2 ) throws Exception {
197
+ return t1 + t2 ;
198
+ }
199
+ };
108
200
109
- @ Test
110
- public void testFlowableScanSeedWhenScanSeedProviderThrows () {
111
- final RuntimeException e = new RuntimeException ();
112
- Flowable .just (1 ,2 ,3 ).scanWith (new Callable <Integer >() {
201
+ private static Callable <Integer > throwingCallable (final RuntimeException e ) {
202
+ return new Callable <Integer >() {
113
203
@ Override
114
204
public Integer call () throws Exception {
115
205
throw e ;
116
206
}
117
- },
118
- new BiFunction <Integer , Integer , Integer >() {
119
-
120
- @ Override
121
- public Integer apply (Integer t1 , Integer t2 ) throws Exception {
122
- return t1 + t2 ;
123
- }
124
- })
125
- .test ()
126
- .assertError (e )
127
- .assertNoValues ();
207
+ };
128
208
}
129
209
}
0 commit comments