| 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 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.mockito.Mockito; |
| 26 |
import org.reactivestreams.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 29 |
import io.reactivex.rxjava3.core.*; |
| 30 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 31 |
import io.reactivex.rxjava3.functions.Function; |
| 32 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 33 |
import io.reactivex.rxjava3.internal.util.CrashingIterable; |
| 34 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 35 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 36 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 37 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 38 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 39 |
import io.reactivex.rxjava3.subscribers.*; |
| 40 |
import io.reactivex.rxjava3.testsupport.*; |
| 41 |
|
| 42 |
public class FlowableFromIterableTest extends RxJavaTest { |
| 43 |
|
| 44 |
@Test |
| 45 |
public void listIterable() { |
| 46 |
Flowable<String> flowable = Flowable.fromIterable(Arrays.<String> asList("one", "two", "three")); |
| 47 |
|
| 48 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 49 |
|
| 50 |
flowable.subscribe(subscriber); |
| 51 |
|
| 52 |
verify(subscriber, times(1)).onNext("one"); |
| 53 |
verify(subscriber, times(1)).onNext("two"); |
| 54 |
verify(subscriber, times(1)).onNext("three"); |
| 55 |
verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 56 |
verify(subscriber, times(1)).onComplete(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* This tests the path that can not optimize based on size so must use setProducer. |
| 61 |
*/ |
| 62 |
@Test |
| 63 |
public void rawIterable() { |
| 64 |
Iterable<String> it = new Iterable<String>() { |
| 65 |
|
| 66 |
@Override |
| 67 |
public Iterator<String> iterator() { |
| 68 |
return new Iterator<String>() { |
| 69 |
|
| 70 |
int i; |
| 71 |
|
| 72 |
@Override |
| 73 |
public boolean hasNext() { |
| 74 |
return i < 3; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public String next() { |
| 79 |
return String.valueOf(++i); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void remove() { |
| 84 |
} |
| 85 |
|
| 86 |
}; |
| 87 |
} |
| 88 |
|
| 89 |
}; |
| 90 |
Flowable<String> flowable = Flowable.fromIterable(it); |
| 91 |
|
| 92 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 93 |
|
| 94 |
flowable.subscribe(subscriber); |
| 95 |
|
| 96 |
verify(subscriber, times(1)).onNext("1"); |
| 97 |
verify(subscriber, times(1)).onNext("2"); |
| 98 |
verify(subscriber, times(1)).onNext("3"); |
| 99 |
verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 100 |
verify(subscriber, times(1)).onComplete(); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void observableFromIterable() { |
| 105 |
Flowable<String> flowable = Flowable.fromIterable(Arrays.<String> asList("one", "two", "three")); |
| 106 |
|
| 107 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 108 |
|
| 109 |
flowable.subscribe(subscriber); |
| 110 |
|
| 111 |
verify(subscriber, times(1)).onNext("one"); |
| 112 |
verify(subscriber, times(1)).onNext("two"); |
| 113 |
verify(subscriber, times(1)).onNext("three"); |
| 114 |
verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 115 |
verify(subscriber, times(1)).onComplete(); |
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void backpressureViaRequest() { |
| 120 |
ArrayList<Integer> list = new ArrayList<>(Flowable.bufferSize()); |
| 121 |
for (int i = 1; i <= Flowable.bufferSize() + 1; i++) { |
| 122 |
list.add(i); |
| 123 |
} |
| 124 |
Flowable<Integer> f = Flowable.fromIterable(list); |
| 125 |
|
| 126 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L); |
| 127 |
|
| 128 |
ts.assertNoValues(); |
| 129 |
ts.request(1); |
| 130 |
|
| 131 |
f.subscribe(ts); |
| 132 |
|
| 133 |
ts.assertValue(1); |
| 134 |
ts.request(2); |
| 135 |
ts.assertValues(1, 2, 3); |
| 136 |
ts.request(3); |
| 137 |
ts.assertValues(1, 2, 3, 4, 5, 6); |
| 138 |
ts.request(list.size()); |
| 139 |
ts.assertTerminated(); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void noBackpressure() { |
| 144 |
Flowable<Integer> f = Flowable.fromIterable(Arrays.asList(1, 2, 3, 4, 5)); |
| 145 |
|
| 146 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L); |
| 147 |
|
| 148 |
ts.assertNoValues(); |
| 149 |
ts.request(Long.MAX_VALUE); // infinite |
| 150 |
|
| 151 |
f.subscribe(ts); |
| 152 |
|
| 153 |
ts.assertValues(1, 2, 3, 4, 5); |
| 154 |
ts.assertTerminated(); |
| 155 |
} |
| 156 |
|
| 157 |
@Test |
| 158 |
public void subscribeMultipleTimes() { |
| 159 |
Flowable<Integer> f = Flowable.fromIterable(Arrays.asList(1, 2, 3)); |
| 160 |
|
| 161 |
for (int i = 0; i < 10; i++) { |
| 162 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 163 |
|
| 164 |
f.subscribe(ts); |
| 165 |
|
| 166 |
ts.assertValues(1, 2, 3); |
| 167 |
ts.assertNoErrors(); |
| 168 |
ts.assertComplete(); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
@Test |
| 173 |
public void fromIterableRequestOverflow() throws InterruptedException { |
| 174 |
Flowable<Integer> f = Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)); |
| 175 |
|
| 176 |
final int expectedCount = 4; |
| 177 |
final CountDownLatch latch = new CountDownLatch(expectedCount); |
| 178 |
|
| 179 |
f.subscribeOn(Schedulers.computation()) |
| 180 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 181 |
|
| 182 |
@Override |
| 183 |
public void onStart() { |
| 184 |
request(2); |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
public void onComplete() { |
| 189 |
//ignore |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public void onError(Throwable e) { |
| 194 |
throw new RuntimeException(e); |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public void onNext(Integer t) { |
| 199 |
latch.countDown(); |
| 200 |
request(Long.MAX_VALUE - 1); |
| 201 |
}}); |
| 202 |
assertTrue(latch.await(10, TimeUnit.SECONDS)); |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void fromEmptyIterableWhenZeroRequestedShouldStillEmitOnCompletedEagerly() { |
| 207 |
|
| 208 |
final AtomicBoolean completed = new AtomicBoolean(false); |
| 209 |
|
| 210 |
Flowable.fromIterable(Collections.emptyList()).subscribe(new DefaultSubscriber<Object>() { |
| 211 |
|
| 212 |
@Override |
| 213 |
public void onStart() { |
| 214 |
// request(0); |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public void onComplete() { |
| 219 |
completed.set(true); |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public void onError(Throwable e) { |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public void onNext(Object t) { |
| 229 |
|
| 230 |
}}); |
| 231 |
assertTrue(completed.get()); |
| 232 |
} |
| 233 |
|
| 234 |
@Test |
| 235 |
public void doesNotCallIteratorHasNextMoreThanRequiredWithBackpressure() { |
| 236 |
final AtomicBoolean called = new AtomicBoolean(false); |
| 237 |
Iterable<Integer> iterable = new Iterable<Integer>() { |
| 238 |
|
| 239 |
@Override |
| 240 |
public Iterator<Integer> iterator() { |
| 241 |
return new Iterator<Integer>() { |
| 242 |
|
| 243 |
int count = 1; |
| 244 |
|
| 245 |
@Override |
| 246 |
public void remove() { |
| 247 |
// ignore |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public boolean hasNext() { |
| 252 |
if (count > 1) { |
| 253 |
called.set(true); |
| 254 |
return false; |
| 255 |
} |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
@Override |
| 260 |
public Integer next() { |
| 261 |
return count++; |
| 262 |
} |
| 263 |
|
| 264 |
}; |
| 265 |
} |
| 266 |
}; |
| 267 |
Flowable.fromIterable(iterable).take(1).subscribe(); |
| 268 |
assertFalse(called.get()); |
| 269 |
} |
| 270 |
|
| 271 |
@Test |
| 272 |
public void doesNotCallIteratorHasNextMoreThanRequiredFastPath() { |
| 273 |
final AtomicBoolean called = new AtomicBoolean(false); |
| 274 |
Iterable<Integer> iterable = new Iterable<Integer>() { |
| 275 |
|
| 276 |
@Override |
| 277 |
public Iterator<Integer> iterator() { |
| 278 |
return new Iterator<Integer>() { |
| 279 |
|
| 280 |
@Override |
| 281 |
public void remove() { |
| 282 |
// ignore |
| 283 |
} |
| 284 |
|
| 285 |
int count = 1; |
| 286 |
|
| 287 |
@Override |
| 288 |
public boolean hasNext() { |
| 289 |
if (count > 1) { |
| 290 |
called.set(true); |
| 291 |
return false; |
| 292 |
} |
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public Integer next() { |
| 298 |
return count++; |
| 299 |
} |
| 300 |
|
| 301 |
}; |
| 302 |
} |
| 303 |
}; |
| 304 |
Flowable.fromIterable(iterable).subscribe(new DefaultSubscriber<Integer>() { |
| 305 |
|
| 306 |
@Override |
| 307 |
public void onComplete() { |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
@Override |
| 312 |
public void onError(Throwable e) { |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
@Override |
| 317 |
public void onNext(Integer t) { |
| 318 |
// unsubscribe on first emission |
| 319 |
cancel(); |
| 320 |
} |
| 321 |
}); |
| 322 |
assertFalse(called.get()); |
| 323 |
} |
| 324 |
|
| 325 |
@Test |
| 326 |
public void getIteratorThrows() { |
| 327 |
Iterable<Integer> it = new Iterable<Integer>() { |
| 328 |
@Override |
| 329 |
public Iterator<Integer> iterator() { |
| 330 |
throw new TestException("Forced failure"); |
| 331 |
} |
| 332 |
}; |
| 333 |
|
| 334 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 335 |
|
| 336 |
Flowable.fromIterable(it).subscribe(ts); |
| 337 |
|
| 338 |
ts.assertNoValues(); |
| 339 |
ts.assertError(TestException.class); |
| 340 |
ts.assertNotComplete(); |
| 341 |
} |
| 342 |
|
| 343 |
@Test |
| 344 |
public void hasNextThrowsImmediately() { |
| 345 |
Iterable<Integer> it = new Iterable<Integer>() { |
| 346 |
@Override |
| 347 |
public Iterator<Integer> iterator() { |
| 348 |
return new Iterator<Integer>() { |
| 349 |
@Override |
| 350 |
public boolean hasNext() { |
| 351 |
throw new TestException("Forced failure"); |
| 352 |
} |
| 353 |
|
| 354 |
@Override |
| 355 |
public Integer next() { |
| 356 |
return null; |
| 357 |
} |
| 358 |
|
| 359 |
@Override |
| 360 |
public void remove() { |
| 361 |
// ignored |
| 362 |
} |
| 363 |
}; |
| 364 |
} |
| 365 |
}; |
| 366 |
|
| 367 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 368 |
|
| 369 |
Flowable.fromIterable(it).subscribe(ts); |
| 370 |
|
| 371 |
ts.assertNoValues(); |
| 372 |
ts.assertError(TestException.class); |
| 373 |
ts.assertNotComplete(); |
| 374 |
} |
| 375 |
|
| 376 |
@Test |
| 377 |
public void hasNextThrowsSecondTimeFastpath() { |
| 378 |
Iterable<Integer> it = new Iterable<Integer>() { |
| 379 |
@Override |
| 380 |
public Iterator<Integer> iterator() { |
| 381 |
return new Iterator<Integer>() { |
| 382 |
int count; |
| 383 |
@Override |
| 384 |
public boolean hasNext() { |
| 385 |
if (++count >= 2) { |
| 386 |
throw new TestException("Forced failure"); |
| 387 |
} |
| 388 |
return true; |
| 389 |
} |
| 390 |
|
| 391 |
@Override |
| 392 |
public Integer next() { |
| 393 |
return 1; |
| 394 |
} |
| 395 |
|
| 396 |
@Override |
| 397 |
public void remove() { |
| 398 |
// ignored |
| 399 |
} |
| 400 |
}; |
| 401 |
} |
| 402 |
}; |
| 403 |
|
| 404 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 405 |
|
| 406 |
Flowable.fromIterable(it).subscribe(ts); |
| 407 |
|
| 408 |
ts.assertValues(1); |
| 409 |
ts.assertError(TestException.class); |
| 410 |
ts.assertNotComplete(); |
| 411 |
} |
| 412 |
|
| 413 |
@Test |
| 414 |
public void hasNextThrowsSecondTimeSlowpath() { |
| 415 |
Iterable<Integer> it = new Iterable<Integer>() { |
| 416 |
@Override |
| 417 |
public Iterator<Integer> iterator() { |
| 418 |
return new Iterator<Integer>() { |
| 419 |
int count; |
| 420 |
@Override |
| 421 |
public boolean hasNext() { |
| 422 |
if (++count >= 2) { |
| 423 |
throw new TestException("Forced failure"); |
| 424 |
} |
| 425 |
return true; |
| 426 |
} |
| 427 |
|
| 428 |
@Override |
| 429 |
public Integer next() { |
| 430 |
return 1; |
| 431 |
} |
| 432 |
|
| 433 |
@Override |
| 434 |
public void remove() { |
| 435 |
// ignored |
| 436 |
} |
| 437 |
}; |
| 438 |
} |
| 439 |
}; |
| 440 |
|
| 441 |
TestSubscriber<Integer> ts = new TestSubscriber<>(5); |
| 442 |
|
| 443 |
Flowable.fromIterable(it).subscribe(ts); |
| 444 |
|
| 445 |
ts.assertValues(1); |
| 446 |
ts.assertError(TestException.class); |
| 447 |
ts.assertNotComplete(); |
| 448 |
} |
| 449 |
|
| 450 |
@Test |
| 451 |
public void nextThrowsFastpath() { |
| 452 |
Iterable<Integer> it = new Iterable<Integer>() { |
| 453 |
@Override |
| 454 |
public Iterator<Integer> iterator() { |
| 455 |
return new Iterator<Integer>() { |
| 456 |
@Override |
| 457 |
public boolean hasNext() { |
| 458 |
return true; |
| 459 |
} |
| 460 |
|
| 461 |
@Override |
| 462 |
public Integer next() { |
| 463 |
throw new TestException("Forced failure"); |
| 464 |
} |
| 465 |
|
| 466 |
@Override |
| 467 |
public void remove() { |
| 468 |
// ignored |
| 469 |
} |
| 470 |
}; |
| 471 |
} |
| 472 |
}; |
| 473 |
|
| 474 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 475 |
|
| 476 |
Flowable.fromIterable(it).subscribe(ts); |
| 477 |
|
| 478 |
ts.assertNoValues(); |
| 479 |
ts.assertError(TestException.class); |
| 480 |
ts.assertNotComplete(); |
| 481 |
} |
| 482 |
|
| 483 |
@Test |
| 484 |
public void nextThrowsSlowpath() { |
| 485 |
Iterable<Integer> it = new Iterable<Integer>() { |
| 486 |
@Override |
| 487 |
public Iterator<Integer> iterator() { |
| 488 |
return new Iterator<Integer>() { |
| 489 |
@Override |
| 490 |
public boolean hasNext() { |
| 491 |
return true; |
| 492 |
} |
| 493 |
|
| 494 |
@Override |
| 495 |
public Integer next() { |
| 496 |
throw new TestException("Forced failure"); |
| 497 |
} |
| 498 |
|
| 499 |
@Override |
| 500 |
public void remove() { |
| 501 |
// ignored |
| 502 |
} |
| 503 |
}; |
| 504 |
} |
| 505 |
}; |
| 506 |
|
| 507 |
TestSubscriber<Integer> ts = new TestSubscriber<>(5); |
| 508 |
|
| 509 |
Flowable.fromIterable(it).subscribe(ts); |
| 510 |
|
| 511 |
ts.assertNoValues(); |
| 512 |
ts.assertError(TestException.class); |
| 513 |
ts.assertNotComplete(); |
| 514 |
} |
| 515 |
|
| 516 |
@Test |
| 517 |
public void deadOnArrival() { |
| 518 |
Iterable<Integer> it = new Iterable<Integer>() { |
| 519 |
@Override |
| 520 |
public Iterator<Integer> iterator() { |
| 521 |
return new Iterator<Integer>() { |
| 522 |
@Override |
| 523 |
public boolean hasNext() { |
| 524 |
return true; |
| 525 |
} |
| 526 |
|
| 527 |
@Override |
| 528 |
public Integer next() { |
| 529 |
throw new NoSuchElementException(); |
| 530 |
} |
| 531 |
|
| 532 |
@Override |
| 533 |
public void remove() { |
| 534 |
// ignored |
| 535 |
} |
| 536 |
}; |
| 537 |
} |
| 538 |
}; |
| 539 |
|
| 540 |
TestSubscriber<Integer> ts = new TestSubscriber<>(5); |
| 541 |
ts.cancel(); |
| 542 |
|
| 543 |
Flowable.fromIterable(it).subscribe(ts); |
| 544 |
|
| 545 |
ts.assertNoValues(); |
| 546 |
ts.assertNoErrors(); |
| 547 |
ts.assertNotComplete(); |
| 548 |
|
| 549 |
} |
| 550 |
|
| 551 |
@Test |
| 552 |
public void fusionWithConcatMap() { |
| 553 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 554 |
|
| 555 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)).concatMap( |
| 556 |
new Function<Integer, Flowable <Integer>>() { |
| 557 |
@Override |
| 558 |
public Flowable<Integer> apply(Integer v) { |
| 559 |
return Flowable.range(v, 2); |
| 560 |
} |
| 561 |
}).subscribe(ts); |
| 562 |
|
| 563 |
ts.assertValues(1, 2, 2, 3, 3, 4, 4, 5); |
| 564 |
ts.assertNoErrors(); |
| 565 |
ts.assertComplete(); |
| 566 |
} |
| 567 |
|
| 568 |
@Test |
| 569 |
public void fusedAPICalls() { |
| 570 |
Flowable.fromIterable(Arrays.asList(1, 2, 3)) |
| 571 |
.subscribe(new FlowableSubscriber<Integer>() { |
| 572 |
|
| 573 |
@Override |
| 574 |
public void onSubscribe(Subscription s) { |
| 575 |
@SuppressWarnings("unchecked") |
| 576 |
QueueSubscription<Integer> qs = (QueueSubscription<Integer>)s; |
| 577 |
|
| 578 |
assertFalse(qs.isEmpty()); |
| 579 |
|
| 580 |
try { |
| 581 |
assertEquals(1, qs.poll().intValue()); |
| 582 |
} catch (Throwable ex) { |
| 583 |
throw new AssertionError(ex); |
| 584 |
} |
| 585 |
|
| 586 |
assertFalse(qs.isEmpty()); |
| 587 |
|
| 588 |
qs.clear(); |
| 589 |
|
| 590 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 591 |
try { |
| 592 |
qs.request(-99); |
| 593 |
|
| 594 |
TestHelper.assertError(errors, 0, IllegalArgumentException.class, "n > 0 required but it was -99"); |
| 595 |
} finally { |
| 596 |
RxJavaPlugins.reset(); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
@Override |
| 601 |
public void onNext(Integer t) { |
| 602 |
} |
| 603 |
|
| 604 |
@Override |
| 605 |
public void onError(Throwable t) { |
| 606 |
} |
| 607 |
|
| 608 |
@Override |
| 609 |
public void onComplete() { |
| 610 |
} |
| 611 |
}); |
| 612 |
} |
| 613 |
|
| 614 |
@Test |
| 615 |
public void normalConditional() { |
| 616 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4, 5)) |
| 617 |
.filter(Functions.alwaysTrue()) |
| 618 |
.test() |
| 619 |
.assertResult(1, 2, 3, 4, 5); |
| 620 |
} |
| 621 |
|
| 622 |
@Test |
| 623 |
public void normalConditionalBackpressured() { |
| 624 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4, 5)) |
| 625 |
.filter(Functions.alwaysTrue()) |
| 626 |
.test(5L) |
| 627 |
.assertResult(1, 2, 3, 4, 5); |
| 628 |
} |
| 629 |
|
| 630 |
@Test |
| 631 |
public void normalConditionalBackpressured2() { |
| 632 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4, 5)) |
| 633 |
.filter(Functions.alwaysTrue()) |
| 634 |
.to(TestHelper.<Integer>testSubscriber(4L)) |
| 635 |
.assertSubscribed() |
| 636 |
.assertValues(1, 2, 3, 4) |
| 637 |
.assertNoErrors() |
| 638 |
.assertNotComplete(); |
| 639 |
} |
| 640 |
|
| 641 |
@Test |
| 642 |
public void emptyConditional() { |
| 643 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4, 5)) |
| 644 |
.filter(Functions.alwaysFalse()) |
| 645 |
.test() |
| 646 |
.assertResult(); |
| 647 |
} |
| 648 |
|
| 649 |
@Test |
| 650 |
public void nullConditional() { |
| 651 |
Flowable.fromIterable(Arrays.asList(1, null, 3, 4, 5)) |
| 652 |
.filter(Functions.alwaysTrue()) |
| 653 |
.test() |
| 654 |
.assertFailure(NullPointerException.class, 1); |
| 655 |
} |
| 656 |
|
| 657 |
@Test |
| 658 |
public void nullConditionalBackpressured() { |
| 659 |
Flowable.fromIterable(Arrays.asList(1, null, 3, 4, 5)) |
| 660 |
.filter(Functions.alwaysTrue()) |
| 661 |
.test(5L) |
| 662 |
.assertFailure(NullPointerException.class, 1); |
| 663 |
} |
| 664 |
|
| 665 |
@Test |
| 666 |
public void normalConditionalCrash() { |
| 667 |
Flowable.fromIterable(new CrashingIterable(100, 2, 100)) |
| 668 |
.filter(Functions.alwaysTrue()) |
| 669 |
.test() |
| 670 |
.assertFailure(TestException.class, 0); |
| 671 |
} |
| 672 |
|
| 673 |
@Test |
| 674 |
public void normalConditionalCrash2() { |
| 675 |
Flowable.fromIterable(new CrashingIterable(100, 100, 2)) |
| 676 |
.filter(Functions.alwaysTrue()) |
| 677 |
.test() |
| 678 |
.assertFailure(TestException.class, 0); |
| 679 |
} |
| 680 |
|
| 681 |
@Test |
| 682 |
public void normalConditionalCrashBackpressured() { |
| 683 |
Flowable.fromIterable(new CrashingIterable(100, 2, 100)) |
| 684 |
.filter(Functions.alwaysTrue()) |
| 685 |
.test(5L) |
| 686 |
.assertFailure(TestException.class, 0); |
| 687 |
} |
| 688 |
|
| 689 |
@Test |
| 690 |
public void normalConditionalCrashBackpressured2() { |
| 691 |
Flowable.fromIterable(new CrashingIterable(100, 100, 2)) |
| 692 |
.filter(Functions.alwaysTrue()) |
| 693 |
.test(5L) |
| 694 |
.assertFailure(TestException.class, 0); |
| 695 |
} |
| 696 |
|
| 697 |
@Test |
| 698 |
public void normalConditionalLong() { |
| 699 |
Flowable.fromIterable(new CrashingIterable(100, 10 * 1000 * 1000, 10 * 1000 * 1000)) |
| 700 |
.filter(Functions.alwaysTrue()) |
| 701 |
.take(1000 * 1000) |
| 702 |
.to(TestHelper.<Integer>testConsumer()) |
| 703 |
.assertSubscribed() |
| 704 |
.assertValueCount(1000 * 1000) |
| 705 |
.assertNoErrors() |
| 706 |
.assertComplete(); |
| 707 |
} |
| 708 |
|
| 709 |
@Test |
| 710 |
public void normalConditionalLong2() { |
| 711 |
Flowable.fromIterable(new CrashingIterable(100, 10 * 1000 * 1000, 10 * 1000 * 1000)) |
| 712 |
.filter(Functions.alwaysTrue()) |
| 713 |
.rebatchRequests(128) |
| 714 |
.take(1000 * 1000) |
| 715 |
.to(TestHelper.<Integer>testConsumer()) |
| 716 |
.assertSubscribed() |
| 717 |
.assertValueCount(1000 * 1000) |
| 718 |
.assertNoErrors() |
| 719 |
.assertComplete(); |
| 720 |
} |
| 721 |
|
| 722 |
@Test |
| 723 |
public void requestRaceConditional() { |
| 724 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 725 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 726 |
|
| 727 |
Runnable r = new Runnable() { |
| 728 |
@Override |
| 729 |
public void run() { |
| 730 |
ts.request(1); |
| 731 |
} |
| 732 |
}; |
| 733 |
|
| 734 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) |
| 735 |
.filter(Functions.alwaysTrue()) |
| 736 |
.subscribe(ts); |
| 737 |
|
| 738 |
TestHelper.race(r, r); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
@Test |
| 743 |
public void requestRaceConditional2() { |
| 744 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 745 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 746 |
|
| 747 |
Runnable r = new Runnable() { |
| 748 |
@Override |
| 749 |
public void run() { |
| 750 |
ts.request(1); |
| 751 |
} |
| 752 |
}; |
| 753 |
|
| 754 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) |
| 755 |
.filter(Functions.alwaysFalse()) |
| 756 |
.subscribe(ts); |
| 757 |
|
| 758 |
TestHelper.race(r, r); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
@Test |
| 763 |
public void requestCancelConditionalRace() { |
| 764 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 765 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 766 |
|
| 767 |
Runnable r1 = new Runnable() { |
| 768 |
@Override |
| 769 |
public void run() { |
| 770 |
ts.request(1); |
| 771 |
} |
| 772 |
}; |
| 773 |
|
| 774 |
Runnable r2 = new Runnable() { |
| 775 |
@Override |
| 776 |
public void run() { |
| 777 |
ts.cancel(); |
| 778 |
} |
| 779 |
}; |
| 780 |
|
| 781 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) |
| 782 |
.filter(Functions.alwaysTrue()) |
| 783 |
.subscribe(ts); |
| 784 |
|
| 785 |
TestHelper.race(r1, r2); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
@Test |
| 790 |
public void requestCancelConditionalRace2() { |
| 791 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 792 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 793 |
|
| 794 |
Runnable r1 = new Runnable() { |
| 795 |
@Override |
| 796 |
public void run() { |
| 797 |
ts.request(Long.MAX_VALUE); |
| 798 |
} |
| 799 |
}; |
| 800 |
|
| 801 |
Runnable r2 = new Runnable() { |
| 802 |
@Override |
| 803 |
public void run() { |
| 804 |
ts.cancel(); |
| 805 |
} |
| 806 |
}; |
| 807 |
|
| 808 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) |
| 809 |
.filter(Functions.alwaysTrue()) |
| 810 |
.subscribe(ts); |
| 811 |
|
| 812 |
TestHelper.race(r1, r2); |
| 813 |
} |
| 814 |
} |
| 815 |
|
| 816 |
@Test |
| 817 |
public void requestCancelRace() { |
| 818 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 819 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 820 |
|
| 821 |
Runnable r1 = new Runnable() { |
| 822 |
@Override |
| 823 |
public void run() { |
| 824 |
ts.request(1); |
| 825 |
} |
| 826 |
}; |
| 827 |
|
| 828 |
Runnable r2 = new Runnable() { |
| 829 |
@Override |
| 830 |
public void run() { |
| 831 |
ts.cancel(); |
| 832 |
} |
| 833 |
}; |
| 834 |
|
| 835 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) |
| 836 |
.subscribe(ts); |
| 837 |
|
| 838 |
TestHelper.race(r1, r2); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
@Test |
| 843 |
public void requestCancelRace2() { |
| 844 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 845 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 846 |
|
| 847 |
Runnable r1 = new Runnable() { |
| 848 |
@Override |
| 849 |
public void run() { |
| 850 |
ts.request(Long.MAX_VALUE); |
| 851 |
} |
| 852 |
}; |
| 853 |
|
| 854 |
Runnable r2 = new Runnable() { |
| 855 |
@Override |
| 856 |
public void run() { |
| 857 |
ts.cancel(); |
| 858 |
} |
| 859 |
}; |
| 860 |
|
| 861 |
Flowable.fromIterable(Arrays.asList(1, 2, 3, 4)) |
| 862 |
.subscribe(ts); |
| 863 |
|
| 864 |
TestHelper.race(r1, r2); |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
@Test |
| 869 |
public void fusionRejected() { |
| 870 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC); |
| 871 |
|
| 872 |
Flowable.fromIterable(Arrays.asList(1, 2, 3)) |
| 873 |
.subscribe(ts); |
| 874 |
|
| 875 |
ts.assertFusionMode(QueueFuseable.NONE) |
| 876 |
.assertResult(1, 2, 3); |
| 877 |
} |
| 878 |
|
| 879 |
@Test |
| 880 |
public void fusionClear() { |
| 881 |
Flowable.fromIterable(Arrays.asList(1, 2, 3)) |
| 882 |
.subscribe(new FlowableSubscriber<Integer>() { |
| 883 |
@Override |
| 884 |
public void onSubscribe(Subscription s) { |
| 885 |
@SuppressWarnings("unchecked") |
| 886 |
QueueSubscription<Integer> qs = (QueueSubscription<Integer>)s; |
| 887 |
|
| 888 |
qs.requestFusion(QueueFuseable.ANY); |
| 889 |
|
| 890 |
try { |
| 891 |
assertEquals(1, qs.poll().intValue()); |
| 892 |
} catch (Throwable ex) { |
| 893 |
fail(ex.toString()); |
| 894 |
} |
| 895 |
|
| 896 |
qs.clear(); |
| 897 |
try { |
| 898 |
assertNull(qs.poll()); |
| 899 |
} catch (Throwable ex) { |
| 900 |
fail(ex.toString()); |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
@Override |
| 905 |
public void onNext(Integer value) { |
| 906 |
} |
| 907 |
|
| 908 |
@Override |
| 909 |
public void onError(Throwable e) { |
| 910 |
} |
| 911 |
|
| 912 |
@Override |
| 913 |
public void onComplete() { |
| 914 |
} |
| 915 |
}); |
| 916 |
} |
| 917 |
|
| 918 |
@Test |
| 919 |
public void iteratorThrows() { |
| 920 |
Flowable.fromIterable(new CrashingIterable(1, 100, 100)) |
| 921 |
.to(TestHelper.<Integer>testConsumer()) |
| 922 |
.assertFailureAndMessage(TestException.class, "iterator()"); |
| 923 |
} |
| 924 |
|
| 925 |
@Test |
| 926 |
public void hasNext2Throws() { |
| 927 |
Flowable.fromIterable(new CrashingIterable(100, 2, 100)) |
| 928 |
.to(TestHelper.<Integer>testConsumer()) |
| 929 |
.assertFailureAndMessage(TestException.class, "hasNext()", 0); |
| 930 |
} |
| 931 |
|
| 932 |
@Test |
| 933 |
public void hasNextCancels() { |
| 934 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 935 |
|
| 936 |
Flowable.fromIterable(new Iterable<Integer>() { |
| 937 |
@Override |
| 938 |
public Iterator<Integer> iterator() { |
| 939 |
return new Iterator<Integer>() { |
| 940 |
int count; |
| 941 |
|
| 942 |
@Override |
| 943 |
public boolean hasNext() { |
| 944 |
if (++count == 2) { |
| 945 |
ts.cancel(); |
| 946 |
} |
| 947 |
return true; |
| 948 |
} |
| 949 |
|
| 950 |
@Override |
| 951 |
public Integer next() { |
| 952 |
return 1; |
| 953 |
} |
| 954 |
|
| 955 |
@Override |
| 956 |
public void remove() { |
| 957 |
throw new UnsupportedOperationException(); |
| 958 |
} |
| 959 |
}; |
| 960 |
} |
| 961 |
}) |
| 962 |
.subscribe(ts); |
| 963 |
|
| 964 |
ts.assertValue(1) |
| 965 |
.assertNoErrors() |
| 966 |
.assertNotComplete(); |
| 967 |
} |
| 968 |
|
| 969 |
@Test |
| 970 |
public void hasNextCancelsAndCompletesFastPath() { |
| 971 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 972 |
|
| 973 |
Flowable.fromIterable(new Iterable<Integer>() { |
| 974 |
@Override |
| 975 |
public Iterator<Integer> iterator() { |
| 976 |
return new Iterator<Integer>() { |
| 977 |
int count; |
| 978 |
|
| 979 |
@Override |
| 980 |
public boolean hasNext() { |
| 981 |
if (++count == 2) { |
| 982 |
ts.cancel(); |
| 983 |
return false; |
| 984 |
} |
| 985 |
return true; |
| 986 |
} |
| 987 |
|
| 988 |
@Override |
| 989 |
public Integer next() { |
| 990 |
return 1; |
| 991 |
} |
| 992 |
|
| 993 |
@Override |
| 994 |
public void remove() { |
| 995 |
throw new UnsupportedOperationException(); |
| 996 |
} |
| 997 |
}; |
| 998 |
} |
| 999 |
}) |
| 1000 |
.subscribe(ts); |
| 1001 |
|
| 1002 |
ts.assertValue(1) |
| 1003 |
.assertNoErrors() |
| 1004 |
.assertNotComplete(); |
| 1005 |
} |
| 1006 |
|
| 1007 |
@Test |
| 1008 |
public void hasNextCancelsAndCompletesSlowPath() { |
| 1009 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(10L); |
| 1010 |
|
| 1011 |
Flowable.fromIterable(new Iterable<Integer>() { |
| 1012 |
@Override |
| 1013 |
public Iterator<Integer> iterator() { |
| 1014 |
return new Iterator<Integer>() { |
| 1015 |
int count; |
| 1016 |
|
| 1017 |
@Override |
| 1018 |
public boolean hasNext() { |
| 1019 |
if (++count == 2) { |
| 1020 |
ts.cancel(); |
| 1021 |
return false; |
| 1022 |
} |
| 1023 |
return true; |
| 1024 |
} |
| 1025 |
|
| 1026 |
@Override |
| 1027 |
public Integer next() { |
| 1028 |
return 1; |
| 1029 |
} |
| 1030 |
|
| 1031 |
@Override |
| 1032 |
public void remove() { |
| 1033 |
throw new UnsupportedOperationException(); |
| 1034 |
} |
| 1035 |
}; |
| 1036 |
} |
| 1037 |
}) |
| 1038 |
.subscribe(ts); |
| 1039 |
|
| 1040 |
ts.assertValue(1) |
| 1041 |
.assertNoErrors() |
| 1042 |
.assertNotComplete(); |
| 1043 |
} |
| 1044 |
|
| 1045 |
@Test |
| 1046 |
public void hasNextCancelsAndCompletesFastPathConditional() { |
| 1047 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1048 |
|
| 1049 |
Flowable.fromIterable(new Iterable<Integer>() { |
| 1050 |
@Override |
| 1051 |
public Iterator<Integer> iterator() { |
| 1052 |
return new Iterator<Integer>() { |
| 1053 |
int count; |
| 1054 |
|
| 1055 |
@Override |
| 1056 |
public boolean hasNext() { |
| 1057 |
if (++count == 2) { |
| 1058 |
ts.cancel(); |
| 1059 |
return false; |
| 1060 |
} |
| 1061 |
return true; |
| 1062 |
} |
| 1063 |
|
| 1064 |
@Override |
| 1065 |
public Integer next() { |
| 1066 |
return 1; |
| 1067 |
} |
| 1068 |
|
| 1069 |
@Override |
| 1070 |
public void remove() { |
| 1071 |
throw new UnsupportedOperationException(); |
| 1072 |
} |
| 1073 |
}; |
| 1074 |
} |
| 1075 |
}) |
| 1076 |
.filter(v -> true) |
| 1077 |
.subscribe(ts); |
| 1078 |
|
| 1079 |
ts.assertValue(1) |
| 1080 |
.assertNoErrors() |
| 1081 |
.assertNotComplete(); |
| 1082 |
} |
| 1083 |
|
| 1084 |
@Test |
| 1085 |
public void hasNextCancelsAndCompletesSlowPathConditional() { |
| 1086 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(10); |
| 1087 |
|
| 1088 |
Flowable.fromIterable(new Iterable<Integer>() { |
| 1089 |
@Override |
| 1090 |
public Iterator<Integer> iterator() { |
| 1091 |
return new Iterator<Integer>() { |
| 1092 |
int count; |
| 1093 |
|
| 1094 |
@Override |
| 1095 |
public boolean hasNext() { |
| 1096 |
if (++count == 2) { |
| 1097 |
ts.cancel(); |
| 1098 |
return false; |
| 1099 |
} |
| 1100 |
return true; |
| 1101 |
} |
| 1102 |
|
| 1103 |
@Override |
| 1104 |
public Integer next() { |
| 1105 |
return 1; |
| 1106 |
} |
| 1107 |
|
| 1108 |
@Override |
| 1109 |
public void remove() { |
| 1110 |
throw new UnsupportedOperationException(); |
| 1111 |
} |
| 1112 |
}; |
| 1113 |
} |
| 1114 |
}) |
| 1115 |
.filter(v -> true) |
| 1116 |
.subscribe(ts); |
| 1117 |
|
| 1118 |
ts.assertValue(1) |
| 1119 |
.assertNoErrors() |
| 1120 |
.assertNotComplete(); |
| 1121 |
} |
| 1122 |
|
| 1123 |
@Test |
| 1124 |
public void fusedPoll() throws Throwable { |
| 1125 |
AtomicReference<SimpleQueue<?>> queue = new AtomicReference<>(); |
| 1126 |
|
| 1127 |
Flowable.fromIterable(Arrays.asList(1)) |
| 1128 |
.subscribe(new FlowableSubscriber<Integer>() { |
| 1129 |
@Override |
| 1130 |
public void onSubscribe(@NonNull Subscription s) { |
| 1131 |
queue.set((SimpleQueue<?>)s); |
| 1132 |
((QueueSubscription<?>)s).requestFusion(QueueFuseable.ANY); |
| 1133 |
} |
| 1134 |
|
| 1135 |
@Override |
| 1136 |
public void onNext(Integer t) { |
| 1137 |
} |
| 1138 |
|
| 1139 |
@Override |
| 1140 |
public void onError(Throwable t) { |
| 1141 |
} |
| 1142 |
|
| 1143 |
@Override |
| 1144 |
public void onComplete() { |
| 1145 |
} |
| 1146 |
}); |
| 1147 |
|
| 1148 |
SimpleQueue<?> q = queue.get(); |
| 1149 |
|
| 1150 |
assertFalse(q.isEmpty()); |
| 1151 |
|
| 1152 |
assertEquals(1, q.poll()); |
| 1153 |
|
| 1154 |
assertTrue(q.isEmpty()); |
| 1155 |
|
| 1156 |
q.clear(); |
| 1157 |
|
| 1158 |
assertTrue(q.isEmpty()); |
| 1159 |
} |
| 1160 |
|
| 1161 |
@Test |
| 1162 |
public void disposeWhileIteratorNext() { |
| 1163 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(10); |
| 1164 |
|
| 1165 |
Flowable.fromIterable(new Iterable<Integer>() { |
| 1166 |
@Override |
| 1167 |
public Iterator<Integer> iterator() { |
| 1168 |
return new Iterator<Integer>() { |
| 1169 |
@Override |
| 1170 |
public boolean hasNext() { |
| 1171 |
return true; |
| 1172 |
} |
| 1173 |
|
| 1174 |
@Override |
| 1175 |
public Integer next() { |
| 1176 |
ts.cancel(); |
| 1177 |
return 1; |
| 1178 |
} |
| 1179 |
|
| 1180 |
@Override |
| 1181 |
public void remove() { |
| 1182 |
throw new UnsupportedOperationException(); |
| 1183 |
} |
| 1184 |
}; |
| 1185 |
} |
| 1186 |
}) |
| 1187 |
.subscribe(ts); |
| 1188 |
|
| 1189 |
ts.assertEmpty(); |
| 1190 |
} |
| 1191 |
|
| 1192 |
@Test |
| 1193 |
public void disposeWhileIteratorNextConditional() { |
| 1194 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(10); |
| 1195 |
|
| 1196 |
Flowable.fromIterable(new Iterable<Integer>() { |
| 1197 |
@Override |
| 1198 |
public Iterator<Integer> iterator() { |
| 1199 |
return new Iterator<Integer>() { |
| 1200 |
@Override |
| 1201 |
public boolean hasNext() { |
| 1202 |
return true; |
| 1203 |
} |
| 1204 |
|
| 1205 |
@Override |
| 1206 |
public Integer next() { |
| 1207 |
ts.cancel(); |
| 1208 |
return 1; |
| 1209 |
} |
| 1210 |
|
| 1211 |
@Override |
| 1212 |
public void remove() { |
| 1213 |
throw new UnsupportedOperationException(); |
| 1214 |
} |
| 1215 |
}; |
| 1216 |
} |
| 1217 |
}) |
| 1218 |
.filter(v -> true) |
| 1219 |
.subscribe(ts); |
| 1220 |
|
| 1221 |
ts.assertEmpty(); |
| 1222 |
} |
| 1223 |
} |