| 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.util.*; |
| 19 |
import java.util.concurrent.Callable; |
| 20 |
import java.util.concurrent.atomic.*; |
| 21 |
|
| 22 |
import org.junit.*; |
| 23 |
import org.reactivestreams.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.exceptions.*; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableFlattenIterable.FlattenIterableSubscriber; |
| 30 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 31 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 32 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 33 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 34 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 35 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 36 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 37 |
import io.reactivex.rxjava3.testsupport.*; |
| 38 |
|
| 39 |
public class FlowableFlattenIterableTest extends RxJavaTest { |
| 40 |
|
| 41 |
@Test |
| 42 |
public void normal0() { |
| 43 |
|
| 44 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 45 |
|
| 46 |
Flowable.range(1, 2) |
| 47 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 48 |
@Override |
| 49 |
public Integer apply(Integer a, Integer b) { |
| 50 |
return Math.max(a, b); |
| 51 |
} |
| 52 |
}) |
| 53 |
.toFlowable() |
| 54 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 55 |
@Override |
| 56 |
public Iterable<Integer> apply(Integer v) { |
| 57 |
return Arrays.asList(v, v + 1); |
| 58 |
} |
| 59 |
}) |
| 60 |
.subscribe(ts); |
| 61 |
|
| 62 |
ts.assertValues(2, 3) |
| 63 |
.assertNoErrors() |
| 64 |
.assertComplete(); |
| 65 |
} |
| 66 |
|
| 67 |
final Function<Integer, Iterable<Integer>> mapper = new Function<Integer, Iterable<Integer>>() { |
| 68 |
@Override |
| 69 |
public Iterable<Integer> apply(Integer v) { |
| 70 |
return Arrays.asList(v, v + 1); |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
@Test |
| 75 |
public void normal() { |
| 76 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 77 |
|
| 78 |
Flowable.range(1, 5).concatMapIterable(mapper) |
| 79 |
.subscribe(ts); |
| 80 |
|
| 81 |
ts.assertValues(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 82 |
ts.assertNoErrors(); |
| 83 |
ts.assertComplete(); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void normalViaFlatMap() { |
| 88 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 89 |
|
| 90 |
Flowable.range(1, 5).flatMapIterable(mapper) |
| 91 |
.subscribe(ts); |
| 92 |
|
| 93 |
ts.assertValues(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 94 |
ts.assertNoErrors(); |
| 95 |
ts.assertComplete(); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void normalBackpressured() { |
| 100 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0); |
| 101 |
|
| 102 |
Flowable.range(1, 5).concatMapIterable(mapper) |
| 103 |
.subscribe(ts); |
| 104 |
|
| 105 |
ts.assertNoValues(); |
| 106 |
ts.assertNoErrors(); |
| 107 |
ts.assertNotComplete(); |
| 108 |
|
| 109 |
ts.request(1); |
| 110 |
|
| 111 |
ts.assertValue(1); |
| 112 |
ts.assertNoErrors(); |
| 113 |
ts.assertNotComplete(); |
| 114 |
|
| 115 |
ts.request(2); |
| 116 |
|
| 117 |
ts.assertValues(1, 2, 2); |
| 118 |
ts.assertNoErrors(); |
| 119 |
ts.assertNotComplete(); |
| 120 |
|
| 121 |
ts.request(7); |
| 122 |
|
| 123 |
ts.assertValues(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 124 |
ts.assertNoErrors(); |
| 125 |
ts.assertComplete(); |
| 126 |
} |
| 127 |
|
| 128 |
@Test |
| 129 |
public void longRunning() { |
| 130 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 131 |
|
| 132 |
int n = 1000 * 1000; |
| 133 |
|
| 134 |
Flowable.range(1, n).concatMapIterable(mapper) |
| 135 |
.subscribe(ts); |
| 136 |
|
| 137 |
ts.assertValueCount(n * 2); |
| 138 |
ts.assertNoErrors(); |
| 139 |
ts.assertComplete(); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void asIntermediate() { |
| 144 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 145 |
|
| 146 |
int n = 1000 * 1000; |
| 147 |
|
| 148 |
Flowable.range(1, n).concatMapIterable(mapper).concatMap(new Function<Integer, Flowable<Integer>>() { |
| 149 |
@Override |
| 150 |
public Flowable<Integer> apply(Integer v) { |
| 151 |
return Flowable.just(v); |
| 152 |
} |
| 153 |
}) |
| 154 |
.subscribe(ts); |
| 155 |
|
| 156 |
ts.assertValueCount(n * 2); |
| 157 |
ts.assertNoErrors(); |
| 158 |
ts.assertComplete(); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void just() { |
| 163 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 164 |
|
| 165 |
Flowable.just(1).concatMapIterable(mapper) |
| 166 |
.subscribe(ts); |
| 167 |
|
| 168 |
ts.assertValues(1, 2); |
| 169 |
ts.assertNoErrors(); |
| 170 |
ts.assertComplete(); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void justHidden() { |
| 175 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 176 |
|
| 177 |
Flowable.just(1).hide().concatMapIterable(mapper) |
| 178 |
.subscribe(ts); |
| 179 |
|
| 180 |
ts.assertValues(1, 2); |
| 181 |
ts.assertNoErrors(); |
| 182 |
ts.assertComplete(); |
| 183 |
} |
| 184 |
|
| 185 |
@Test |
| 186 |
public void empty() { |
| 187 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 188 |
|
| 189 |
Flowable.<Integer>empty().concatMapIterable(mapper) |
| 190 |
.subscribe(ts); |
| 191 |
|
| 192 |
ts.assertNoValues(); |
| 193 |
ts.assertNoErrors(); |
| 194 |
ts.assertComplete(); |
| 195 |
} |
| 196 |
|
| 197 |
@Test |
| 198 |
public void error() { |
| 199 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 200 |
|
| 201 |
Flowable.<Integer>just(1).concatWith(Flowable.<Integer>error(new TestException())) |
| 202 |
.concatMapIterable(mapper) |
| 203 |
.subscribe(ts); |
| 204 |
|
| 205 |
ts.assertValues(1, 2); |
| 206 |
ts.assertError(TestException.class); |
| 207 |
ts.assertNotComplete(); |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void iteratorHasNextThrowsImmediately() { |
| 212 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 213 |
|
| 214 |
final Iterable<Integer> it = new Iterable<Integer>() { |
| 215 |
@Override |
| 216 |
public Iterator<Integer> iterator() { |
| 217 |
return new Iterator<Integer>() { |
| 218 |
@Override |
| 219 |
public boolean hasNext() { |
| 220 |
throw new TestException(); |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public Integer next() { |
| 225 |
return 1; |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public void remove() { |
| 230 |
throw new UnsupportedOperationException(); |
| 231 |
} |
| 232 |
}; |
| 233 |
} |
| 234 |
}; |
| 235 |
|
| 236 |
Flowable.range(1, 2) |
| 237 |
.concatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 238 |
@Override |
| 239 |
public Iterable<Integer> apply(Integer v) { |
| 240 |
return it; |
| 241 |
} |
| 242 |
}) |
| 243 |
.subscribe(ts); |
| 244 |
|
| 245 |
ts.assertNoValues(); |
| 246 |
ts.assertError(TestException.class); |
| 247 |
ts.assertNotComplete(); |
| 248 |
} |
| 249 |
|
| 250 |
@Test |
| 251 |
public void iteratorHasNextThrowsImmediatelyJust() { |
| 252 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 253 |
|
| 254 |
final Iterable<Integer> it = new Iterable<Integer>() { |
| 255 |
@Override |
| 256 |
public Iterator<Integer> iterator() { |
| 257 |
return new Iterator<Integer>() { |
| 258 |
@Override |
| 259 |
public boolean hasNext() { |
| 260 |
throw new TestException(); |
| 261 |
} |
| 262 |
|
| 263 |
@Override |
| 264 |
public Integer next() { |
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public void remove() { |
| 270 |
throw new UnsupportedOperationException(); |
| 271 |
} |
| 272 |
}; |
| 273 |
} |
| 274 |
}; |
| 275 |
|
| 276 |
Flowable.just(1) |
| 277 |
.concatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 278 |
@Override |
| 279 |
public Iterable<Integer> apply(Integer v) { |
| 280 |
return it; |
| 281 |
} |
| 282 |
}) |
| 283 |
.subscribe(ts); |
| 284 |
|
| 285 |
ts.assertNoValues(); |
| 286 |
ts.assertError(TestException.class); |
| 287 |
ts.assertNotComplete(); |
| 288 |
} |
| 289 |
|
| 290 |
@Test |
| 291 |
public void iteratorHasNextThrowsSecondCall() { |
| 292 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 293 |
|
| 294 |
final Iterable<Integer> it = new Iterable<Integer>() { |
| 295 |
@Override |
| 296 |
public Iterator<Integer> iterator() { |
| 297 |
return new Iterator<Integer>() { |
| 298 |
int count; |
| 299 |
@Override |
| 300 |
public boolean hasNext() { |
| 301 |
if (++count >= 2) { |
| 302 |
throw new TestException(); |
| 303 |
} |
| 304 |
return true; |
| 305 |
} |
| 306 |
|
| 307 |
@Override |
| 308 |
public Integer next() { |
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
@Override |
| 313 |
public void remove() { |
| 314 |
throw new UnsupportedOperationException(); |
| 315 |
} |
| 316 |
}; |
| 317 |
} |
| 318 |
}; |
| 319 |
|
| 320 |
Flowable.range(1, 2) |
| 321 |
.concatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 322 |
@Override |
| 323 |
public Iterable<Integer> apply(Integer v) { |
| 324 |
return it; |
| 325 |
} |
| 326 |
}) |
| 327 |
.subscribe(ts); |
| 328 |
|
| 329 |
ts.assertValue(1); |
| 330 |
ts.assertError(TestException.class); |
| 331 |
ts.assertNotComplete(); |
| 332 |
} |
| 333 |
|
| 334 |
@Test |
| 335 |
public void iteratorNextThrows() { |
| 336 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 337 |
|
| 338 |
final Iterable<Integer> it = new Iterable<Integer>() { |
| 339 |
@Override |
| 340 |
public Iterator<Integer> iterator() { |
| 341 |
return new Iterator<Integer>() { |
| 342 |
@Override |
| 343 |
public boolean hasNext() { |
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
@Override |
| 348 |
public Integer next() { |
| 349 |
throw new TestException(); |
| 350 |
} |
| 351 |
|
| 352 |
@Override |
| 353 |
public void remove() { |
| 354 |
throw new UnsupportedOperationException(); |
| 355 |
} |
| 356 |
}; |
| 357 |
} |
| 358 |
}; |
| 359 |
|
| 360 |
Flowable.range(1, 2) |
| 361 |
.concatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 362 |
@Override |
| 363 |
public Iterable<Integer> apply(Integer v) { |
| 364 |
return it; |
| 365 |
} |
| 366 |
}) |
| 367 |
.subscribe(ts); |
| 368 |
|
| 369 |
ts.assertNoValues(); |
| 370 |
ts.assertError(TestException.class); |
| 371 |
ts.assertNotComplete(); |
| 372 |
} |
| 373 |
|
| 374 |
@Test |
| 375 |
public void iteratorNextThrowsAndUnsubscribes() { |
| 376 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 377 |
|
| 378 |
final Iterable<Integer> it = new Iterable<Integer>() { |
| 379 |
@Override |
| 380 |
public Iterator<Integer> iterator() { |
| 381 |
return new Iterator<Integer>() { |
| 382 |
@Override |
| 383 |
public boolean hasNext() { |
| 384 |
return true; |
| 385 |
} |
| 386 |
|
| 387 |
@Override |
| 388 |
public Integer next() { |
| 389 |
throw new TestException(); |
| 390 |
} |
| 391 |
|
| 392 |
@Override |
| 393 |
public void remove() { |
| 394 |
throw new UnsupportedOperationException(); |
| 395 |
} |
| 396 |
}; |
| 397 |
} |
| 398 |
}; |
| 399 |
|
| 400 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 401 |
|
| 402 |
pp |
| 403 |
.concatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 404 |
@Override |
| 405 |
public Iterable<Integer> apply(Integer v) { |
| 406 |
return it; |
| 407 |
} |
| 408 |
}) |
| 409 |
.subscribe(ts); |
| 410 |
|
| 411 |
pp.onNext(1); |
| 412 |
|
| 413 |
ts.assertNoValues(); |
| 414 |
ts.assertError(TestException.class); |
| 415 |
ts.assertNotComplete(); |
| 416 |
|
| 417 |
Assert.assertFalse("PublishProcessor has Subscribers?!", pp.hasSubscribers()); |
| 418 |
} |
| 419 |
|
| 420 |
@Test |
| 421 |
public void mixture() { |
| 422 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 423 |
|
| 424 |
Flowable.range(0, 1000) |
| 425 |
.concatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 426 |
@Override |
| 427 |
public Iterable<Integer> apply(Integer v) { |
| 428 |
return (v % 2) == 0 ? Collections.singleton(1) : Collections.<Integer>emptySet(); |
| 429 |
} |
| 430 |
}) |
| 431 |
.subscribe(ts); |
| 432 |
|
| 433 |
ts.assertValueCount(500); |
| 434 |
ts.assertNoErrors(); |
| 435 |
ts.assertComplete(); |
| 436 |
} |
| 437 |
|
| 438 |
@Test |
| 439 |
public void emptyInnerThenSingleBackpressured() { |
| 440 |
TestSubscriber<Integer> ts = new TestSubscriber<>(1); |
| 441 |
|
| 442 |
Flowable.range(1, 2) |
| 443 |
.concatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 444 |
@Override |
| 445 |
public Iterable<Integer> apply(Integer v) { |
| 446 |
return v == 2 ? Collections.singleton(1) : Collections.<Integer>emptySet(); |
| 447 |
} |
| 448 |
}) |
| 449 |
.subscribe(ts); |
| 450 |
|
| 451 |
ts.assertValue(1); |
| 452 |
ts.assertNoErrors(); |
| 453 |
ts.assertComplete(); |
| 454 |
} |
| 455 |
|
| 456 |
@Test |
| 457 |
public void manyEmptyInnerThenSingleBackpressured() { |
| 458 |
TestSubscriber<Integer> ts = new TestSubscriber<>(1); |
| 459 |
|
| 460 |
Flowable.range(1, 1000) |
| 461 |
.concatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 462 |
@Override |
| 463 |
public Iterable<Integer> apply(Integer v) { |
| 464 |
return v == 1000 ? Collections.singleton(1) : Collections.<Integer>emptySet(); |
| 465 |
} |
| 466 |
}) |
| 467 |
.subscribe(ts); |
| 468 |
|
| 469 |
ts.assertValue(1); |
| 470 |
ts.assertNoErrors(); |
| 471 |
ts.assertComplete(); |
| 472 |
} |
| 473 |
|
| 474 |
@Test |
| 475 |
public void hasNextIsNotCalledAfterChildUnsubscribedOnNext() { |
| 476 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 477 |
|
| 478 |
final AtomicInteger counter = new AtomicInteger(); |
| 479 |
|
| 480 |
final Iterable<Integer> it = new Iterable<Integer>() { |
| 481 |
@Override |
| 482 |
public Iterator<Integer> iterator() { |
| 483 |
return new Iterator<Integer>() { |
| 484 |
@Override |
| 485 |
public boolean hasNext() { |
| 486 |
counter.getAndIncrement(); |
| 487 |
return true; |
| 488 |
} |
| 489 |
|
| 490 |
@Override |
| 491 |
public Integer next() { |
| 492 |
return 1; |
| 493 |
} |
| 494 |
|
| 495 |
@Override |
| 496 |
public void remove() { |
| 497 |
throw new UnsupportedOperationException(); |
| 498 |
} |
| 499 |
}; |
| 500 |
} |
| 501 |
}; |
| 502 |
|
| 503 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 504 |
|
| 505 |
pp |
| 506 |
.concatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 507 |
@Override |
| 508 |
public Iterable<Integer> apply(Integer v) { |
| 509 |
return it; |
| 510 |
} |
| 511 |
}) |
| 512 |
.take(1) |
| 513 |
.subscribe(ts); |
| 514 |
|
| 515 |
pp.onNext(1); |
| 516 |
|
| 517 |
ts.assertValue(1); |
| 518 |
ts.assertNoErrors(); |
| 519 |
ts.assertComplete(); |
| 520 |
|
| 521 |
Assert.assertFalse("PublishProcessor has Subscribers?!", pp.hasSubscribers()); |
| 522 |
Assert.assertEquals(1, counter.get()); |
| 523 |
} |
| 524 |
|
| 525 |
@Test |
| 526 |
public void normalPrefetchViaFlatMap() { |
| 527 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 528 |
|
| 529 |
Flowable.range(1, 5).flatMapIterable(mapper, 2) |
| 530 |
.subscribe(ts); |
| 531 |
|
| 532 |
ts.assertValues(1, 2, 2, 3, 3, 4, 4, 5, 5, 6); |
| 533 |
ts.assertNoErrors(); |
| 534 |
ts.assertComplete(); |
| 535 |
} |
| 536 |
|
| 537 |
@Test |
| 538 |
public void withResultSelectorMaxConcurrent() { |
| 539 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 540 |
|
| 541 |
Flowable.range(1, 5) |
| 542 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 543 |
@Override |
| 544 |
public Iterable<Integer> apply(Integer v) { |
| 545 |
return Collections.singletonList(1); |
| 546 |
} |
| 547 |
}, new BiFunction<Integer, Integer, Integer>() { |
| 548 |
@Override |
| 549 |
public Integer apply(Integer a, Integer b) { |
| 550 |
return a * 10 + b; |
| 551 |
} |
| 552 |
}, 2) |
| 553 |
.subscribe(ts) |
| 554 |
; |
| 555 |
|
| 556 |
ts.assertValues(11, 21, 31, 41, 51); |
| 557 |
ts.assertNoErrors(); |
| 558 |
ts.assertComplete(); |
| 559 |
} |
| 560 |
|
| 561 |
@Test |
| 562 |
public void flatMapIterablePrefetch() { |
| 563 |
Flowable.just(1, 2) |
| 564 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 565 |
@Override |
| 566 |
public Iterable<Integer> apply(Integer t) throws Exception { |
| 567 |
return Arrays.asList(t * 10); |
| 568 |
} |
| 569 |
}, 1) |
| 570 |
.test() |
| 571 |
.assertResult(10, 20); |
| 572 |
} |
| 573 |
|
| 574 |
@Test |
| 575 |
public void dispose() { |
| 576 |
TestHelper.checkDisposed(PublishProcessor.create().flatMapIterable(new Function<Object, Iterable<Integer>>() { |
| 577 |
@Override |
| 578 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 579 |
return Arrays.asList(10, 20); |
| 580 |
} |
| 581 |
})); |
| 582 |
} |
| 583 |
|
| 584 |
@Test |
| 585 |
public void badSource() { |
| 586 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 587 |
@Override |
| 588 |
public Object apply(Flowable<Integer> f) throws Exception { |
| 589 |
return f.flatMapIterable(new Function<Object, Iterable<Integer>>() { |
| 590 |
@Override |
| 591 |
public Iterable<Integer> apply(Object v) throws Exception { |
| 592 |
return Arrays.asList(10, 20); |
| 593 |
} |
| 594 |
}); |
| 595 |
} |
| 596 |
}, false, 1, 1, 10, 20); |
| 597 |
} |
| 598 |
|
| 599 |
@Test |
| 600 |
public void callableThrows() { |
| 601 |
Flowable.fromCallable(new Callable<Object>() { |
| 602 |
@Override |
| 603 |
public Object call() throws Exception { |
| 604 |
throw new TestException(); |
| 605 |
} |
| 606 |
}) |
| 607 |
.flatMapIterable(Functions.justFunction(Arrays.asList(1, 2, 3))) |
| 608 |
.test() |
| 609 |
.assertFailure(TestException.class); |
| 610 |
} |
| 611 |
|
| 612 |
@Test |
| 613 |
public void fusionMethods() { |
| 614 |
Flowable.just(1, 2) |
| 615 |
.flatMapIterable(Functions.justFunction(Arrays.asList(1, 2, 3))) |
| 616 |
.subscribe(new FlowableSubscriber<Integer>() { |
| 617 |
@Override |
| 618 |
public void onSubscribe(Subscription s) { |
| 619 |
@SuppressWarnings("unchecked") |
| 620 |
QueueSubscription<Integer> qs = (QueueSubscription<Integer>)s; |
| 621 |
|
| 622 |
assertEquals(QueueFuseable.SYNC, qs.requestFusion(QueueFuseable.ANY)); |
| 623 |
|
| 624 |
try { |
| 625 |
assertFalse("Source reports being empty!", qs.isEmpty()); |
| 626 |
|
| 627 |
assertEquals(1, qs.poll().intValue()); |
| 628 |
|
| 629 |
assertFalse("Source reports being empty!", qs.isEmpty()); |
| 630 |
|
| 631 |
assertEquals(2, qs.poll().intValue()); |
| 632 |
|
| 633 |
assertFalse("Source reports being empty!", qs.isEmpty()); |
| 634 |
|
| 635 |
qs.clear(); |
| 636 |
|
| 637 |
assertTrue("Source reports not empty!", qs.isEmpty()); |
| 638 |
|
| 639 |
assertNull(qs.poll()); |
| 640 |
} catch (Throwable ex) { |
| 641 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
@Override |
| 646 |
public void onNext(Integer t) { |
| 647 |
} |
| 648 |
|
| 649 |
@Override |
| 650 |
public void onError(Throwable t) { |
| 651 |
} |
| 652 |
|
| 653 |
@Override |
| 654 |
public void onComplete() { |
| 655 |
} |
| 656 |
}); |
| 657 |
} |
| 658 |
|
| 659 |
@Test |
| 660 |
public void smallPrefetch() { |
| 661 |
Flowable.just(1, 2, 3) |
| 662 |
.flatMapIterable(Functions.justFunction(Arrays.asList(1, 2, 3)), 1) |
| 663 |
.test() |
| 664 |
.assertResult(1, 2, 3, 1, 2, 3, 1, 2, 3); |
| 665 |
} |
| 666 |
|
| 667 |
@Test |
| 668 |
public void smallPrefetch2() { |
| 669 |
Flowable.just(1, 2, 3).hide() |
| 670 |
.flatMapIterable(Functions.justFunction(Collections.emptyList()), 1) |
| 671 |
.test() |
| 672 |
.assertResult(); |
| 673 |
} |
| 674 |
|
| 675 |
@Test |
| 676 |
public void mixedInnerSource() { |
| 677 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 678 |
|
| 679 |
Flowable.just(1, 2, 3) |
| 680 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 681 |
@Override |
| 682 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 683 |
if ((v & 1) == 0) { |
| 684 |
return Collections.emptyList(); |
| 685 |
} |
| 686 |
return Arrays.asList(1, 2); |
| 687 |
} |
| 688 |
}) |
| 689 |
.subscribe(ts); |
| 690 |
|
| 691 |
ts.assertFusionMode(QueueFuseable.SYNC) |
| 692 |
.assertResult(1, 2, 1, 2); |
| 693 |
} |
| 694 |
|
| 695 |
@Test |
| 696 |
public void mixedInnerSource2() { |
| 697 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 698 |
|
| 699 |
Flowable.just(1, 2, 3) |
| 700 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 701 |
@Override |
| 702 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 703 |
if ((v & 1) == 1) { |
| 704 |
return Collections.emptyList(); |
| 705 |
} |
| 706 |
return Arrays.asList(1, 2); |
| 707 |
} |
| 708 |
}) |
| 709 |
.subscribe(ts); |
| 710 |
|
| 711 |
ts.assertFusionMode(QueueFuseable.SYNC) |
| 712 |
.assertResult(1, 2); |
| 713 |
} |
| 714 |
|
| 715 |
@Test |
| 716 |
public void fusionRejected() { |
| 717 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 718 |
|
| 719 |
Flowable.just(1, 2, 3).hide() |
| 720 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 721 |
@Override |
| 722 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 723 |
return Arrays.asList(1, 2); |
| 724 |
} |
| 725 |
}) |
| 726 |
.subscribe(ts); |
| 727 |
|
| 728 |
ts.assertFusionMode(QueueFuseable.NONE) |
| 729 |
.assertResult(1, 2, 1, 2, 1, 2); |
| 730 |
} |
| 731 |
|
| 732 |
@Test |
| 733 |
public void fusedIsEmptyWithEmptySource() { |
| 734 |
Flowable.just(1, 2, 3) |
| 735 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 736 |
@Override |
| 737 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 738 |
if ((v & 1) == 0) { |
| 739 |
return Collections.emptyList(); |
| 740 |
} |
| 741 |
return Arrays.asList(v); |
| 742 |
} |
| 743 |
}) |
| 744 |
.subscribe(new FlowableSubscriber<Integer>() { |
| 745 |
@Override |
| 746 |
public void onSubscribe(Subscription s) { |
| 747 |
@SuppressWarnings("unchecked") |
| 748 |
QueueSubscription<Integer> qs = (QueueSubscription<Integer>)s; |
| 749 |
|
| 750 |
assertEquals(QueueFuseable.SYNC, qs.requestFusion(QueueFuseable.ANY)); |
| 751 |
|
| 752 |
try { |
| 753 |
assertFalse("Source reports being empty!", qs.isEmpty()); |
| 754 |
|
| 755 |
assertEquals(1, qs.poll().intValue()); |
| 756 |
|
| 757 |
assertFalse("Source reports being empty!", qs.isEmpty()); |
| 758 |
|
| 759 |
assertEquals(3, qs.poll().intValue()); |
| 760 |
|
| 761 |
assertTrue("Source reports being non-empty!", qs.isEmpty()); |
| 762 |
} catch (Throwable ex) { |
| 763 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
@Override |
| 768 |
public void onNext(Integer t) { |
| 769 |
} |
| 770 |
|
| 771 |
@Override |
| 772 |
public void onError(Throwable t) { |
| 773 |
} |
| 774 |
|
| 775 |
@Override |
| 776 |
public void onComplete() { |
| 777 |
} |
| 778 |
}); |
| 779 |
} |
| 780 |
|
| 781 |
@Test |
| 782 |
public void fusedSourceCrash() { |
| 783 |
Flowable.range(1, 3) |
| 784 |
.map(new Function<Integer, Object>() { |
| 785 |
@Override |
| 786 |
public Object apply(Integer v) throws Exception { |
| 787 |
throw new TestException(); |
| 788 |
} |
| 789 |
}) |
| 790 |
.flatMapIterable(Functions.justFunction(Collections.emptyList()), 1) |
| 791 |
.test() |
| 792 |
.assertFailure(TestException.class); |
| 793 |
} |
| 794 |
|
| 795 |
@Test |
| 796 |
public void take() { |
| 797 |
Flowable.range(1, 3) |
| 798 |
.flatMapIterable(Functions.justFunction(Arrays.asList(1)), 1) |
| 799 |
.take(1) |
| 800 |
.test() |
| 801 |
.assertResult(1); |
| 802 |
} |
| 803 |
|
| 804 |
@Test |
| 805 |
public void overflowSource() { |
| 806 |
new Flowable<Integer>() { |
| 807 |
@Override |
| 808 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 809 |
s.onSubscribe(new BooleanSubscription()); |
| 810 |
s.onNext(1); |
| 811 |
s.onNext(2); |
| 812 |
s.onNext(3); |
| 813 |
} |
| 814 |
} |
| 815 |
.flatMapIterable(Functions.justFunction(Arrays.asList(1)), 1) |
| 816 |
.test(0L) |
| 817 |
.assertFailure(MissingBackpressureException.class); |
| 818 |
} |
| 819 |
|
| 820 |
@Test |
| 821 |
public void oneByOne() { |
| 822 |
Flowable.range(1, 3).hide() |
| 823 |
.flatMapIterable(Functions.justFunction(Arrays.asList(1)), 1) |
| 824 |
.rebatchRequests(1) |
| 825 |
.test() |
| 826 |
.assertResult(1, 1, 1); |
| 827 |
} |
| 828 |
|
| 829 |
@Test |
| 830 |
public void cancelAfterHasNext() { |
| 831 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 832 |
|
| 833 |
Flowable.range(1, 3).hide() |
| 834 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 835 |
@Override |
| 836 |
public Iterable<Integer> apply(Integer v) throws Exception { |
| 837 |
return new Iterable<Integer>() { |
| 838 |
int count; |
| 839 |
@Override |
| 840 |
public Iterator<Integer> iterator() { |
| 841 |
return new Iterator<Integer>() { |
| 842 |
|
| 843 |
@Override |
| 844 |
public boolean hasNext() { |
| 845 |
if (++count == 2) { |
| 846 |
ts.cancel(); |
| 847 |
ts.onComplete(); |
| 848 |
} |
| 849 |
return true; |
| 850 |
} |
| 851 |
|
| 852 |
@Override |
| 853 |
public Integer next() { |
| 854 |
return 1; |
| 855 |
} |
| 856 |
|
| 857 |
@Override |
| 858 |
public void remove() { |
| 859 |
throw new UnsupportedOperationException(); |
| 860 |
} |
| 861 |
}; |
| 862 |
} |
| 863 |
}; |
| 864 |
} |
| 865 |
}) |
| 866 |
.subscribe(ts); |
| 867 |
|
| 868 |
ts.assertResult(1); |
| 869 |
} |
| 870 |
|
| 871 |
@Test |
| 872 |
public void doubleShare() { |
| 873 |
Iterable<Integer> it = Flowable.range(1, 300).blockingIterable(); |
| 874 |
Flowable.just(it, it) |
| 875 |
.flatMapIterable(Functions.<Iterable<Integer>>identity()) |
| 876 |
.share() |
| 877 |
.share() |
| 878 |
.count() |
| 879 |
.test() |
| 880 |
.assertResult(600L); |
| 881 |
} |
| 882 |
|
| 883 |
@Test |
| 884 |
public void multiShare() { |
| 885 |
Iterable<Integer> it = Flowable.range(1, 300).blockingIterable(); |
| 886 |
for (int i = 0; i < 5; i++) { |
| 887 |
Flowable<Integer> f = Flowable.just(it, it) |
| 888 |
.flatMapIterable(Functions.<Iterable<Integer>>identity()); |
| 889 |
|
| 890 |
for (int j = 0; j < i; j++) { |
| 891 |
f = f.share(); |
| 892 |
} |
| 893 |
|
| 894 |
f |
| 895 |
.count() |
| 896 |
.test() |
| 897 |
.withTag("Share: " + i) |
| 898 |
.assertResult(600L); |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
@Test |
| 903 |
public void multiShareHidden() { |
| 904 |
Iterable<Integer> it = Flowable.range(1, 300).blockingIterable(); |
| 905 |
for (int i = 0; i < 5; i++) { |
| 906 |
Flowable<Integer> f = Flowable.just(it, it) |
| 907 |
.flatMapIterable(Functions.<Iterable<Integer>>identity()) |
| 908 |
.hide(); |
| 909 |
|
| 910 |
for (int j = 0; j < i; j++) { |
| 911 |
f = f.share(); |
| 912 |
} |
| 913 |
|
| 914 |
f |
| 915 |
.count() |
| 916 |
.test() |
| 917 |
.withTag("Share: " + i) |
| 918 |
.assertResult(600L); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
@Test |
| 923 |
public void failingInnerCancelsSource() { |
| 924 |
final AtomicInteger counter = new AtomicInteger(); |
| 925 |
Flowable.range(1, 5) |
| 926 |
.doOnNext(new Consumer<Integer>() { |
| 927 |
@Override |
| 928 |
public void accept(Integer v) throws Exception { |
| 929 |
counter.getAndIncrement(); |
| 930 |
} |
| 931 |
}) |
| 932 |
.flatMapIterable(new Function<Integer, Iterable<Integer>>() { |
| 933 |
@Override |
| 934 |
public Iterable<Integer> apply(Integer v) |
| 935 |
throws Exception { |
| 936 |
return new Iterable<Integer>() { |
| 937 |
@Override |
| 938 |
public Iterator<Integer> iterator() { |
| 939 |
return new Iterator<Integer>() { |
| 940 |
@Override |
| 941 |
public boolean hasNext() { |
| 942 |
return true; |
| 943 |
} |
| 944 |
|
| 945 |
@Override |
| 946 |
public Integer next() { |
| 947 |
throw new TestException(); |
| 948 |
} |
| 949 |
|
| 950 |
@Override |
| 951 |
public void remove() { |
| 952 |
throw new UnsupportedOperationException(); |
| 953 |
} |
| 954 |
}; |
| 955 |
} |
| 956 |
}; |
| 957 |
} |
| 958 |
}) |
| 959 |
.test() |
| 960 |
.assertFailure(TestException.class); |
| 961 |
|
| 962 |
assertEquals(1, counter.get()); |
| 963 |
} |
| 964 |
|
| 965 |
@Test |
| 966 |
public void doubleOnSubscribe() { |
| 967 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() { |
| 968 |
@Override |
| 969 |
public Publisher<Object> apply(Flowable<Object> f) |
| 970 |
throws Exception { |
| 971 |
return f.flatMapIterable(Functions.justFunction(Collections.emptyList())); |
| 972 |
} |
| 973 |
}); |
| 974 |
} |
| 975 |
|
| 976 |
@Test |
| 977 |
public void upstreamFusionRejected() { |
| 978 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 979 |
FlattenIterableSubscriber<Integer, Integer> f = new FlattenIterableSubscriber<>(ts, |
| 980 |
Functions.justFunction(Collections.<Integer>emptyList()), 128); |
| 981 |
|
| 982 |
final AtomicLong requested = new AtomicLong(); |
| 983 |
|
| 984 |
f.onSubscribe(new QueueSubscription<Integer>() { |
| 985 |
|
| 986 |
@Override |
| 987 |
public int requestFusion(int mode) { |
| 988 |
return 0; |
| 989 |
} |
| 990 |
|
| 991 |
@Override |
| 992 |
public boolean offer(Integer value) { |
| 993 |
return false; |
| 994 |
} |
| 995 |
|
| 996 |
@Override |
| 997 |
public boolean offer(Integer v1, Integer v2) { |
| 998 |
return false; |
| 999 |
} |
| 1000 |
|
| 1001 |
@Override |
| 1002 |
public Integer poll() throws Exception { |
| 1003 |
return null; |
| 1004 |
} |
| 1005 |
|
| 1006 |
@Override |
| 1007 |
public boolean isEmpty() { |
| 1008 |
return false; |
| 1009 |
} |
| 1010 |
|
| 1011 |
@Override |
| 1012 |
public void clear() { |
| 1013 |
} |
| 1014 |
|
| 1015 |
@Override |
| 1016 |
public void request(long n) { |
| 1017 |
requested.set(n); |
| 1018 |
} |
| 1019 |
|
| 1020 |
@Override |
| 1021 |
public void cancel() { |
| 1022 |
} |
| 1023 |
}); |
| 1024 |
|
| 1025 |
assertEquals(128, requested.get()); |
| 1026 |
assertNotNull(f.queue); |
| 1027 |
|
| 1028 |
ts.assertEmpty(); |
| 1029 |
} |
| 1030 |
|
| 1031 |
@Test |
| 1032 |
public void onErrorLate() { |
| 1033 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1034 |
try { |
| 1035 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 1036 |
FlattenIterableSubscriber<Integer, Integer> f = new FlattenIterableSubscriber<>(ts, |
| 1037 |
Functions.justFunction(Collections.<Integer>emptyList()), 128); |
| 1038 |
|
| 1039 |
f.onSubscribe(new BooleanSubscription()); |
| 1040 |
|
| 1041 |
f.onError(new TestException("first")); |
| 1042 |
|
| 1043 |
ts.assertFailureAndMessage(TestException.class, "first"); |
| 1044 |
|
| 1045 |
assertTrue(errors.isEmpty()); |
| 1046 |
|
| 1047 |
f.done = false; |
| 1048 |
f.onError(new TestException("second")); |
| 1049 |
|
| 1050 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "second"); |
| 1051 |
} finally { |
| 1052 |
RxJavaPlugins.reset(); |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
@Test |
| 1057 |
public void badRequest() { |
| 1058 |
TestHelper.assertBadRequestReported(Flowable.never().flatMapIterable(Functions.justFunction(Collections.emptyList()))); |
| 1059 |
} |
| 1060 |
|
| 1061 |
@Test |
| 1062 |
public void fusedCurrentIteratorEmpty() throws Throwable { |
| 1063 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0); |
| 1064 |
FlattenIterableSubscriber<Integer, Integer> f = new FlattenIterableSubscriber<>(ts, |
| 1065 |
Functions.justFunction(Arrays.<Integer>asList(1, 2)), 128); |
| 1066 |
|
| 1067 |
f.onSubscribe(new BooleanSubscription()); |
| 1068 |
|
| 1069 |
f.onNext(1); |
| 1070 |
|
| 1071 |
assertFalse(f.isEmpty()); |
| 1072 |
|
| 1073 |
assertEquals(1, f.poll().intValue()); |
| 1074 |
|
| 1075 |
assertFalse(f.isEmpty()); |
| 1076 |
|
| 1077 |
assertEquals(2, f.poll().intValue()); |
| 1078 |
|
| 1079 |
assertTrue(f.isEmpty()); |
| 1080 |
} |
| 1081 |
|
| 1082 |
@Test |
| 1083 |
public void fusionRequestedState() throws Exception { |
| 1084 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0); |
| 1085 |
FlattenIterableSubscriber<Integer, Integer> f = new FlattenIterableSubscriber<>(ts, |
| 1086 |
Functions.justFunction(Arrays.<Integer>asList(1, 2)), 128); |
| 1087 |
|
| 1088 |
f.onSubscribe(new BooleanSubscription()); |
| 1089 |
|
| 1090 |
f.fusionMode = QueueFuseable.NONE; |
| 1091 |
|
| 1092 |
assertEquals(QueueFuseable.NONE, f.requestFusion(QueueFuseable.SYNC)); |
| 1093 |
|
| 1094 |
assertEquals(QueueFuseable.NONE, f.requestFusion(QueueFuseable.ASYNC)); |
| 1095 |
|
| 1096 |
f.fusionMode = QueueFuseable.SYNC; |
| 1097 |
|
| 1098 |
assertEquals(QueueFuseable.SYNC, f.requestFusion(QueueFuseable.SYNC)); |
| 1099 |
|
| 1100 |
assertEquals(QueueFuseable.NONE, f.requestFusion(QueueFuseable.ASYNC)); |
| 1101 |
} |
| 1102 |
} |