Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableMergeMaxConcurrentTest.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.*;
19 import java.util.concurrent.*;
20 import java.util.concurrent.atomic.AtomicInteger;
21
22 import org.junit.Test;
23 import org.reactivestreams.*;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.internal.schedulers.IoScheduler;
27 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
28 import io.reactivex.rxjava3.schedulers.Schedulers;
29 import io.reactivex.rxjava3.subscribers.TestSubscriber;
30 import io.reactivex.rxjava3.testsupport.TestSubscriberEx;
31
32 public class FlowableMergeMaxConcurrentTest extends RxJavaTest {
33
34 @Test
35 public void whenMaxConcurrentIsOne() {
36 for (int i = 0; i < 100; i++) {
37 List<Flowable<String>> os = new ArrayList<>();
38 os.add(Flowable.just("one", "two", "three", "four", "five").subscribeOn(Schedulers.newThread()));
39 os.add(Flowable.just("one", "two", "three", "four", "five").subscribeOn(Schedulers.newThread()));
40 os.add(Flowable.just("one", "two", "three", "four", "five").subscribeOn(Schedulers.newThread()));
41
42 List<String> expected = Arrays.asList("one", "two", "three", "four", "five", "one", "two", "three", "four", "five", "one", "two", "three", "four", "five");
43 Iterator<String> iter = Flowable.merge(os, 1).blockingIterable().iterator();
44 List<String> actual = new ArrayList<>();
45 while (iter.hasNext()) {
46 actual.add(iter.next());
47 }
48 assertEquals(expected, actual);
49 }
50 }
51
52 @Test
53 public void maxConcurrent() {
54 for (int times = 0; times < 100; times++) {
55 int observableCount = 100;
56 // Test maxConcurrent from 2 to 12
57 int maxConcurrent = 2 + (times % 10);
58 AtomicInteger subscriptionCount = new AtomicInteger(0);
59
60 List<Flowable<String>> os = new ArrayList<>();
61 List<SubscriptionCheckObservable> scos = new ArrayList<>();
62 for (int i = 0; i < observableCount; i++) {
63 SubscriptionCheckObservable sco = new SubscriptionCheckObservable(subscriptionCount, maxConcurrent);
64 scos.add(sco);
65 os.add(Flowable.unsafeCreate(sco));
66 }
67
68 Iterator<String> iter = Flowable.merge(os, maxConcurrent).blockingIterable().iterator();
69 List<String> actual = new ArrayList<>();
70 while (iter.hasNext()) {
71 actual.add(iter.next());
72 }
73 // System.out.println("actual: " + actual);
74 assertEquals(5 * observableCount, actual.size());
75 for (SubscriptionCheckObservable sco : scos) {
76 assertFalse(sco.failed);
77 }
78 }
79 }
80
81 private static class SubscriptionCheckObservable implements Publisher<String> {
82
83 private final AtomicInteger subscriptionCount;
84 private final int maxConcurrent;
85 volatile boolean failed;
86
87 SubscriptionCheckObservable(AtomicInteger subscriptionCount, int maxConcurrent) {
88 this.subscriptionCount = subscriptionCount;
89 this.maxConcurrent = maxConcurrent;
90 }
91
92 @Override
93 public void subscribe(final Subscriber<? super String> t1) {
94 t1.onSubscribe(new BooleanSubscription());
95 new Thread(new Runnable() {
96
97 @Override
98 public void run() {
99 if (subscriptionCount.incrementAndGet() > maxConcurrent) {
100 failed = true;
101 }
102 t1.onNext("one");
103 t1.onNext("two");
104 t1.onNext("three");
105 t1.onNext("four");
106 t1.onNext("five");
107 // We could not decrement subscriptionCount in the unsubscribe method
108 // as "unsubscribe" is not guaranteed to be called before the next "subscribe".
109 subscriptionCount.decrementAndGet();
110 t1.onComplete();
111 }
112
113 }).start();
114 }
115
116 }
117
118 @Test
119 public void mergeALotOfSourcesOneByOneSynchronously() {
120 int n = 10000;
121 List<Flowable<Integer>> sourceList = new ArrayList<>(n);
122 for (int i = 0; i < n; i++) {
123 sourceList.add(Flowable.just(i));
124 }
125 Iterator<Integer> it = Flowable.merge(Flowable.fromIterable(sourceList), 1).blockingIterable().iterator();
126 int j = 0;
127 while (it.hasNext()) {
128 assertEquals((Integer)j, it.next());
129 j++;
130 }
131 assertEquals(j, n);
132 }
133
134 @Test
135 public void mergeALotOfSourcesOneByOneSynchronouslyTakeHalf() {
136 int n = 10000;
137 List<Flowable<Integer>> sourceList = new ArrayList<>(n);
138 for (int i = 0; i < n; i++) {
139 sourceList.add(Flowable.just(i));
140 }
141 Iterator<Integer> it = Flowable.merge(Flowable.fromIterable(sourceList), 1).take(n / 2).blockingIterable().iterator();
142 int j = 0;
143 while (it.hasNext()) {
144 assertEquals((Integer)j, it.next());
145 j++;
146 }
147 assertEquals(j, n / 2);
148 }
149
150 @Test
151 public void simple() {
152 for (int i = 1; i < 100; i++) {
153 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
154 List<Flowable<Integer>> sourceList = new ArrayList<>(i);
155 List<Integer> result = new ArrayList<>(i);
156 for (int j = 1; j <= i; j++) {
157 sourceList.add(Flowable.just(j));
158 result.add(j);
159 }
160
161 Flowable.merge(sourceList, i).subscribe(ts);
162
163 ts.assertNoErrors();
164 ts.assertTerminated();
165 ts.assertValueSequence(result);
166 }
167 }
168
169 @Test
170 public void simpleOneLess() {
171 for (int i = 2; i < 100; i++) {
172 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
173 List<Flowable<Integer>> sourceList = new ArrayList<>(i);
174 List<Integer> result = new ArrayList<>(i);
175 for (int j = 1; j <= i; j++) {
176 sourceList.add(Flowable.just(j));
177 result.add(j);
178 }
179
180 Flowable.merge(sourceList, i - 1).subscribe(ts);
181
182 ts.assertNoErrors();
183 ts.assertTerminated();
184 ts.assertValueSequence(result);
185 }
186 }
187
188 @Test
189 public void simpleAsyncLoop() {
190 IoScheduler ios = (IoScheduler)Schedulers.io();
191 int c = ios.size();
192 for (int i = 0; i < 200; i++) {
193 simpleAsync();
194 int c1 = ios.size();
195 if (c + 60 < c1) {
196 throw new AssertionError("Worker leak: " + c + " - " + c1);
197 }
198 }
199 }
200
201 @Test
202 public void simpleAsync() {
203 for (int i = 1; i < 50; i++) {
204 TestSubscriber<Integer> ts = new TestSubscriber<>();
205 List<Flowable<Integer>> sourceList = new ArrayList<>(i);
206 Set<Integer> expected = new HashSet<>(i);
207 for (int j = 1; j <= i; j++) {
208 sourceList.add(Flowable.just(j).subscribeOn(Schedulers.io()));
209 expected.add(j);
210 }
211
212 Flowable.merge(sourceList, i).subscribe(ts);
213
214 ts.awaitDone(1, TimeUnit.SECONDS);
215 ts.assertNoErrors();
216 Set<Integer> actual = new HashSet<>(ts.values());
217
218 assertEquals(expected, actual);
219 }
220 }
221
222 @Test
223 public void simpleOneLessAsyncLoop() {
224 for (int i = 0; i < 200; i++) {
225 simpleOneLessAsync();
226 }
227 }
228
229 @Test
230 public void simpleOneLessAsync() {
231 long t = System.currentTimeMillis();
232 for (int i = 2; i < 50; i++) {
233 if (System.currentTimeMillis() - t > TimeUnit.SECONDS.toMillis(9)) {
234 break;
235 }
236 TestSubscriber<Integer> ts = new TestSubscriber<>();
237 List<Flowable<Integer>> sourceList = new ArrayList<>(i);
238 Set<Integer> expected = new HashSet<>(i);
239 for (int j = 1; j <= i; j++) {
240 sourceList.add(Flowable.just(j).subscribeOn(Schedulers.io()));
241 expected.add(j);
242 }
243
244 Flowable.merge(sourceList, i - 1).subscribe(ts);
245
246 ts.awaitDone(1, TimeUnit.SECONDS);
247 ts.assertNoErrors();
248 Set<Integer> actual = new HashSet<>(ts.values());
249
250 assertEquals(expected, actual);
251 }
252 }
253
254 @Test
255 public void backpressureHonored() throws Exception {
256 List<Flowable<Integer>> sourceList = new ArrayList<>(3);
257
258 sourceList.add(Flowable.range(0, 100000).subscribeOn(Schedulers.io()));
259 sourceList.add(Flowable.range(0, 100000).subscribeOn(Schedulers.io()));
260 sourceList.add(Flowable.range(0, 100000).subscribeOn(Schedulers.io()));
261
262 final CountDownLatch cdl = new CountDownLatch(5);
263
264 TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L) {
265 @Override
266 public void onNext(Integer t) {
267 super.onNext(t);
268 cdl.countDown();
269 }
270 };
271
272 Flowable.merge(sourceList, 2).subscribe(ts);
273
274 ts.request(5);
275
276 cdl.await();
277
278 ts.assertNoErrors();
279 ts.assertValueCount(5);
280 ts.assertNotComplete();
281
282 ts.cancel();
283 }
284
285 @Test
286 public void take() throws Exception {
287 List<Flowable<Integer>> sourceList = new ArrayList<>(3);
288
289 sourceList.add(Flowable.range(0, 100000).subscribeOn(Schedulers.io()));
290 sourceList.add(Flowable.range(0, 100000).subscribeOn(Schedulers.io()));
291 sourceList.add(Flowable.range(0, 100000).subscribeOn(Schedulers.io()));
292
293 TestSubscriber<Integer> ts = new TestSubscriber<>();
294
295 Flowable.merge(sourceList, 2).take(5).subscribe(ts);
296
297 ts.awaitDone(5, TimeUnit.SECONDS);
298 ts.assertNoErrors();
299 ts.assertValueCount(5);
300 }
301 }