| 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.*; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.util.*; |
| 20 |
import java.util.concurrent.atomic.AtomicReference; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
import org.reactivestreams.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.functions.Cancellable; |
| 29 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 32 |
import io.reactivex.rxjava3.testsupport.*; |
| 33 |
|
| 34 |
public class FlowableCreateTest extends RxJavaTest { |
| 35 |
|
| 36 |
@Test |
| 37 |
public void basic() { |
| 38 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 39 |
try { |
| 40 |
final Disposable d = Disposable.empty(); |
| 41 |
|
| 42 |
Flowable.<Integer>create(new FlowableOnSubscribe<Integer>() { |
| 43 |
@Override |
| 44 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 45 |
e.setDisposable(d); |
| 46 |
|
| 47 |
e.onNext(1); |
| 48 |
e.onNext(2); |
| 49 |
e.onNext(3); |
| 50 |
e.onComplete(); |
| 51 |
e.onError(new TestException("first")); |
| 52 |
e.onNext(4); |
| 53 |
e.onError(new TestException("second")); |
| 54 |
e.onComplete(); |
| 55 |
} |
| 56 |
}, BackpressureStrategy.BUFFER) |
| 57 |
.test() |
| 58 |
.assertResult(1, 2, 3); |
| 59 |
|
| 60 |
assertTrue(d.isDisposed()); |
| 61 |
|
| 62 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "first"); |
| 63 |
TestHelper.assertUndeliverable(errors, 1, TestException.class, "second"); |
| 64 |
} finally { |
| 65 |
RxJavaPlugins.reset(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void basicWithCancellable() { |
| 71 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 72 |
try { |
| 73 |
final Disposable d1 = Disposable.empty(); |
| 74 |
final Disposable d2 = Disposable.empty(); |
| 75 |
|
| 76 |
Flowable.<Integer>create(new FlowableOnSubscribe<Integer>() { |
| 77 |
@Override |
| 78 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 79 |
e.setDisposable(d1); |
| 80 |
e.setCancellable(new Cancellable() { |
| 81 |
@Override |
| 82 |
public void cancel() throws Exception { |
| 83 |
d2.dispose(); |
| 84 |
} |
| 85 |
}); |
| 86 |
|
| 87 |
e.onNext(1); |
| 88 |
e.onNext(2); |
| 89 |
e.onNext(3); |
| 90 |
e.onComplete(); |
| 91 |
e.onError(new TestException("first")); |
| 92 |
e.onNext(4); |
| 93 |
e.onError(new TestException("second")); |
| 94 |
e.onComplete(); |
| 95 |
} |
| 96 |
}, BackpressureStrategy.BUFFER) |
| 97 |
.test() |
| 98 |
.assertResult(1, 2, 3); |
| 99 |
|
| 100 |
assertTrue(d1.isDisposed()); |
| 101 |
assertTrue(d2.isDisposed()); |
| 102 |
|
| 103 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "first"); |
| 104 |
TestHelper.assertUndeliverable(errors, 1, TestException.class, "second"); |
| 105 |
} finally { |
| 106 |
RxJavaPlugins.reset(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
public void basicWithError() { |
| 112 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 113 |
try { |
| 114 |
final Disposable d = Disposable.empty(); |
| 115 |
|
| 116 |
Flowable.<Integer>create(new FlowableOnSubscribe<Integer>() { |
| 117 |
@Override |
| 118 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 119 |
e.setDisposable(d); |
| 120 |
|
| 121 |
e.onNext(1); |
| 122 |
e.onNext(2); |
| 123 |
e.onNext(3); |
| 124 |
e.onError(new TestException()); |
| 125 |
e.onComplete(); |
| 126 |
e.onNext(4); |
| 127 |
e.onError(new TestException("second")); |
| 128 |
} |
| 129 |
}, BackpressureStrategy.BUFFER) |
| 130 |
.test() |
| 131 |
.assertFailure(TestException.class, 1, 2, 3); |
| 132 |
|
| 133 |
assertTrue(d.isDisposed()); |
| 134 |
|
| 135 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "second"); |
| 136 |
} finally { |
| 137 |
RxJavaPlugins.reset(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void basicSerialized() { |
| 143 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 144 |
try { |
| 145 |
final Disposable d = Disposable.empty(); |
| 146 |
|
| 147 |
Flowable.<Integer>create(new FlowableOnSubscribe<Integer>() { |
| 148 |
@Override |
| 149 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 150 |
e = e.serialize(); |
| 151 |
|
| 152 |
e.setDisposable(d); |
| 153 |
|
| 154 |
e.onNext(1); |
| 155 |
e.onNext(2); |
| 156 |
e.onNext(3); |
| 157 |
e.onComplete(); |
| 158 |
e.onError(new TestException("first")); |
| 159 |
e.onNext(4); |
| 160 |
e.onError(new TestException("second")); |
| 161 |
e.onComplete(); |
| 162 |
} |
| 163 |
}, BackpressureStrategy.BUFFER) |
| 164 |
.test() |
| 165 |
.assertResult(1, 2, 3); |
| 166 |
|
| 167 |
assertTrue(d.isDisposed()); |
| 168 |
|
| 169 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "first"); |
| 170 |
TestHelper.assertUndeliverable(errors, 1, TestException.class, "second"); |
| 171 |
} finally { |
| 172 |
RxJavaPlugins.reset(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void basicWithErrorSerialized() { |
| 178 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 179 |
try { |
| 180 |
final Disposable d = Disposable.empty(); |
| 181 |
|
| 182 |
Flowable.<Integer>create(new FlowableOnSubscribe<Integer>() { |
| 183 |
@Override |
| 184 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 185 |
e = e.serialize(); |
| 186 |
|
| 187 |
e.setDisposable(d); |
| 188 |
|
| 189 |
e.onNext(1); |
| 190 |
e.onNext(2); |
| 191 |
e.onNext(3); |
| 192 |
e.onError(new TestException()); |
| 193 |
e.onComplete(); |
| 194 |
e.onNext(4); |
| 195 |
e.onError(new TestException("second")); |
| 196 |
} |
| 197 |
}, BackpressureStrategy.BUFFER) |
| 198 |
.test() |
| 199 |
.assertFailure(TestException.class, 1, 2, 3); |
| 200 |
|
| 201 |
assertTrue(d.isDisposed()); |
| 202 |
|
| 203 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "second"); |
| 204 |
} finally { |
| 205 |
RxJavaPlugins.reset(); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
@Test |
| 210 |
public void wrap() { |
| 211 |
Flowable.fromPublisher(new Publisher<Integer>() { |
| 212 |
@Override |
| 213 |
public void subscribe(Subscriber<? super Integer> subscriber) { |
| 214 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 215 |
subscriber.onNext(1); |
| 216 |
subscriber.onNext(2); |
| 217 |
subscriber.onNext(3); |
| 218 |
subscriber.onNext(4); |
| 219 |
subscriber.onNext(5); |
| 220 |
subscriber.onComplete(); |
| 221 |
} |
| 222 |
}) |
| 223 |
.test() |
| 224 |
.assertResult(1, 2, 3, 4, 5); |
| 225 |
} |
| 226 |
|
| 227 |
@Test |
| 228 |
public void unsafe() { |
| 229 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 230 |
@Override |
| 231 |
public void subscribe(Subscriber<? super Integer> subscriber) { |
| 232 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 233 |
subscriber.onNext(1); |
| 234 |
subscriber.onNext(2); |
| 235 |
subscriber.onNext(3); |
| 236 |
subscriber.onNext(4); |
| 237 |
subscriber.onNext(5); |
| 238 |
subscriber.onComplete(); |
| 239 |
} |
| 240 |
}) |
| 241 |
.test() |
| 242 |
.assertResult(1, 2, 3, 4, 5); |
| 243 |
} |
| 244 |
|
| 245 |
@Test(expected = IllegalArgumentException.class) |
| 246 |
public void unsafeWithFlowable() { |
| 247 |
Flowable.unsafeCreate(Flowable.just(1)); |
| 248 |
} |
| 249 |
|
| 250 |
@Test |
| 251 |
public void createNullValueBuffer() { |
| 252 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 253 |
try { |
| 254 |
|
| 255 |
final Throwable[] error = { null }; |
| 256 |
|
| 257 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 258 |
@Override |
| 259 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 260 |
try { |
| 261 |
e.onNext(null); |
| 262 |
e.onNext(1); |
| 263 |
e.onError(new TestException()); |
| 264 |
e.onComplete(); |
| 265 |
} catch (Throwable ex) { |
| 266 |
error[0] = ex; |
| 267 |
} |
| 268 |
} |
| 269 |
}, BackpressureStrategy.BUFFER) |
| 270 |
.test() |
| 271 |
.assertFailure(NullPointerException.class); |
| 272 |
|
| 273 |
assertNull(error[0]); |
| 274 |
|
| 275 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 276 |
} finally { |
| 277 |
RxJavaPlugins.reset(); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
@Test |
| 282 |
public void createNullValueLatest() { |
| 283 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 284 |
try { |
| 285 |
final Throwable[] error = { null }; |
| 286 |
|
| 287 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 288 |
@Override |
| 289 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 290 |
try { |
| 291 |
e.onNext(null); |
| 292 |
e.onNext(1); |
| 293 |
e.onError(new TestException()); |
| 294 |
e.onComplete(); |
| 295 |
} catch (Throwable ex) { |
| 296 |
error[0] = ex; |
| 297 |
} |
| 298 |
} |
| 299 |
}, BackpressureStrategy.LATEST) |
| 300 |
.test() |
| 301 |
.assertFailure(NullPointerException.class); |
| 302 |
|
| 303 |
assertNull(error[0]); |
| 304 |
|
| 305 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 306 |
} finally { |
| 307 |
RxJavaPlugins.reset(); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
@Test |
| 312 |
public void createNullValueError() { |
| 313 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 314 |
try { |
| 315 |
final Throwable[] error = { null }; |
| 316 |
|
| 317 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 318 |
@Override |
| 319 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 320 |
try { |
| 321 |
e.onNext(null); |
| 322 |
e.onNext(1); |
| 323 |
e.onError(new TestException()); |
| 324 |
e.onComplete(); |
| 325 |
} catch (Throwable ex) { |
| 326 |
error[0] = ex; |
| 327 |
} |
| 328 |
} |
| 329 |
}, BackpressureStrategy.ERROR) |
| 330 |
.test() |
| 331 |
.assertFailure(NullPointerException.class); |
| 332 |
|
| 333 |
assertNull(error[0]); |
| 334 |
|
| 335 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 336 |
} finally { |
| 337 |
RxJavaPlugins.reset(); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
@Test |
| 342 |
public void createNullValueDrop() { |
| 343 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 344 |
try { |
| 345 |
final Throwable[] error = { null }; |
| 346 |
|
| 347 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 348 |
@Override |
| 349 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 350 |
try { |
| 351 |
e.onNext(null); |
| 352 |
e.onNext(1); |
| 353 |
e.onError(new TestException()); |
| 354 |
e.onComplete(); |
| 355 |
} catch (Throwable ex) { |
| 356 |
error[0] = ex; |
| 357 |
} |
| 358 |
} |
| 359 |
}, BackpressureStrategy.DROP) |
| 360 |
.test() |
| 361 |
.assertFailure(NullPointerException.class); |
| 362 |
|
| 363 |
assertNull(error[0]); |
| 364 |
|
| 365 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 366 |
} finally { |
| 367 |
RxJavaPlugins.reset(); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
@Test |
| 372 |
public void createNullValueMissing() { |
| 373 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 374 |
try { |
| 375 |
final Throwable[] error = { null }; |
| 376 |
|
| 377 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 378 |
@Override |
| 379 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 380 |
try { |
| 381 |
e.onNext(null); |
| 382 |
e.onNext(1); |
| 383 |
e.onError(new TestException()); |
| 384 |
e.onComplete(); |
| 385 |
} catch (Throwable ex) { |
| 386 |
error[0] = ex; |
| 387 |
} |
| 388 |
} |
| 389 |
}, BackpressureStrategy.MISSING) |
| 390 |
.test() |
| 391 |
.assertFailure(NullPointerException.class); |
| 392 |
|
| 393 |
assertNull(error[0]); |
| 394 |
|
| 395 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 396 |
} finally { |
| 397 |
RxJavaPlugins.reset(); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
@Test |
| 402 |
public void createNullValueBufferSerialized() { |
| 403 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 404 |
try { |
| 405 |
final Throwable[] error = { null }; |
| 406 |
|
| 407 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 408 |
@Override |
| 409 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 410 |
e = e.serialize(); |
| 411 |
try { |
| 412 |
e.onNext(null); |
| 413 |
e.onNext(1); |
| 414 |
e.onError(new TestException()); |
| 415 |
e.onComplete(); |
| 416 |
} catch (Throwable ex) { |
| 417 |
error[0] = ex; |
| 418 |
} |
| 419 |
} |
| 420 |
}, BackpressureStrategy.BUFFER) |
| 421 |
.test() |
| 422 |
.assertFailure(NullPointerException.class); |
| 423 |
|
| 424 |
assertNull(error[0]); |
| 425 |
|
| 426 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 427 |
} finally { |
| 428 |
RxJavaPlugins.reset(); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
@Test |
| 433 |
public void createNullValueLatestSerialized() { |
| 434 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 435 |
try { |
| 436 |
final Throwable[] error = { null }; |
| 437 |
|
| 438 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 439 |
@Override |
| 440 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 441 |
e = e.serialize(); |
| 442 |
try { |
| 443 |
e.onNext(null); |
| 444 |
e.onNext(1); |
| 445 |
e.onError(new TestException()); |
| 446 |
e.onComplete(); |
| 447 |
} catch (Throwable ex) { |
| 448 |
error[0] = ex; |
| 449 |
} |
| 450 |
} |
| 451 |
}, BackpressureStrategy.LATEST) |
| 452 |
.test() |
| 453 |
.assertFailure(NullPointerException.class); |
| 454 |
|
| 455 |
assertNull(error[0]); |
| 456 |
|
| 457 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 458 |
} finally { |
| 459 |
RxJavaPlugins.reset(); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
@Test |
| 464 |
public void createNullValueErrorSerialized() { |
| 465 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 466 |
try { |
| 467 |
final Throwable[] error = { null }; |
| 468 |
|
| 469 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 470 |
@Override |
| 471 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 472 |
e = e.serialize(); |
| 473 |
try { |
| 474 |
e.onNext(null); |
| 475 |
e.onNext(1); |
| 476 |
e.onError(new TestException()); |
| 477 |
e.onComplete(); |
| 478 |
} catch (Throwable ex) { |
| 479 |
error[0] = ex; |
| 480 |
} |
| 481 |
} |
| 482 |
}, BackpressureStrategy.ERROR) |
| 483 |
.test() |
| 484 |
.assertFailure(NullPointerException.class); |
| 485 |
|
| 486 |
assertNull(error[0]); |
| 487 |
|
| 488 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 489 |
} finally { |
| 490 |
RxJavaPlugins.reset(); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
@Test |
| 495 |
public void createNullValueDropSerialized() { |
| 496 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 497 |
try { |
| 498 |
final Throwable[] error = { null }; |
| 499 |
|
| 500 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 501 |
@Override |
| 502 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 503 |
e = e.serialize(); |
| 504 |
try { |
| 505 |
e.onNext(null); |
| 506 |
e.onNext(1); |
| 507 |
e.onError(new TestException()); |
| 508 |
e.onComplete(); |
| 509 |
} catch (Throwable ex) { |
| 510 |
error[0] = ex; |
| 511 |
} |
| 512 |
} |
| 513 |
}, BackpressureStrategy.DROP) |
| 514 |
.test() |
| 515 |
.assertFailure(NullPointerException.class); |
| 516 |
|
| 517 |
assertNull(error[0]); |
| 518 |
|
| 519 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 520 |
} finally { |
| 521 |
RxJavaPlugins.reset(); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
@Test |
| 526 |
public void createNullValueMissingSerialized() { |
| 527 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 528 |
try { |
| 529 |
final Throwable[] error = { null }; |
| 530 |
|
| 531 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 532 |
@Override |
| 533 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 534 |
e = e.serialize(); |
| 535 |
try { |
| 536 |
e.onNext(null); |
| 537 |
e.onNext(1); |
| 538 |
e.onError(new TestException()); |
| 539 |
e.onComplete(); |
| 540 |
} catch (Throwable ex) { |
| 541 |
error[0] = ex; |
| 542 |
} |
| 543 |
} |
| 544 |
}, BackpressureStrategy.MISSING) |
| 545 |
.test() |
| 546 |
.assertFailure(NullPointerException.class); |
| 547 |
|
| 548 |
assertNull(error[0]); |
| 549 |
|
| 550 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 551 |
} finally { |
| 552 |
RxJavaPlugins.reset(); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
@Test |
| 557 |
public void onErrorRace() { |
| 558 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 559 |
Flowable<Object> source = Flowable.create(new FlowableOnSubscribe<Object>() { |
| 560 |
@Override |
| 561 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 562 |
final FlowableEmitter<Object> f = e.serialize(); |
| 563 |
|
| 564 |
final TestException ex = new TestException(); |
| 565 |
|
| 566 |
Runnable r1 = new Runnable() { |
| 567 |
@Override |
| 568 |
public void run() { |
| 569 |
f.onError(null); |
| 570 |
} |
| 571 |
}; |
| 572 |
|
| 573 |
Runnable r2 = new Runnable() { |
| 574 |
@Override |
| 575 |
public void run() { |
| 576 |
f.onError(ex); |
| 577 |
} |
| 578 |
}; |
| 579 |
|
| 580 |
TestHelper.race(r1, r2); |
| 581 |
} |
| 582 |
}, m); |
| 583 |
|
| 584 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 585 |
|
| 586 |
try { |
| 587 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 588 |
source |
| 589 |
.test() |
| 590 |
.assertFailure(Throwable.class); |
| 591 |
} |
| 592 |
} finally { |
| 593 |
RxJavaPlugins.reset(); |
| 594 |
} |
| 595 |
assertFalse(errors.isEmpty()); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
@Test |
| 600 |
public void onCompleteRace() { |
| 601 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 602 |
Flowable<Object> source = Flowable.create(new FlowableOnSubscribe<Object>() { |
| 603 |
@Override |
| 604 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 605 |
final FlowableEmitter<Object> f = e.serialize(); |
| 606 |
|
| 607 |
Runnable r1 = new Runnable() { |
| 608 |
@Override |
| 609 |
public void run() { |
| 610 |
f.onComplete(); |
| 611 |
} |
| 612 |
}; |
| 613 |
|
| 614 |
Runnable r2 = new Runnable() { |
| 615 |
@Override |
| 616 |
public void run() { |
| 617 |
f.onComplete(); |
| 618 |
} |
| 619 |
}; |
| 620 |
|
| 621 |
TestHelper.race(r1, r2); |
| 622 |
} |
| 623 |
}, m); |
| 624 |
|
| 625 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 626 |
source |
| 627 |
.test() |
| 628 |
.assertResult(); |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
@Test |
| 634 |
public void nullValue() { |
| 635 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 636 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 637 |
@Override |
| 638 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 639 |
e.onNext(null); |
| 640 |
} |
| 641 |
}, m) |
| 642 |
.test() |
| 643 |
.assertFailure(NullPointerException.class); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
@Test |
| 648 |
public void nullThrowable() { |
| 649 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 650 |
System.out.println(m); |
| 651 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 652 |
@Override |
| 653 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 654 |
e.onError(null); |
| 655 |
} |
| 656 |
}, m) |
| 657 |
.test() |
| 658 |
.assertFailure(NullPointerException.class); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
@Test |
| 663 |
public void serializedConcurrentOnNextOnError() { |
| 664 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 665 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 666 |
@Override |
| 667 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 668 |
final FlowableEmitter<Object> f = e.serialize(); |
| 669 |
|
| 670 |
Runnable r1 = new Runnable() { |
| 671 |
@Override |
| 672 |
public void run() { |
| 673 |
for (int i = 0; i < 1000; i++) { |
| 674 |
f.onNext(1); |
| 675 |
} |
| 676 |
} |
| 677 |
}; |
| 678 |
|
| 679 |
Runnable r2 = new Runnable() { |
| 680 |
@Override |
| 681 |
public void run() { |
| 682 |
for (int i = 0; i < 100; i++) { |
| 683 |
f.onNext(1); |
| 684 |
} |
| 685 |
f.onError(new TestException()); |
| 686 |
} |
| 687 |
}; |
| 688 |
|
| 689 |
TestHelper.race(r1, r2); |
| 690 |
} |
| 691 |
}, m) |
| 692 |
.to(TestHelper.<Object>testConsumer()) |
| 693 |
.assertSubscribed() |
| 694 |
.assertNotComplete() |
| 695 |
.assertError(TestException.class); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
@Test |
| 700 |
public void callbackThrows() { |
| 701 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 702 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 703 |
@Override |
| 704 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 705 |
throw new TestException(); |
| 706 |
} |
| 707 |
}, m) |
| 708 |
.test() |
| 709 |
.assertFailure(TestException.class); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
@Test |
| 714 |
public void nullValueSync() { |
| 715 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 716 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 717 |
@Override |
| 718 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 719 |
e.serialize().onNext(null); |
| 720 |
} |
| 721 |
}, m) |
| 722 |
.test() |
| 723 |
.assertFailure(NullPointerException.class); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
@Test |
| 728 |
public void createNullValue() { |
| 729 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 730 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 731 |
try { |
| 732 |
final Throwable[] error = { null }; |
| 733 |
|
| 734 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 735 |
@Override |
| 736 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 737 |
try { |
| 738 |
e.onNext(null); |
| 739 |
e.onNext(1); |
| 740 |
e.onError(new TestException()); |
| 741 |
e.onComplete(); |
| 742 |
} catch (Throwable ex) { |
| 743 |
error[0] = ex; |
| 744 |
} |
| 745 |
} |
| 746 |
}, m) |
| 747 |
.test() |
| 748 |
.assertFailure(NullPointerException.class); |
| 749 |
|
| 750 |
assertNull(error[0]); |
| 751 |
|
| 752 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 753 |
} finally { |
| 754 |
RxJavaPlugins.reset(); |
| 755 |
} |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
@Test |
| 760 |
public void onErrorCrash() { |
| 761 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 762 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 763 |
@Override |
| 764 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 765 |
Disposable d = Disposable.empty(); |
| 766 |
e.setDisposable(d); |
| 767 |
try { |
| 768 |
e.onError(new IOException()); |
| 769 |
fail("Should have thrown"); |
| 770 |
} catch (TestException ex) { |
| 771 |
// expected |
| 772 |
} |
| 773 |
assertTrue(d.isDisposed()); |
| 774 |
} |
| 775 |
}, m) |
| 776 |
.subscribe(new FlowableSubscriber<Object>() { |
| 777 |
@Override |
| 778 |
public void onSubscribe(Subscription s) { |
| 779 |
} |
| 780 |
|
| 781 |
@Override |
| 782 |
public void onNext(Object value) { |
| 783 |
} |
| 784 |
|
| 785 |
@Override |
| 786 |
public void onError(Throwable e) { |
| 787 |
throw new TestException(); |
| 788 |
} |
| 789 |
|
| 790 |
@Override |
| 791 |
public void onComplete() { |
| 792 |
} |
| 793 |
}); |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
@Test |
| 798 |
public void onCompleteCrash() { |
| 799 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 800 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 801 |
@Override |
| 802 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 803 |
Disposable d = Disposable.empty(); |
| 804 |
e.setDisposable(d); |
| 805 |
try { |
| 806 |
e.onComplete(); |
| 807 |
fail("Should have thrown"); |
| 808 |
} catch (TestException ex) { |
| 809 |
// expected |
| 810 |
} |
| 811 |
assertTrue(d.isDisposed()); |
| 812 |
} |
| 813 |
}, m) |
| 814 |
.subscribe(new FlowableSubscriber<Object>() { |
| 815 |
@Override |
| 816 |
public void onSubscribe(Subscription s) { |
| 817 |
} |
| 818 |
|
| 819 |
@Override |
| 820 |
public void onNext(Object value) { |
| 821 |
} |
| 822 |
|
| 823 |
@Override |
| 824 |
public void onError(Throwable e) { |
| 825 |
} |
| 826 |
|
| 827 |
@Override |
| 828 |
public void onComplete() { |
| 829 |
throw new TestException(); |
| 830 |
} |
| 831 |
}); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
@Test |
| 836 |
public void createNullValueSerialized() { |
| 837 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 838 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 839 |
try { |
| 840 |
final Throwable[] error = { null }; |
| 841 |
|
| 842 |
Flowable.create(new FlowableOnSubscribe<Integer>() { |
| 843 |
@Override |
| 844 |
public void subscribe(FlowableEmitter<Integer> e) throws Exception { |
| 845 |
e = e.serialize(); |
| 846 |
try { |
| 847 |
e.onNext(null); |
| 848 |
e.onNext(1); |
| 849 |
e.onError(new TestException()); |
| 850 |
e.onComplete(); |
| 851 |
} catch (Throwable ex) { |
| 852 |
error[0] = ex; |
| 853 |
} |
| 854 |
} |
| 855 |
}, m) |
| 856 |
.test() |
| 857 |
.assertFailure(NullPointerException.class); |
| 858 |
|
| 859 |
assertNull(error[0]); |
| 860 |
|
| 861 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 862 |
} finally { |
| 863 |
RxJavaPlugins.reset(); |
| 864 |
} |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
@Test |
| 869 |
public void nullThrowableSync() { |
| 870 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 871 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 872 |
@Override |
| 873 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 874 |
e.serialize().onError(null); |
| 875 |
} |
| 876 |
}, m) |
| 877 |
.test() |
| 878 |
.assertFailure(NullPointerException.class); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
@Test |
| 883 |
public void serializedConcurrentOnNext() { |
| 884 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 885 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 886 |
@Override |
| 887 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 888 |
final FlowableEmitter<Object> f = e.serialize(); |
| 889 |
|
| 890 |
Runnable r1 = new Runnable() { |
| 891 |
@Override |
| 892 |
public void run() { |
| 893 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 894 |
f.onNext(1); |
| 895 |
} |
| 896 |
} |
| 897 |
}; |
| 898 |
|
| 899 |
TestHelper.race(r1, r1); |
| 900 |
} |
| 901 |
}, m) |
| 902 |
.take(TestHelper.RACE_DEFAULT_LOOPS) |
| 903 |
.to(TestHelper.<Object>testConsumer()) |
| 904 |
.assertSubscribed() |
| 905 |
.assertValueCount(TestHelper.RACE_DEFAULT_LOOPS) |
| 906 |
.assertComplete() |
| 907 |
.assertNoErrors(); |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
@Test |
| 912 |
public void serializedConcurrentOnNextOnComplete() { |
| 913 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 914 |
TestSubscriberEx<Object> ts = Flowable.create(new FlowableOnSubscribe<Object>() { |
| 915 |
@Override |
| 916 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 917 |
final FlowableEmitter<Object> f = e.serialize(); |
| 918 |
|
| 919 |
Runnable r1 = new Runnable() { |
| 920 |
@Override |
| 921 |
public void run() { |
| 922 |
for (int i = 0; i < 1000; i++) { |
| 923 |
f.onNext(1); |
| 924 |
} |
| 925 |
} |
| 926 |
}; |
| 927 |
|
| 928 |
Runnable r2 = new Runnable() { |
| 929 |
@Override |
| 930 |
public void run() { |
| 931 |
for (int i = 0; i < 100; i++) { |
| 932 |
f.onNext(1); |
| 933 |
} |
| 934 |
f.onComplete(); |
| 935 |
} |
| 936 |
}; |
| 937 |
|
| 938 |
TestHelper.race(r1, r2); |
| 939 |
} |
| 940 |
}, m) |
| 941 |
.to(TestHelper.<Object>testConsumer()) |
| 942 |
.assertSubscribed() |
| 943 |
.assertComplete() |
| 944 |
.assertNoErrors(); |
| 945 |
|
| 946 |
int c = ts.values().size(); |
| 947 |
assertTrue("" + c, c >= 100); |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
@Test |
| 952 |
public void serialized() { |
| 953 |
for (BackpressureStrategy m : BackpressureStrategy.values()) { |
| 954 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 955 |
try { |
| 956 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 957 |
@Override |
| 958 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 959 |
FlowableEmitter<Object> f = e.serialize(); |
| 960 |
|
| 961 |
assertSame(f, f.serialize()); |
| 962 |
|
| 963 |
assertFalse(f.isCancelled()); |
| 964 |
|
| 965 |
final int[] calls = { 0 }; |
| 966 |
|
| 967 |
f.setCancellable(new Cancellable() { |
| 968 |
@Override |
| 969 |
public void cancel() throws Exception { |
| 970 |
calls[0]++; |
| 971 |
} |
| 972 |
}); |
| 973 |
|
| 974 |
e.onComplete(); |
| 975 |
|
| 976 |
assertTrue(f.isCancelled()); |
| 977 |
|
| 978 |
assertEquals(1, calls[0]); |
| 979 |
} |
| 980 |
}, m) |
| 981 |
.test() |
| 982 |
.assertResult(); |
| 983 |
|
| 984 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 985 |
} finally { |
| 986 |
RxJavaPlugins.reset(); |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
@Test |
| 992 |
public void tryOnError() { |
| 993 |
for (BackpressureStrategy strategy : BackpressureStrategy.values()) { |
| 994 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 995 |
try { |
| 996 |
final Boolean[] response = { null }; |
| 997 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 998 |
@Override |
| 999 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 1000 |
e.onNext(1); |
| 1001 |
response[0] = e.tryOnError(new TestException()); |
| 1002 |
} |
| 1003 |
}, strategy) |
| 1004 |
.take(1) |
| 1005 |
.test() |
| 1006 |
.withTag(strategy.toString()) |
| 1007 |
.assertResult(1); |
| 1008 |
|
| 1009 |
assertFalse(response[0]); |
| 1010 |
|
| 1011 |
assertTrue(strategy + ": " + errors.toString(), errors.isEmpty()); |
| 1012 |
} finally { |
| 1013 |
RxJavaPlugins.reset(); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
@Test |
| 1019 |
public void tryOnErrorSerialized() { |
| 1020 |
for (BackpressureStrategy strategy : BackpressureStrategy.values()) { |
| 1021 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1022 |
try { |
| 1023 |
final Boolean[] response = { null }; |
| 1024 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 1025 |
@Override |
| 1026 |
public void subscribe(FlowableEmitter<Object> e) throws Exception { |
| 1027 |
e = e.serialize(); |
| 1028 |
e.onNext(1); |
| 1029 |
response[0] = e.tryOnError(new TestException()); |
| 1030 |
} |
| 1031 |
}, strategy) |
| 1032 |
.take(1) |
| 1033 |
.test() |
| 1034 |
.withTag(strategy.toString()) |
| 1035 |
.assertResult(1); |
| 1036 |
|
| 1037 |
assertFalse(response[0]); |
| 1038 |
|
| 1039 |
assertTrue(strategy + ": " + errors.toString(), errors.isEmpty()); |
| 1040 |
} finally { |
| 1041 |
RxJavaPlugins.reset(); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
@SuppressWarnings("rawtypes") |
| 1047 |
@Test |
| 1048 |
public void emittersHasToString() { |
| 1049 |
Map<BackpressureStrategy, Class<? extends FlowableEmitter>> emitterMap = |
| 1050 |
new HashMap<>(); |
| 1051 |
|
| 1052 |
emitterMap.put(BackpressureStrategy.MISSING, FlowableCreate.MissingEmitter.class); |
| 1053 |
emitterMap.put(BackpressureStrategy.ERROR, FlowableCreate.ErrorAsyncEmitter.class); |
| 1054 |
emitterMap.put(BackpressureStrategy.DROP, FlowableCreate.DropAsyncEmitter.class); |
| 1055 |
emitterMap.put(BackpressureStrategy.LATEST, FlowableCreate.LatestAsyncEmitter.class); |
| 1056 |
emitterMap.put(BackpressureStrategy.BUFFER, FlowableCreate.BufferAsyncEmitter.class); |
| 1057 |
|
| 1058 |
for (final Map.Entry<BackpressureStrategy, Class<? extends FlowableEmitter>> entry : emitterMap.entrySet()) { |
| 1059 |
Flowable.create(new FlowableOnSubscribe<Object>() { |
| 1060 |
@Override |
| 1061 |
public void subscribe(FlowableEmitter<Object> emitter) throws Exception { |
| 1062 |
assertTrue(emitter.toString().contains(entry.getValue().getSimpleName())); |
| 1063 |
assertTrue(emitter.serialize().toString().contains(entry.getValue().getSimpleName())); |
| 1064 |
} |
| 1065 |
}, entry.getKey()).test().assertEmpty(); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
@Test |
| 1070 |
public void serializedMissingMoreWorkWithComplete() { |
| 1071 |
AtomicReference<FlowableEmitter<Integer>> ref = new AtomicReference<>(); |
| 1072 |
|
| 1073 |
Flowable.<Integer>create(emitter -> { |
| 1074 |
emitter = emitter.serialize(); |
| 1075 |
ref.set(emitter); |
| 1076 |
assertEquals(Long.MAX_VALUE, emitter.requested()); |
| 1077 |
emitter.onNext(1); |
| 1078 |
}, BackpressureStrategy.MISSING) |
| 1079 |
.doOnNext(v -> { |
| 1080 |
if (v == 1) { |
| 1081 |
ref.get().onNext(2); |
| 1082 |
ref.get().onComplete(); |
| 1083 |
} |
| 1084 |
}) |
| 1085 |
.test() |
| 1086 |
.assertResult(1, 2); |
| 1087 |
} |
| 1088 |
|
| 1089 |
@Test |
| 1090 |
public void badRequest() { |
| 1091 |
TestHelper.assertBadRequestReported(Flowable.create(e -> { }, BackpressureStrategy.BUFFER)); |
| 1092 |
} |
| 1093 |
|
| 1094 |
@Test |
| 1095 |
public void tryOnErrorNull() { |
| 1096 |
Flowable.create(emitter -> emitter.tryOnError(null), BackpressureStrategy.MISSING) |
| 1097 |
.test() |
| 1098 |
.assertFailure(NullPointerException.class); |
| 1099 |
} |
| 1100 |
|
| 1101 |
@Test |
| 1102 |
public void serializedCompleteOnNext() { |
| 1103 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1104 |
|
| 1105 |
Flowable.<Integer>create(emitter -> { |
| 1106 |
emitter = emitter.serialize(); |
| 1107 |
|
| 1108 |
emitter.onComplete(); |
| 1109 |
emitter.onNext(1); |
| 1110 |
}, BackpressureStrategy.MISSING) |
| 1111 |
.subscribe(ts); |
| 1112 |
|
| 1113 |
ts.assertResult(); |
| 1114 |
} |
| 1115 |
|
| 1116 |
@Test |
| 1117 |
public void serializedCancelOnNext() { |
| 1118 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1119 |
|
| 1120 |
Flowable.<Integer>create(emitter -> { |
| 1121 |
emitter = emitter.serialize(); |
| 1122 |
|
| 1123 |
ts.cancel(); |
| 1124 |
emitter.onNext(1); |
| 1125 |
}, BackpressureStrategy.MISSING) |
| 1126 |
.subscribe(ts); |
| 1127 |
|
| 1128 |
ts.assertEmpty(); |
| 1129 |
} |
| 1130 |
} |