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