| 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.subscribers; |
| 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.List; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.*; |
| 25 |
import org.reactivestreams.*; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 29 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 30 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 31 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.testsupport.*; |
| 34 |
|
| 35 |
public class SerializedSubscriberTest extends RxJavaTest { |
| 36 |
|
| 37 |
Subscriber<String> subscriber; |
| 38 |
|
| 39 |
@Before |
| 40 |
public void before() { |
| 41 |
subscriber = TestHelper.mockSubscriber(); |
| 42 |
} |
| 43 |
|
| 44 |
private Subscriber<String> serializedSubscriber(Subscriber<String> subscriber) { |
| 45 |
return new SerializedSubscriber<>(subscriber); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void singleThreadedBasic() { |
| 50 |
TestSingleThreadedPublisher onSubscribe = new TestSingleThreadedPublisher("one", "two", "three"); |
| 51 |
Flowable<String> w = Flowable.unsafeCreate(onSubscribe); |
| 52 |
|
| 53 |
Subscriber<String> aw = serializedSubscriber(subscriber); |
| 54 |
|
| 55 |
w.subscribe(aw); |
| 56 |
onSubscribe.waitToFinish(); |
| 57 |
|
| 58 |
verify(subscriber, times(1)).onNext("one"); |
| 59 |
verify(subscriber, times(1)).onNext("two"); |
| 60 |
verify(subscriber, times(1)).onNext("three"); |
| 61 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 62 |
verify(subscriber, times(1)).onComplete(); |
| 63 |
// non-deterministic because unsubscribe happens after 'waitToFinish' releases |
| 64 |
// so commenting out for now as this is not a critical thing to test here |
| 65 |
// verify(s, times(1)).unsubscribe(); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void multiThreadedBasic() { |
| 70 |
TestMultiThreadedObservable onSubscribe = new TestMultiThreadedObservable("one", "two", "three"); |
| 71 |
Flowable<String> w = Flowable.unsafeCreate(onSubscribe); |
| 72 |
|
| 73 |
BusySubscriber busySubscriber = new BusySubscriber(); |
| 74 |
Subscriber<String> aw = serializedSubscriber(busySubscriber); |
| 75 |
|
| 76 |
w.subscribe(aw); |
| 77 |
onSubscribe.waitToFinish(); |
| 78 |
|
| 79 |
assertEquals(3, busySubscriber.onNextCount.get()); |
| 80 |
assertFalse(busySubscriber.onError); |
| 81 |
assertTrue(busySubscriber.onComplete); |
| 82 |
// non-deterministic because unsubscribe happens after 'waitToFinish' releases |
| 83 |
// so commenting out for now as this is not a critical thing to test here |
| 84 |
// verify(s, times(1)).unsubscribe(); |
| 85 |
|
| 86 |
// we can have concurrency ... |
| 87 |
assertTrue(onSubscribe.maxConcurrentThreads.get() > 1); |
| 88 |
// ... but the onNext execution should be single threaded |
| 89 |
assertEquals(1, busySubscriber.maxConcurrentThreads.get()); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void multiThreadedWithNPE() throws InterruptedException { |
| 94 |
TestMultiThreadedObservable onSubscribe = new TestMultiThreadedObservable("one", "two", "three", null); |
| 95 |
Flowable<String> w = Flowable.unsafeCreate(onSubscribe); |
| 96 |
|
| 97 |
BusySubscriber busySubscriber = new BusySubscriber(); |
| 98 |
Subscriber<String> aw = serializedSubscriber(busySubscriber); |
| 99 |
|
| 100 |
w.subscribe(aw); |
| 101 |
onSubscribe.waitToFinish(); |
| 102 |
busySubscriber.terminalEvent.await(); |
| 103 |
|
| 104 |
System.out.println("OnSubscribe maxConcurrentThreads: " + onSubscribe.maxConcurrentThreads.get() + " Subscriber maxConcurrentThreads: " + busySubscriber.maxConcurrentThreads.get()); |
| 105 |
|
| 106 |
// we can't know how many onNext calls will occur since they each run on a separate thread |
| 107 |
// that depends on thread scheduling so 0, 1, 2 and 3 are all valid options |
| 108 |
// assertEquals(3, busySubscriber.onNextCount.get()); |
| 109 |
assertTrue(busySubscriber.onNextCount.get() < 4); |
| 110 |
assertTrue(busySubscriber.onError); |
| 111 |
// no onComplete because onError was invoked |
| 112 |
assertFalse(busySubscriber.onComplete); |
| 113 |
// non-deterministic because unsubscribe happens after 'waitToFinish' releases |
| 114 |
// so commenting out for now as this is not a critical thing to test here |
| 115 |
//verify(s, times(1)).unsubscribe(); |
| 116 |
|
| 117 |
// we can have concurrency ... |
| 118 |
assertTrue(onSubscribe.maxConcurrentThreads.get() > 1); |
| 119 |
// ... but the onNext execution should be single threaded |
| 120 |
assertEquals(1, busySubscriber.maxConcurrentThreads.get()); |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void multiThreadedWithNPEinMiddle() { |
| 125 |
int n = 10; |
| 126 |
for (int i = 0; i < n; i++) { |
| 127 |
TestMultiThreadedObservable onSubscribe = new TestMultiThreadedObservable("one", "two", "three", null, |
| 128 |
"four", "five", "six", "seven", "eight", "nine"); |
| 129 |
Flowable<String> w = Flowable.unsafeCreate(onSubscribe); |
| 130 |
|
| 131 |
BusySubscriber busySubscriber = new BusySubscriber(); |
| 132 |
Subscriber<String> aw = serializedSubscriber(busySubscriber); |
| 133 |
|
| 134 |
w.subscribe(aw); |
| 135 |
onSubscribe.waitToFinish(); |
| 136 |
|
| 137 |
System.out.println("OnSubscribe maxConcurrentThreads: " + onSubscribe.maxConcurrentThreads.get() + " Subscriber maxConcurrentThreads: " + busySubscriber.maxConcurrentThreads.get()); |
| 138 |
|
| 139 |
// we can have concurrency ... |
| 140 |
assertTrue(onSubscribe.maxConcurrentThreads.get() > 1); |
| 141 |
// ... but the onNext execution should be single threaded |
| 142 |
assertEquals(1, busySubscriber.maxConcurrentThreads.get()); |
| 143 |
|
| 144 |
// this should not be the full number of items since the error should stop it before it completes all 9 |
| 145 |
System.out.println("onNext count: " + busySubscriber.onNextCount.get()); |
| 146 |
assertFalse(busySubscriber.onComplete); |
| 147 |
assertTrue(busySubscriber.onError); |
| 148 |
assertTrue(busySubscriber.onNextCount.get() < 9); |
| 149 |
// no onComplete because onError was invoked |
| 150 |
// non-deterministic because unsubscribe happens after 'waitToFinish' releases |
| 151 |
// so commenting out for now as this is not a critical thing to test here |
| 152 |
// verify(s, times(1)).unsubscribe(); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* A non-realistic use case that tries to expose thread-safety issues by throwing lots of out-of-order |
| 158 |
* events on many threads. |
| 159 |
*/ |
| 160 |
@Test |
| 161 |
public void runOutOfOrderConcurrencyTest() { |
| 162 |
ExecutorService tp = Executors.newFixedThreadPool(20); |
| 163 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 164 |
try { |
| 165 |
TestConcurrencySubscriber tw = new TestConcurrencySubscriber(); |
| 166 |
// we need Synchronized + SafeSubscriber to handle synchronization plus life-cycle |
| 167 |
Subscriber<String> w = serializedSubscriber(new SafeSubscriber<>(tw)); |
| 168 |
|
| 169 |
Future<?> f1 = tp.submit(new OnNextThread(w, 12000)); |
| 170 |
Future<?> f2 = tp.submit(new OnNextThread(w, 5000)); |
| 171 |
Future<?> f3 = tp.submit(new OnNextThread(w, 75000)); |
| 172 |
Future<?> f4 = tp.submit(new OnNextThread(w, 13500)); |
| 173 |
Future<?> f5 = tp.submit(new OnNextThread(w, 22000)); |
| 174 |
Future<?> f6 = tp.submit(new OnNextThread(w, 15000)); |
| 175 |
Future<?> f7 = tp.submit(new OnNextThread(w, 7500)); |
| 176 |
Future<?> f8 = tp.submit(new OnNextThread(w, 23500)); |
| 177 |
|
| 178 |
Future<?> f10 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onComplete, f1, f2, f3, f4)); |
| 179 |
try { |
| 180 |
Thread.sleep(1); |
| 181 |
} catch (InterruptedException e) { |
| 182 |
// ignore |
| 183 |
} |
| 184 |
Future<?> f11 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onComplete, f4, f6, f7)); |
| 185 |
Future<?> f12 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onComplete, f4, f6, f7)); |
| 186 |
Future<?> f13 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onComplete, f4, f6, f7)); |
| 187 |
Future<?> f14 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onComplete, f4, f6, f7)); |
| 188 |
// // the next 4 onError events should wait on same as f10 |
| 189 |
Future<?> f15 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onError, f1, f2, f3, f4)); |
| 190 |
Future<?> f16 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onError, f1, f2, f3, f4)); |
| 191 |
Future<?> f17 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onError, f1, f2, f3, f4)); |
| 192 |
Future<?> f18 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onError, f1, f2, f3, f4)); |
| 193 |
|
| 194 |
waitOnThreads(f1, f2, f3, f4, f5, f6, f7, f8, f10, f11, f12, f13, f14, f15, f16, f17, f18); |
| 195 |
@SuppressWarnings("unused") |
| 196 |
int numNextEvents = tw.assertEvents(null); // no check of type since we don't want to test barging results here, just interleaving behavior |
| 197 |
// System.out.println("Number of events executed: " + numNextEvents); |
| 198 |
|
| 199 |
for (int i = 0; i < errors.size(); i++) { |
| 200 |
TestHelper.assertUndeliverable(errors, i, RuntimeException.class); |
| 201 |
} |
| 202 |
} catch (Throwable e) { |
| 203 |
fail("Concurrency test failed: " + e.getMessage()); |
| 204 |
e.printStackTrace(); |
| 205 |
} finally { |
| 206 |
tp.shutdown(); |
| 207 |
try { |
| 208 |
tp.awaitTermination(5000, TimeUnit.MILLISECONDS); |
| 209 |
} catch (InterruptedException e) { |
| 210 |
e.printStackTrace(); |
| 211 |
} |
| 212 |
|
| 213 |
RxJavaPlugins.reset(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
@Test |
| 218 |
public void runConcurrencyTest() { |
| 219 |
ExecutorService tp = Executors.newFixedThreadPool(20); |
| 220 |
try { |
| 221 |
TestConcurrencySubscriber tw = new TestConcurrencySubscriber(); |
| 222 |
// we need Synchronized + SafeSubscriber to handle synchronization plus life-cycle |
| 223 |
Subscriber<String> w = serializedSubscriber(new SafeSubscriber<>(tw)); |
| 224 |
w.onSubscribe(new BooleanSubscription()); |
| 225 |
|
| 226 |
Future<?> f1 = tp.submit(new OnNextThread(w, 12000)); |
| 227 |
Future<?> f2 = tp.submit(new OnNextThread(w, 5000)); |
| 228 |
Future<?> f3 = tp.submit(new OnNextThread(w, 75000)); |
| 229 |
Future<?> f4 = tp.submit(new OnNextThread(w, 13500)); |
| 230 |
Future<?> f5 = tp.submit(new OnNextThread(w, 22000)); |
| 231 |
Future<?> f6 = tp.submit(new OnNextThread(w, 15000)); |
| 232 |
Future<?> f7 = tp.submit(new OnNextThread(w, 7500)); |
| 233 |
Future<?> f8 = tp.submit(new OnNextThread(w, 23500)); |
| 234 |
|
| 235 |
// 12000 + 5000 + 75000 + 13500 + 22000 + 15000 + 7500 + 23500 = 173500 |
| 236 |
|
| 237 |
Future<?> f10 = tp.submit(new CompletionThread(w, TestConcurrencySubscriberEvent.onComplete, f1, f2, f3, f4, f5, f6, f7, f8)); |
| 238 |
try { |
| 239 |
Thread.sleep(1); |
| 240 |
} catch (InterruptedException e) { |
| 241 |
// ignore |
| 242 |
} |
| 243 |
|
| 244 |
waitOnThreads(f1, f2, f3, f4, f5, f6, f7, f8, f10); |
| 245 |
int numNextEvents = tw.assertEvents(null); // no check of type since we don't want to test barging results here, just interleaving behavior |
| 246 |
assertEquals(173500, numNextEvents); |
| 247 |
// System.out.println("Number of events executed: " + numNextEvents); |
| 248 |
} catch (Throwable e) { |
| 249 |
fail("Concurrency test failed: " + e.getMessage()); |
| 250 |
e.printStackTrace(); |
| 251 |
} finally { |
| 252 |
tp.shutdown(); |
| 253 |
try { |
| 254 |
tp.awaitTermination(25000, TimeUnit.MILLISECONDS); |
| 255 |
} catch (InterruptedException e) { |
| 256 |
e.printStackTrace(); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Test that a notification does not get delayed in the queue waiting for the next event to push it through. |
| 263 |
* |
| 264 |
* @throws InterruptedException if the await is interrupted |
| 265 |
*/ |
| 266 |
@Ignore("this is non-deterministic ... haven't figured out what's wrong with the test yet (benjchristensen: July 2014)") |
| 267 |
@Test |
| 268 |
public void notificationDelay() throws InterruptedException { |
| 269 |
ExecutorService tp1 = Executors.newFixedThreadPool(1); |
| 270 |
ExecutorService tp2 = Executors.newFixedThreadPool(1); |
| 271 |
try { |
| 272 |
int n = 10; |
| 273 |
for (int i = 0; i < n; i++) { |
| 274 |
final CountDownLatch firstOnNext = new CountDownLatch(1); |
| 275 |
final CountDownLatch onNextCount = new CountDownLatch(2); |
| 276 |
final CountDownLatch latch = new CountDownLatch(1); |
| 277 |
final CountDownLatch running = new CountDownLatch(2); |
| 278 |
|
| 279 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(new DefaultSubscriber<String>() { |
| 280 |
|
| 281 |
@Override |
| 282 |
public void onComplete() { |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public void onError(Throwable e) { |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public void onNext(String t) { |
| 293 |
firstOnNext.countDown(); |
| 294 |
// force it to take time when delivering so the second one is enqueued |
| 295 |
try { |
| 296 |
latch.await(); |
| 297 |
} catch (InterruptedException e) { |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
}); |
| 302 |
Subscriber<String> subscriber = serializedSubscriber(ts); |
| 303 |
|
| 304 |
Future<?> f1 = tp1.submit(new OnNextThread(subscriber, 1, onNextCount, running)); |
| 305 |
Future<?> f2 = tp2.submit(new OnNextThread(subscriber, 1, onNextCount, running)); |
| 306 |
|
| 307 |
running.await(); // let one of the OnNextThread actually run before proceeding |
| 308 |
|
| 309 |
firstOnNext.await(); |
| 310 |
|
| 311 |
Thread t1 = ts.lastThread(); |
| 312 |
System.out.println("first onNext on thread: " + t1); |
| 313 |
|
| 314 |
latch.countDown(); |
| 315 |
|
| 316 |
waitOnThreads(f1, f2); |
| 317 |
// not completed yet |
| 318 |
|
| 319 |
assertEquals(2, ts.values().size()); |
| 320 |
|
| 321 |
Thread t2 = ts.lastThread(); |
| 322 |
System.out.println("second onNext on thread: " + t2); |
| 323 |
|
| 324 |
assertSame(t1, t2); |
| 325 |
|
| 326 |
System.out.println(ts.values()); |
| 327 |
subscriber.onComplete(); |
| 328 |
System.out.println(ts.values()); |
| 329 |
} |
| 330 |
} finally { |
| 331 |
tp1.shutdown(); |
| 332 |
tp2.shutdown(); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Demonstrates thread starvation problem. |
| 338 |
* |
| 339 |
* No solution on this for now. Trade-off in this direction as per https://github.com/ReactiveX/RxJava/issues/998#issuecomment-38959474 |
| 340 |
* Probably need backpressure for this to work |
| 341 |
* |
| 342 |
* When using SynchronizedSubscriber we get this output: |
| 343 |
* |
| 344 |
* {@code p1: 18 p2: 68 =>} should be close to each other unless we have thread starvation |
| 345 |
* |
| 346 |
* When using SerializedSubscriber we get: |
| 347 |
* |
| 348 |
* {@code p1: 1 p2: 2445261 =>} should be close to each other unless we have thread starvation |
| 349 |
* |
| 350 |
* This demonstrates how SynchronizedSubscriber balances back and forth better, and blocks emission. |
| 351 |
* The real issue in this example is the async buffer-bloat, so we need backpressure. |
| 352 |
* |
| 353 |
* |
| 354 |
* @throws InterruptedException if the await is interrupted |
| 355 |
*/ |
| 356 |
@Ignore("Demonstrates thread starvation problem. Read JavaDoc") |
| 357 |
@Test |
| 358 |
public void threadStarvation() throws InterruptedException { |
| 359 |
|
| 360 |
TestSubscriber<String> ts = new TestSubscriber<>(new DefaultSubscriber<String>() { |
| 361 |
|
| 362 |
@Override |
| 363 |
public void onComplete() { |
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public void onError(Throwable e) { |
| 369 |
|
| 370 |
} |
| 371 |
|
| 372 |
@Override |
| 373 |
public void onNext(String t) { |
| 374 |
// force it to take time when delivering |
| 375 |
try { |
| 376 |
Thread.sleep(1); |
| 377 |
} catch (InterruptedException e) { |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
}); |
| 382 |
final Subscriber<String> subscriber = serializedSubscriber(ts); |
| 383 |
|
| 384 |
AtomicInteger p1 = new AtomicInteger(); |
| 385 |
AtomicInteger p2 = new AtomicInteger(); |
| 386 |
|
| 387 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 388 |
ResourceSubscriber<String> as1 = new ResourceSubscriber<String>() { |
| 389 |
@Override |
| 390 |
public void onNext(String t) { |
| 391 |
subscriber.onNext(t); |
| 392 |
} |
| 393 |
|
| 394 |
@Override |
| 395 |
public void onError(Throwable t) { |
| 396 |
RxJavaPlugins.onError(t); |
| 397 |
} |
| 398 |
|
| 399 |
@Override |
| 400 |
public void onComplete() { |
| 401 |
|
| 402 |
} |
| 403 |
}; |
| 404 |
|
| 405 |
ResourceSubscriber<String> as2 = new ResourceSubscriber<String>() { |
| 406 |
@Override |
| 407 |
public void onNext(String t) { |
| 408 |
subscriber.onNext(t); |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public void onError(Throwable t) { |
| 413 |
RxJavaPlugins.onError(t); |
| 414 |
} |
| 415 |
|
| 416 |
@Override |
| 417 |
public void onComplete() { |
| 418 |
|
| 419 |
} |
| 420 |
}; |
| 421 |
|
| 422 |
infinite(p1).subscribe(as1); |
| 423 |
infinite(p2).subscribe(as2); |
| 424 |
|
| 425 |
Thread.sleep(100); |
| 426 |
|
| 427 |
System.out.println("p1: " + p1.get() + " p2: " + p2.get() + " => should be close to each other unless we have thread starvation"); |
| 428 |
assertEquals(p1.get(), p2.get(), 10000); // fairly distributed within 10000 of each other |
| 429 |
|
| 430 |
as1.dispose(); |
| 431 |
as2.dispose(); |
| 432 |
} |
| 433 |
|
| 434 |
private static void waitOnThreads(Future<?>... futures) { |
| 435 |
for (Future<?> f : futures) { |
| 436 |
try { |
| 437 |
f.get(20, TimeUnit.SECONDS); |
| 438 |
} catch (Throwable e) { |
| 439 |
System.err.println("Failed while waiting on future."); |
| 440 |
e.printStackTrace(); |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
private static Flowable<String> infinite(final AtomicInteger produced) { |
| 446 |
return Flowable.unsafeCreate(new Publisher<String>() { |
| 447 |
|
| 448 |
@Override |
| 449 |
public void subscribe(Subscriber<? super String> s) { |
| 450 |
BooleanSubscription bs = new BooleanSubscription(); |
| 451 |
s.onSubscribe(bs); |
| 452 |
while (!bs.isCancelled()) { |
| 453 |
s.onNext("onNext"); |
| 454 |
produced.incrementAndGet(); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
}).subscribeOn(Schedulers.newThread()); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* A thread that will pass data to onNext. |
| 463 |
*/ |
| 464 |
public static class OnNextThread implements Runnable { |
| 465 |
|
| 466 |
private final CountDownLatch latch; |
| 467 |
private final Subscriber<String> subscriber; |
| 468 |
private final int numStringsToSend; |
| 469 |
final AtomicInteger produced; |
| 470 |
private final CountDownLatch running; |
| 471 |
|
| 472 |
OnNextThread(Subscriber<String> subscriber, int numStringsToSend, CountDownLatch latch, CountDownLatch running) { |
| 473 |
this(subscriber, numStringsToSend, new AtomicInteger(), latch, running); |
| 474 |
} |
| 475 |
|
| 476 |
OnNextThread(Subscriber<String> subscriber, int numStringsToSend, AtomicInteger produced) { |
| 477 |
this(subscriber, numStringsToSend, produced, null, null); |
| 478 |
} |
| 479 |
|
| 480 |
OnNextThread(Subscriber<String> subscriber, int numStringsToSend, AtomicInteger produced, CountDownLatch latch, CountDownLatch running) { |
| 481 |
this.subscriber = subscriber; |
| 482 |
this.numStringsToSend = numStringsToSend; |
| 483 |
this.produced = produced; |
| 484 |
this.latch = latch; |
| 485 |
this.running = running; |
| 486 |
} |
| 487 |
|
| 488 |
OnNextThread(Subscriber<String> subscriber, int numStringsToSend) { |
| 489 |
this(subscriber, numStringsToSend, new AtomicInteger()); |
| 490 |
} |
| 491 |
|
| 492 |
@Override |
| 493 |
public void run() { |
| 494 |
if (running != null) { |
| 495 |
running.countDown(); |
| 496 |
} |
| 497 |
for (int i = 0; i < numStringsToSend; i++) { |
| 498 |
subscriber.onNext(Thread.currentThread().getId() + "-" + i); |
| 499 |
if (latch != null) { |
| 500 |
latch.countDown(); |
| 501 |
} |
| 502 |
produced.incrementAndGet(); |
| 503 |
} |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
* A thread that will call onError or onNext. |
| 509 |
*/ |
| 510 |
public static class CompletionThread implements Runnable { |
| 511 |
|
| 512 |
private final Subscriber<String> subscriber; |
| 513 |
private final TestConcurrencySubscriberEvent event; |
| 514 |
private final Future<?>[] waitOnThese; |
| 515 |
|
| 516 |
CompletionThread(Subscriber<String> Subscriber, TestConcurrencySubscriberEvent event, Future<?>... waitOnThese) { |
| 517 |
this.subscriber = Subscriber; |
| 518 |
this.event = event; |
| 519 |
this.waitOnThese = waitOnThese; |
| 520 |
} |
| 521 |
|
| 522 |
@Override |
| 523 |
public void run() { |
| 524 |
/* if we have 'waitOnThese' futures, we'll wait on them before proceeding */ |
| 525 |
if (waitOnThese != null) { |
| 526 |
for (Future<?> f : waitOnThese) { |
| 527 |
try { |
| 528 |
f.get(); |
| 529 |
} catch (Throwable e) { |
| 530 |
System.err.println("Error while waiting on future in CompletionThread"); |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
/* send the event */ |
| 536 |
if (event == TestConcurrencySubscriberEvent.onError) { |
| 537 |
subscriber.onError(new RuntimeException("mocked exception")); |
| 538 |
} else if (event == TestConcurrencySubscriberEvent.onComplete) { |
| 539 |
subscriber.onComplete(); |
| 540 |
|
| 541 |
} else { |
| 542 |
throw new IllegalArgumentException("Expecting either onError or onComplete"); |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
enum TestConcurrencySubscriberEvent { |
| 548 |
onComplete, onError, onNext |
| 549 |
} |
| 550 |
|
| 551 |
private static class TestConcurrencySubscriber extends DefaultSubscriber<String> { |
| 552 |
|
| 553 |
/** |
| 554 |
* Used to store the order and number of events received. |
| 555 |
*/ |
| 556 |
private final LinkedBlockingQueue<TestConcurrencySubscriberEvent> events = new LinkedBlockingQueue<>(); |
| 557 |
private final int waitTime; |
| 558 |
|
| 559 |
@SuppressWarnings("unused") |
| 560 |
TestConcurrencySubscriber(int waitTimeInNext) { |
| 561 |
this.waitTime = waitTimeInNext; |
| 562 |
} |
| 563 |
|
| 564 |
TestConcurrencySubscriber() { |
| 565 |
this.waitTime = 0; |
| 566 |
} |
| 567 |
|
| 568 |
@Override |
| 569 |
public void onComplete() { |
| 570 |
events.add(TestConcurrencySubscriberEvent.onComplete); |
| 571 |
} |
| 572 |
|
| 573 |
@Override |
| 574 |
public void onError(Throwable e) { |
| 575 |
events.add(TestConcurrencySubscriberEvent.onError); |
| 576 |
} |
| 577 |
|
| 578 |
@Override |
| 579 |
public void onNext(String args) { |
| 580 |
events.add(TestConcurrencySubscriberEvent.onNext); |
| 581 |
// do some artificial work to make the thread scheduling/timing vary |
| 582 |
int s = 0; |
| 583 |
for (int i = 0; i < 20; i++) { |
| 584 |
s += s * i; |
| 585 |
} |
| 586 |
|
| 587 |
if (waitTime > 0) { |
| 588 |
try { |
| 589 |
Thread.sleep(waitTime); |
| 590 |
} catch (InterruptedException e) { |
| 591 |
// ignore |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
/** |
| 597 |
* Assert the order of events is correct and return the number of onNext executions. |
| 598 |
* |
| 599 |
* @param expectedEndingEvent the last event |
| 600 |
* @return int count of onNext calls |
| 601 |
* @throws IllegalStateException |
| 602 |
* If order of events was invalid. |
| 603 |
*/ |
| 604 |
public int assertEvents(TestConcurrencySubscriberEvent expectedEndingEvent) throws IllegalStateException { |
| 605 |
int nextCount = 0; |
| 606 |
boolean finished = false; |
| 607 |
for (TestConcurrencySubscriberEvent e : events) { |
| 608 |
if (e == TestConcurrencySubscriberEvent.onNext) { |
| 609 |
if (finished) { |
| 610 |
// already finished, we shouldn't get this again |
| 611 |
throw new IllegalStateException("Received onNext but we're already finished."); |
| 612 |
} |
| 613 |
nextCount++; |
| 614 |
} else if (e == TestConcurrencySubscriberEvent.onError) { |
| 615 |
if (finished) { |
| 616 |
// already finished, we shouldn't get this again |
| 617 |
throw new IllegalStateException("Received onError but we're already finished."); |
| 618 |
} |
| 619 |
if (expectedEndingEvent != null && TestConcurrencySubscriberEvent.onError != expectedEndingEvent) { |
| 620 |
throw new IllegalStateException("Received onError ending event but expected " + expectedEndingEvent); |
| 621 |
} |
| 622 |
finished = true; |
| 623 |
} else if (e == TestConcurrencySubscriberEvent.onComplete) { |
| 624 |
if (finished) { |
| 625 |
// already finished, we shouldn't get this again |
| 626 |
throw new IllegalStateException("Received onComplete but we're already finished."); |
| 627 |
} |
| 628 |
if (expectedEndingEvent != null && TestConcurrencySubscriberEvent.onComplete != expectedEndingEvent) { |
| 629 |
throw new IllegalStateException("Received onComplete ending event but expected " + expectedEndingEvent); |
| 630 |
} |
| 631 |
finished = true; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
return nextCount; |
| 636 |
} |
| 637 |
|
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* This spawns a single thread for the subscribe execution. |
| 642 |
*/ |
| 643 |
static class TestSingleThreadedPublisher implements Publisher<String> { |
| 644 |
|
| 645 |
final String[] values; |
| 646 |
private Thread t; |
| 647 |
|
| 648 |
TestSingleThreadedPublisher(final String... values) { |
| 649 |
this.values = values; |
| 650 |
|
| 651 |
} |
| 652 |
|
| 653 |
@Override |
| 654 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 655 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 656 |
System.out.println("TestSingleThreadedObservable subscribed to ..."); |
| 657 |
t = new Thread(new Runnable() { |
| 658 |
|
| 659 |
@Override |
| 660 |
public void run() { |
| 661 |
try { |
| 662 |
System.out.println("running TestSingleThreadedObservable thread"); |
| 663 |
for (String s : values) { |
| 664 |
System.out.println("TestSingleThreadedObservable onNext: " + s); |
| 665 |
subscriber.onNext(s); |
| 666 |
} |
| 667 |
subscriber.onComplete(); |
| 668 |
} catch (Throwable e) { |
| 669 |
throw new RuntimeException(e); |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
}); |
| 674 |
System.out.println("starting TestSingleThreadedObservable thread"); |
| 675 |
t.start(); |
| 676 |
System.out.println("done starting TestSingleThreadedObservable thread"); |
| 677 |
} |
| 678 |
|
| 679 |
public void waitToFinish() { |
| 680 |
try { |
| 681 |
t.join(); |
| 682 |
} catch (InterruptedException e) { |
| 683 |
throw new RuntimeException(e); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* This spawns a thread for the subscription, then a separate thread for each onNext call. |
| 691 |
*/ |
| 692 |
static class TestMultiThreadedObservable implements Publisher<String> { |
| 693 |
|
| 694 |
final String[] values; |
| 695 |
Thread t; |
| 696 |
AtomicInteger threadsRunning = new AtomicInteger(); |
| 697 |
AtomicInteger maxConcurrentThreads = new AtomicInteger(); |
| 698 |
ExecutorService threadPool; |
| 699 |
|
| 700 |
TestMultiThreadedObservable(String... values) { |
| 701 |
this.values = values; |
| 702 |
this.threadPool = Executors.newCachedThreadPool(); |
| 703 |
} |
| 704 |
|
| 705 |
@Override |
| 706 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 707 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 708 |
final NullPointerException npe = new NullPointerException(); |
| 709 |
System.out.println("TestMultiThreadedObservable subscribed to ..."); |
| 710 |
t = new Thread(new Runnable() { |
| 711 |
|
| 712 |
@Override |
| 713 |
public void run() { |
| 714 |
try { |
| 715 |
System.out.println("running TestMultiThreadedObservable thread"); |
| 716 |
int j = 0; |
| 717 |
for (final String s : values) { |
| 718 |
final int fj = ++j; |
| 719 |
threadPool.execute(new Runnable() { |
| 720 |
|
| 721 |
@Override |
| 722 |
public void run() { |
| 723 |
threadsRunning.incrementAndGet(); |
| 724 |
try { |
| 725 |
// perform onNext call |
| 726 |
System.out.println("TestMultiThreadedObservable onNext: " + s + " on thread " + Thread.currentThread().getName()); |
| 727 |
if (s == null) { |
| 728 |
// force an error |
| 729 |
throw npe; |
| 730 |
} else { |
| 731 |
// allow the exception to queue up |
| 732 |
int sleep = (fj % 3) * 10; |
| 733 |
if (sleep != 0) { |
| 734 |
Thread.sleep(sleep); |
| 735 |
} |
| 736 |
} |
| 737 |
subscriber.onNext(s); |
| 738 |
// capture 'maxThreads' |
| 739 |
int concurrentThreads = threadsRunning.get(); |
| 740 |
int maxThreads = maxConcurrentThreads.get(); |
| 741 |
if (concurrentThreads > maxThreads) { |
| 742 |
maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads); |
| 743 |
} |
| 744 |
} catch (Throwable e) { |
| 745 |
subscriber.onError(e); |
| 746 |
} finally { |
| 747 |
threadsRunning.decrementAndGet(); |
| 748 |
} |
| 749 |
} |
| 750 |
}); |
| 751 |
} |
| 752 |
// we are done spawning threads |
| 753 |
threadPool.shutdown(); |
| 754 |
} catch (Throwable e) { |
| 755 |
throw new RuntimeException(e); |
| 756 |
} |
| 757 |
|
| 758 |
// wait until all threads are done, then mark it as COMPLETED |
| 759 |
try { |
| 760 |
// wait for all the threads to finish |
| 761 |
if (!threadPool.awaitTermination(5, TimeUnit.SECONDS)) { |
| 762 |
System.out.println("Threadpool did not terminate in time."); |
| 763 |
} |
| 764 |
} catch (InterruptedException e) { |
| 765 |
throw new RuntimeException(e); |
| 766 |
} |
| 767 |
subscriber.onComplete(); |
| 768 |
} |
| 769 |
}); |
| 770 |
System.out.println("starting TestMultiThreadedObservable thread"); |
| 771 |
t.start(); |
| 772 |
System.out.println("done starting TestMultiThreadedObservable thread"); |
| 773 |
} |
| 774 |
|
| 775 |
public void waitToFinish() { |
| 776 |
try { |
| 777 |
t.join(); |
| 778 |
} catch (InterruptedException e) { |
| 779 |
throw new RuntimeException(e); |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
private static class BusySubscriber extends DefaultSubscriber<String> { |
| 785 |
volatile boolean onComplete; |
| 786 |
volatile boolean onError; |
| 787 |
AtomicInteger onNextCount = new AtomicInteger(); |
| 788 |
AtomicInteger threadsRunning = new AtomicInteger(); |
| 789 |
AtomicInteger maxConcurrentThreads = new AtomicInteger(); |
| 790 |
final CountDownLatch terminalEvent = new CountDownLatch(1); |
| 791 |
|
| 792 |
@Override |
| 793 |
public void onComplete() { |
| 794 |
threadsRunning.incrementAndGet(); |
| 795 |
try { |
| 796 |
onComplete = true; |
| 797 |
} finally { |
| 798 |
captureMaxThreads(); |
| 799 |
threadsRunning.decrementAndGet(); |
| 800 |
terminalEvent.countDown(); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
@Override |
| 805 |
public void onError(Throwable e) { |
| 806 |
System.out.println(">>>>>>>>>>>>>>>>>>>> onError received: " + e); |
| 807 |
threadsRunning.incrementAndGet(); |
| 808 |
try { |
| 809 |
onError = true; |
| 810 |
} finally { |
| 811 |
captureMaxThreads(); |
| 812 |
threadsRunning.decrementAndGet(); |
| 813 |
terminalEvent.countDown(); |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
@Override |
| 818 |
public void onNext(String args) { |
| 819 |
threadsRunning.incrementAndGet(); |
| 820 |
try { |
| 821 |
onNextCount.incrementAndGet(); |
| 822 |
try { |
| 823 |
// simulate doing something computational |
| 824 |
Thread.sleep(100); |
| 825 |
} catch (InterruptedException e) { |
| 826 |
e.printStackTrace(); |
| 827 |
} |
| 828 |
} finally { |
| 829 |
// capture 'maxThreads' |
| 830 |
captureMaxThreads(); |
| 831 |
threadsRunning.decrementAndGet(); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
protected void captureMaxThreads() { |
| 836 |
int concurrentThreads = threadsRunning.get(); |
| 837 |
int maxThreads = maxConcurrentThreads.get(); |
| 838 |
if (concurrentThreads > maxThreads) { |
| 839 |
maxConcurrentThreads.compareAndSet(maxThreads, concurrentThreads); |
| 840 |
if (concurrentThreads > 1) { |
| 841 |
new RuntimeException("should not be greater than 1").printStackTrace(); |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
} |
| 847 |
|
| 848 |
@Test |
| 849 |
public void errorReentry() { |
| 850 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 851 |
try { |
| 852 |
final AtomicReference<Subscriber<Integer>> serial = new AtomicReference<>(); |
| 853 |
|
| 854 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 855 |
@Override |
| 856 |
public void onNext(Integer v) { |
| 857 |
serial.get().onError(new TestException()); |
| 858 |
serial.get().onError(new TestException()); |
| 859 |
super.onNext(v); |
| 860 |
} |
| 861 |
}; |
| 862 |
SerializedSubscriber<Integer> sobs = new SerializedSubscriber<>(ts); |
| 863 |
sobs.onSubscribe(new BooleanSubscription()); |
| 864 |
serial.set(sobs); |
| 865 |
|
| 866 |
sobs.onNext(1); |
| 867 |
|
| 868 |
ts.assertValue(1); |
| 869 |
ts.assertError(TestException.class); |
| 870 |
|
| 871 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 872 |
} finally { |
| 873 |
RxJavaPlugins.reset(); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
@Test |
| 878 |
public void completeReentry() { |
| 879 |
final AtomicReference<Subscriber<Integer>> serial = new AtomicReference<>(); |
| 880 |
|
| 881 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 882 |
@Override |
| 883 |
public void onNext(Integer v) { |
| 884 |
serial.get().onComplete(); |
| 885 |
serial.get().onComplete(); |
| 886 |
super.onNext(v); |
| 887 |
} |
| 888 |
}; |
| 889 |
SerializedSubscriber<Integer> sobs = new SerializedSubscriber<>(ts); |
| 890 |
sobs.onSubscribe(new BooleanSubscription()); |
| 891 |
serial.set(sobs); |
| 892 |
|
| 893 |
sobs.onNext(1); |
| 894 |
|
| 895 |
ts.assertValue(1); |
| 896 |
ts.assertComplete(); |
| 897 |
ts.assertNoErrors(); |
| 898 |
} |
| 899 |
|
| 900 |
@Test |
| 901 |
public void dispose() { |
| 902 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 903 |
|
| 904 |
SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts); |
| 905 |
|
| 906 |
BooleanSubscription bs = new BooleanSubscription(); |
| 907 |
|
| 908 |
so.onSubscribe(bs); |
| 909 |
|
| 910 |
ts.cancel(); |
| 911 |
|
| 912 |
assertTrue(bs.isCancelled()); |
| 913 |
} |
| 914 |
|
| 915 |
@Test |
| 916 |
public void onCompleteRace() { |
| 917 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 918 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 919 |
|
| 920 |
final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts); |
| 921 |
|
| 922 |
BooleanSubscription bs = new BooleanSubscription(); |
| 923 |
|
| 924 |
so.onSubscribe(bs); |
| 925 |
|
| 926 |
Runnable r = new Runnable() { |
| 927 |
@Override |
| 928 |
public void run() { |
| 929 |
so.onComplete(); |
| 930 |
} |
| 931 |
}; |
| 932 |
|
| 933 |
TestHelper.race(r, r); |
| 934 |
|
| 935 |
ts.awaitDone(5, TimeUnit.SECONDS) |
| 936 |
.assertResult(); |
| 937 |
} |
| 938 |
|
| 939 |
} |
| 940 |
|
| 941 |
@Test |
| 942 |
public void onNextOnCompleteRace() { |
| 943 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 944 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 945 |
|
| 946 |
final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts); |
| 947 |
|
| 948 |
BooleanSubscription bs = new BooleanSubscription(); |
| 949 |
|
| 950 |
so.onSubscribe(bs); |
| 951 |
|
| 952 |
Runnable r1 = new Runnable() { |
| 953 |
@Override |
| 954 |
public void run() { |
| 955 |
so.onComplete(); |
| 956 |
} |
| 957 |
}; |
| 958 |
|
| 959 |
Runnable r2 = new Runnable() { |
| 960 |
@Override |
| 961 |
public void run() { |
| 962 |
so.onNext(1); |
| 963 |
} |
| 964 |
}; |
| 965 |
|
| 966 |
TestHelper.race(r1, r2); |
| 967 |
|
| 968 |
ts.awaitDone(5, TimeUnit.SECONDS) |
| 969 |
.assertNoErrors() |
| 970 |
.assertComplete(); |
| 971 |
|
| 972 |
assertTrue(ts.values().size() <= 1); |
| 973 |
} |
| 974 |
|
| 975 |
} |
| 976 |
|
| 977 |
@Test |
| 978 |
public void onNextOnErrorRace() { |
| 979 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 980 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 981 |
|
| 982 |
final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts); |
| 983 |
|
| 984 |
BooleanSubscription bs = new BooleanSubscription(); |
| 985 |
|
| 986 |
so.onSubscribe(bs); |
| 987 |
|
| 988 |
final Throwable ex = new TestException(); |
| 989 |
|
| 990 |
Runnable r1 = new Runnable() { |
| 991 |
@Override |
| 992 |
public void run() { |
| 993 |
so.onError(ex); |
| 994 |
} |
| 995 |
}; |
| 996 |
|
| 997 |
Runnable r2 = new Runnable() { |
| 998 |
@Override |
| 999 |
public void run() { |
| 1000 |
so.onNext(1); |
| 1001 |
} |
| 1002 |
}; |
| 1003 |
|
| 1004 |
TestHelper.race(r1, r2); |
| 1005 |
|
| 1006 |
ts.awaitDone(5, TimeUnit.SECONDS) |
| 1007 |
.assertError(ex) |
| 1008 |
.assertNotComplete(); |
| 1009 |
|
| 1010 |
assertTrue(ts.values().size() <= 1); |
| 1011 |
} |
| 1012 |
|
| 1013 |
} |
| 1014 |
|
| 1015 |
@Test |
| 1016 |
public void onNextOnErrorRaceDelayError() { |
| 1017 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1018 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1019 |
|
| 1020 |
final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts, true); |
| 1021 |
|
| 1022 |
BooleanSubscription bs = new BooleanSubscription(); |
| 1023 |
|
| 1024 |
so.onSubscribe(bs); |
| 1025 |
|
| 1026 |
final Throwable ex = new TestException(); |
| 1027 |
|
| 1028 |
Runnable r1 = new Runnable() { |
| 1029 |
@Override |
| 1030 |
public void run() { |
| 1031 |
so.onError(ex); |
| 1032 |
} |
| 1033 |
}; |
| 1034 |
|
| 1035 |
Runnable r2 = new Runnable() { |
| 1036 |
@Override |
| 1037 |
public void run() { |
| 1038 |
so.onNext(1); |
| 1039 |
} |
| 1040 |
}; |
| 1041 |
|
| 1042 |
TestHelper.race(r1, r2); |
| 1043 |
|
| 1044 |
ts.awaitDone(5, TimeUnit.SECONDS) |
| 1045 |
.assertError(ex) |
| 1046 |
.assertNotComplete(); |
| 1047 |
|
| 1048 |
assertTrue(ts.values().size() <= 1); |
| 1049 |
} |
| 1050 |
|
| 1051 |
} |
| 1052 |
|
| 1053 |
@Test |
| 1054 |
public void startOnce() { |
| 1055 |
|
| 1056 |
List<Throwable> error = TestHelper.trackPluginErrors(); |
| 1057 |
|
| 1058 |
try { |
| 1059 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1060 |
|
| 1061 |
final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts); |
| 1062 |
|
| 1063 |
so.onSubscribe(new BooleanSubscription()); |
| 1064 |
|
| 1065 |
BooleanSubscription bs = new BooleanSubscription(); |
| 1066 |
|
| 1067 |
so.onSubscribe(bs); |
| 1068 |
|
| 1069 |
assertTrue(bs.isCancelled()); |
| 1070 |
|
| 1071 |
TestHelper.assertError(error, 0, IllegalStateException.class, "Subscription already set!"); |
| 1072 |
} finally { |
| 1073 |
RxJavaPlugins.reset(); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
@Test |
| 1078 |
public void onCompleteOnErrorRace() { |
| 1079 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1080 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1081 |
try { |
| 1082 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 1083 |
|
| 1084 |
final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts); |
| 1085 |
|
| 1086 |
BooleanSubscription bs = new BooleanSubscription(); |
| 1087 |
|
| 1088 |
so.onSubscribe(bs); |
| 1089 |
|
| 1090 |
final Throwable ex = new TestException(); |
| 1091 |
|
| 1092 |
Runnable r1 = new Runnable() { |
| 1093 |
@Override |
| 1094 |
public void run() { |
| 1095 |
so.onError(ex); |
| 1096 |
} |
| 1097 |
}; |
| 1098 |
|
| 1099 |
Runnable r2 = new Runnable() { |
| 1100 |
@Override |
| 1101 |
public void run() { |
| 1102 |
so.onComplete(); |
| 1103 |
} |
| 1104 |
}; |
| 1105 |
|
| 1106 |
TestHelper.race(r1, r2); |
| 1107 |
|
| 1108 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 1109 |
|
| 1110 |
if (ts.completions() != 0) { |
| 1111 |
ts.assertResult(); |
| 1112 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1113 |
} else { |
| 1114 |
ts.assertFailure(TestException.class).assertError(ex); |
| 1115 |
assertTrue("" + errors, errors.isEmpty()); |
| 1116 |
} |
| 1117 |
} finally { |
| 1118 |
RxJavaPlugins.reset(); |
| 1119 |
} |
| 1120 |
} |
| 1121 |
|
| 1122 |
} |
| 1123 |
|
| 1124 |
@Test |
| 1125 |
public void nullOnNext() { |
| 1126 |
|
| 1127 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 1128 |
|
| 1129 |
final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts); |
| 1130 |
|
| 1131 |
so.onSubscribe(new BooleanSubscription()); |
| 1132 |
|
| 1133 |
so.onNext(null); |
| 1134 |
|
| 1135 |
ts.assertFailureAndMessage(NullPointerException.class, ExceptionHelper.nullWarning("onNext called with a null value.")); |
| 1136 |
} |
| 1137 |
|
| 1138 |
@Test |
| 1139 |
@SuppressUndeliverable |
| 1140 |
public void onErrorQueuedUp() { |
| 1141 |
AtomicReference<SerializedSubscriber<Integer>> ssRef = new AtomicReference<>(); |
| 1142 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>() { |
| 1143 |
@Override |
| 1144 |
public void onNext(Integer t) { |
| 1145 |
super.onNext(t); |
| 1146 |
ssRef.get().onNext(2); |
| 1147 |
ssRef.get().onError(new TestException()); |
| 1148 |
} |
| 1149 |
}; |
| 1150 |
|
| 1151 |
final SerializedSubscriber<Integer> so = new SerializedSubscriber<>(ts, true); |
| 1152 |
ssRef.set(so); |
| 1153 |
|
| 1154 |
BooleanSubscription bs = new BooleanSubscription(); |
| 1155 |
|
| 1156 |
so.onSubscribe(bs); |
| 1157 |
|
| 1158 |
so.onNext(1); |
| 1159 |
|
| 1160 |
ts.assertFailure(TestException.class, 1, 2); |
| 1161 |
} |
| 1162 |
} |