Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowablePublishFunctionTest.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.*;
17
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.atomic.AtomicBoolean;
20 import java.util.concurrent.atomic.AtomicInteger;
21
22 import org.junit.*;
23 import org.reactivestreams.*;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.exceptions.*;
27 import io.reactivex.rxjava3.internal.functions.Functions;
28 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
29 import io.reactivex.rxjava3.processors.PublishProcessor;
30 import io.reactivex.rxjava3.schedulers.Schedulers;
31 import io.reactivex.rxjava3.subscribers.TestSubscriber;
32 import io.reactivex.rxjava3.testsupport.*;
33
34 public class FlowablePublishFunctionTest extends RxJavaTest {
35 @Test
36 public void concatTakeFirstLastCompletes() {
37 TestSubscriber<Integer> ts = new TestSubscriber<>();
38
39 Flowable.range(1, 3)
40 .publish(f -> Flowable.concat(f.take(5), f.takeLast(5)))
41 .subscribe(ts);
42
43 ts.assertValues(1, 2, 3);
44 ts.assertNoErrors();
45 ts.assertComplete();
46 }
47
48 @Test
49 public void concatTakeFirstLastBackpressureCompletes() {
50 TestSubscriber<Integer> ts = TestSubscriber.create(0L);
51
52 Flowable.range(1, 6)
53 .publish(f -> Flowable.concat(f.take(5), f.takeLast(5)))
54 .subscribe(ts);
55
56 ts.assertNoValues();
57 ts.assertNoErrors();
58 ts.assertNotComplete();
59
60 ts.request(1); // make sure take() doesn't go unbounded
61 ts.request(4);
62
63 ts.assertValues(1, 2, 3, 4, 5);
64 ts.assertNoErrors();
65 ts.assertNotComplete();
66
67 ts.request(5);
68
69 ts.assertValues(1, 2, 3, 4, 5, 6);
70 ts.assertNoErrors();
71 ts.assertComplete();
72 }
73
74 @Test
75 public void canBeCancelled() {
76 TestSubscriber<Integer> ts = TestSubscriber.create();
77
78 PublishProcessor<Integer> pp = PublishProcessor.create();
79
80 pp.publish(f -> Flowable.concat(f.take(5), f.takeLast(5))).subscribe(ts);
81
82 pp.onNext(1);
83 pp.onNext(2);
84
85 ts.assertValues(1, 2);
86 ts.assertNoErrors();
87 ts.assertNotComplete();
88
89 ts.cancel();
90
91 Assert.assertFalse("Source has subscribers?", pp.hasSubscribers());
92 }
93
94 @Test
95 public void invalidPrefetch() {
96 try {
97 Flowable.<Integer>never().publish(Functions.identity(), -99);
98 fail("Didn't throw IllegalArgumentException");
99 } catch (IllegalArgumentException ex) {
100 Assert.assertEquals("prefetch > 0 required but it was -99", ex.getMessage());
101 }
102 }
103
104 @Test
105 public void takeCompletes() {
106 TestSubscriber<Integer> ts = TestSubscriber.create();
107
108 PublishProcessor<Integer> pp = PublishProcessor.create();
109
110 pp.publish(f -> f.take(1)).subscribe(ts);
111
112 pp.onNext(1);
113
114 ts.assertValues(1);
115 ts.assertNoErrors();
116 ts.assertComplete();
117
118 Assert.assertFalse("Source has subscribers?", pp.hasSubscribers());
119
120 }
121
122 @Test
123 public void oneStartOnly() {
124
125 final AtomicInteger startCount = new AtomicInteger();
126
127 TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {
128 @Override
129 public void onStart() {
130 startCount.incrementAndGet();
131 }
132 };
133
134 PublishProcessor<Integer> pp = PublishProcessor.create();
135
136 pp.publish(f -> f.take(1)).subscribe(ts);
137
138 Assert.assertEquals(1, startCount.get());
139 }
140
141 @Test
142 public void takeCompletesUnsafe() {
143 TestSubscriber<Integer> ts = TestSubscriber.create();
144
145 PublishProcessor<Integer> pp = PublishProcessor.create();
146
147 pp.publish(f -> f.take(1)).subscribe(ts);
148
149 pp.onNext(1);
150
151 ts.assertValues(1);
152 ts.assertNoErrors();
153 ts.assertComplete();
154
155 Assert.assertFalse("Source has subscribers?", pp.hasSubscribers());
156 }
157
158 @Test
159 public void directCompletesUnsafe() {
160 TestSubscriber<Integer> ts = TestSubscriber.create();
161
162 PublishProcessor<Integer> pp = PublishProcessor.create();
163
164 pp.publish(Functions.identity()).subscribe(ts);
165
166 pp.onNext(1);
167 pp.onComplete();
168
169 ts.assertValues(1);
170 ts.assertNoErrors();
171 ts.assertComplete();
172
173 Assert.assertFalse("Source has subscribers?", pp.hasSubscribers());
174 }
175
176 @Test
177 public void overflowMissingBackpressureException() {
178 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0);
179
180 PublishProcessor<Integer> pp = PublishProcessor.create();
181
182 pp.publish(Functions.identity()).subscribe(ts);
183
184 for (int i = 0; i < Flowable.bufferSize() * 2; i++) {
185 pp.onNext(i);
186 }
187
188 ts.assertNoValues();
189 ts.assertError(MissingBackpressureException.class);
190 ts.assertNotComplete();
191
192 Assert.assertEquals("Could not emit value due to lack of requests",
193 ts.errors().get(0).getMessage());
194 Assert.assertFalse("Source has subscribers?", pp.hasSubscribers());
195 }
196
197 @Test
198 public void overflowMissingBackpressureExceptionDelayed() {
199 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0);
200
201 PublishProcessor<Integer> pp = PublishProcessor.create();
202
203 new FlowablePublishMulticast<>(pp, Functions.identity(), Flowable.bufferSize(), true).subscribe(ts);
204
205 for (int i = 0; i < Flowable.bufferSize() * 2; i++) {
206 pp.onNext(i);
207 }
208
209 ts.request(Flowable.bufferSize());
210
211 ts.assertValueCount(Flowable.bufferSize());
212 ts.assertError(MissingBackpressureException.class);
213 ts.assertNotComplete();
214
215 Assert.assertEquals("Could not emit value due to lack of requests", ts.errors().get(0).getMessage());
216 Assert.assertFalse("Source has subscribers?", pp.hasSubscribers());
217 }
218
219 @Test
220 public void emptyIdentityMapped() {
221 Flowable.empty()
222 .publish(Functions.identity())
223 .test()
224 .assertResult();
225 }
226
227 @Test
228 public void independentlyMapped() {
229 PublishProcessor<Integer> pp = PublishProcessor.create();
230
231 TestSubscriber<Integer> ts = pp.publish(v -> Flowable.range(1, 5)).test(0);
232
233 assertTrue("pp has no Subscribers?!", pp.hasSubscribers());
234
235 ts.assertNoValues()
236 .assertNoErrors()
237 .assertNotComplete();
238
239 ts.request(5);
240
241 ts.assertResult(1, 2, 3, 4, 5);
242
243 assertFalse("pp has Subscribers?!", pp.hasSubscribers());
244 }
245
246 @Test
247 public void badSource() {
248 TestHelper.checkBadSourceFlowable(f -> f.publish(Functions.identity()), false, 1, 1, 1);
249 }
250
251 @Test
252 public void frontOverflow() {
253 new Flowable<Integer>() {
254 @Override
255 protected void subscribeActual(Subscriber<? super Integer> s) {
256 s.onSubscribe(new BooleanSubscription());
257 for (int i = 0; i < 9; i++) {
258 s.onNext(i);
259 }
260 }
261 }
262 .publish(Functions.identity(), 8)
263 .test(0)
264 .assertFailure(MissingBackpressureException.class);
265 }
266
267 @Test
268 public void errorResubscribe() {
269 Flowable.error(new TestException())
270 .publish(f -> f.onErrorResumeWith(f))
271 .test()
272 .assertFailure(TestException.class);
273 }
274
275 @Test
276 public void fusedInputCrash() {
277 Flowable.just(1)
278 .map(v -> {
279 throw new TestException();
280 })
281 .publish(Functions.identity())
282 .test()
283 .assertFailure(TestException.class);
284 }
285
286 @Test
287 public void error() {
288 new FlowablePublishMulticast<>(Flowable.just(1).concatWith(Flowable.error(new TestException())),
289 Functions.identity(), 16, true)
290 .test()
291 .assertFailure(TestException.class, 1);
292 }
293
294 @Test
295 public void backpressuredEmpty() {
296 Flowable.<Integer>empty()
297 .publish(Functions.identity())
298 .test(0L)
299 .assertResult();
300 }
301
302 @Test
303 public void oneByOne() {
304 Flowable.range(1, 10)
305 .publish(Functions.identity())
306 .rebatchRequests(1)
307 .test()
308 .assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
309 }
310
311 @Test
312 public void completeCancelRaceNoRequest() {
313 final PublishProcessor<Integer> pp = PublishProcessor.create();
314
315 final TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1L) {
316 @Override
317 public void onNext(Integer t) {
318 super.onNext(t);
319 if (t == 1) {
320 cancel();
321 onComplete();
322 }
323 }
324 };
325
326 pp.publish(Functions.identity()).subscribe(ts);
327
328 pp.onNext(1);
329
330 assertFalse(pp.hasSubscribers());
331
332 ts.assertResult(1);
333 }
334
335 @Test
336 public void inputOutputSubscribeRace() {
337 Flowable<Integer> source = Flowable.just(1)
338 .publish(f -> f.subscribeOn(Schedulers.single()));
339
340 for (int i = 0; i < 500; i++) {
341 source.test()
342 .awaitDone(5, TimeUnit.SECONDS)
343 .assertResult(1);
344 }
345 }
346
347 @Test
348 public void inputOutputSubscribeRace2() {
349 Flowable<Integer> source = Flowable.just(1).subscribeOn(Schedulers.single())
350 .publish(Functions.identity());
351
352 for (int i = 0; i < 500; i++) {
353 source.test()
354 .awaitDone(5, TimeUnit.SECONDS)
355 .assertResult(1);
356 }
357 }
358
359 @Test
360 public void sourceSubscriptionDelayed() {
361 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
362 final TestSubscriber<Integer> ts1 = new TestSubscriber<>(0L);
363
364 Flowable.just(1)
365 .publish(f -> {
366 Runnable r1 = () -> f.subscribe(ts1);
367
368 Runnable r2 = () -> {
369 for (int j = 0; j < 100; j++) {
370 ts1.request(1);
371 }
372 };
373
374 TestHelper.race(r1, r2);
375 return f;
376 }).test()
377 .assertResult(1);
378
379 ts1.assertResult(1);
380 }
381 }
382
383 @Test
384 public void longFlow() {
385 Flowable.range(1, 1000000)
386 .publish(v -> Flowable.mergeArray(
387 v.filter(w -> w % 2 == 0),
388 v.filter(w -> w % 2 != 0)))
389 .takeLast(1)
390 .test()
391 .assertResult(1000000);
392 }
393
394 @Test
395 public void longFlow2() {
396 Flowable.range(1, 100000)
397 .publish(v -> Flowable.mergeArray(
398 v.filter(w -> w % 2 == 0),
399 v.filter(w -> w % 2 != 0)))
400 .test()
401 .assertValueCount(100000)
402 .assertNoErrors()
403 .assertComplete();
404 }
405
406 @Test
407 public void longFlowHidden() {
408 Flowable.range(1, 1000000).hide()
409 .publish(v -> Flowable.mergeArray(
410 v.filter(w -> w % 2 == 0),
411 v.filter(w -> w % 2 != 0)))
412 .takeLast(1)
413 .test()
414 .assertResult(1000000);
415 }
416
417 @Test
418 public void noUpstreamCancelOnCasualChainClose() {
419 AtomicBoolean parentUpstreamCancelled = new AtomicBoolean(false);
420 Flowable.range(1, 10)
421 .doOnCancel(() -> parentUpstreamCancelled.set(true))
422 .publish(Functions.identity())
423 .test()
424 .awaitDone(1, TimeUnit.SECONDS);
425 assertFalse("Unnecessary upstream .cancel() call in FlowablePublishMulticast", parentUpstreamCancelled.get());
426 }
427
428 @Test
429 public void noUpstreamCancelOnCasualChainCloseWithInnerCancels() {
430 AtomicBoolean parentUpstreamCancelled = new AtomicBoolean(false);
431 Flowable.range(1, 10)
432 .doOnCancel(() -> parentUpstreamCancelled.set(true))
433 .publish(v -> Flowable.concat(v.take(1), v.skip(5)))
434 .test()
435 .awaitDone(1, TimeUnit.SECONDS);
436 assertFalse("Unnecessary upstream .cancel() call in FlowablePublishMulticast", parentUpstreamCancelled.get());
437 }
438
439 @Test
440 public void upstreamCancelOnDownstreamCancel() {
441 AtomicBoolean parentUpstreamCancelled = new AtomicBoolean(false);
442 Flowable.range(1, 10)
443 .doOnCancel(() -> parentUpstreamCancelled.set(true))
444 .publish(Functions.identity())
445 .take(1)
446 .test()
447 .awaitDone(1, TimeUnit.SECONDS);
448 assertTrue("Upstream .cancel() not called in FlowablePublishMulticast", parentUpstreamCancelled.get());
449 }
450 }