| 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 |
|
| 18 |
import java.util.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.Observable; |
| 23 |
import io.reactivex.rxjava3.core.Observer; |
| 24 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 25 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 26 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 30 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 31 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 32 |
import io.reactivex.rxjava3.subjects.UnicastSubject; |
| 33 |
import io.reactivex.rxjava3.testsupport.*; |
| 34 |
|
| 35 |
public class ObservableDoFinallyTest extends RxJavaTest implements Action { |
| 36 |
|
| 37 |
int calls; |
| 38 |
|
| 39 |
@Override |
| 40 |
public void run() throws Exception { |
| 41 |
calls++; |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void normalJust() { |
| 46 |
Observable.just(1) |
| 47 |
.doFinally(this) |
| 48 |
.test() |
| 49 |
.assertResult(1); |
| 50 |
|
| 51 |
assertEquals(1, calls); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void normalEmpty() { |
| 56 |
Observable.empty() |
| 57 |
.doFinally(this) |
| 58 |
.test() |
| 59 |
.assertResult(); |
| 60 |
|
| 61 |
assertEquals(1, calls); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void normalError() { |
| 66 |
Observable.error(new TestException()) |
| 67 |
.doFinally(this) |
| 68 |
.test() |
| 69 |
.assertFailure(TestException.class); |
| 70 |
|
| 71 |
assertEquals(1, calls); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void normalTake() { |
| 76 |
Observable.range(1, 10) |
| 77 |
.doFinally(this) |
| 78 |
.take(5) |
| 79 |
.test() |
| 80 |
.assertResult(1, 2, 3, 4, 5); |
| 81 |
|
| 82 |
assertEquals(1, calls); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void doubleOnSubscribe() { |
| 87 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, Observable<Object>>() { |
| 88 |
@Override |
| 89 |
public Observable<Object> apply(Observable<Object> f) throws Exception { |
| 90 |
return f.doFinally(ObservableDoFinallyTest.this); |
| 91 |
} |
| 92 |
}); |
| 93 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, Observable<Object>>() { |
| 94 |
@Override |
| 95 |
public Observable<Object> apply(Observable<Object> f) throws Exception { |
| 96 |
return f.doFinally(ObservableDoFinallyTest.this).filter(Functions.alwaysTrue()); |
| 97 |
} |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void syncFused() { |
| 103 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.SYNC); |
| 104 |
|
| 105 |
Observable.range(1, 5) |
| 106 |
.doFinally(this) |
| 107 |
.subscribe(to); |
| 108 |
|
| 109 |
to.assertFusionMode(QueueFuseable.SYNC) |
| 110 |
.assertResult(1, 2, 3, 4, 5); |
| 111 |
|
| 112 |
assertEquals(1, calls); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void syncFusedBoundary() { |
| 117 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.SYNC | QueueFuseable.BOUNDARY); |
| 118 |
|
| 119 |
Observable.range(1, 5) |
| 120 |
.doFinally(this) |
| 121 |
.subscribe(to); |
| 122 |
|
| 123 |
to.assertFusionMode(QueueFuseable.NONE) |
| 124 |
.assertResult(1, 2, 3, 4, 5); |
| 125 |
|
| 126 |
assertEquals(1, calls); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void asyncFused() { |
| 131 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ASYNC); |
| 132 |
|
| 133 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 134 |
TestHelper.emit(us, 1, 2, 3, 4, 5); |
| 135 |
|
| 136 |
us |
| 137 |
.doFinally(this) |
| 138 |
.subscribe(to); |
| 139 |
|
| 140 |
to.assertFusionMode(QueueFuseable.ASYNC) |
| 141 |
.assertResult(1, 2, 3, 4, 5); |
| 142 |
|
| 143 |
assertEquals(1, calls); |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void asyncFusedBoundary() { |
| 148 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ASYNC | QueueFuseable.BOUNDARY); |
| 149 |
|
| 150 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 151 |
TestHelper.emit(us, 1, 2, 3, 4, 5); |
| 152 |
|
| 153 |
us |
| 154 |
.doFinally(this) |
| 155 |
.subscribe(to); |
| 156 |
|
| 157 |
to.assertFusionMode(QueueFuseable.NONE) |
| 158 |
.assertResult(1, 2, 3, 4, 5); |
| 159 |
|
| 160 |
assertEquals(1, calls); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void normalJustConditional() { |
| 165 |
Observable.just(1) |
| 166 |
.doFinally(this) |
| 167 |
.filter(Functions.alwaysTrue()) |
| 168 |
.test() |
| 169 |
.assertResult(1); |
| 170 |
|
| 171 |
assertEquals(1, calls); |
| 172 |
} |
| 173 |
|
| 174 |
@Test |
| 175 |
public void normalEmptyConditional() { |
| 176 |
Observable.empty() |
| 177 |
.doFinally(this) |
| 178 |
.filter(Functions.alwaysTrue()) |
| 179 |
.test() |
| 180 |
.assertResult(); |
| 181 |
|
| 182 |
assertEquals(1, calls); |
| 183 |
} |
| 184 |
|
| 185 |
@Test |
| 186 |
public void normalErrorConditional() { |
| 187 |
Observable.error(new TestException()) |
| 188 |
.doFinally(this) |
| 189 |
.filter(Functions.alwaysTrue()) |
| 190 |
.test() |
| 191 |
.assertFailure(TestException.class); |
| 192 |
|
| 193 |
assertEquals(1, calls); |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void normalTakeConditional() { |
| 198 |
Observable.range(1, 10) |
| 199 |
.doFinally(this) |
| 200 |
.filter(Functions.alwaysTrue()) |
| 201 |
.take(5) |
| 202 |
.test() |
| 203 |
.assertResult(1, 2, 3, 4, 5); |
| 204 |
|
| 205 |
assertEquals(1, calls); |
| 206 |
} |
| 207 |
|
| 208 |
@Test |
| 209 |
public void syncFusedConditional() { |
| 210 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.SYNC); |
| 211 |
|
| 212 |
Observable.range(1, 5) |
| 213 |
.doFinally(this) |
| 214 |
.filter(Functions.alwaysTrue()) |
| 215 |
.subscribe(to); |
| 216 |
|
| 217 |
to.assertFusionMode(QueueFuseable.SYNC) |
| 218 |
.assertResult(1, 2, 3, 4, 5); |
| 219 |
|
| 220 |
assertEquals(1, calls); |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void nonFused() { |
| 225 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.SYNC); |
| 226 |
|
| 227 |
Observable.range(1, 5).hide() |
| 228 |
.doFinally(this) |
| 229 |
.subscribe(to); |
| 230 |
|
| 231 |
to.assertFusionMode(QueueFuseable.NONE) |
| 232 |
.assertResult(1, 2, 3, 4, 5); |
| 233 |
|
| 234 |
assertEquals(1, calls); |
| 235 |
} |
| 236 |
|
| 237 |
@Test |
| 238 |
public void nonFusedConditional() { |
| 239 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.SYNC); |
| 240 |
|
| 241 |
Observable.range(1, 5).hide() |
| 242 |
.doFinally(this) |
| 243 |
.filter(Functions.alwaysTrue()) |
| 244 |
.subscribe(to); |
| 245 |
|
| 246 |
to.assertFusionMode(QueueFuseable.NONE) |
| 247 |
.assertResult(1, 2, 3, 4, 5); |
| 248 |
|
| 249 |
assertEquals(1, calls); |
| 250 |
} |
| 251 |
|
| 252 |
@Test |
| 253 |
public void syncFusedBoundaryConditional() { |
| 254 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.SYNC | QueueFuseable.BOUNDARY); |
| 255 |
|
| 256 |
Observable.range(1, 5) |
| 257 |
.doFinally(this) |
| 258 |
.filter(Functions.alwaysTrue()) |
| 259 |
.subscribe(to); |
| 260 |
|
| 261 |
to.assertFusionMode(QueueFuseable.NONE) |
| 262 |
.assertResult(1, 2, 3, 4, 5); |
| 263 |
|
| 264 |
assertEquals(1, calls); |
| 265 |
} |
| 266 |
|
| 267 |
@Test |
| 268 |
public void asyncFusedConditional() { |
| 269 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ASYNC); |
| 270 |
|
| 271 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 272 |
TestHelper.emit(us, 1, 2, 3, 4, 5); |
| 273 |
|
| 274 |
us |
| 275 |
.doFinally(this) |
| 276 |
.filter(Functions.alwaysTrue()) |
| 277 |
.subscribe(to); |
| 278 |
|
| 279 |
to.assertFusionMode(QueueFuseable.ASYNC) |
| 280 |
.assertResult(1, 2, 3, 4, 5); |
| 281 |
|
| 282 |
assertEquals(1, calls); |
| 283 |
} |
| 284 |
|
| 285 |
@Test |
| 286 |
public void asyncFusedBoundaryConditional() { |
| 287 |
TestObserverEx<Integer> to = new TestObserverEx<>(QueueFuseable.ASYNC | QueueFuseable.BOUNDARY); |
| 288 |
|
| 289 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 290 |
TestHelper.emit(us, 1, 2, 3, 4, 5); |
| 291 |
|
| 292 |
us |
| 293 |
.doFinally(this) |
| 294 |
.filter(Functions.alwaysTrue()) |
| 295 |
.subscribe(to); |
| 296 |
|
| 297 |
to.assertFusionMode(QueueFuseable.NONE) |
| 298 |
.assertResult(1, 2, 3, 4, 5); |
| 299 |
|
| 300 |
assertEquals(1, calls); |
| 301 |
} |
| 302 |
|
| 303 |
@Test |
| 304 |
public void actionThrows() { |
| 305 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 306 |
try { |
| 307 |
Observable.just(1) |
| 308 |
.doFinally(new Action() { |
| 309 |
@Override |
| 310 |
public void run() throws Exception { |
| 311 |
throw new TestException(); |
| 312 |
} |
| 313 |
}) |
| 314 |
.test() |
| 315 |
.assertResult(1) |
| 316 |
.dispose(); |
| 317 |
|
| 318 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 319 |
} finally { |
| 320 |
RxJavaPlugins.reset(); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
@Test |
| 325 |
public void actionThrowsConditional() { |
| 326 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 327 |
try { |
| 328 |
Observable.just(1) |
| 329 |
.doFinally(new Action() { |
| 330 |
@Override |
| 331 |
public void run() throws Exception { |
| 332 |
throw new TestException(); |
| 333 |
} |
| 334 |
}) |
| 335 |
.filter(Functions.alwaysTrue()) |
| 336 |
.test() |
| 337 |
.assertResult(1) |
| 338 |
.dispose(); |
| 339 |
|
| 340 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 341 |
} finally { |
| 342 |
RxJavaPlugins.reset(); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
@Test |
| 347 |
public void clearIsEmpty() { |
| 348 |
Observable.range(1, 5) |
| 349 |
.doFinally(this) |
| 350 |
.subscribe(new Observer<Integer>() { |
| 351 |
|
| 352 |
@Override |
| 353 |
public void onSubscribe(Disposable d) { |
| 354 |
@SuppressWarnings("unchecked") |
| 355 |
QueueDisposable<Integer> qd = (QueueDisposable<Integer>)d; |
| 356 |
|
| 357 |
qd.requestFusion(QueueFuseable.ANY); |
| 358 |
|
| 359 |
assertFalse(qd.isEmpty()); |
| 360 |
|
| 361 |
try { |
| 362 |
assertEquals(1, qd.poll().intValue()); |
| 363 |
} catch (Throwable ex) { |
| 364 |
throw new RuntimeException(ex); |
| 365 |
} |
| 366 |
|
| 367 |
assertFalse(qd.isEmpty()); |
| 368 |
|
| 369 |
qd.clear(); |
| 370 |
|
| 371 |
assertTrue(qd.isEmpty()); |
| 372 |
|
| 373 |
qd.dispose(); |
| 374 |
} |
| 375 |
|
| 376 |
@Override |
| 377 |
public void onNext(Integer t) { |
| 378 |
} |
| 379 |
|
| 380 |
@Override |
| 381 |
public void onError(Throwable t) { |
| 382 |
} |
| 383 |
|
| 384 |
@Override |
| 385 |
public void onComplete() { |
| 386 |
} |
| 387 |
}); |
| 388 |
|
| 389 |
assertEquals(1, calls); |
| 390 |
} |
| 391 |
|
| 392 |
@Test |
| 393 |
public void clearIsEmptyConditional() { |
| 394 |
Observable.range(1, 5) |
| 395 |
.doFinally(this) |
| 396 |
.filter(Functions.alwaysTrue()) |
| 397 |
.subscribe(new Observer<Integer>() { |
| 398 |
|
| 399 |
@Override |
| 400 |
public void onSubscribe(Disposable d) { |
| 401 |
@SuppressWarnings("unchecked") |
| 402 |
QueueDisposable<Integer> qd = (QueueDisposable<Integer>)d; |
| 403 |
|
| 404 |
qd.requestFusion(QueueFuseable.ANY); |
| 405 |
|
| 406 |
assertFalse(qd.isEmpty()); |
| 407 |
|
| 408 |
assertFalse(qd.isDisposed()); |
| 409 |
|
| 410 |
try { |
| 411 |
assertEquals(1, qd.poll().intValue()); |
| 412 |
} catch (Throwable ex) { |
| 413 |
throw new RuntimeException(ex); |
| 414 |
} |
| 415 |
|
| 416 |
assertFalse(qd.isEmpty()); |
| 417 |
|
| 418 |
qd.clear(); |
| 419 |
|
| 420 |
assertTrue(qd.isEmpty()); |
| 421 |
|
| 422 |
qd.dispose(); |
| 423 |
|
| 424 |
assertTrue(qd.isDisposed()); |
| 425 |
} |
| 426 |
|
| 427 |
@Override |
| 428 |
public void onNext(Integer t) { |
| 429 |
} |
| 430 |
|
| 431 |
@Override |
| 432 |
public void onError(Throwable t) { |
| 433 |
} |
| 434 |
|
| 435 |
@Override |
| 436 |
public void onComplete() { |
| 437 |
} |
| 438 |
}); |
| 439 |
|
| 440 |
assertEquals(1, calls); |
| 441 |
} |
| 442 |
|
| 443 |
@Test |
| 444 |
public void eventOrdering() { |
| 445 |
final List<String> list = new ArrayList<>(); |
| 446 |
|
| 447 |
Observable.error(new TestException()) |
| 448 |
.doOnDispose(new Action() { |
| 449 |
@Override |
| 450 |
public void run() throws Exception { |
| 451 |
list.add("dispose"); |
| 452 |
} |
| 453 |
}) |
| 454 |
.doFinally(new Action() { |
| 455 |
@Override |
| 456 |
public void run() throws Exception { |
| 457 |
list.add("finally"); |
| 458 |
} |
| 459 |
}) |
| 460 |
.subscribe( |
| 461 |
new Consumer<Object>() { |
| 462 |
@Override |
| 463 |
public void accept(Object v) throws Exception { |
| 464 |
list.add("onNext"); |
| 465 |
} |
| 466 |
}, |
| 467 |
new Consumer<Throwable>() { |
| 468 |
@Override |
| 469 |
public void accept(Throwable e) throws Exception { |
| 470 |
list.add("onError"); |
| 471 |
} |
| 472 |
}, |
| 473 |
new Action() { |
| 474 |
@Override |
| 475 |
public void run() throws Exception { |
| 476 |
list.add("onComplete"); |
| 477 |
} |
| 478 |
}); |
| 479 |
|
| 480 |
assertEquals(Arrays.asList("onError", "finally"), list); |
| 481 |
} |
| 482 |
|
| 483 |
@Test |
| 484 |
public void eventOrdering2() { |
| 485 |
final List<String> list = new ArrayList<>(); |
| 486 |
|
| 487 |
Observable.just(1) |
| 488 |
.doOnDispose(new Action() { |
| 489 |
@Override |
| 490 |
public void run() throws Exception { |
| 491 |
list.add("dispose"); |
| 492 |
} |
| 493 |
}) |
| 494 |
.doFinally(new Action() { |
| 495 |
@Override |
| 496 |
public void run() throws Exception { |
| 497 |
list.add("finally"); |
| 498 |
} |
| 499 |
}) |
| 500 |
.subscribe( |
| 501 |
new Consumer<Object>() { |
| 502 |
@Override |
| 503 |
public void accept(Object v) throws Exception { |
| 504 |
list.add("onNext"); |
| 505 |
} |
| 506 |
}, |
| 507 |
new Consumer<Throwable>() { |
| 508 |
@Override |
| 509 |
public void accept(Throwable e) throws Exception { |
| 510 |
list.add("onError"); |
| 511 |
} |
| 512 |
}, |
| 513 |
new Action() { |
| 514 |
@Override |
| 515 |
public void run() throws Exception { |
| 516 |
list.add("onComplete"); |
| 517 |
} |
| 518 |
}); |
| 519 |
|
| 520 |
assertEquals(Arrays.asList("onNext", "onComplete", "finally"), list); |
| 521 |
} |
| 522 |
|
| 523 |
@Test |
| 524 |
public void fusionRejected() { |
| 525 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 526 |
to.setInitialFusionMode(QueueFuseable.ANY); |
| 527 |
|
| 528 |
TestHelper.rejectObservableFusion() |
| 529 |
.doFinally(() -> { }) |
| 530 |
.subscribeWith(to); |
| 531 |
|
| 532 |
to.assertFuseable() |
| 533 |
.assertFusionMode(QueueFuseable.NONE); |
| 534 |
} |
| 535 |
} |