| 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.observable; |
| 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 io.reactivex.rxjava3.disposables.Disposable; |
| 24 |
import org.junit.*; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.observers.DefaultObserver; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class ObservableSerializeTest extends RxJavaTest { |
| 31 |
|
| 32 |
Observer<String> observer; |
| 33 |
|
| 34 |
@Before |
| 35 |
public void before() { |
| 36 |
observer = TestHelper.mockObserver(); |
| 37 |
} |
| 38 |
|
| 39 |
@Test |
| 40 |
public void singleThreadedBasic() { |
| 41 |
TestSingleThreadedObservable onSubscribe = new TestSingleThreadedObservable("one", "two", "three"); |
| 42 |
Observable<String> w = Observable.unsafeCreate(onSubscribe); |
| 43 |
|
| 44 |
w.serialize().subscribe(observer); |
| 45 |
onSubscribe.waitToFinish(); |
| 46 |
|
| 47 |
verify(observer, times(1)).onNext("one"); |
| 48 |
verify(observer, times(1)).onNext("two"); |
| 49 |
verify(observer, times(1)).onNext("three"); |
| 50 |
verify(observer, never()).onError(any(Throwable.class)); |
| 51 |
verify(observer, times(1)).onComplete(); |
| 52 |
// non-deterministic because unsubscribe happens after 'waitToFinish' releases |
| 53 |
// so commenting out for now as this is not a critical thing to test here |
| 54 |
// verify(s, times(1)).unsubscribe(); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void multiThreadedBasic() { |
| 59 |
TestMultiThreadedObservable onSubscribe = new TestMultiThreadedObservable("one", "two", "three"); |
| 60 |
Observable<String> w = Observable.unsafeCreate(onSubscribe); |
| 61 |
|
| 62 |
BusyObserver busyobserver = new BusyObserver(); |
| 63 |
|
| 64 |
w.serialize().subscribe(busyobserver); |
| 65 |
onSubscribe.waitToFinish(); |
| 66 |
|
| 67 |
assertEquals(3, busyobserver.onNextCount.get()); |
| 68 |
assertFalse(busyobserver.onError); |
| 69 |
assertTrue(busyobserver.onComplete); |
| 70 |
// non-deterministic because unsubscribe happens after 'waitToFinish' releases |
| 71 |
// so commenting out for now as this is not a critical thing to test here |
| 72 |
// verify(s, times(1)).unsubscribe(); |
| 73 |
|
| 74 |
// we can have concurrency ... |
| 75 |
assertTrue(onSubscribe.maxConcurrentThreads.get() > 1); |
| 76 |
// ... but the onNext execution should be single threaded |
| 77 |
assertEquals(1, busyobserver.maxConcurrentThreads.get()); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void multiThreadedWithNPEFlaky() throws InterruptedException { |
| 82 |
int max = 9; |
| 83 |
for (int i = 0; i <= max; i++) { |
| 84 |
try { |
| 85 |
multiThreadedWithNPE(); |
| 86 |
return; |
| 87 |
} catch (AssertionError ex) { |
| 88 |
if (i == max) { |
| 89 |
throw ex; |
| 90 |
} |
| 91 |
} |
| 92 |
Thread.sleep((long)(1000 * Math.random() + 100)); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void multiThreadedWithNPE() { |
| 97 |
TestMultiThreadedObservable onSubscribe = new TestMultiThreadedObservable("one", "two", "three", null); |
| 98 |
Observable<String> w = Observable.unsafeCreate(onSubscribe); |
| 99 |
|
| 100 |
BusyObserver busyobserver = new BusyObserver(); |
| 101 |
|
| 102 |
w.serialize().subscribe(busyobserver); |
| 103 |
onSubscribe.waitToFinish(); |
| 104 |
|
| 105 |
System.out.println("maxConcurrentThreads: " + onSubscribe.maxConcurrentThreads.get()); |
| 106 |
|
| 107 |
// we can't know how many onNext calls will occur since they each run on a separate thread |
| 108 |
// that depends on thread scheduling so 0, 1, 2 and 3 are all valid options |
| 109 |
// assertEquals(3, busyobserver.onNextCount.get()); |
| 110 |
assertTrue(busyobserver.onNextCount.get() < 4); |
| 111 |
assertTrue(busyobserver.onError); |
| 112 |
// no onComplete because onError was invoked |
| 113 |
assertFalse(busyobserver.onComplete); |
| 114 |
// non-deterministic because unsubscribe happens after 'waitToFinish' releases |
| 115 |
// so commenting out for now as this is not a critical thing to test here |
| 116 |
//verify(s, times(1)).unsubscribe(); |
| 117 |
|
| 118 |
// we can have concurrency ... |
| 119 |
assertTrue(onSubscribe.maxConcurrentThreads.get() > 1); |
| 120 |
// ... but the onNext execution should be single threaded |
| 121 |
assertEquals(1, busyobserver.maxConcurrentThreads.get()); |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void multiThreadedWithNPEinMiddleFlaky() throws InterruptedException { |
| 126 |
int max = 9; |
| 127 |
for (int i = 0; i <= max; i++) { |
| 128 |
try { |
| 129 |
multiThreadedWithNPEinMiddle(); |
| 130 |
return; |
| 131 |
} catch (AssertionError ex) { |
| 132 |
if (i == max) { |
| 133 |
throw ex; |
| 134 |
} |
| 135 |
} |
| 136 |
Thread.sleep((long)(1000 * Math.random() + 100)); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void multiThreadedWithNPEinMiddle() { |
| 141 |
boolean lessThan9 = false; |
| 142 |
for (int i = 0; i < 3; i++) { |
| 143 |
TestMultiThreadedObservable onSubscribe = new TestMultiThreadedObservable("one", "two", "three", null, "four", "five", "six", "seven", "eight", "nine"); |
| 144 |
Observable<String> w = Observable.unsafeCreate(onSubscribe); |
| 145 |
|
| 146 |
BusyObserver busyobserver = new BusyObserver(); |
| 147 |
|
| 148 |
w.serialize().subscribe(busyobserver); |
| 149 |
onSubscribe.waitToFinish(); |
| 150 |
|
| 151 |
System.out.println("maxConcurrentThreads: " + onSubscribe.maxConcurrentThreads.get()); |
| 152 |
// this should not always be the full number of items since the error should (very often) |
| 153 |
// stop it before it completes all 9 |
| 154 |
System.out.println("onNext count: " + busyobserver.onNextCount.get()); |
| 155 |
if (busyobserver.onNextCount.get() < 9) { |
| 156 |
lessThan9 = true; |
| 157 |
} |
| 158 |
assertTrue(busyobserver.onError); |
| 159 |
// no onComplete because onError was invoked |
| 160 |
assertFalse(busyobserver.onComplete); |
| 161 |
// non-deterministic because unsubscribe happens after 'waitToFinish' releases |
| 162 |
// so commenting out for now as this is not a critical thing to test here |
| 163 |
// verify(s, times(1)).unsubscribe(); |
| 164 |
|
| 165 |
// we can have concurrency ... |
| 166 |
int n = onSubscribe.maxConcurrentThreads.get(); |
| 167 |
assertTrue("" + n, n > 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 |
public static class OnNextThread implements Runnable { |
| 178 |
|
| 179 |
private final DefaultObserver<String> observer; |
| 180 |
private final int numStringsToSend; |
| 181 |
|
| 182 |
OnNextThread(DefaultObserver<String> observer, int numStringsToSend) { |
| 183 |
this.observer = observer; |
| 184 |
this.numStringsToSend = numStringsToSend; |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
public void run() { |
| 189 |
for (int i = 0; i < numStringsToSend; i++) { |
| 190 |
observer.onNext("aString"); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* A thread that will call onError or onNext. |
| 197 |
*/ |
| 198 |
public static class CompletionThread implements Runnable { |
| 199 |
|
| 200 |
private final DefaultObserver<String> observer; |
| 201 |
private final TestConcurrencyobserverEvent event; |
| 202 |
private final Future<?>[] waitOnThese; |
| 203 |
|
| 204 |
CompletionThread(DefaultObserver<String> observer, TestConcurrencyobserverEvent event, Future<?>... waitOnThese) { |
| 205 |
this.observer = observer; |
| 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 |
observer.onError(new RuntimeException("mocked exception")); |
| 226 |
} else if (event == TestConcurrencyobserverEvent.onComplete) { |
| 227 |
observer.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 |
static class TestSingleThreadedObservable implements ObservableSource<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 Observer<? super String> observer) { |
| 254 |
observer.onSubscribe(Disposable.empty()); |
| 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 |
observer.onNext(s); |
| 265 |
} |
| 266 |
observer.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 ObservableSource<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 Observer<? super String> observer) { |
| 305 |
observer.onSubscribe(Disposable.empty()); |
| 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 |
System.out.println("TestMultiThreadedObservable onNext: " + s); |
| 328 |
} |
| 329 |
observer.onNext(s); |
| 330 |
// capture 'maxThreads' |
| 331 |
int concurrentThreads = threadsRunning.get(); |
| 332 |
int maxThreads = maxConcurrentThreads.get(); |
| 333 |
if (concurrentThreads > maxThreads) { |
| 334 |
maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads); |
| 335 |
} |
| 336 |
} catch (Throwable e) { |
| 337 |
observer.onError(e); |
| 338 |
} finally { |
| 339 |
threadsRunning.decrementAndGet(); |
| 340 |
} |
| 341 |
} |
| 342 |
}); |
| 343 |
} |
| 344 |
// we are done spawning threads |
| 345 |
threadPool.shutdown(); |
| 346 |
} catch (Throwable e) { |
| 347 |
throw new RuntimeException(e); |
| 348 |
} |
| 349 |
|
| 350 |
// wait until all threads are done, then mark it as COMPLETED |
| 351 |
try { |
| 352 |
// wait for all the threads to finish |
| 353 |
threadPool.awaitTermination(2, TimeUnit.SECONDS); |
| 354 |
} catch (InterruptedException e) { |
| 355 |
throw new RuntimeException(e); |
| 356 |
} |
| 357 |
observer.onComplete(); |
| 358 |
} |
| 359 |
}); |
| 360 |
System.out.println("starting TestMultiThreadedObservable thread"); |
| 361 |
t.start(); |
| 362 |
System.out.println("done starting TestMultiThreadedObservable thread"); |
| 363 |
} |
| 364 |
|
| 365 |
public void waitToFinish() { |
| 366 |
try { |
| 367 |
t.join(); |
| 368 |
} catch (InterruptedException e) { |
| 369 |
throw new RuntimeException(e); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
private static class BusyObserver extends DefaultObserver<String> { |
| 375 |
volatile boolean onComplete; |
| 376 |
volatile boolean onError; |
| 377 |
AtomicInteger onNextCount = new AtomicInteger(); |
| 378 |
AtomicInteger threadsRunning = new AtomicInteger(); |
| 379 |
AtomicInteger maxConcurrentThreads = new AtomicInteger(); |
| 380 |
|
| 381 |
@Override |
| 382 |
public void onComplete() { |
| 383 |
threadsRunning.incrementAndGet(); |
| 384 |
|
| 385 |
System.out.println(">>> Busyobserver received onComplete"); |
| 386 |
onComplete = true; |
| 387 |
|
| 388 |
int concurrentThreads = threadsRunning.get(); |
| 389 |
int maxThreads = maxConcurrentThreads.get(); |
| 390 |
if (concurrentThreads > maxThreads) { |
| 391 |
maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads); |
| 392 |
} |
| 393 |
threadsRunning.decrementAndGet(); |
| 394 |
} |
| 395 |
|
| 396 |
@Override |
| 397 |
public void onError(Throwable e) { |
| 398 |
threadsRunning.incrementAndGet(); |
| 399 |
|
| 400 |
System.out.println(">>> Busyobserver received onError: " + e.getMessage()); |
| 401 |
onError = true; |
| 402 |
|
| 403 |
int concurrentThreads = threadsRunning.get(); |
| 404 |
int maxThreads = maxConcurrentThreads.get(); |
| 405 |
if (concurrentThreads > maxThreads) { |
| 406 |
maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads); |
| 407 |
} |
| 408 |
threadsRunning.decrementAndGet(); |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public void onNext(String args) { |
| 413 |
threadsRunning.incrementAndGet(); |
| 414 |
try { |
| 415 |
onNextCount.incrementAndGet(); |
| 416 |
System.out.println(">>> Busyobserver received onNext: " + args); |
| 417 |
try { |
| 418 |
// simulate doing something computational |
| 419 |
Thread.sleep(200); |
| 420 |
} catch (InterruptedException e) { |
| 421 |
e.printStackTrace(); |
| 422 |
} |
| 423 |
} finally { |
| 424 |
// capture 'maxThreads' |
| 425 |
int concurrentThreads = threadsRunning.get(); |
| 426 |
int maxThreads = maxConcurrentThreads.get(); |
| 427 |
if (concurrentThreads > maxThreads) { |
| 428 |
maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads); |
| 429 |
} |
| 430 |
threadsRunning.decrementAndGet(); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
} |
| 435 |
} |