| 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.flowable; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertEquals; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.util.List; |
| 22 |
import java.util.concurrent.atomic.AtomicInteger; |
| 23 |
|
| 24 |
import org.junit.*; |
| 25 |
import org.reactivestreams.*; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.exceptions.*; |
| 29 |
import io.reactivex.rxjava3.flowables.ConnectableFlowable; |
| 30 |
import io.reactivex.rxjava3.functions.*; |
| 31 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 32 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 33 |
import io.reactivex.rxjava3.operators.ConditionalSubscriber; |
| 34 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 35 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 36 |
import io.reactivex.rxjava3.processors.UnicastProcessor; |
| 37 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 38 |
import io.reactivex.rxjava3.testsupport.*; |
| 39 |
|
| 40 |
public class FlowableDoOnEachTest extends RxJavaTest { |
| 41 |
|
| 42 |
Subscriber<String> subscribedSubscriber; |
| 43 |
Subscriber<String> sideEffectSubscriber; |
| 44 |
|
| 45 |
@Before |
| 46 |
public void before() { |
| 47 |
subscribedSubscriber = TestHelper.mockSubscriber(); |
| 48 |
sideEffectSubscriber = TestHelper.mockSubscriber(); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void doOnEach() { |
| 53 |
Flowable<String> base = Flowable.just("a", "b", "c"); |
| 54 |
Flowable<String> doOnEach = base.doOnEach(sideEffectSubscriber); |
| 55 |
|
| 56 |
doOnEach.subscribe(subscribedSubscriber); |
| 57 |
|
| 58 |
// ensure the leaf observer is still getting called |
| 59 |
verify(subscribedSubscriber, never()).onError(any(Throwable.class)); |
| 60 |
verify(subscribedSubscriber, times(1)).onNext("a"); |
| 61 |
verify(subscribedSubscriber, times(1)).onNext("b"); |
| 62 |
verify(subscribedSubscriber, times(1)).onNext("c"); |
| 63 |
verify(subscribedSubscriber, times(1)).onComplete(); |
| 64 |
|
| 65 |
// ensure our injected observer is getting called |
| 66 |
verify(sideEffectSubscriber, never()).onError(any(Throwable.class)); |
| 67 |
verify(sideEffectSubscriber, times(1)).onNext("a"); |
| 68 |
verify(sideEffectSubscriber, times(1)).onNext("b"); |
| 69 |
verify(sideEffectSubscriber, times(1)).onNext("c"); |
| 70 |
verify(sideEffectSubscriber, times(1)).onComplete(); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void doOnEachWithError() { |
| 75 |
Flowable<String> base = Flowable.just("one", "fail", "two", "three", "fail"); |
| 76 |
Flowable<String> errs = base.map(new Function<String, String>() { |
| 77 |
@Override |
| 78 |
public String apply(String s) { |
| 79 |
if ("fail".equals(s)) { |
| 80 |
throw new RuntimeException("Forced Failure"); |
| 81 |
} |
| 82 |
return s; |
| 83 |
} |
| 84 |
}); |
| 85 |
|
| 86 |
Flowable<String> doOnEach = errs.doOnEach(sideEffectSubscriber); |
| 87 |
|
| 88 |
doOnEach.subscribe(subscribedSubscriber); |
| 89 |
verify(subscribedSubscriber, times(1)).onNext("one"); |
| 90 |
verify(subscribedSubscriber, never()).onNext("two"); |
| 91 |
verify(subscribedSubscriber, never()).onNext("three"); |
| 92 |
verify(subscribedSubscriber, never()).onComplete(); |
| 93 |
verify(subscribedSubscriber, times(1)).onError(any(Throwable.class)); |
| 94 |
|
| 95 |
verify(sideEffectSubscriber, times(1)).onNext("one"); |
| 96 |
verify(sideEffectSubscriber, never()).onNext("two"); |
| 97 |
verify(sideEffectSubscriber, never()).onNext("three"); |
| 98 |
verify(sideEffectSubscriber, never()).onComplete(); |
| 99 |
verify(sideEffectSubscriber, times(1)).onError(any(Throwable.class)); |
| 100 |
} |
| 101 |
|
| 102 |
@Test |
| 103 |
public void doOnEachWithErrorInCallback() { |
| 104 |
Flowable<String> base = Flowable.just("one", "two", "fail", "three"); |
| 105 |
Flowable<String> doOnEach = base.doOnNext(new Consumer<String>() { |
| 106 |
@Override |
| 107 |
public void accept(String s) { |
| 108 |
if ("fail".equals(s)) { |
| 109 |
throw new RuntimeException("Forced Failure"); |
| 110 |
} |
| 111 |
} |
| 112 |
}); |
| 113 |
|
| 114 |
doOnEach.subscribe(subscribedSubscriber); |
| 115 |
verify(subscribedSubscriber, times(1)).onNext("one"); |
| 116 |
verify(subscribedSubscriber, times(1)).onNext("two"); |
| 117 |
verify(subscribedSubscriber, never()).onNext("three"); |
| 118 |
verify(subscribedSubscriber, never()).onComplete(); |
| 119 |
verify(subscribedSubscriber, times(1)).onError(any(Throwable.class)); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void issue1451Case1() { |
| 125 |
// https://github.com/Netflix/RxJava/issues/1451 |
| 126 |
final int expectedCount = 3; |
| 127 |
final AtomicInteger count = new AtomicInteger(); |
| 128 |
for (int i = 0; i < expectedCount; i++) { |
| 129 |
Flowable |
| 130 |
.just(Boolean.TRUE, Boolean.FALSE) |
| 131 |
.takeWhile(new Predicate<Boolean>() { |
| 132 |
@Override |
| 133 |
public boolean test(Boolean value) { |
| 134 |
return value; |
| 135 |
} |
| 136 |
}) |
| 137 |
.toList() |
| 138 |
.doOnSuccess(new Consumer<List<Boolean>>() { |
| 139 |
@Override |
| 140 |
public void accept(List<Boolean> booleans) { |
| 141 |
count.incrementAndGet(); |
| 142 |
} |
| 143 |
}) |
| 144 |
.subscribe(); |
| 145 |
} |
| 146 |
assertEquals(expectedCount, count.get()); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void issue1451Case2() { |
| 151 |
// https://github.com/Netflix/RxJava/issues/1451 |
| 152 |
final int expectedCount = 3; |
| 153 |
final AtomicInteger count = new AtomicInteger(); |
| 154 |
for (int i = 0; i < expectedCount; i++) { |
| 155 |
Flowable |
| 156 |
.just(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE) |
| 157 |
.takeWhile(new Predicate<Boolean>() { |
| 158 |
@Override |
| 159 |
public boolean test(Boolean value) { |
| 160 |
return value; |
| 161 |
} |
| 162 |
}) |
| 163 |
.toList() |
| 164 |
.doOnSuccess(new Consumer<List<Boolean>>() { |
| 165 |
@Override |
| 166 |
public void accept(List<Boolean> booleans) { |
| 167 |
count.incrementAndGet(); |
| 168 |
} |
| 169 |
}) |
| 170 |
.subscribe(); |
| 171 |
} |
| 172 |
assertEquals(expectedCount, count.get()); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void onErrorThrows() { |
| 177 |
TestSubscriberEx<Object> ts = new TestSubscriberEx<>(); |
| 178 |
|
| 179 |
Flowable.error(new TestException()) |
| 180 |
.doOnError(new Consumer<Throwable>() { |
| 181 |
@Override |
| 182 |
public void accept(Throwable e) { |
| 183 |
throw new TestException(); |
| 184 |
} |
| 185 |
}).subscribe(ts); |
| 186 |
|
| 187 |
ts.assertNoValues(); |
| 188 |
ts.assertNotComplete(); |
| 189 |
ts.assertError(CompositeException.class); |
| 190 |
|
| 191 |
CompositeException ex = (CompositeException)ts.errors().get(0); |
| 192 |
|
| 193 |
List<Throwable> exceptions = ex.getExceptions(); |
| 194 |
assertEquals(2, exceptions.size()); |
| 195 |
Assert.assertTrue(exceptions.get(0) instanceof TestException); |
| 196 |
Assert.assertTrue(exceptions.get(1) instanceof TestException); |
| 197 |
} |
| 198 |
|
| 199 |
@Test |
| 200 |
public void ignoreCancel() { |
| 201 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 202 |
|
| 203 |
try { |
| 204 |
Flowable.fromPublisher(new Publisher<Object>() { |
| 205 |
@Override |
| 206 |
public void subscribe(Subscriber<? super Object> s) { |
| 207 |
s.onSubscribe(new BooleanSubscription()); |
| 208 |
s.onNext(1); |
| 209 |
s.onNext(2); |
| 210 |
s.onError(new IOException()); |
| 211 |
s.onComplete(); |
| 212 |
} |
| 213 |
}) |
| 214 |
.doOnNext(new Consumer<Object>() { |
| 215 |
@Override |
| 216 |
public void accept(Object e) throws Exception { |
| 217 |
throw new TestException(); |
| 218 |
} |
| 219 |
}) |
| 220 |
.test() |
| 221 |
.assertFailure(TestException.class); |
| 222 |
|
| 223 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 224 |
} finally { |
| 225 |
RxJavaPlugins.reset(); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
@Test |
| 230 |
public void onErrorAfterCrash() { |
| 231 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 232 |
|
| 233 |
try { |
| 234 |
Flowable.fromPublisher(new Publisher<Object>() { |
| 235 |
@Override |
| 236 |
public void subscribe(Subscriber<? super Object> s) { |
| 237 |
s.onSubscribe(new BooleanSubscription()); |
| 238 |
s.onError(new TestException()); |
| 239 |
} |
| 240 |
}) |
| 241 |
.doAfterTerminate(new Action() { |
| 242 |
@Override |
| 243 |
public void run() throws Exception { |
| 244 |
throw new IOException(); |
| 245 |
} |
| 246 |
}) |
| 247 |
.test() |
| 248 |
.assertFailure(TestException.class); |
| 249 |
|
| 250 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 251 |
} finally { |
| 252 |
RxJavaPlugins.reset(); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
@Test |
| 257 |
public void onCompleteAfterCrash() { |
| 258 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 259 |
|
| 260 |
try { |
| 261 |
Flowable.fromPublisher(new Publisher<Object>() { |
| 262 |
@Override |
| 263 |
public void subscribe(Subscriber<? super Object> s) { |
| 264 |
s.onSubscribe(new BooleanSubscription()); |
| 265 |
s.onComplete(); |
| 266 |
} |
| 267 |
}) |
| 268 |
.doAfterTerminate(new Action() { |
| 269 |
@Override |
| 270 |
public void run() throws Exception { |
| 271 |
throw new IOException(); |
| 272 |
} |
| 273 |
}) |
| 274 |
.test() |
| 275 |
.assertResult(); |
| 276 |
|
| 277 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 278 |
} finally { |
| 279 |
RxJavaPlugins.reset(); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
@Test |
| 284 |
public void onCompleteCrash() { |
| 285 |
Flowable.fromPublisher(new Publisher<Object>() { |
| 286 |
@Override |
| 287 |
public void subscribe(Subscriber<? super Object> s) { |
| 288 |
s.onSubscribe(new BooleanSubscription()); |
| 289 |
s.onComplete(); |
| 290 |
} |
| 291 |
}) |
| 292 |
.doOnComplete(new Action() { |
| 293 |
@Override |
| 294 |
public void run() throws Exception { |
| 295 |
throw new IOException(); |
| 296 |
} |
| 297 |
}) |
| 298 |
.test() |
| 299 |
.assertFailure(IOException.class); |
| 300 |
} |
| 301 |
|
| 302 |
@Test |
| 303 |
public void ignoreCancelConditional() { |
| 304 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 305 |
|
| 306 |
try { |
| 307 |
Flowable.fromPublisher(new Publisher<Object>() { |
| 308 |
@Override |
| 309 |
public void subscribe(Subscriber<? super Object> s) { |
| 310 |
s.onSubscribe(new BooleanSubscription()); |
| 311 |
s.onNext(1); |
| 312 |
s.onNext(2); |
| 313 |
s.onError(new IOException()); |
| 314 |
s.onComplete(); |
| 315 |
} |
| 316 |
}) |
| 317 |
.doOnNext(new Consumer<Object>() { |
| 318 |
@Override |
| 319 |
public void accept(Object e) throws Exception { |
| 320 |
throw new TestException(); |
| 321 |
} |
| 322 |
}) |
| 323 |
.filter(Functions.alwaysTrue()) |
| 324 |
.test() |
| 325 |
.assertFailure(TestException.class); |
| 326 |
|
| 327 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 328 |
} finally { |
| 329 |
RxJavaPlugins.reset(); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
@Test |
| 334 |
public void ignoreCancelConditional2() { |
| 335 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 336 |
|
| 337 |
try { |
| 338 |
Flowable.fromPublisher(new Publisher<Object>() { |
| 339 |
@Override |
| 340 |
public void subscribe(Subscriber<? super Object> s) { |
| 341 |
ConditionalSubscriber<? super Object> cs = (ConditionalSubscriber<? super Object>)s; |
| 342 |
cs.onSubscribe(new BooleanSubscription()); |
| 343 |
cs.tryOnNext(1); |
| 344 |
cs.tryOnNext(2); |
| 345 |
cs.onError(new IOException()); |
| 346 |
cs.onComplete(); |
| 347 |
} |
| 348 |
}) |
| 349 |
.doOnNext(new Consumer<Object>() { |
| 350 |
@Override |
| 351 |
public void accept(Object e) throws Exception { |
| 352 |
throw new TestException(); |
| 353 |
} |
| 354 |
}) |
| 355 |
.filter(Functions.alwaysTrue()) |
| 356 |
.test() |
| 357 |
.assertFailure(TestException.class); |
| 358 |
|
| 359 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 360 |
} finally { |
| 361 |
RxJavaPlugins.reset(); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
@Test |
| 366 |
public void onErrorAfterCrashConditional() { |
| 367 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 368 |
|
| 369 |
try { |
| 370 |
Flowable.fromPublisher(new Publisher<Object>() { |
| 371 |
@Override |
| 372 |
public void subscribe(Subscriber<? super Object> s) { |
| 373 |
s.onSubscribe(new BooleanSubscription()); |
| 374 |
s.onError(new TestException()); |
| 375 |
} |
| 376 |
}) |
| 377 |
.doAfterTerminate(new Action() { |
| 378 |
@Override |
| 379 |
public void run() throws Exception { |
| 380 |
throw new IOException(); |
| 381 |
} |
| 382 |
}) |
| 383 |
.filter(Functions.alwaysTrue()) |
| 384 |
.test() |
| 385 |
.assertFailure(TestException.class); |
| 386 |
|
| 387 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 388 |
} finally { |
| 389 |
RxJavaPlugins.reset(); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
@Test |
| 394 |
public void onCompleteAfter() { |
| 395 |
final int[] call = { 0 }; |
| 396 |
Flowable.just(1) |
| 397 |
.doAfterTerminate(new Action() { |
| 398 |
@Override |
| 399 |
public void run() throws Exception { |
| 400 |
call[0]++; |
| 401 |
} |
| 402 |
}) |
| 403 |
.test() |
| 404 |
.assertResult(1); |
| 405 |
|
| 406 |
assertEquals(1, call[0]); |
| 407 |
} |
| 408 |
|
| 409 |
@Test |
| 410 |
public void onCompleteAfterCrashConditional() { |
| 411 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 412 |
|
| 413 |
try { |
| 414 |
Flowable.fromPublisher(new Publisher<Object>() { |
| 415 |
@Override |
| 416 |
public void subscribe(Subscriber<? super Object> s) { |
| 417 |
s.onSubscribe(new BooleanSubscription()); |
| 418 |
s.onComplete(); |
| 419 |
} |
| 420 |
}) |
| 421 |
.doAfterTerminate(new Action() { |
| 422 |
@Override |
| 423 |
public void run() throws Exception { |
| 424 |
throw new IOException(); |
| 425 |
} |
| 426 |
}) |
| 427 |
.filter(Functions.alwaysTrue()) |
| 428 |
.test() |
| 429 |
.assertResult(); |
| 430 |
|
| 431 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 432 |
} finally { |
| 433 |
RxJavaPlugins.reset(); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
@Test |
| 438 |
public void onCompleteCrashConditional() { |
| 439 |
Flowable.fromPublisher(new Publisher<Object>() { |
| 440 |
@Override |
| 441 |
public void subscribe(Subscriber<? super Object> s) { |
| 442 |
s.onSubscribe(new BooleanSubscription()); |
| 443 |
s.onComplete(); |
| 444 |
} |
| 445 |
}) |
| 446 |
.doOnComplete(new Action() { |
| 447 |
@Override |
| 448 |
public void run() throws Exception { |
| 449 |
throw new IOException(); |
| 450 |
} |
| 451 |
}) |
| 452 |
.filter(Functions.alwaysTrue()) |
| 453 |
.test() |
| 454 |
.assertFailure(IOException.class); |
| 455 |
} |
| 456 |
|
| 457 |
@Test |
| 458 |
public void onErrorOnErrorCrashConditional() { |
| 459 |
TestSubscriberEx<Object> ts = Flowable.error(new TestException("Outer")) |
| 460 |
.doOnError(new Consumer<Throwable>() { |
| 461 |
@Override |
| 462 |
public void accept(Throwable e) throws Exception { |
| 463 |
throw new TestException("Inner"); |
| 464 |
} |
| 465 |
}) |
| 466 |
.filter(Functions.alwaysTrue()) |
| 467 |
.to(TestHelper.testConsumer()) |
| 468 |
.assertFailure(CompositeException.class); |
| 469 |
|
| 470 |
List<Throwable> errors = TestHelper.compositeList(ts.errors().get(0)); |
| 471 |
|
| 472 |
TestHelper.assertError(errors, 0, TestException.class, "Outer"); |
| 473 |
TestHelper.assertError(errors, 1, TestException.class, "Inner"); |
| 474 |
} |
| 475 |
|
| 476 |
@Test |
| 477 |
public void fused() { |
| 478 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 479 |
|
| 480 |
final int[] call = { 0, 0 }; |
| 481 |
|
| 482 |
Flowable.range(1, 5) |
| 483 |
.doOnNext(new Consumer<Integer>() { |
| 484 |
@Override |
| 485 |
public void accept(Integer v) throws Exception { |
| 486 |
call[0]++; |
| 487 |
} |
| 488 |
}) |
| 489 |
.doOnComplete(new Action() { |
| 490 |
@Override |
| 491 |
public void run() throws Exception { |
| 492 |
call[1]++; |
| 493 |
} |
| 494 |
}) |
| 495 |
.subscribe(ts); |
| 496 |
|
| 497 |
ts.assertFuseable() |
| 498 |
.assertFusionMode(QueueFuseable.SYNC) |
| 499 |
.assertResult(1, 2, 3, 4, 5); |
| 500 |
|
| 501 |
assertEquals(5, call[0]); |
| 502 |
assertEquals(1, call[1]); |
| 503 |
} |
| 504 |
|
| 505 |
@Test |
| 506 |
public void fusedOnErrorCrash() { |
| 507 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 508 |
|
| 509 |
final int[] call = { 0 }; |
| 510 |
|
| 511 |
Flowable.range(1, 5) |
| 512 |
.doOnNext(new Consumer<Integer>() { |
| 513 |
@Override |
| 514 |
public void accept(Integer v) throws Exception { |
| 515 |
throw new TestException(); |
| 516 |
} |
| 517 |
}) |
| 518 |
.doOnComplete(new Action() { |
| 519 |
@Override |
| 520 |
public void run() throws Exception { |
| 521 |
call[0]++; |
| 522 |
} |
| 523 |
}) |
| 524 |
.subscribe(ts); |
| 525 |
|
| 526 |
ts.assertFuseable() |
| 527 |
.assertFusionMode(QueueFuseable.SYNC) |
| 528 |
.assertFailure(TestException.class); |
| 529 |
|
| 530 |
assertEquals(0, call[0]); |
| 531 |
} |
| 532 |
|
| 533 |
@Test |
| 534 |
public void fusedConditional() { |
| 535 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 536 |
|
| 537 |
final int[] call = { 0, 0 }; |
| 538 |
|
| 539 |
Flowable.range(1, 5) |
| 540 |
.doOnNext(new Consumer<Integer>() { |
| 541 |
@Override |
| 542 |
public void accept(Integer v) throws Exception { |
| 543 |
call[0]++; |
| 544 |
} |
| 545 |
}) |
| 546 |
.doOnComplete(new Action() { |
| 547 |
@Override |
| 548 |
public void run() throws Exception { |
| 549 |
call[1]++; |
| 550 |
} |
| 551 |
}) |
| 552 |
.filter(Functions.alwaysTrue()) |
| 553 |
.subscribe(ts); |
| 554 |
|
| 555 |
ts.assertFuseable() |
| 556 |
.assertFusionMode(QueueFuseable.SYNC) |
| 557 |
.assertResult(1, 2, 3, 4, 5); |
| 558 |
|
| 559 |
assertEquals(5, call[0]); |
| 560 |
assertEquals(1, call[1]); |
| 561 |
} |
| 562 |
|
| 563 |
@Test |
| 564 |
public void fusedOnErrorCrashConditional() { |
| 565 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 566 |
|
| 567 |
final int[] call = { 0 }; |
| 568 |
|
| 569 |
Flowable.range(1, 5) |
| 570 |
.doOnNext(new Consumer<Integer>() { |
| 571 |
@Override |
| 572 |
public void accept(Integer v) throws Exception { |
| 573 |
throw new TestException(); |
| 574 |
} |
| 575 |
}) |
| 576 |
.doOnComplete(new Action() { |
| 577 |
@Override |
| 578 |
public void run() throws Exception { |
| 579 |
call[0]++; |
| 580 |
} |
| 581 |
}) |
| 582 |
.filter(Functions.alwaysTrue()) |
| 583 |
.subscribe(ts); |
| 584 |
|
| 585 |
ts.assertFuseable() |
| 586 |
.assertFusionMode(QueueFuseable.SYNC) |
| 587 |
.assertFailure(TestException.class); |
| 588 |
|
| 589 |
assertEquals(0, call[0]); |
| 590 |
} |
| 591 |
|
| 592 |
@Test |
| 593 |
public void fusedAsync() { |
| 594 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 595 |
|
| 596 |
final int[] call = { 0, 0 }; |
| 597 |
|
| 598 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 599 |
|
| 600 |
up |
| 601 |
.doOnNext(new Consumer<Integer>() { |
| 602 |
@Override |
| 603 |
public void accept(Integer v) throws Exception { |
| 604 |
call[0]++; |
| 605 |
} |
| 606 |
}) |
| 607 |
.doOnComplete(new Action() { |
| 608 |
@Override |
| 609 |
public void run() throws Exception { |
| 610 |
call[1]++; |
| 611 |
} |
| 612 |
}) |
| 613 |
.subscribe(ts); |
| 614 |
|
| 615 |
TestHelper.emit(up, 1, 2, 3, 4, 5); |
| 616 |
|
| 617 |
ts.assertFuseable() |
| 618 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 619 |
.assertResult(1, 2, 3, 4, 5); |
| 620 |
|
| 621 |
assertEquals(5, call[0]); |
| 622 |
assertEquals(1, call[1]); |
| 623 |
} |
| 624 |
|
| 625 |
@Test |
| 626 |
public void fusedAsyncConditional() { |
| 627 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 628 |
|
| 629 |
final int[] call = { 0, 0 }; |
| 630 |
|
| 631 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 632 |
|
| 633 |
up |
| 634 |
.doOnNext(new Consumer<Integer>() { |
| 635 |
@Override |
| 636 |
public void accept(Integer v) throws Exception { |
| 637 |
call[0]++; |
| 638 |
} |
| 639 |
}) |
| 640 |
.doOnComplete(new Action() { |
| 641 |
@Override |
| 642 |
public void run() throws Exception { |
| 643 |
call[1]++; |
| 644 |
} |
| 645 |
}) |
| 646 |
.filter(Functions.alwaysTrue()) |
| 647 |
.subscribe(ts); |
| 648 |
|
| 649 |
TestHelper.emit(up, 1, 2, 3, 4, 5); |
| 650 |
|
| 651 |
ts.assertFuseable() |
| 652 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 653 |
.assertResult(1, 2, 3, 4, 5); |
| 654 |
|
| 655 |
assertEquals(5, call[0]); |
| 656 |
assertEquals(1, call[1]); |
| 657 |
} |
| 658 |
|
| 659 |
@Test |
| 660 |
public void fusedAsyncConditional2() { |
| 661 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 662 |
|
| 663 |
final int[] call = { 0, 0 }; |
| 664 |
|
| 665 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 666 |
|
| 667 |
up.hide() |
| 668 |
.doOnNext(new Consumer<Integer>() { |
| 669 |
@Override |
| 670 |
public void accept(Integer v) throws Exception { |
| 671 |
call[0]++; |
| 672 |
} |
| 673 |
}) |
| 674 |
.doOnComplete(new Action() { |
| 675 |
@Override |
| 676 |
public void run() throws Exception { |
| 677 |
call[1]++; |
| 678 |
} |
| 679 |
}) |
| 680 |
.filter(Functions.alwaysTrue()) |
| 681 |
.subscribe(ts); |
| 682 |
|
| 683 |
TestHelper.emit(up, 1, 2, 3, 4, 5); |
| 684 |
|
| 685 |
ts.assertFuseable() |
| 686 |
.assertFusionMode(QueueFuseable.NONE) |
| 687 |
.assertResult(1, 2, 3, 4, 5); |
| 688 |
|
| 689 |
assertEquals(5, call[0]); |
| 690 |
assertEquals(1, call[1]); |
| 691 |
} |
| 692 |
|
| 693 |
@Test |
| 694 |
public void dispose() { |
| 695 |
TestHelper.checkDisposed(Flowable.just(1).doOnEach(new TestSubscriber<>())); |
| 696 |
} |
| 697 |
|
| 698 |
@Test |
| 699 |
public void doubleOnSubscribe() { |
| 700 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 701 |
@Override |
| 702 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 703 |
return f.doOnEach(new TestSubscriber<>()); |
| 704 |
} |
| 705 |
}); |
| 706 |
} |
| 707 |
|
| 708 |
@Test |
| 709 |
public void doOnNextDoOnErrorFused() { |
| 710 |
ConnectableFlowable<Integer> cf = Flowable.just(1) |
| 711 |
.doOnNext(new Consumer<Integer>() { |
| 712 |
@Override |
| 713 |
public void accept(Integer v) throws Exception { |
| 714 |
throw new TestException("First"); |
| 715 |
} |
| 716 |
}) |
| 717 |
.doOnError(new Consumer<Throwable>() { |
| 718 |
@Override |
| 719 |
public void accept(Throwable e) throws Exception { |
| 720 |
throw new TestException("Second"); |
| 721 |
} |
| 722 |
}) |
| 723 |
.publish(); |
| 724 |
|
| 725 |
TestSubscriberEx<Integer> ts = cf.to(TestHelper.<Integer>testConsumer()); |
| 726 |
cf.connect(); |
| 727 |
|
| 728 |
ts.assertFailure(CompositeException.class); |
| 729 |
|
| 730 |
TestHelper.assertError(ts, 0, TestException.class, "First"); |
| 731 |
TestHelper.assertError(ts, 1, TestException.class, "Second"); |
| 732 |
} |
| 733 |
|
| 734 |
@Test |
| 735 |
public void doOnNextDoOnErrorCombinedFused() { |
| 736 |
ConnectableFlowable<Integer> cf = Flowable.just(1) |
| 737 |
.compose(new FlowableTransformer<Integer, Integer>() { |
| 738 |
@Override |
| 739 |
public Publisher<Integer> apply(Flowable<Integer> v) { |
| 740 |
return new FlowableDoOnEach<>(v, |
| 741 |
new Consumer<Integer>() { |
| 742 |
@Override |
| 743 |
public void accept(Integer v) throws Exception { |
| 744 |
throw new TestException("First"); |
| 745 |
} |
| 746 |
}, |
| 747 |
new Consumer<Throwable>() { |
| 748 |
@Override |
| 749 |
public void accept(Throwable e) throws Exception { |
| 750 |
throw new TestException("Second"); |
| 751 |
} |
| 752 |
}, |
| 753 |
Functions.EMPTY_ACTION |
| 754 |
, |
| 755 |
Functions.EMPTY_ACTION |
| 756 |
); |
| 757 |
} |
| 758 |
}) |
| 759 |
.publish(); |
| 760 |
|
| 761 |
TestSubscriberEx<Integer> ts = cf.to(TestHelper.<Integer>testConsumer()); |
| 762 |
cf.connect(); |
| 763 |
|
| 764 |
ts.assertFailure(CompositeException.class); |
| 765 |
|
| 766 |
TestHelper.assertError(ts, 0, TestException.class, "First"); |
| 767 |
TestHelper.assertError(ts, 1, TestException.class, "Second"); |
| 768 |
} |
| 769 |
|
| 770 |
@Test |
| 771 |
public void doOnNextDoOnErrorFused2() { |
| 772 |
ConnectableFlowable<Integer> cf = Flowable.just(1) |
| 773 |
.doOnNext(new Consumer<Integer>() { |
| 774 |
@Override |
| 775 |
public void accept(Integer v) throws Exception { |
| 776 |
throw new TestException("First"); |
| 777 |
} |
| 778 |
}) |
| 779 |
.doOnError(new Consumer<Throwable>() { |
| 780 |
@Override |
| 781 |
public void accept(Throwable e) throws Exception { |
| 782 |
throw new TestException("Second"); |
| 783 |
} |
| 784 |
}) |
| 785 |
.doOnError(new Consumer<Throwable>() { |
| 786 |
@Override |
| 787 |
public void accept(Throwable e) throws Exception { |
| 788 |
throw new TestException("Third"); |
| 789 |
} |
| 790 |
}) |
| 791 |
.publish(); |
| 792 |
|
| 793 |
TestSubscriberEx<Integer> ts = cf.to(TestHelper.<Integer>testConsumer()); |
| 794 |
cf.connect(); |
| 795 |
|
| 796 |
ts.assertFailure(CompositeException.class); |
| 797 |
|
| 798 |
TestHelper.assertError(ts, 0, TestException.class, "First"); |
| 799 |
TestHelper.assertError(ts, 1, TestException.class, "Second"); |
| 800 |
TestHelper.assertError(ts, 2, TestException.class, "Third"); |
| 801 |
} |
| 802 |
|
| 803 |
@Test |
| 804 |
public void doOnNextDoOnErrorFusedConditional() { |
| 805 |
ConnectableFlowable<Integer> cf = Flowable.just(1) |
| 806 |
.doOnNext(new Consumer<Integer>() { |
| 807 |
@Override |
| 808 |
public void accept(Integer v) throws Exception { |
| 809 |
throw new TestException("First"); |
| 810 |
} |
| 811 |
}) |
| 812 |
.doOnError(new Consumer<Throwable>() { |
| 813 |
@Override |
| 814 |
public void accept(Throwable e) throws Exception { |
| 815 |
throw new TestException("Second"); |
| 816 |
} |
| 817 |
}) |
| 818 |
.filter(Functions.alwaysTrue()) |
| 819 |
.publish(); |
| 820 |
|
| 821 |
TestSubscriberEx<Integer> ts = cf.to(TestHelper.<Integer>testConsumer()); |
| 822 |
cf.connect(); |
| 823 |
|
| 824 |
ts.assertFailure(CompositeException.class); |
| 825 |
|
| 826 |
TestHelper.assertError(ts, 0, TestException.class, "First"); |
| 827 |
TestHelper.assertError(ts, 1, TestException.class, "Second"); |
| 828 |
} |
| 829 |
|
| 830 |
@Test |
| 831 |
public void doOnNextDoOnErrorFusedConditional2() { |
| 832 |
ConnectableFlowable<Integer> cf = Flowable.just(1) |
| 833 |
.doOnNext(new Consumer<Integer>() { |
| 834 |
@Override |
| 835 |
public void accept(Integer v) throws Exception { |
| 836 |
throw new TestException("First"); |
| 837 |
} |
| 838 |
}) |
| 839 |
.doOnError(new Consumer<Throwable>() { |
| 840 |
@Override |
| 841 |
public void accept(Throwable e) throws Exception { |
| 842 |
throw new TestException("Second"); |
| 843 |
} |
| 844 |
}) |
| 845 |
.doOnError(new Consumer<Throwable>() { |
| 846 |
@Override |
| 847 |
public void accept(Throwable e) throws Exception { |
| 848 |
throw new TestException("Third"); |
| 849 |
} |
| 850 |
}) |
| 851 |
.filter(Functions.alwaysTrue()) |
| 852 |
.publish(); |
| 853 |
|
| 854 |
TestSubscriberEx<Integer> ts = cf.to(TestHelper.<Integer>testConsumer()); |
| 855 |
cf.connect(); |
| 856 |
|
| 857 |
ts.assertFailure(CompositeException.class); |
| 858 |
|
| 859 |
TestHelper.assertError(ts, 0, TestException.class, "First"); |
| 860 |
TestHelper.assertError(ts, 1, TestException.class, "Second"); |
| 861 |
TestHelper.assertError(ts, 2, TestException.class, "Third"); |
| 862 |
} |
| 863 |
|
| 864 |
@Test |
| 865 |
public void doOnNextDoOnErrorCombinedFusedConditional() { |
| 866 |
ConnectableFlowable<Integer> cf = Flowable.just(1) |
| 867 |
.compose(new FlowableTransformer<Integer, Integer>() { |
| 868 |
@Override |
| 869 |
public Publisher<Integer> apply(Flowable<Integer> v) { |
| 870 |
return new FlowableDoOnEach<>(v, |
| 871 |
new Consumer<Integer>() { |
| 872 |
@Override |
| 873 |
public void accept(Integer v) throws Exception { |
| 874 |
throw new TestException("First"); |
| 875 |
} |
| 876 |
}, |
| 877 |
new Consumer<Throwable>() { |
| 878 |
@Override |
| 879 |
public void accept(Throwable e) throws Exception { |
| 880 |
throw new TestException("Second"); |
| 881 |
} |
| 882 |
}, |
| 883 |
Functions.EMPTY_ACTION |
| 884 |
, |
| 885 |
Functions.EMPTY_ACTION |
| 886 |
); |
| 887 |
} |
| 888 |
}) |
| 889 |
.filter(Functions.alwaysTrue()) |
| 890 |
.publish(); |
| 891 |
|
| 892 |
TestSubscriberEx<Integer> ts = cf.to(TestHelper.<Integer>testConsumer()); |
| 893 |
cf.connect(); |
| 894 |
|
| 895 |
ts.assertFailure(CompositeException.class); |
| 896 |
|
| 897 |
TestHelper.assertError(ts, 0, TestException.class, "First"); |
| 898 |
TestHelper.assertError(ts, 1, TestException.class, "Second"); |
| 899 |
} |
| 900 |
} |