| 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 ReplayProcessorBoundedConcurrencyTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void replaySubjectConcurrentSubscribersDoingReplayDontBlockEachOther() throws InterruptedException { |
| 35 |
final ReplayProcessor<Long> replay = ReplayProcessor.createUnbounded(); |
| 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.createUnbounded(); |
| 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.createUnbounded(); |
| 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 |
private 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.createWithSize(2); |
| 334 |
|
| 335 |
final CountDownLatch finish = new CountDownLatch(1); |
| 336 |
final CountDownLatch start = new CountDownLatch(1); |
| 337 |
|
| 338 |
// int j = i; |
| 339 |
|
| 340 |
worker.schedule(new Runnable() { |
| 341 |
@Override |
| 342 |
public void run() { |
| 343 |
try { |
| 344 |
start.await(); |
| 345 |
} catch (Exception e1) { |
| 346 |
e1.printStackTrace(); |
| 347 |
} |
| 348 |
// System.out.println("> " + j); |
| 349 |
rs.onNext(1); |
| 350 |
} |
| 351 |
}); |
| 352 |
|
| 353 |
final AtomicReference<Object> o = new AtomicReference<>(); |
| 354 |
|
| 355 |
rs |
| 356 |
// .doOnSubscribe(v -> System.out.println("!! " + j)) |
| 357 |
// .doOnNext(e -> System.out.println(">> " + j)) |
| 358 |
.subscribeOn(s) |
| 359 |
.observeOn(Schedulers.io()) |
| 360 |
// .doOnNext(e -> System.out.println(">>> " + j)) |
| 361 |
.subscribe(new DefaultSubscriber<Object>() { |
| 362 |
|
| 363 |
@Override |
| 364 |
protected void onStart() { |
| 365 |
super.onStart(); |
| 366 |
} |
| 367 |
|
| 368 |
@Override |
| 369 |
public void onComplete() { |
| 370 |
o.set(-1); |
| 371 |
finish.countDown(); |
| 372 |
} |
| 373 |
|
| 374 |
@Override |
| 375 |
public void onError(Throwable e) { |
| 376 |
o.set(e); |
| 377 |
finish.countDown(); |
| 378 |
} |
| 379 |
|
| 380 |
@Override |
| 381 |
public void onNext(Object t) { |
| 382 |
o.set(t); |
| 383 |
finish.countDown(); |
| 384 |
} |
| 385 |
|
| 386 |
}); |
| 387 |
start.countDown(); |
| 388 |
|
| 389 |
if (!finish.await(5, TimeUnit.SECONDS)) { |
| 390 |
System.out.println(o.get()); |
| 391 |
System.out.println(rs.hasSubscribers()); |
| 392 |
rs.onComplete(); |
| 393 |
Assert.fail("Timeout @ " + i); |
| 394 |
break; |
| 395 |
} else { |
| 396 |
Assert.assertEquals(1, o.get()); |
| 397 |
worker.schedule(new Runnable() { |
| 398 |
@Override |
| 399 |
public void run() { |
| 400 |
rs.onComplete(); |
| 401 |
} |
| 402 |
}); |
| 403 |
} |
| 404 |
} |
| 405 |
} finally { |
| 406 |
worker.dispose(); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
@Test |
| 411 |
public void concurrentSizeAndHasAnyValue() throws InterruptedException { |
| 412 |
final ReplayProcessor<Object> rs = ReplayProcessor.createUnbounded(); |
| 413 |
final CyclicBarrier cb = new CyclicBarrier(2); |
| 414 |
|
| 415 |
Thread t = new Thread(new Runnable() { |
| 416 |
@Override |
| 417 |
public void run() { |
| 418 |
try { |
| 419 |
cb.await(); |
| 420 |
} catch (InterruptedException e) { |
| 421 |
return; |
| 422 |
} catch (BrokenBarrierException e) { |
| 423 |
return; |
| 424 |
} |
| 425 |
for (int i = 0; i < 1000000; i++) { |
| 426 |
rs.onNext(i); |
| 427 |
} |
| 428 |
rs.onComplete(); |
| 429 |
System.out.println("Replay fill Thread finished!"); |
| 430 |
} |
| 431 |
}); |
| 432 |
t.start(); |
| 433 |
try { |
| 434 |
cb.await(); |
| 435 |
} catch (InterruptedException e) { |
| 436 |
return; |
| 437 |
} catch (BrokenBarrierException e) { |
| 438 |
return; |
| 439 |
} |
| 440 |
int lastSize = 0; |
| 441 |
for (; !rs.hasThrowable() && !rs.hasComplete();) { |
| 442 |
int size = rs.size(); |
| 443 |
boolean hasAny = rs.hasValue(); |
| 444 |
Object[] values = rs.getValues(); |
| 445 |
if (size < lastSize) { |
| 446 |
Assert.fail("Size decreased! " + lastSize + " -> " + size); |
| 447 |
} |
| 448 |
if ((size > 0) && !hasAny) { |
| 449 |
Assert.fail("hasAnyValue reports emptyness but size doesn't"); |
| 450 |
} |
| 451 |
if (size > values.length) { |
| 452 |
Assert.fail("Got fewer values than size! " + size + " -> " + values.length); |
| 453 |
} |
| 454 |
for (int i = 0; i < values.length - 1; i++) { |
| 455 |
Integer v1 = (Integer)values[i]; |
| 456 |
Integer v2 = (Integer)values[i + 1]; |
| 457 |
assertEquals(1, v2 - v1); |
| 458 |
} |
| 459 |
lastSize = size; |
| 460 |
} |
| 461 |
|
| 462 |
t.join(); |
| 463 |
} |
| 464 |
|
| 465 |
@Test |
| 466 |
public void concurrentSizeAndHasAnyValueBounded() throws InterruptedException { |
| 467 |
final ReplayProcessor<Object> rs = ReplayProcessor.createWithSize(3); |
| 468 |
final CyclicBarrier cb = new CyclicBarrier(2); |
| 469 |
|
| 470 |
Thread t = new Thread(new Runnable() { |
| 471 |
@Override |
| 472 |
public void run() { |
| 473 |
try { |
| 474 |
cb.await(); |
| 475 |
} catch (InterruptedException e) { |
| 476 |
return; |
| 477 |
} catch (BrokenBarrierException e) { |
| 478 |
return; |
| 479 |
} |
| 480 |
for (int i = 0; i < 1000000; i++) { |
| 481 |
rs.onNext(i); |
| 482 |
} |
| 483 |
rs.onComplete(); |
| 484 |
System.out.println("Replay fill Thread finished!"); |
| 485 |
} |
| 486 |
}); |
| 487 |
t.start(); |
| 488 |
try { |
| 489 |
cb.await(); |
| 490 |
} catch (InterruptedException e) { |
| 491 |
return; |
| 492 |
} catch (BrokenBarrierException e) { |
| 493 |
return; |
| 494 |
} |
| 495 |
for (; !rs.hasThrowable() && !rs.hasComplete();) { |
| 496 |
rs.size(); // can't use value so just call to detect hangs |
| 497 |
rs.hasValue(); // can't use value so just call to detect hangs |
| 498 |
Object[] values = rs.getValues(); |
| 499 |
for (int i = 0; i < values.length - 1; i++) { |
| 500 |
Integer v1 = (Integer)values[i]; |
| 501 |
Integer v2 = (Integer)values[i + 1]; |
| 502 |
assertEquals(1, v2 - v1); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
t.join(); |
| 507 |
} |
| 508 |
|
| 509 |
@Test |
| 510 |
public void concurrentSizeAndHasAnyValueTimeBounded() throws InterruptedException { |
| 511 |
final ReplayProcessor<Object> rs = ReplayProcessor.createWithTime(1, TimeUnit.MILLISECONDS, Schedulers.computation()); |
| 512 |
final CyclicBarrier cb = new CyclicBarrier(2); |
| 513 |
|
| 514 |
Thread t = new Thread(new Runnable() { |
| 515 |
@Override |
| 516 |
public void run() { |
| 517 |
try { |
| 518 |
cb.await(); |
| 519 |
} catch (InterruptedException e) { |
| 520 |
return; |
| 521 |
} catch (BrokenBarrierException e) { |
| 522 |
return; |
| 523 |
} |
| 524 |
for (int i = 0; i < 1000000; i++) { |
| 525 |
rs.onNext(i); |
| 526 |
if (i % 10000 == 0) { |
| 527 |
try { |
| 528 |
Thread.sleep(1); |
| 529 |
} catch (InterruptedException e) { |
| 530 |
return; |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
rs.onComplete(); |
| 535 |
System.out.println("Replay fill Thread finished!"); |
| 536 |
} |
| 537 |
}); |
| 538 |
t.start(); |
| 539 |
try { |
| 540 |
cb.await(); |
| 541 |
} catch (InterruptedException e) { |
| 542 |
return; |
| 543 |
} catch (BrokenBarrierException e) { |
| 544 |
return; |
| 545 |
} |
| 546 |
for (; !rs.hasThrowable() && !rs.hasComplete();) { |
| 547 |
rs.size(); // can't use value so just call to detect hangs |
| 548 |
rs.hasValue(); // can't use value so just call to detect hangs |
| 549 |
Object[] values = rs.getValues(); |
| 550 |
for (int i = 0; i < values.length - 1; i++) { |
| 551 |
Integer v1 = (Integer)values[i]; |
| 552 |
Integer v2 = (Integer)values[i + 1]; |
| 553 |
assertEquals(1, v2 - v1); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
t.join(); |
| 558 |
} |
| 559 |
} |