Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableGenerateTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import static org.junit.Assert.assertEquals;
17
18 import java.util.List;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.*;
25 import io.reactivex.rxjava3.internal.functions.Functions;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.subscribers.TestSubscriber;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class FlowableGenerateTest extends RxJavaTest {
31
32 @Test
33 public void statefulBiconsumer() {
34 Flowable.generate(new Supplier<Object>() {
35 @Override
36 public Object get() throws Exception {
37 return 10;
38 }
39 }, new BiConsumer<Object, Emitter<Object>>() {
40 @Override
41 public void accept(Object s, Emitter<Object> e) throws Exception {
42 e.onNext(s);
43 }
44 }, new Consumer<Object>() {
45 @Override
46 public void accept(Object d) throws Exception {
47
48 }
49 })
50 .take(5)
51 .test()
52 .assertResult(10, 10, 10, 10, 10);
53 }
54
55 @Test
56 public void stateSupplierThrows() {
57 Flowable.generate(new Supplier<Object>() {
58 @Override
59 public Object get() throws Exception {
60 throw new TestException();
61 }
62 }, new BiConsumer<Object, Emitter<Object>>() {
63 @Override
64 public void accept(Object s, Emitter<Object> e) throws Exception {
65 e.onNext(s);
66 }
67 }, Functions.emptyConsumer())
68 .test()
69 .assertFailure(TestException.class);
70 }
71
72 @Test
73 public void generatorThrows() {
74 Flowable.generate(new Supplier<Object>() {
75 @Override
76 public Object get() throws Exception {
77 return 1;
78 }
79 }, new BiConsumer<Object, Emitter<Object>>() {
80 @Override
81 public void accept(Object s, Emitter<Object> e) throws Exception {
82 throw new TestException();
83 }
84 }, Functions.emptyConsumer())
85 .test()
86 .assertFailure(TestException.class);
87 }
88
89 @Test
90 public void disposerThrows() {
91 List<Throwable> errors = TestHelper.trackPluginErrors();
92 try {
93 Flowable.generate(new Supplier<Object>() {
94 @Override
95 public Object get() throws Exception {
96 return 1;
97 }
98 }, new BiConsumer<Object, Emitter<Object>>() {
99 @Override
100 public void accept(Object s, Emitter<Object> e) throws Exception {
101 e.onComplete();
102 }
103 }, new Consumer<Object>() {
104 @Override
105 public void accept(Object d) throws Exception {
106 throw new TestException();
107 }
108 })
109 .test()
110 .assertResult();
111
112 TestHelper.assertUndeliverable(errors, 0, TestException.class);
113 } finally {
114 RxJavaPlugins.reset();
115 }
116 }
117
118 @Test
119 public void dispose() {
120 TestHelper.checkDisposed(Flowable.generate(new Supplier<Object>() {
121 @Override
122 public Object get() throws Exception {
123 return 1;
124 }
125 }, new BiConsumer<Object, Emitter<Object>>() {
126 @Override
127 public void accept(Object s, Emitter<Object> e) throws Exception {
128 e.onComplete();
129 }
130 }, Functions.emptyConsumer()));
131 }
132
133 @Test
134 public void nullError() {
135 final int[] call = { 0 };
136 Flowable.generate(Functions.justSupplier(1),
137 new BiConsumer<Integer, Emitter<Object>>() {
138 @Override
139 public void accept(Integer s, Emitter<Object> e) throws Exception {
140 try {
141 e.onError(null);
142 } catch (NullPointerException ex) {
143 call[0]++;
144 }
145 }
146 }, Functions.emptyConsumer())
147 .test()
148 .assertFailure(NullPointerException.class);
149
150 assertEquals(0, call[0]);
151 }
152
153 @Test
154 public void badRequest() {
155 TestHelper.assertBadRequestReported(Flowable.generate(new Supplier<Object>() {
156 @Override
157 public Object get() throws Exception {
158 return 1;
159 }
160 }, new BiConsumer<Object, Emitter<Object>>() {
161 @Override
162 public void accept(Object s, Emitter<Object> e) throws Exception {
163 e.onComplete();
164 }
165 }, Functions.emptyConsumer()));
166 }
167
168 @Test
169 public void rebatchAndTake() {
170 Flowable.generate(new Supplier<Object>() {
171 @Override
172 public Object get() throws Exception {
173 return 1;
174 }
175 }, new BiConsumer<Object, Emitter<Object>>() {
176 @Override
177 public void accept(Object s, Emitter<Object> e) throws Exception {
178 e.onNext(1);
179 }
180 }, Functions.emptyConsumer())
181 .rebatchRequests(1)
182 .take(5)
183 .test()
184 .assertResult(1, 1, 1, 1, 1);
185 }
186
187 @Test
188 public void backpressure() {
189 Flowable.generate(new Supplier<Object>() {
190 @Override
191 public Object get() throws Exception {
192 return 1;
193 }
194 }, new BiConsumer<Object, Emitter<Object>>() {
195 @Override
196 public void accept(Object s, Emitter<Object> e) throws Exception {
197 e.onNext(1);
198 }
199 }, Functions.emptyConsumer())
200 .rebatchRequests(1)
201 .to(TestHelper.<Object>testSubscriber(5L))
202 .assertSubscribed()
203 .assertValues(1, 1, 1, 1, 1)
204 .assertNoErrors()
205 .assertNotComplete();
206 }
207
208 @Test
209 public void requestRace() {
210 Flowable<Object> source = Flowable.generate(new Supplier<Object>() {
211 @Override
212 public Object get() throws Exception {
213 return 1;
214 }
215 }, new BiConsumer<Object, Emitter<Object>>() {
216 @Override
217 public void accept(Object s, Emitter<Object> e) throws Exception {
218 e.onNext(1);
219 }
220 }, Functions.emptyConsumer());
221
222 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
223 final TestSubscriber<Object> ts = source.test(0L);
224
225 Runnable r = new Runnable() {
226 @Override
227 public void run() {
228 for (int j = 0; j < 500; j++) {
229 ts.request(1);
230 }
231 }
232 };
233
234 TestHelper.race(r, r);
235
236 ts.assertValueCount(1000);
237 }
238 }
239
240 @Test
241 public void multipleOnNext() {
242 Flowable.generate(new Consumer<Emitter<Object>>() {
243 @Override
244 public void accept(Emitter<Object> e) throws Exception {
245 e.onNext(1);
246 e.onNext(2);
247 }
248 })
249 .test(1)
250 .assertFailure(IllegalStateException.class, 1);
251 }
252
253 @Test
254 public void multipleOnError() {
255 List<Throwable> errors = TestHelper.trackPluginErrors();
256 try {
257 Flowable.generate(new Consumer<Emitter<Object>>() {
258 @Override
259 public void accept(Emitter<Object> e) throws Exception {
260 e.onError(new TestException("First"));
261 e.onError(new TestException("Second"));
262 }
263 })
264 .test(1)
265 .assertFailure(TestException.class);
266
267 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
268 } finally {
269 RxJavaPlugins.reset();
270 }
271 }
272
273 @Test
274 public void multipleOnComplete() {
275 Flowable.generate(new Consumer<Emitter<Object>>() {
276 @Override
277 public void accept(Emitter<Object> e) throws Exception {
278 e.onComplete();
279 e.onComplete();
280 }
281 })
282 .test(1)
283 .assertResult();
284 }
285
286 @Test
287 public void onNextAfterOnComplete() {
288 Flowable.generate(new Consumer<Emitter<Object>>() {
289 @Override
290 public void accept(Emitter<Object> e) throws Exception {
291 e.onComplete();
292 e.onNext(1);
293 }
294 })
295 .test()
296 .assertResult();
297 }
298 }