| 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 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
import java.util.concurrent.atomic.AtomicInteger; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
import org.reactivestreams.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 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.internal.subscriptions.BooleanSubscription; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 34 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 35 |
|
| 36 |
public class FlowableBlockingTest extends RxJavaTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void blockingFirst() { |
| 40 |
assertEquals(1, Flowable.range(1, 10) |
| 41 |
.subscribeOn(Schedulers.computation()).blockingFirst().intValue()); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void blockingFirstDefault() { |
| 46 |
assertEquals(1, Flowable.<Integer>empty() |
| 47 |
.subscribeOn(Schedulers.computation()).blockingFirst(1).intValue()); |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void blockingSubscribeConsumer() { |
| 52 |
final List<Integer> list = new ArrayList<>(); |
| 53 |
|
| 54 |
Flowable.range(1, 5) |
| 55 |
.subscribeOn(Schedulers.computation()) |
| 56 |
.blockingSubscribe(new Consumer<Integer>() { |
| 57 |
@Override |
| 58 |
public void accept(Integer v) throws Exception { |
| 59 |
list.add(v); |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void boundedBlockingSubscribeConsumer() { |
| 68 |
final List<Integer> list = new ArrayList<>(); |
| 69 |
|
| 70 |
Flowable.range(1, 5) |
| 71 |
.subscribeOn(Schedulers.computation()) |
| 72 |
.blockingSubscribe(new Consumer<Integer>() { |
| 73 |
@Override |
| 74 |
public void accept(Integer v) throws Exception { |
| 75 |
list.add(v); |
| 76 |
} |
| 77 |
}, 128); |
| 78 |
|
| 79 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void boundedBlockingSubscribeConsumerBufferExceed() { |
| 84 |
final List<Integer> list = new ArrayList<>(); |
| 85 |
|
| 86 |
Flowable.range(1, 5) |
| 87 |
.subscribeOn(Schedulers.computation()) |
| 88 |
.blockingSubscribe(new Consumer<Integer>() { |
| 89 |
@Override |
| 90 |
public void accept(Integer v) throws Exception { |
| 91 |
list.add(v); |
| 92 |
} |
| 93 |
}, 3); |
| 94 |
|
| 95 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void blockingSubscribeConsumerConsumer() { |
| 100 |
final List<Object> list = new ArrayList<>(); |
| 101 |
|
| 102 |
Flowable.range(1, 5) |
| 103 |
.subscribeOn(Schedulers.computation()) |
| 104 |
.blockingSubscribe(new Consumer<Integer>() { |
| 105 |
@Override |
| 106 |
public void accept(Integer v) throws Exception { |
| 107 |
list.add(v); |
| 108 |
} |
| 109 |
}, Functions.emptyConsumer()); |
| 110 |
|
| 111 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void boundedBlockingSubscribeConsumerConsumer() { |
| 116 |
final List<Object> list = new ArrayList<>(); |
| 117 |
|
| 118 |
Flowable.range(1, 5) |
| 119 |
.subscribeOn(Schedulers.computation()) |
| 120 |
.blockingSubscribe(new Consumer<Integer>() { |
| 121 |
@Override |
| 122 |
public void accept(Integer v) throws Exception { |
| 123 |
list.add(v); |
| 124 |
} |
| 125 |
}, Functions.emptyConsumer(), 128); |
| 126 |
|
| 127 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void boundedBlockingSubscribeConsumerConsumerBufferExceed() { |
| 132 |
final List<Object> list = new ArrayList<>(); |
| 133 |
|
| 134 |
Flowable.range(1, 5) |
| 135 |
.subscribeOn(Schedulers.computation()) |
| 136 |
.blockingSubscribe(new Consumer<Integer>() { |
| 137 |
@Override |
| 138 |
public void accept(Integer v) throws Exception { |
| 139 |
list.add(v); |
| 140 |
} |
| 141 |
}, Functions.emptyConsumer(), 3); |
| 142 |
|
| 143 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void blockingSubscribeConsumerConsumerError() { |
| 148 |
final List<Object> list = new ArrayList<>(); |
| 149 |
|
| 150 |
TestException ex = new TestException(); |
| 151 |
|
| 152 |
Consumer<Object> cons = new Consumer<Object>() { |
| 153 |
@Override |
| 154 |
public void accept(Object v) throws Exception { |
| 155 |
list.add(v); |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
Flowable.range(1, 5).concatWith(Flowable.<Integer>error(ex)) |
| 160 |
.subscribeOn(Schedulers.computation()) |
| 161 |
.blockingSubscribe(cons, cons); |
| 162 |
|
| 163 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, ex), list); |
| 164 |
} |
| 165 |
|
| 166 |
@Test |
| 167 |
public void boundedBlockingSubscribeConsumerConsumerError() { |
| 168 |
final List<Object> list = new ArrayList<>(); |
| 169 |
|
| 170 |
TestException ex = new TestException(); |
| 171 |
|
| 172 |
Consumer<Object> cons = new Consumer<Object>() { |
| 173 |
@Override |
| 174 |
public void accept(Object v) throws Exception { |
| 175 |
list.add(v); |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
Flowable.range(1, 5).concatWith(Flowable.<Integer>error(ex)) |
| 180 |
.subscribeOn(Schedulers.computation()) |
| 181 |
.blockingSubscribe(cons, cons, 128); |
| 182 |
|
| 183 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, ex), list); |
| 184 |
} |
| 185 |
|
| 186 |
@Test |
| 187 |
public void blockingSubscribeConsumerConsumerAction() { |
| 188 |
final List<Object> list = new ArrayList<>(); |
| 189 |
|
| 190 |
Consumer<Object> cons = new Consumer<Object>() { |
| 191 |
@Override |
| 192 |
public void accept(Object v) throws Exception { |
| 193 |
list.add(v); |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
Flowable.range(1, 5) |
| 198 |
.subscribeOn(Schedulers.computation()) |
| 199 |
.blockingSubscribe(cons, cons, new Action() { |
| 200 |
@Override |
| 201 |
public void run() throws Exception { |
| 202 |
list.add(100); |
| 203 |
} |
| 204 |
}); |
| 205 |
|
| 206 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list); |
| 207 |
} |
| 208 |
|
| 209 |
@Test |
| 210 |
public void boundedBlockingSubscribeConsumerConsumerAction() { |
| 211 |
final List<Object> list = new ArrayList<>(); |
| 212 |
|
| 213 |
Consumer<Object> cons = new Consumer<Object>() { |
| 214 |
@Override |
| 215 |
public void accept(Object v) throws Exception { |
| 216 |
list.add(v); |
| 217 |
} |
| 218 |
}; |
| 219 |
|
| 220 |
Action action = new Action() { |
| 221 |
@Override |
| 222 |
public void run() throws Exception { |
| 223 |
list.add(100); |
| 224 |
} |
| 225 |
}; |
| 226 |
|
| 227 |
Flowable.range(1, 5) |
| 228 |
.subscribeOn(Schedulers.computation()) |
| 229 |
.blockingSubscribe(cons, cons, action, 128); |
| 230 |
|
| 231 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list); |
| 232 |
} |
| 233 |
|
| 234 |
@Test |
| 235 |
public void boundedBlockingSubscribeConsumerConsumerActionBufferExceed() { |
| 236 |
final List<Object> list = new ArrayList<>(); |
| 237 |
|
| 238 |
Consumer<Object> cons = new Consumer<Object>() { |
| 239 |
@Override |
| 240 |
public void accept(Object v) throws Exception { |
| 241 |
list.add(v); |
| 242 |
} |
| 243 |
}; |
| 244 |
|
| 245 |
Action action = new Action() { |
| 246 |
@Override |
| 247 |
public void run() throws Exception { |
| 248 |
list.add(100); |
| 249 |
} |
| 250 |
}; |
| 251 |
|
| 252 |
Flowable.range(1, 5) |
| 253 |
.subscribeOn(Schedulers.computation()) |
| 254 |
.blockingSubscribe(cons, cons, action, 3); |
| 255 |
|
| 256 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list); |
| 257 |
} |
| 258 |
|
| 259 |
@Test |
| 260 |
public void boundedBlockingSubscribeConsumerConsumerActionBufferExceedMillionItem() { |
| 261 |
final List<Object> list = new ArrayList<>(); |
| 262 |
|
| 263 |
Consumer<Object> cons = new Consumer<Object>() { |
| 264 |
@Override |
| 265 |
public void accept(Object v) throws Exception { |
| 266 |
list.add(v); |
| 267 |
} |
| 268 |
}; |
| 269 |
|
| 270 |
Action action = new Action() { |
| 271 |
@Override |
| 272 |
public void run() throws Exception { |
| 273 |
list.add(1000001); |
| 274 |
} |
| 275 |
}; |
| 276 |
|
| 277 |
Flowable.range(1, 1000000) |
| 278 |
.subscribeOn(Schedulers.computation()) |
| 279 |
.blockingSubscribe(cons, cons, action, 128); |
| 280 |
|
| 281 |
assertEquals(1000000 + 1, list.size()); |
| 282 |
} |
| 283 |
|
| 284 |
@Test |
| 285 |
public void blockingSubscribeObserver() { |
| 286 |
final List<Object> list = new ArrayList<>(); |
| 287 |
|
| 288 |
Flowable.range(1, 5) |
| 289 |
.subscribeOn(Schedulers.computation()) |
| 290 |
.blockingSubscribe(new FlowableSubscriber<Object>() { |
| 291 |
|
| 292 |
@Override |
| 293 |
public void onSubscribe(Subscription s) { |
| 294 |
s.request(Long.MAX_VALUE); |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public void onNext(Object value) { |
| 299 |
list.add(value); |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public void onError(Throwable e) { |
| 304 |
list.add(e); |
| 305 |
} |
| 306 |
|
| 307 |
@Override |
| 308 |
public void onComplete() { |
| 309 |
list.add(100); |
| 310 |
} |
| 311 |
|
| 312 |
}); |
| 313 |
|
| 314 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list); |
| 315 |
} |
| 316 |
|
| 317 |
@Test |
| 318 |
public void blockingSubscribeObserverError() { |
| 319 |
final List<Object> list = new ArrayList<>(); |
| 320 |
|
| 321 |
final TestException ex = new TestException(); |
| 322 |
|
| 323 |
Flowable.range(1, 5).concatWith(Flowable.<Integer>error(ex)) |
| 324 |
.subscribeOn(Schedulers.computation()) |
| 325 |
.blockingSubscribe(new FlowableSubscriber<Object>() { |
| 326 |
|
| 327 |
@Override |
| 328 |
public void onSubscribe(Subscription s) { |
| 329 |
s.request(Long.MAX_VALUE); |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public void onNext(Object value) { |
| 334 |
list.add(value); |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
public void onError(Throwable e) { |
| 339 |
list.add(e); |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
public void onComplete() { |
| 344 |
list.add(100); |
| 345 |
} |
| 346 |
|
| 347 |
}); |
| 348 |
|
| 349 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, ex), list); |
| 350 |
} |
| 351 |
|
| 352 |
@Test(expected = TestException.class) |
| 353 |
public void blockingForEachThrows() { |
| 354 |
Flowable.just(1) |
| 355 |
.blockingForEach(new Consumer<Integer>() { |
| 356 |
@Override |
| 357 |
public void accept(Integer e) throws Exception { |
| 358 |
throw new TestException(); |
| 359 |
} |
| 360 |
}); |
| 361 |
} |
| 362 |
|
| 363 |
@Test(expected = NoSuchElementException.class) |
| 364 |
public void blockingFirstEmpty() { |
| 365 |
Flowable.empty().blockingFirst(); |
| 366 |
} |
| 367 |
|
| 368 |
@Test(expected = NoSuchElementException.class) |
| 369 |
public void blockingLastEmpty() { |
| 370 |
Flowable.empty().blockingLast(); |
| 371 |
} |
| 372 |
|
| 373 |
@Test |
| 374 |
public void blockingFirstNormal() { |
| 375 |
assertEquals(1, Flowable.just(1, 2).blockingFirst(3).intValue()); |
| 376 |
} |
| 377 |
|
| 378 |
@Test |
| 379 |
public void blockingLastNormal() { |
| 380 |
assertEquals(2, Flowable.just(1, 2).blockingLast(3).intValue()); |
| 381 |
} |
| 382 |
|
| 383 |
@Test |
| 384 |
public void firstFgnoredCancelAndOnNext() { |
| 385 |
Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() { |
| 386 |
@Override |
| 387 |
public void subscribe(Subscriber<? super Integer> s) { |
| 388 |
s.onSubscribe(new BooleanSubscription()); |
| 389 |
s.onNext(1); |
| 390 |
s.onNext(2); |
| 391 |
} |
| 392 |
}); |
| 393 |
|
| 394 |
assertEquals(1, source.blockingFirst().intValue()); |
| 395 |
} |
| 396 |
|
| 397 |
@Test |
| 398 |
public void firstIgnoredCancelAndOnError() { |
| 399 |
List<Throwable> list = TestHelper.trackPluginErrors(); |
| 400 |
try { |
| 401 |
Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() { |
| 402 |
@Override |
| 403 |
public void subscribe(Subscriber<? super Integer> s) { |
| 404 |
s.onSubscribe(new BooleanSubscription()); |
| 405 |
s.onNext(1); |
| 406 |
s.onError(new TestException()); |
| 407 |
} |
| 408 |
}); |
| 409 |
|
| 410 |
assertEquals(1, source.blockingFirst().intValue()); |
| 411 |
|
| 412 |
TestHelper.assertUndeliverable(list, 0, TestException.class); |
| 413 |
} finally { |
| 414 |
RxJavaPlugins.reset(); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
@Test(expected = TestException.class) |
| 419 |
public void firstOnError() { |
| 420 |
Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() { |
| 421 |
@Override |
| 422 |
public void subscribe(Subscriber<? super Integer> s) { |
| 423 |
s.onSubscribe(new BooleanSubscription()); |
| 424 |
s.onError(new TestException()); |
| 425 |
} |
| 426 |
}); |
| 427 |
|
| 428 |
source.blockingFirst(); |
| 429 |
} |
| 430 |
|
| 431 |
@Test |
| 432 |
public void interrupt() { |
| 433 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 434 |
|
| 435 |
Thread.currentThread().interrupt(); |
| 436 |
|
| 437 |
try { |
| 438 |
Flowable.just(1) |
| 439 |
.blockingSubscribe(ts); |
| 440 |
|
| 441 |
ts.assertFailure(InterruptedException.class); |
| 442 |
} finally { |
| 443 |
Thread.interrupted(); // clear interrupted status just in case |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
@Test(expected = NoSuchElementException.class) |
| 448 |
public void blockingSingleEmpty() { |
| 449 |
Flowable.empty().blockingSingle(); |
| 450 |
} |
| 451 |
|
| 452 |
@Test |
| 453 |
public void onCompleteDelayed() { |
| 454 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 455 |
|
| 456 |
Flowable.empty().delay(100, TimeUnit.MILLISECONDS) |
| 457 |
.blockingSubscribe(ts); |
| 458 |
|
| 459 |
ts.assertResult(); |
| 460 |
} |
| 461 |
|
| 462 |
@Test |
| 463 |
public void utilityClass() { |
| 464 |
TestHelper.checkUtilityClass(FlowableBlockingSubscribe.class); |
| 465 |
} |
| 466 |
|
| 467 |
@Test |
| 468 |
public void disposeUpFront() { |
| 469 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 470 |
ts.cancel(); |
| 471 |
Flowable.just(1).blockingSubscribe(ts); |
| 472 |
|
| 473 |
ts.assertEmpty(); |
| 474 |
} |
| 475 |
|
| 476 |
@SuppressWarnings("rawtypes") |
| 477 |
@Test |
| 478 |
public void delayed() throws Exception { |
| 479 |
final TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 480 |
final Subscriber[] s = { null }; |
| 481 |
|
| 482 |
Schedulers.single().scheduleDirect(new Runnable() { |
| 483 |
@SuppressWarnings("unchecked") |
| 484 |
@Override |
| 485 |
public void run() { |
| 486 |
ts.cancel(); |
| 487 |
s[0].onNext(1); |
| 488 |
} |
| 489 |
}, 200, TimeUnit.MILLISECONDS); |
| 490 |
|
| 491 |
new Flowable<Integer>() { |
| 492 |
@Override |
| 493 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 494 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 495 |
s[0] = subscriber; |
| 496 |
} |
| 497 |
}.blockingSubscribe(ts); |
| 498 |
|
| 499 |
while (!ts.isCancelled()) { |
| 500 |
Thread.sleep(100); |
| 501 |
} |
| 502 |
|
| 503 |
ts.assertEmpty(); |
| 504 |
} |
| 505 |
|
| 506 |
@Test |
| 507 |
public void blockinsSubscribeCancelAsync() { |
| 508 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 509 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 510 |
|
| 511 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 512 |
|
| 513 |
final Runnable r1 = new Runnable() { |
| 514 |
@Override |
| 515 |
public void run() { |
| 516 |
ts.cancel(); |
| 517 |
} |
| 518 |
}; |
| 519 |
|
| 520 |
final Runnable r2 = new Runnable() { |
| 521 |
@Override |
| 522 |
public void run() { |
| 523 |
pp.onNext(1); |
| 524 |
} |
| 525 |
}; |
| 526 |
|
| 527 |
final AtomicInteger c = new AtomicInteger(2); |
| 528 |
|
| 529 |
Schedulers.computation().scheduleDirect(new Runnable() { |
| 530 |
@Override |
| 531 |
public void run() { |
| 532 |
c.decrementAndGet(); |
| 533 |
while (c.get() != 0 && !pp.hasSubscribers()) { } |
| 534 |
|
| 535 |
TestHelper.race(r1, r2); |
| 536 |
} |
| 537 |
}); |
| 538 |
|
| 539 |
c.decrementAndGet(); |
| 540 |
while (c.get() != 0) { } |
| 541 |
|
| 542 |
pp |
| 543 |
.blockingSubscribe(ts); |
| 544 |
} |
| 545 |
} |
| 546 |
} |