Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSerializeTest.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 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.concurrent.*;
21 import java.util.concurrent.atomic.AtomicInteger;
22
23 import org.junit.*;
24 import org.reactivestreams.*;
25
26 import io.reactivex.rxjava3.core.*;
27 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
28 import io.reactivex.rxjava3.subscribers.DefaultSubscriber;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class FlowableSerializeTest extends RxJavaTest {
32
33 Subscriber<String> subscriber;
34
35 @Before
36 public void before() {
37 subscriber = TestHelper.mockSubscriber();
38 }
39
40 @Test
41 public void singleThreadedBasic() {
42 TestSingleThreadedObservable onSubscribe = new TestSingleThreadedObservable("one", "two", "three");
43 Flowable<String> w = Flowable.unsafeCreate(onSubscribe);
44
45 w.serialize().subscribe(subscriber);
46 onSubscribe.waitToFinish();
47
48 verify(subscriber, times(1)).onNext("one");
49 verify(subscriber, times(1)).onNext("two");
50 verify(subscriber, times(1)).onNext("three");
51 verify(subscriber, never()).onError(any(Throwable.class));
52 verify(subscriber, times(1)).onComplete();
53 // non-deterministic because unsubscribe happens after 'waitToFinish' releases
54 // so commenting out for now as this is not a critical thing to test here
55 // verify(s, times(1)).unsubscribe();
56 }
57
58 @Test
59 public void multiThreadedBasic() {
60 TestMultiThreadedObservable onSubscribe = new TestMultiThreadedObservable("one", "two", "three");
61 Flowable<String> w = Flowable.unsafeCreate(onSubscribe);
62
63 BusyObserver busyobserver = new BusyObserver();
64
65 w.serialize().subscribe(busyobserver);
66 onSubscribe.waitToFinish();
67
68 assertEquals(3, busyobserver.onNextCount.get());
69 assertFalse(busyobserver.onError);
70 assertTrue(busyobserver.onComplete);
71 // non-deterministic because unsubscribe happens after 'waitToFinish' releases
72 // so commenting out for now as this is not a critical thing to test here
73 // verify(s, times(1)).unsubscribe();
74
75 // we can have concurrency ...
76 assertTrue(onSubscribe.maxConcurrentThreads.get() > 1);
77 // ... but the onNext execution should be single threaded
78 assertEquals(1, busyobserver.maxConcurrentThreads.get());
79 }
80
81 @Test
82 public void multiThreadedWithNPEFlaky() throws InterruptedException {
83 int max = 9;
84 for (int i = 0; i <= max; i++) {
85 try {
86 multiThreadedWithNPE();
87 return;
88 } catch (AssertionError ex) {
89 if (i == max) {
90 throw ex;
91 }
92 }
93 Thread.sleep((long)(1000 * Math.random() + 100));
94 }
95 }
96
97 void multiThreadedWithNPE() {
98 TestMultiThreadedObservable onSubscribe = new TestMultiThreadedObservable("one", "two", "three", null);
99 Flowable<String> w = Flowable.unsafeCreate(onSubscribe);
100
101 BusyObserver busyobserver = new BusyObserver();
102
103 w.serialize().subscribe(busyobserver);
104 onSubscribe.waitToFinish();
105
106 System.out.println("maxConcurrentThreads: " + onSubscribe.maxConcurrentThreads.get());
107
108 // we can't know how many onNext calls will occur since they each run on a separate thread
109 // that depends on thread scheduling so 0, 1, 2 and 3 are all valid options
110 // assertEquals(3, busyobserver.onNextCount.get());
111 assertTrue(busyobserver.onNextCount.get() < 4);
112 assertTrue(busyobserver.onError);
113 // no onComplete because onError was invoked
114 assertFalse(busyobserver.onComplete);
115 // non-deterministic because unsubscribe happens after 'waitToFinish' releases
116 // so commenting out for now as this is not a critical thing to test here
117 //verify(s, times(1)).unsubscribe();
118
119 // we can have concurrency ...
120 assertTrue(onSubscribe.maxConcurrentThreads.get() > 1);
121 // ... but the onNext execution should be single threaded
122 assertEquals(1, busyobserver.maxConcurrentThreads.get());
123 }
124
125 @Test
126 public void multiThreadedWithNPEinMiddleFlaky() throws InterruptedException {
127 int max = 9;
128 for (int i = 0; i <= max; i++) {
129 try {
130 multiThreadedWithNPEinMiddle();
131 return;
132 } catch (AssertionError ex) {
133 if (i == max) {
134 throw ex;
135 }
136 }
137 Thread.sleep((long)(1000 * Math.random() + 100));
138 }
139 }
140
141 void multiThreadedWithNPEinMiddle() {
142 boolean lessThan9 = false;
143 for (int i = 0; i < 3; i++) {
144 TestMultiThreadedObservable onSubscribe = new TestMultiThreadedObservable("one", "two", "three", null, "four", "five", "six", "seven", "eight", "nine");
145 Flowable<String> w = Flowable.unsafeCreate(onSubscribe);
146
147 BusyObserver busyobserver = new BusyObserver();
148
149 w.serialize().subscribe(busyobserver);
150 onSubscribe.waitToFinish();
151
152 System.out.println("maxConcurrentThreads: " + onSubscribe.maxConcurrentThreads.get());
153 // this should not always be the full number of items since the error should (very often)
154 // stop it before it completes all 9
155 System.out.println("onNext count: " + busyobserver.onNextCount.get());
156 if (busyobserver.onNextCount.get() < 9) {
157 lessThan9 = true;
158 }
159 assertTrue(busyobserver.onError);
160 // no onComplete because onError was invoked
161 assertFalse(busyobserver.onComplete);
162 // non-deterministic because unsubscribe happens after 'waitToFinish' releases
163 // so commenting out for now as this is not a critical thing to test here
164 // verify(s, times(1)).unsubscribe();
165
166 // we can have concurrency ...
167 assertTrue(onSubscribe.maxConcurrentThreads.get() > 1);
168 // ... but the onNext execution should be single threaded
169 assertEquals(1, busyobserver.maxConcurrentThreads.get());
170 }
171 assertTrue(lessThan9);
172 }
173
174 /**
175 * A thread that will pass data to onNext.
176 */
177 static class OnNextThread implements Runnable {
178
179 private final DefaultSubscriber<String> subscriber;
180 private final int numStringsToSend;
181
182 OnNextThread(DefaultSubscriber<String> subscriber, int numStringsToSend) {
183 this.subscriber = subscriber;
184 this.numStringsToSend = numStringsToSend;
185 }
186
187 @Override
188 public void run() {
189 for (int i = 0; i < numStringsToSend; i++) {
190 subscriber.onNext("aString");
191 }
192 }
193 }
194
195 /**
196 * A thread that will call onError or onNext.
197 */
198 static class CompletionThread implements Runnable {
199
200 private final DefaultSubscriber<String> subscriber;
201 private final TestConcurrencyobserverEvent event;
202 private final Future<?>[] waitOnThese;
203
204 CompletionThread(DefaultSubscriber<String> subscriber, TestConcurrencyobserverEvent event, Future<?>... waitOnThese) {
205 this.subscriber = subscriber;
206 this.event = event;
207 this.waitOnThese = waitOnThese;
208 }
209
210 @Override
211 public void run() {
212 /* if we have 'waitOnThese' futures, we'll wait on them before proceeding */
213 if (waitOnThese != null) {
214 for (Future<?> f : waitOnThese) {
215 try {
216 f.get();
217 } catch (Throwable e) {
218 System.err.println("Error while waiting on future in CompletionThread");
219 }
220 }
221 }
222
223 /* send the event */
224 if (event == TestConcurrencyobserverEvent.onError) {
225 subscriber.onError(new RuntimeException("mocked exception"));
226 } else if (event == TestConcurrencyobserverEvent.onComplete) {
227 subscriber.onComplete();
228
229 } else {
230 throw new IllegalArgumentException("Expecting either onError or onComplete");
231 }
232 }
233 }
234
235 enum TestConcurrencyobserverEvent {
236 onComplete, onError, onNext
237 }
238
239 /**
240 * This spawns a single thread for the subscribe execution.
241 */
242 private static class TestSingleThreadedObservable implements Publisher<String> {
243
244 final String[] values;
245 private Thread t;
246
247 TestSingleThreadedObservable(final String... values) {
248 this.values = values;
249
250 }
251
252 @Override
253 public void subscribe(final Subscriber<? super String> subscriber) {
254 subscriber.onSubscribe(new BooleanSubscription());
255 System.out.println("TestSingleThreadedObservable subscribed to ...");
256 t = new Thread(new Runnable() {
257
258 @Override
259 public void run() {
260 try {
261 System.out.println("running TestSingleThreadedObservable thread");
262 for (String s : values) {
263 System.out.println("TestSingleThreadedObservable onNext: " + s);
264 subscriber.onNext(s);
265 }
266 subscriber.onComplete();
267 } catch (Throwable e) {
268 throw new RuntimeException(e);
269 }
270 }
271
272 });
273 System.out.println("starting TestSingleThreadedObservable thread");
274 t.start();
275 System.out.println("done starting TestSingleThreadedObservable thread");
276 }
277
278 public void waitToFinish() {
279 try {
280 t.join();
281 } catch (InterruptedException e) {
282 throw new RuntimeException(e);
283 }
284 }
285
286 }
287
288 /**
289 * This spawns a thread for the subscription, then a separate thread for each onNext call.
290 */
291 private static class TestMultiThreadedObservable implements Publisher<String> {
292 final String[] values;
293 Thread t;
294 AtomicInteger threadsRunning = new AtomicInteger();
295 AtomicInteger maxConcurrentThreads = new AtomicInteger();
296 ExecutorService threadPool;
297
298 TestMultiThreadedObservable(String... values) {
299 this.values = values;
300 this.threadPool = Executors.newCachedThreadPool();
301 }
302
303 @Override
304 public void subscribe(final Subscriber<? super String> subscriber) {
305 subscriber.onSubscribe(new BooleanSubscription());
306 System.out.println("TestMultiThreadedObservable subscribed to ...");
307 final NullPointerException npe = new NullPointerException();
308 t = new Thread(new Runnable() {
309
310 @Override
311 public void run() {
312 try {
313 System.out.println("running TestMultiThreadedObservable thread");
314 for (final String s : values) {
315 threadPool.execute(new Runnable() {
316
317 @Override
318 public void run() {
319 threadsRunning.incrementAndGet();
320 try {
321 // perform onNext call
322 if (s == null) {
323 System.out.println("TestMultiThreadedObservable onNext: null");
324 // force an error
325 throw npe;
326 } else {
327 try {
328 Thread.sleep(10);
329 } catch (InterruptedException ex) {
330 // ignored
331 }
332 System.out.println("TestMultiThreadedObservable onNext: " + s);
333 }
334 subscriber.onNext(s);
335 // capture 'maxThreads'
336 int concurrentThreads = threadsRunning.get();
337 int maxThreads = maxConcurrentThreads.get();
338 if (concurrentThreads > maxThreads) {
339 maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads);
340 }
341 } catch (Throwable e) {
342 subscriber.onError(e);
343 } finally {
344 threadsRunning.decrementAndGet();
345 }
346 }
347 });
348 }
349 // we are done spawning threads
350 threadPool.shutdown();
351 } catch (Throwable e) {
352 throw new RuntimeException(e);
353 }
354
355 // wait until all threads are done, then mark it as COMPLETED
356 try {
357 // wait for all the threads to finish
358 threadPool.awaitTermination(2, TimeUnit.SECONDS);
359 } catch (InterruptedException e) {
360 throw new RuntimeException(e);
361 }
362 subscriber.onComplete();
363 }
364 });
365 System.out.println("starting TestMultiThreadedObservable thread");
366 t.start();
367 System.out.println("done starting TestMultiThreadedObservable thread");
368 }
369
370 public void waitToFinish() {
371 try {
372 t.join();
373 } catch (InterruptedException e) {
374 throw new RuntimeException(e);
375 }
376 }
377 }
378
379 private static class BusyObserver extends DefaultSubscriber<String> {
380 volatile boolean onComplete;
381 volatile boolean onError;
382 AtomicInteger onNextCount = new AtomicInteger();
383 AtomicInteger threadsRunning = new AtomicInteger();
384 AtomicInteger maxConcurrentThreads = new AtomicInteger();
385
386 @Override
387 public void onComplete() {
388 threadsRunning.incrementAndGet();
389
390 System.out.println(">>> Busyobserver received onComplete");
391 onComplete = true;
392
393 int concurrentThreads = threadsRunning.get();
394 int maxThreads = maxConcurrentThreads.get();
395 if (concurrentThreads > maxThreads) {
396 maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads);
397 }
398 threadsRunning.decrementAndGet();
399 }
400
401 @Override
402 public void onError(Throwable e) {
403 threadsRunning.incrementAndGet();
404
405 System.out.println(">>> Busyobserver received onError: " + e.getMessage());
406 onError = true;
407
408 int concurrentThreads = threadsRunning.get();
409 int maxThreads = maxConcurrentThreads.get();
410 if (concurrentThreads > maxThreads) {
411 maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads);
412 }
413 threadsRunning.decrementAndGet();
414 }
415
416 @Override
417 public void onNext(String args) {
418 threadsRunning.incrementAndGet();
419 try {
420 onNextCount.incrementAndGet();
421 System.out.println(">>> Busyobserver received onNext: " + args);
422 try {
423 // simulate doing something computational
424 Thread.sleep(200);
425 } catch (InterruptedException e) {
426 e.printStackTrace();
427 }
428 } finally {
429 // capture 'maxThreads'
430 int concurrentThreads = threadsRunning.get();
431 int maxThreads = maxConcurrentThreads.get();
432 if (concurrentThreads > maxThreads) {
433 maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads);
434 }
435 threadsRunning.decrementAndGet();
436 }
437 }
438
439 }
440 }