Debian Codegraph @

src/test/java/io/reactivex/rxjava3/processors/ReplayProcessorConcurrencyTest.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.processors;
15
16 import static org.junit.Assert.assertEquals;
17
18 import java.util.*;
19 import java.util.concurrent.*;
20 import java.util.concurrent.atomic.AtomicReference;
21
22 import org.junit.*;
23 import org.reactivestreams.*;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.functions.Consumer;
27 import io.reactivex.rxjava3.schedulers.Schedulers;
28 import io.reactivex.rxjava3.subscribers.DefaultSubscriber;
29 import io.reactivex.rxjava3.testsupport.TestSubscriberEx;
30
31 public class ReplayProcessorConcurrencyTest extends RxJavaTest {
32
33 @Test
34 public void replaySubjectConcurrentSubscribersDoingReplayDontBlockEachOther() throws InterruptedException {
35 final ReplayProcessor<Long> replay = ReplayProcessor.create();
36 Thread source = new Thread(new Runnable() {
37
38 @Override
39 public void run() {
40 Flowable.unsafeCreate(new Publisher<Long>() {
41
42 @Override
43 public void subscribe(Subscriber<? super Long> subscriber) {
44 System.out.println("********* Start Source Data ***********");
45 for (long l = 1; l <= 10000; l++) {
46 subscriber.onNext(l);
47 }
48 System.out.println("********* Finished Source Data ***********");
49 subscriber.onComplete();
50 }
51 }).subscribe(replay);
52 }
53 });
54 source.start();
55
56 long v = replay.blockingLast();
57 assertEquals(10000, v);
58
59 // it's been played through once so now it will all be replays
60 final CountDownLatch slowLatch = new CountDownLatch(1);
61 Thread slowThread = new Thread(new Runnable() {
62
63 @Override
64 public void run() {
65 Subscriber<Long> slow = new DefaultSubscriber<Long>() {
66
67 @Override
68 public void onComplete() {
69 System.out.println("*** Slow Observer completed");
70 slowLatch.countDown();
71 }
72
73 @Override
74 public void onError(Throwable e) {
75 }
76
77 @Override
78 public void onNext(Long args) {
79 if (args == 1) {
80 System.out.println("*** Slow Observer STARTED");
81 }
82 try {
83 if (args % 10 == 0) {
84 Thread.sleep(1);
85 }
86 } catch (InterruptedException e) {
87 e.printStackTrace();
88 }
89 }
90 };
91 replay.subscribe(slow);
92 try {
93 slowLatch.await();
94 } catch (InterruptedException e1) {
95 e1.printStackTrace();
96 }
97 }
98 });
99 slowThread.start();
100
101 Thread fastThread = new Thread(new Runnable() {
102
103 @Override
104 public void run() {
105 final CountDownLatch fastLatch = new CountDownLatch(1);
106 Subscriber<Long> fast = new DefaultSubscriber<Long>() {
107
108 @Override
109 public void onComplete() {
110 System.out.println("*** Fast Observer completed");
111 fastLatch.countDown();
112 }
113
114 @Override
115 public void onError(Throwable e) {
116 }
117
118 @Override
119 public void onNext(Long args) {
120 if (args == 1) {
121 System.out.println("*** Fast Observer STARTED");
122 }
123 }
124 };
125 replay.subscribe(fast);
126 try {
127 fastLatch.await();
128 } catch (InterruptedException e1) {
129 e1.printStackTrace();
130 }
131 }
132 });
133 fastThread.start();
134 fastThread.join();
135
136 // slow should not yet be completed when fast completes
137 assertEquals(1, slowLatch.getCount());
138
139 slowThread.join();
140 }
141
142 @Test
143 public void replaySubjectConcurrentSubscriptions() throws InterruptedException {
144 final ReplayProcessor<Long> replay = ReplayProcessor.create();
145 Thread source = new Thread(new Runnable() {
146
147 @Override
148 public void run() {
149 Flowable.unsafeCreate(new Publisher<Long>() {
150
151 @Override
152 public void subscribe(Subscriber<? super Long> subscriber) {
153 System.out.println("********* Start Source Data ***********");
154 for (long l = 1; l <= 10000; l++) {
155 subscriber.onNext(l);
156 }
157 System.out.println("********* Finished Source Data ***********");
158 subscriber.onComplete();
159 }
160 }).subscribe(replay);
161 }
162 });
163
164 // used to collect results of each thread
165 final List<List<Long>> listOfListsOfValues = Collections.synchronizedList(new ArrayList<>());
166 final List<Thread> threads = Collections.synchronizedList(new ArrayList<>());
167
168 for (int i = 1; i <= 200; i++) {
169 final int count = i;
170 if (count == 20) {
171 // start source data after we have some already subscribed
172 // and while others are in process of subscribing
173 source.start();
174 }
175 if (count == 100) {
176 // wait for source to finish then keep adding after it's done
177 source.join();
178 }
179 Thread t = new Thread(new Runnable() {
180
181 @Override
182 public void run() {
183 List<Long> values = replay.toList().blockingGet();
184 listOfListsOfValues.add(values);
185 System.out.println("Finished thread: " + count);
186 }
187 });
188 t.start();
189 System.out.println("Started thread: " + i);
190 threads.add(t);
191 }
192
193 // wait for all threads to complete
194 for (Thread t : threads) {
195 t.join();
196 }
197
198 // assert all threads got the same results
199 List<Long> sums = new ArrayList<>();
200 for (List<Long> values : listOfListsOfValues) {
201 long v = 0;
202 for (long l : values) {
203 v += l;
204 }
205 sums.add(v);
206 }
207
208 long expected = sums.get(0);
209 boolean success = true;
210 for (long l : sums) {
211 if (l != expected) {
212 success = false;
213 System.out.println("FAILURE => Expected " + expected + " but got: " + l);
214 }
215 }
216
217 if (success) {
218 System.out.println("Success! " + sums.size() + " each had the same sum of " + expected);
219 } else {
220 throw new RuntimeException("Concurrency Bug");
221 }
222
223 }
224
225 /**
226 * Can receive timeout if subscribe never receives an onError/onComplete ... which reveals a race condition.
227 */
228 @Test
229 public void subscribeCompletionRaceCondition() {
230 for (int i = 0; i < 50; i++) {
231 final ReplayProcessor<String> processor = ReplayProcessor.create();
232 final AtomicReference<String> value1 = new AtomicReference<>();
233
234 processor.subscribe(new Consumer<String>() {
235
236 @Override
237 public void accept(String t1) {
238 try {
239 // simulate a slow observer
240 Thread.sleep(50);
241 } catch (InterruptedException e) {
242 e.printStackTrace();
243 }
244 value1.set(t1);
245 }
246
247 });
248
249 Thread t1 = new Thread(new Runnable() {
250
251 @Override
252 public void run() {
253 processor.onNext("value");
254 processor.onComplete();
255 }
256 });
257
258 SubjectObserverThread t2 = new SubjectObserverThread(processor);
259 SubjectObserverThread t3 = new SubjectObserverThread(processor);
260 SubjectObserverThread t4 = new SubjectObserverThread(processor);
261 SubjectObserverThread t5 = new SubjectObserverThread(processor);
262
263 t2.start();
264 t3.start();
265 t1.start();
266 t4.start();
267 t5.start();
268 try {
269 t1.join();
270 t2.join();
271 t3.join();
272 t4.join();
273 t5.join();
274 } catch (InterruptedException e) {
275 throw new RuntimeException(e);
276 }
277
278 assertEquals("value", value1.get());
279 assertEquals("value", t2.value.get());
280 assertEquals("value", t3.value.get());
281 assertEquals("value", t4.value.get());
282 assertEquals("value", t5.value.get());
283 }
284
285 }
286
287 /**
288 * Make sure emission-subscription races are handled correctly.
289 * https://github.com/ReactiveX/RxJava/issues/1147
290 */
291 @Test
292 public void raceForTerminalState() {
293 final List<Integer> expected = Arrays.asList(1);
294 for (int i = 0; i < 100000; i++) {
295 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
296 Flowable.just(1).subscribeOn(Schedulers.computation()).cache().subscribe(ts);
297 ts.awaitDone(5, TimeUnit.SECONDS);
298 ts.assertValueSequence(expected);
299 ts.assertTerminated();
300 }
301 }
302
303 static class SubjectObserverThread extends Thread {
304
305 private final ReplayProcessor<String> processor;
306 private final AtomicReference<String> value = new AtomicReference<>();
307
308 SubjectObserverThread(ReplayProcessor<String> processor) {
309 this.processor = processor;
310 }
311
312 @Override
313 public void run() {
314 try {
315 // a timeout exception will happen if we don't get a terminal state
316 String v = processor.timeout(2000, TimeUnit.MILLISECONDS).blockingSingle();
317 value.set(v);
318 } catch (Exception e) {
319 e.printStackTrace();
320 }
321 }
322 }
323
324 @Test
325 public void replaySubjectEmissionSubscriptionRace() throws Exception {
326 Scheduler s = Schedulers.io();
327 Scheduler.Worker worker = Schedulers.io().createWorker();
328 try {
329 for (int i = 0; i < 50000; i++) {
330 if (i % 1000 == 0) {
331 System.out.println(i);
332 }
333 final ReplayProcessor<Object> rs = ReplayProcessor.create();
334
335 final CountDownLatch finish = new CountDownLatch(1);
336 final CountDownLatch start = new CountDownLatch(1);
337
338 worker.schedule(new Runnable() {
339 @Override
340 public void run() {
341 try {
342 start.await();
343 } catch (Exception e1) {
344 e1.printStackTrace();
345 }
346 rs.onNext(1);
347 }
348 });
349
350 final AtomicReference<Object> o = new AtomicReference<>();
351
352 rs.subscribeOn(s).observeOn(Schedulers.io())
353 .subscribe(new DefaultSubscriber<Object>() {
354
355 @Override
356 public void onComplete() {
357 o.set(-1);
358 finish.countDown();
359 }
360
361 @Override
362 public void onError(Throwable e) {
363 o.set(e);
364 finish.countDown();
365 }
366
367 @Override
368 public void onNext(Object t) {
369 o.set(t);
370 finish.countDown();
371 }
372
373 });
374 start.countDown();
375
376 if (!finish.await(5, TimeUnit.SECONDS)) {
377 System.out.println(o.get());
378 System.out.println(rs.hasSubscribers());
379 rs.onComplete();
380 Assert.fail("Timeout @ " + i);
381 break;
382 } else {
383 Assert.assertEquals(1, o.get());
384 worker.schedule(new Runnable() {
385 @Override
386 public void run() {
387 rs.onComplete();
388 }
389 });
390
391 }
392 }
393 } finally {
394 worker.dispose();
395 }
396 }
397
398 @Test
399 public void concurrentSizeAndHasAnyValue() throws InterruptedException {
400 final ReplayProcessor<Object> rs = ReplayProcessor.create();
401 final CyclicBarrier cb = new CyclicBarrier(2);
402
403 Thread t = new Thread(new Runnable() {
404 @Override
405 public void run() {
406 try {
407 cb.await();
408 } catch (InterruptedException e) {
409 return;
410 } catch (BrokenBarrierException e) {
411 return;
412 }
413 for (int i = 0; i < 1000000; i++) {
414 rs.onNext(i);
415 }
416 rs.onComplete();
417 System.out.println("Replay fill Thread finished!");
418 }
419 });
420 t.start();
421 try {
422 cb.await();
423 } catch (InterruptedException e) {
424 return;
425 } catch (BrokenBarrierException e) {
426 return;
427 }
428 int lastSize = 0;
429 for (; !rs.hasThrowable() && !rs.hasComplete();) {
430 int size = rs.size();
431 boolean hasAny = rs.hasValue();
432 Object[] values = rs.getValues();
433 if (size < lastSize) {
434 Assert.fail("Size decreased! " + lastSize + " -> " + size);
435 }
436 if ((size > 0) && !hasAny) {
437 Assert.fail("hasAnyValue reports emptyness but size doesn't");
438 }
439 if (size > values.length) {
440 Assert.fail("Got fewer values than size! " + size + " -> " + values.length);
441 }
442 lastSize = size;
443 }
444
445 t.join();
446 }
447 }