| 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.io.IOException; |
| 21 |
import java.util.*; |
| 22 |
import java.util.concurrent.*; |
| 23 |
import java.util.concurrent.atomic.*; |
| 24 |
|
| 25 |
import org.junit.*; |
| 26 |
import org.reactivestreams.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 29 |
import io.reactivex.rxjava3.core.*; |
| 30 |
import io.reactivex.rxjava3.exceptions.*; |
| 31 |
import io.reactivex.rxjava3.functions.*; |
| 32 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 33 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 34 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 35 |
import io.reactivex.rxjava3.processors.*; |
| 36 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 37 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 38 |
import io.reactivex.rxjava3.testsupport.*; |
| 39 |
|
| 40 |
public class FlowableFlatMapTest extends RxJavaTest { |
| 41 |
@Test |
| 42 |
public void normal() { |
| 43 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 44 |
|
| 45 |
final List<Integer> list = Arrays.asList(1, 2, 3); |
| 46 |
|
| 47 |
Function<Integer, List<Integer>> func = new Function<Integer, List<Integer>>() { |
| 48 |
@Override |
| 49 |
public List<Integer> apply(Integer t1) { |
| 50 |
return list; |
| 51 |
} |
| 52 |
}; |
| 53 |
BiFunction<Integer, Integer, Integer> resFunc = new BiFunction<Integer, Integer, Integer>() { |
| 54 |
|
| 55 |
@Override |
| 56 |
public Integer apply(Integer t1, Integer t2) { |
| 57 |
return t1 | t2; |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
List<Integer> source = Arrays.asList(16, 32, 64); |
| 62 |
|
| 63 |
Flowable.fromIterable(source).flatMapIterable(func, resFunc).subscribe(subscriber); |
| 64 |
|
| 65 |
for (Integer s : source) { |
| 66 |
for (Integer v : list) { |
| 67 |
verify(subscriber).onNext(s | v); |
| 68 |
} |
| 69 |
} |
| 70 |
verify(subscriber).onComplete(); |
| 71 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void collectionFunctionThrows() { |
| 76 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 77 |
|
| 78 |
Function<Integer, List<Integer>> func = new Function<Integer, List<Integer>>() { |
| 79 |
@Override |
| 80 |
public List<Integer> apply(Integer t1) { |
| 81 |
throw new TestException(); |
| 82 |
} |
| 83 |
}; |
| 84 |
BiFunction<Integer, Integer, Integer> resFunc = new BiFunction<Integer, Integer, Integer>() { |
| 85 |
|
| 86 |
@Override |
| 87 |
public Integer apply(Integer t1, Integer t2) { |
| 88 |
return t1 | t2; |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
List<Integer> source = Arrays.asList(16, 32, 64); |
| 93 |
|
| 94 |
Flowable.fromIterable(source).flatMapIterable(func, resFunc).subscribe(subscriber); |
| 95 |
|
| 96 |
verify(subscriber, never()).onComplete(); |
| 97 |
verify(subscriber, never()).onNext(any()); |
| 98 |
verify(subscriber).onError(any(TestException.class)); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void resultFunctionThrows() { |
| 103 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 104 |
|
| 105 |
final List<Integer> list = Arrays.asList(1, 2, 3); |
| 106 |
|
| 107 |
Function<Integer, List<Integer>> func = new Function<Integer, List<Integer>>() { |
| 108 |
@Override |
| 109 |
public List<Integer> apply(Integer t1) { |
| 110 |
return list; |
| 111 |
} |
| 112 |
}; |
| 113 |
BiFunction<Integer, Integer, Integer> resFunc = new BiFunction<Integer, Integer, Integer>() { |
| 114 |
|
| 115 |
@Override |
| 116 |
public Integer apply(Integer t1, Integer t2) { |
| 117 |
throw new TestException(); |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
List<Integer> source = Arrays.asList(16, 32, 64); |
| 122 |
|
| 123 |
Flowable.fromIterable(source).flatMapIterable(func, resFunc).subscribe(subscriber); |
| 124 |
|
| 125 |
verify(subscriber, never()).onComplete(); |
| 126 |
verify(subscriber, never()).onNext(any()); |
| 127 |
verify(subscriber).onError(any(TestException.class)); |
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void mergeError() { |
| 132 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 133 |
|
| 134 |
Function<Integer, Flowable<Integer>> func = new Function<Integer, Flowable<Integer>>() { |
| 135 |
@Override |
| 136 |
public Flowable<Integer> apply(Integer t1) { |
| 137 |
return Flowable.error(new TestException()); |
| 138 |
} |
| 139 |
}; |
| 140 |
BiFunction<Integer, Integer, Integer> resFunc = new BiFunction<Integer, Integer, Integer>() { |
| 141 |
|
| 142 |
@Override |
| 143 |
public Integer apply(Integer t1, Integer t2) { |
| 144 |
return t1 | t2; |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
List<Integer> source = Arrays.asList(16, 32, 64); |
| 149 |
|
| 150 |
Flowable.fromIterable(source).flatMap(func, resFunc).subscribe(subscriber); |
| 151 |
|
| 152 |
verify(subscriber, never()).onComplete(); |
| 153 |
verify(subscriber, never()).onNext(any()); |
| 154 |
verify(subscriber).onError(any(TestException.class)); |
| 155 |
} |
| 156 |
|
| 157 |
<T, R> Function<T, R> just(final R value) { |
| 158 |
return new Function<T, R>() { |
| 159 |
|
| 160 |
@Override |
| 161 |
public R apply(T t1) { |
| 162 |
return value; |
| 163 |
} |
| 164 |
}; |
| 165 |
} |
| 166 |
|
| 167 |
<R> Supplier<R> just0(final R value) { |
| 168 |
return new Supplier<R>() { |
| 169 |
|
| 170 |
@Override |
| 171 |
public R get() { |
| 172 |
return value; |
| 173 |
} |
| 174 |
}; |
| 175 |
} |
| 176 |
|
| 177 |
@Test |
| 178 |
public void flatMapTransformsNormal() { |
| 179 |
Flowable<Integer> onNext = Flowable.fromIterable(Arrays.asList(1, 2, 3)); |
| 180 |
Flowable<Integer> onComplete = Flowable.fromIterable(Arrays.asList(4)); |
| 181 |
Flowable<Integer> onError = Flowable.fromIterable(Arrays.asList(5)); |
| 182 |
|
| 183 |
Flowable<Integer> source = Flowable.fromIterable(Arrays.asList(10, 20, 30)); |
| 184 |
|
| 185 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 186 |
|
| 187 |
source.flatMap(just(onNext), just(onError), just0(onComplete)).subscribe(subscriber); |
| 188 |
|
| 189 |
verify(subscriber, times(3)).onNext(1); |
| 190 |
verify(subscriber, times(3)).onNext(2); |
| 191 |
verify(subscriber, times(3)).onNext(3); |
| 192 |
verify(subscriber).onNext(4); |
| 193 |
verify(subscriber).onComplete(); |
| 194 |
|
| 195 |
verify(subscriber, never()).onNext(5); |
| 196 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 197 |
} |
| 198 |
|
| 199 |
@Test |
| 200 |
public void flatMapTransformsException() { |
| 201 |
Flowable<Integer> onNext = Flowable.fromIterable(Arrays.asList(1, 2, 3)); |
| 202 |
Flowable<Integer> onComplete = Flowable.fromIterable(Arrays.asList(4)); |
| 203 |
Flowable<Integer> onError = Flowable.fromIterable(Arrays.asList(5)); |
| 204 |
|
| 205 |
Flowable<Integer> source = Flowable.concat( |
| 206 |
Flowable.fromIterable(Arrays.asList(10, 20, 30)), |
| 207 |
Flowable.<Integer> error(new RuntimeException("Forced failure!")) |
| 208 |
); |
| 209 |
|
| 210 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 211 |
|
| 212 |
source.flatMap(just(onNext), just(onError), just0(onComplete)).subscribe(subscriber); |
| 213 |
|
| 214 |
verify(subscriber, times(3)).onNext(1); |
| 215 |
verify(subscriber, times(3)).onNext(2); |
| 216 |
verify(subscriber, times(3)).onNext(3); |
| 217 |
verify(subscriber).onNext(5); |
| 218 |
verify(subscriber).onComplete(); |
| 219 |
verify(subscriber, never()).onNext(4); |
| 220 |
|
| 221 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 222 |
} |
| 223 |
|
| 224 |
<R> Supplier<R> funcThrow0(R r) { |
| 225 |
return new Supplier<R>() { |
| 226 |
@Override |
| 227 |
public R get() { |
| 228 |
throw new TestException(); |
| 229 |
} |
| 230 |
}; |
| 231 |
} |
| 232 |
|
| 233 |
<T, R> Function<T, R> funcThrow(T t, R r) { |
| 234 |
return new Function<T, R>() { |
| 235 |
@Override |
| 236 |
public R apply(T t) { |
| 237 |
throw new TestException(); |
| 238 |
} |
| 239 |
}; |
| 240 |
} |
| 241 |
|
| 242 |
@Test |
| 243 |
public void flatMapTransformsOnNextFuncThrows() { |
| 244 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 245 |
try { |
| 246 |
Flowable<Integer> onComplete = Flowable.fromIterable(Arrays.asList(4)); |
| 247 |
Flowable<Integer> onError = Flowable.fromIterable(Arrays.asList(5)); |
| 248 |
|
| 249 |
Flowable<Integer> source = Flowable.fromIterable(Arrays.asList(10, 20, 30)); |
| 250 |
|
| 251 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 252 |
|
| 253 |
source.flatMap(funcThrow(1, onError), just(onError), just0(onComplete)).subscribe(subscriber); |
| 254 |
|
| 255 |
verify(subscriber).onError(any(TestException.class)); |
| 256 |
verify(subscriber, never()).onNext(any()); |
| 257 |
verify(subscriber, never()).onComplete(); |
| 258 |
|
| 259 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 260 |
} finally { |
| 261 |
RxJavaPlugins.reset(); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
@Test |
| 266 |
public void flatMapTransformsOnErrorFuncThrows() { |
| 267 |
Flowable<Integer> onNext = Flowable.fromIterable(Arrays.asList(1, 2, 3)); |
| 268 |
Flowable<Integer> onComplete = Flowable.fromIterable(Arrays.asList(4)); |
| 269 |
Flowable<Integer> onError = Flowable.fromIterable(Arrays.asList(5)); |
| 270 |
|
| 271 |
Flowable<Integer> source = Flowable.error(new TestException()); |
| 272 |
|
| 273 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 274 |
|
| 275 |
source.flatMap(just(onNext), funcThrow((Throwable) null, onError), just0(onComplete)).subscribe(subscriber); |
| 276 |
|
| 277 |
verify(subscriber).onError(any(CompositeException.class)); |
| 278 |
verify(subscriber, never()).onNext(any()); |
| 279 |
verify(subscriber, never()).onComplete(); |
| 280 |
} |
| 281 |
|
| 282 |
@Test |
| 283 |
public void flatMapTransformsOnCompletedFuncThrows() { |
| 284 |
Flowable<Integer> onNext = Flowable.fromIterable(Arrays.asList(1, 2, 3)); |
| 285 |
Flowable<Integer> onComplete = Flowable.fromIterable(Arrays.asList(4)); |
| 286 |
Flowable<Integer> onError = Flowable.fromIterable(Arrays.asList(5)); |
| 287 |
|
| 288 |
Flowable<Integer> source = Flowable.fromIterable(Arrays.<Integer> asList()); |
| 289 |
|
| 290 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 291 |
|
| 292 |
source.flatMap(just(onNext), just(onError), funcThrow0(onComplete)).subscribe(subscriber); |
| 293 |
|
| 294 |
verify(subscriber).onError(any(TestException.class)); |
| 295 |
verify(subscriber, never()).onNext(any()); |
| 296 |
verify(subscriber, never()).onComplete(); |
| 297 |
} |
| 298 |
|
| 299 |
@Test |
| 300 |
public void flatMapTransformsMergeException() { |
| 301 |
Flowable<Integer> onNext = Flowable.error(new TestException()); |
| 302 |
Flowable<Integer> onComplete = Flowable.fromIterable(Arrays.asList(4)); |
| 303 |
Flowable<Integer> onError = Flowable.fromIterable(Arrays.asList(5)); |
| 304 |
|
| 305 |
Flowable<Integer> source = Flowable.fromIterable(Arrays.asList(10, 20, 30)); |
| 306 |
|
| 307 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 308 |
|
| 309 |
source.flatMap(just(onNext), just(onError), funcThrow0(onComplete)).subscribe(subscriber); |
| 310 |
|
| 311 |
verify(subscriber).onError(any(TestException.class)); |
| 312 |
verify(subscriber, never()).onNext(any()); |
| 313 |
verify(subscriber, never()).onComplete(); |
| 314 |
} |
| 315 |
|
| 316 |
private static <T> Flowable<T> composer(Flowable<T> source, final AtomicInteger subscriptionCount, final int m) { |
| 317 |
return source.doOnSubscribe(new Consumer<Subscription>() { |
| 318 |
@Override |
| 319 |
public void accept(Subscription s) { |
| 320 |
int n = subscriptionCount.getAndIncrement(); |
| 321 |
if (n >= m) { |
| 322 |
Assert.fail("Too many subscriptions! " + (n + 1)); |
| 323 |
} |
| 324 |
} |
| 325 |
}).doOnComplete(new Action() { |
| 326 |
@Override |
| 327 |
public void run() { |
| 328 |
int n = subscriptionCount.decrementAndGet(); |
| 329 |
if (n < 0) { |
| 330 |
Assert.fail("Too many unsubscriptions! " + (n - 1)); |
| 331 |
} |
| 332 |
} |
| 333 |
}); |
| 334 |
} |
| 335 |
|
| 336 |
@Test |
| 337 |
public void flatMapMaxConcurrent() { |
| 338 |
final int m = 4; |
| 339 |
final AtomicInteger subscriptionCount = new AtomicInteger(); |
| 340 |
Flowable<Integer> source = Flowable.range(1, 10) |
| 341 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 342 |
@Override |
| 343 |
public Flowable<Integer> apply(Integer t1) { |
| 344 |
return composer(Flowable.range(t1 * 10, 2), subscriptionCount, m) |
| 345 |
.subscribeOn(Schedulers.computation()); |
| 346 |
} |
| 347 |
}, m); |
| 348 |
|
| 349 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 350 |
|
| 351 |
source.subscribe(ts); |
| 352 |
|
| 353 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 354 |
ts.assertNoErrors(); |
| 355 |
Set<Integer> expected = new HashSet<>(Arrays.asList( |
| 356 |
10, 11, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61, 70, 71, 80, 81, 90, 91, 100, 101 |
| 357 |
)); |
| 358 |
Assert.assertEquals(expected.size(), ts.values().size()); |
| 359 |
Assert.assertTrue(expected.containsAll(ts.values())); |
| 360 |
} |
| 361 |
|
| 362 |
@Test |
| 363 |
public void flatMapSelectorMaxConcurrent() { |
| 364 |
final int m = 4; |
| 365 |
final AtomicInteger subscriptionCount = new AtomicInteger(); |
| 366 |
Flowable<Integer> source = Flowable.range(1, 10) |
| 367 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 368 |
@Override |
| 369 |
public Flowable<Integer> apply(Integer t1) { |
| 370 |
return composer(Flowable.range(t1 * 10, 2), subscriptionCount, m) |
| 371 |
.subscribeOn(Schedulers.computation()); |
| 372 |
} |
| 373 |
}, new BiFunction<Integer, Integer, Integer>() { |
| 374 |
@Override |
| 375 |
public Integer apply(Integer t1, Integer t2) { |
| 376 |
return t1 * 1000 + t2; |
| 377 |
} |
| 378 |
}, m); |
| 379 |
|
| 380 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 381 |
|
| 382 |
source.subscribe(ts); |
| 383 |
|
| 384 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 385 |
ts.assertNoErrors(); |
| 386 |
Set<Integer> expected = new HashSet<>(Arrays.asList( |
| 387 |
1010, 1011, 2020, 2021, 3030, 3031, 4040, 4041, 5050, 5051, |
| 388 |
6060, 6061, 7070, 7071, 8080, 8081, 9090, 9091, 10100, 10101 |
| 389 |
)); |
| 390 |
Assert.assertEquals(expected.size(), ts.values().size()); |
| 391 |
System.out.println("--> testFlatMapSelectorMaxConcurrent: " + ts.values()); |
| 392 |
Assert.assertTrue(expected.containsAll(ts.values())); |
| 393 |
} |
| 394 |
|
| 395 |
@Test |
| 396 |
public void flatMapTransformsMaxConcurrentNormalLoop() { |
| 397 |
for (int i = 0; i < 1000; i++) { |
| 398 |
if (i % 100 == 0) { |
| 399 |
System.out.println("testFlatMapTransformsMaxConcurrentNormalLoop => " + i); |
| 400 |
} |
| 401 |
flatMapTransformsMaxConcurrentNormal(); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
@Test |
| 406 |
public void flatMapTransformsMaxConcurrentNormal() { |
| 407 |
final int m = 2; |
| 408 |
final AtomicInteger subscriptionCount = new AtomicInteger(); |
| 409 |
Flowable<Integer> onNext = |
| 410 |
composer( |
| 411 |
Flowable.fromIterable(Arrays.asList(1, 2, 3)) |
| 412 |
.observeOn(Schedulers.computation()) |
| 413 |
, |
| 414 |
subscriptionCount, m) |
| 415 |
.subscribeOn(Schedulers.computation()) |
| 416 |
; |
| 417 |
|
| 418 |
Flowable<Integer> onComplete = composer(Flowable.fromIterable(Arrays.asList(4)), subscriptionCount, m) |
| 419 |
.subscribeOn(Schedulers.computation()); |
| 420 |
|
| 421 |
Flowable<Integer> onError = Flowable.fromIterable(Arrays.asList(5)); |
| 422 |
|
| 423 |
Flowable<Integer> source = Flowable.fromIterable(Arrays.asList(10, 20, 30)); |
| 424 |
|
| 425 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 426 |
TestSubscriberEx<Object> ts = new TestSubscriberEx<>(subscriber); |
| 427 |
|
| 428 |
Function<Integer, Flowable<Integer>> just = just(onNext); |
| 429 |
Function<Throwable, Flowable<Integer>> just2 = just(onError); |
| 430 |
Supplier<Flowable<Integer>> just0 = just0(onComplete); |
| 431 |
source.flatMap(just, just2, just0, m).subscribe(ts); |
| 432 |
|
| 433 |
ts.awaitDone(1, TimeUnit.SECONDS); |
| 434 |
ts.assertNoErrors(); |
| 435 |
ts.assertTerminated(); |
| 436 |
|
| 437 |
verify(subscriber, times(3)).onNext(1); |
| 438 |
verify(subscriber, times(3)).onNext(2); |
| 439 |
verify(subscriber, times(3)).onNext(3); |
| 440 |
verify(subscriber).onNext(4); |
| 441 |
verify(subscriber).onComplete(); |
| 442 |
|
| 443 |
verify(subscriber, never()).onNext(5); |
| 444 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 445 |
} |
| 446 |
|
| 447 |
@Test |
| 448 |
public void flatMapRangeMixedAsyncLoop() { |
| 449 |
for (int i = 0; i < 2000; i++) { |
| 450 |
if (i % 10 == 0) { |
| 451 |
System.out.println("flatMapRangeAsyncLoop > " + i); |
| 452 |
} |
| 453 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 454 |
Flowable.range(0, 1000) |
| 455 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 456 |
final Random rnd = new Random(); |
| 457 |
@Override |
| 458 |
public Flowable<Integer> apply(Integer t) { |
| 459 |
Flowable<Integer> r = Flowable.just(t); |
| 460 |
if (rnd.nextBoolean()) { |
| 461 |
r = r.hide(); |
| 462 |
} |
| 463 |
return r; |
| 464 |
} |
| 465 |
}) |
| 466 |
.observeOn(Schedulers.computation()) |
| 467 |
.subscribe(ts); |
| 468 |
|
| 469 |
ts.awaitDone(2500, TimeUnit.MILLISECONDS); |
| 470 |
if (ts.completions() == 0) { |
| 471 |
System.out.println(ts.values().size()); |
| 472 |
} |
| 473 |
ts.assertTerminated(); |
| 474 |
ts.assertNoErrors(); |
| 475 |
List<Integer> list = ts.values(); |
| 476 |
if (list.size() < 1000) { |
| 477 |
Set<Integer> set = new HashSet<>(list); |
| 478 |
for (int j = 0; j < 1000; j++) { |
| 479 |
if (!set.contains(j)) { |
| 480 |
System.out.println(j + " missing"); |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
assertEquals(1000, list.size()); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
@Test |
| 489 |
public void flatMapIntPassthruAsync() { |
| 490 |
for (int i = 0; i < 1000; i++) { |
| 491 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 492 |
|
| 493 |
Flowable.range(1, 1000).flatMap(new Function<Integer, Flowable<Integer>>() { |
| 494 |
@Override |
| 495 |
public Flowable<Integer> apply(Integer t) { |
| 496 |
return Flowable.just(1).subscribeOn(Schedulers.computation()); |
| 497 |
} |
| 498 |
}).subscribe(ts); |
| 499 |
|
| 500 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 501 |
ts.assertNoErrors(); |
| 502 |
ts.assertComplete(); |
| 503 |
ts.assertValueCount(1000); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
@Test |
| 508 |
public void flatMapTwoNestedSync() { |
| 509 |
for (final int n : new int[] { 1, 1000, 1000000 }) { |
| 510 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 511 |
|
| 512 |
Flowable.just(1, 2).flatMap(new Function<Integer, Flowable<Integer>>() { |
| 513 |
@Override |
| 514 |
public Flowable<Integer> apply(Integer t) { |
| 515 |
return Flowable.range(1, n); |
| 516 |
} |
| 517 |
}).subscribe(ts); |
| 518 |
|
| 519 |
System.out.println("flatMapTwoNestedSync >> @ " + n); |
| 520 |
ts.assertNoErrors(); |
| 521 |
ts.assertComplete(); |
| 522 |
ts.assertValueCount(n * 2); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
@Test |
| 527 |
public void justEmptyMixture() { |
| 528 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 529 |
|
| 530 |
Flowable.range(0, 4 * Flowable.bufferSize()) |
| 531 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 532 |
@Override |
| 533 |
public Flowable<Integer> apply(Integer v) { |
| 534 |
return (v & 1) == 0 ? Flowable.<Integer>empty() : Flowable.just(v); |
| 535 |
} |
| 536 |
}) |
| 537 |
.subscribe(ts); |
| 538 |
|
| 539 |
ts.assertValueCount(2 * Flowable.bufferSize()); |
| 540 |
ts.assertNoErrors(); |
| 541 |
ts.assertComplete(); |
| 542 |
|
| 543 |
int j = 1; |
| 544 |
for (Integer v : ts.values()) { |
| 545 |
Assert.assertEquals(j, v.intValue()); |
| 546 |
|
| 547 |
j += 2; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
@Test |
| 552 |
public void rangeEmptyMixture() { |
| 553 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 554 |
|
| 555 |
Flowable.range(0, 4 * Flowable.bufferSize()) |
| 556 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 557 |
@Override |
| 558 |
public Flowable<Integer> apply(Integer v) { |
| 559 |
return (v & 1) == 0 ? Flowable.<Integer>empty() : Flowable.range(v, 2); |
| 560 |
} |
| 561 |
}) |
| 562 |
.subscribe(ts); |
| 563 |
|
| 564 |
ts.assertValueCount(4 * Flowable.bufferSize()); |
| 565 |
ts.assertNoErrors(); |
| 566 |
ts.assertComplete(); |
| 567 |
|
| 568 |
int j = 1; |
| 569 |
List<Integer> list = ts.values(); |
| 570 |
for (int i = 0; i < list.size(); i += 2) { |
| 571 |
Assert.assertEquals(j, list.get(i).intValue()); |
| 572 |
Assert.assertEquals(j + 1, list.get(i + 1).intValue()); |
| 573 |
|
| 574 |
j += 2; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
@Test |
| 579 |
public void justEmptyMixtureMaxConcurrent() { |
| 580 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 581 |
|
| 582 |
Flowable.range(0, 4 * Flowable.bufferSize()) |
| 583 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 584 |
@Override |
| 585 |
public Flowable<Integer> apply(Integer v) { |
| 586 |
return (v & 1) == 0 ? Flowable.<Integer>empty() : Flowable.just(v); |
| 587 |
} |
| 588 |
}, 16) |
| 589 |
.subscribe(ts); |
| 590 |
|
| 591 |
ts.assertValueCount(2 * Flowable.bufferSize()); |
| 592 |
ts.assertNoErrors(); |
| 593 |
ts.assertComplete(); |
| 594 |
|
| 595 |
int j = 1; |
| 596 |
for (Integer v : ts.values()) { |
| 597 |
Assert.assertEquals(j, v.intValue()); |
| 598 |
|
| 599 |
j += 2; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
@Test |
| 604 |
public void rangeEmptyMixtureMaxConcurrent() { |
| 605 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 606 |
|
| 607 |
Flowable.range(0, 4 * Flowable.bufferSize()) |
| 608 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 609 |
@Override |
| 610 |
public Flowable<Integer> apply(Integer v) { |
| 611 |
return (v & 1) == 0 ? Flowable.<Integer>empty() : Flowable.range(v, 2); |
| 612 |
} |
| 613 |
}, 16) |
| 614 |
.subscribe(ts); |
| 615 |
|
| 616 |
ts.assertValueCount(4 * Flowable.bufferSize()); |
| 617 |
ts.assertNoErrors(); |
| 618 |
ts.assertComplete(); |
| 619 |
|
| 620 |
int j = 1; |
| 621 |
List<Integer> list = ts.values(); |
| 622 |
for (int i = 0; i < list.size(); i += 2) { |
| 623 |
Assert.assertEquals(j, list.get(i).intValue()); |
| 624 |
Assert.assertEquals(j + 1, list.get(i + 1).intValue()); |
| 625 |
|
| 626 |
j += 2; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
@Test |
| 631 |
public void castCrashUnsubscribes() { |
| 632 |
|
| 633 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 634 |
|
| 635 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 636 |
|
| 637 |
pp.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 638 |
@Override |
| 639 |
public Publisher<Integer> apply(Integer t) { |
| 640 |
throw new TestException(); |
| 641 |
} |
| 642 |
}, new BiFunction<Integer, Integer, Integer>() { |
| 643 |
@Override |
| 644 |
public Integer apply(Integer t1, Integer t2) { |
| 645 |
return t1; |
| 646 |
} |
| 647 |
}).subscribe(ts); |
| 648 |
|
| 649 |
Assert.assertTrue("Not subscribed?", pp.hasSubscribers()); |
| 650 |
|
| 651 |
pp.onNext(1); |
| 652 |
|
| 653 |
Assert.assertFalse("Subscribed?", pp.hasSubscribers()); |
| 654 |
|
| 655 |
ts.assertError(TestException.class); |
| 656 |
} |
| 657 |
|
| 658 |
@Test |
| 659 |
public void flatMapBiMapper() { |
| 660 |
Flowable.just(1) |
| 661 |
.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 662 |
@Override |
| 663 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 664 |
return Flowable.just(v * 10); |
| 665 |
} |
| 666 |
}, new BiFunction<Integer, Integer, Integer>() { |
| 667 |
@Override |
| 668 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 669 |
return a + b; |
| 670 |
} |
| 671 |
}, true) |
| 672 |
.test() |
| 673 |
.assertResult(11); |
| 674 |
} |
| 675 |
|
| 676 |
@Test |
| 677 |
public void flatMapBiMapperWithError() { |
| 678 |
Flowable.just(1) |
| 679 |
.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 680 |
@Override |
| 681 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 682 |
return Flowable.just(v * 10).concatWith(Flowable.<Integer>error(new TestException())); |
| 683 |
} |
| 684 |
}, new BiFunction<Integer, Integer, Integer>() { |
| 685 |
@Override |
| 686 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 687 |
return a + b; |
| 688 |
} |
| 689 |
}, true) |
| 690 |
.test() |
| 691 |
.assertFailure(TestException.class, 11); |
| 692 |
} |
| 693 |
|
| 694 |
@Test |
| 695 |
public void flatMapBiMapperMaxConcurrency() { |
| 696 |
Flowable.just(1, 2) |
| 697 |
.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 698 |
@Override |
| 699 |
public Publisher<Integer> apply(Integer v) throws Exception { |
| 700 |
return Flowable.just(v * 10); |
| 701 |
} |
| 702 |
}, new BiFunction<Integer, Integer, Integer>() { |
| 703 |
@Override |
| 704 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 705 |
return a + b; |
| 706 |
} |
| 707 |
}, true, 1) |
| 708 |
.test() |
| 709 |
.assertResult(11, 22); |
| 710 |
} |
| 711 |
|
| 712 |
@Test |
| 713 |
public void flatMapEmpty() { |
| 714 |
assertSame(Flowable.empty(), Flowable.empty().flatMap(new Function<Object, Publisher<Object>>() { |
| 715 |
@Override |
| 716 |
public Publisher<Object> apply(Object v) throws Exception { |
| 717 |
return Flowable.just(v); |
| 718 |
} |
| 719 |
})); |
| 720 |
} |
| 721 |
|
| 722 |
@Test |
| 723 |
public void mergeScalar() { |
| 724 |
Flowable.merge(Flowable.just(Flowable.just(1))) |
| 725 |
.test() |
| 726 |
.assertResult(1); |
| 727 |
} |
| 728 |
|
| 729 |
@Test |
| 730 |
public void mergeScalar2() { |
| 731 |
Flowable.merge(Flowable.just(Flowable.just(1)).hide()) |
| 732 |
.test() |
| 733 |
.assertResult(1); |
| 734 |
} |
| 735 |
|
| 736 |
@Test |
| 737 |
public void mergeScalarEmpty() { |
| 738 |
Flowable.merge(Flowable.just(Flowable.empty()).hide()) |
| 739 |
.test() |
| 740 |
.assertResult(); |
| 741 |
} |
| 742 |
|
| 743 |
@Test |
| 744 |
public void mergeScalarError() { |
| 745 |
Flowable.merge(Flowable.just(Flowable.fromCallable(new Callable<Object>() { |
| 746 |
@Override |
| 747 |
public Object call() throws Exception { |
| 748 |
throw new TestException(); |
| 749 |
} |
| 750 |
})).hide()) |
| 751 |
.test() |
| 752 |
.assertFailure(TestException.class); |
| 753 |
} |
| 754 |
|
| 755 |
@Test |
| 756 |
public void scalarReentrant() { |
| 757 |
final PublishProcessor<Flowable<Integer>> pp = PublishProcessor.create(); |
| 758 |
|
| 759 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 760 |
@Override |
| 761 |
public void onNext(Integer t) { |
| 762 |
super.onNext(t); |
| 763 |
if (t == 1) { |
| 764 |
pp.onNext(Flowable.just(2)); |
| 765 |
} |
| 766 |
} |
| 767 |
}; |
| 768 |
|
| 769 |
Flowable.merge(pp) |
| 770 |
.subscribe(ts); |
| 771 |
|
| 772 |
pp.onNext(Flowable.just(1)); |
| 773 |
pp.onComplete(); |
| 774 |
|
| 775 |
ts.assertResult(1, 2); |
| 776 |
} |
| 777 |
|
| 778 |
@Test |
| 779 |
public void scalarReentrant2() { |
| 780 |
final PublishProcessor<Flowable<Integer>> pp = PublishProcessor.create(); |
| 781 |
|
| 782 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 783 |
@Override |
| 784 |
public void onNext(Integer t) { |
| 785 |
super.onNext(t); |
| 786 |
if (t == 1) { |
| 787 |
pp.onNext(Flowable.just(2)); |
| 788 |
} |
| 789 |
} |
| 790 |
}; |
| 791 |
|
| 792 |
Flowable.merge(pp, 2) |
| 793 |
.subscribe(ts); |
| 794 |
|
| 795 |
pp.onNext(Flowable.just(1)); |
| 796 |
pp.onComplete(); |
| 797 |
|
| 798 |
ts.assertResult(1, 2); |
| 799 |
} |
| 800 |
|
| 801 |
@Test |
| 802 |
public void innerCompleteCancelRace() { |
| 803 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 804 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 805 |
|
| 806 |
final TestSubscriber<Integer> ts = Flowable.merge(Flowable.just(pp)).test(); |
| 807 |
|
| 808 |
Runnable r1 = new Runnable() { |
| 809 |
@Override |
| 810 |
public void run() { |
| 811 |
pp.onComplete(); |
| 812 |
} |
| 813 |
}; |
| 814 |
|
| 815 |
Runnable r2 = new Runnable() { |
| 816 |
@Override |
| 817 |
public void run() { |
| 818 |
ts.cancel(); |
| 819 |
} |
| 820 |
}; |
| 821 |
|
| 822 |
TestHelper.race(r1, r2); |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
@Test |
| 827 |
public void fusedInnerThrows() { |
| 828 |
Flowable.just(1).hide() |
| 829 |
.flatMap(new Function<Integer, Flowable<Object>>() { |
| 830 |
@Override |
| 831 |
public Flowable<Object> apply(Integer v) throws Exception { |
| 832 |
return Flowable.range(1, 2).map(new Function<Integer, Object>() { |
| 833 |
@Override |
| 834 |
public Object apply(Integer w) throws Exception { |
| 835 |
throw new TestException(); |
| 836 |
} |
| 837 |
}); |
| 838 |
} |
| 839 |
}) |
| 840 |
.test() |
| 841 |
.assertFailure(TestException.class); |
| 842 |
} |
| 843 |
|
| 844 |
@Test |
| 845 |
public void fusedInnerThrows2() { |
| 846 |
TestSubscriberEx<Integer> ts = Flowable.range(1, 2).hide() |
| 847 |
.flatMap(new Function<Integer, Flowable<Integer>>() { |
| 848 |
@Override |
| 849 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 850 |
return Flowable.range(1, 2).map(new Function<Integer, Integer>() { |
| 851 |
@Override |
| 852 |
public Integer apply(Integer w) throws Exception { |
| 853 |
throw new TestException(); |
| 854 |
} |
| 855 |
}); |
| 856 |
} |
| 857 |
}, true) |
| 858 |
.to(TestHelper.<Integer>testConsumer()) |
| 859 |
.assertFailure(CompositeException.class); |
| 860 |
|
| 861 |
List<Throwable> errors = TestHelper.errorList(ts); |
| 862 |
|
| 863 |
TestHelper.assertError(errors, 0, TestException.class); |
| 864 |
|
| 865 |
TestHelper.assertError(errors, 1, TestException.class); |
| 866 |
} |
| 867 |
|
| 868 |
@Test |
| 869 |
public void scalarXMap() { |
| 870 |
Flowable.fromCallable(Functions.justCallable(1)) |
| 871 |
.flatMap(Functions.justFunction(Flowable.fromCallable(Functions.justCallable(2)))) |
| 872 |
.test() |
| 873 |
.assertResult(2); |
| 874 |
} |
| 875 |
|
| 876 |
@Test |
| 877 |
public void noCrossBoundaryFusion() { |
| 878 |
for (int i = 0; i < 500; i++) { |
| 879 |
TestSubscriber<Object> ts = Flowable.merge( |
| 880 |
Flowable.just(1).observeOn(Schedulers.single()).map(new Function<Integer, Object>() { |
| 881 |
@Override |
| 882 |
public Object apply(Integer v) throws Exception { |
| 883 |
return Thread.currentThread().getName().substring(0, 4); |
| 884 |
} |
| 885 |
}), |
| 886 |
Flowable.just(1).observeOn(Schedulers.computation()).map(new Function<Integer, Object>() { |
| 887 |
@Override |
| 888 |
public Object apply(Integer v) throws Exception { |
| 889 |
return Thread.currentThread().getName().substring(0, 4); |
| 890 |
} |
| 891 |
}) |
| 892 |
) |
| 893 |
.test() |
| 894 |
.awaitDone(5, TimeUnit.SECONDS) |
| 895 |
.assertValueCount(2); |
| 896 |
|
| 897 |
List<Object> list = ts.values(); |
| 898 |
|
| 899 |
assertTrue(list.toString(), list.contains("RxSi")); |
| 900 |
assertTrue(list.toString(), list.contains("RxCo")); |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
@Test |
| 905 |
public void cancelScalarDrainRace() { |
| 906 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 907 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 908 |
try { |
| 909 |
|
| 910 |
final PublishProcessor<Flowable<Integer>> pp = PublishProcessor.create(); |
| 911 |
|
| 912 |
final TestSubscriber<Integer> ts = pp.flatMap(Functions.<Flowable<Integer>>identity()).test(0); |
| 913 |
|
| 914 |
Runnable r1 = new Runnable() { |
| 915 |
@Override |
| 916 |
public void run() { |
| 917 |
ts.cancel(); |
| 918 |
} |
| 919 |
}; |
| 920 |
Runnable r2 = new Runnable() { |
| 921 |
@Override |
| 922 |
public void run() { |
| 923 |
pp.onComplete(); |
| 924 |
} |
| 925 |
}; |
| 926 |
|
| 927 |
TestHelper.race(r1, r2); |
| 928 |
|
| 929 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 930 |
} finally { |
| 931 |
RxJavaPlugins.reset(); |
| 932 |
} |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
@Test |
| 937 |
public void cancelDrainRace() { |
| 938 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 939 |
for (int j = 1; j < 50; j += 5) { |
| 940 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 941 |
try { |
| 942 |
|
| 943 |
final PublishProcessor<Flowable<Integer>> pp = PublishProcessor.create(); |
| 944 |
|
| 945 |
final TestSubscriber<Integer> ts = pp.flatMap(Functions.<Flowable<Integer>>identity()).test(0); |
| 946 |
|
| 947 |
final PublishProcessor<Integer> just = PublishProcessor.create(); |
| 948 |
pp.onNext(just); |
| 949 |
|
| 950 |
Runnable r1 = new Runnable() { |
| 951 |
@Override |
| 952 |
public void run() { |
| 953 |
ts.request(1); |
| 954 |
ts.cancel(); |
| 955 |
} |
| 956 |
}; |
| 957 |
Runnable r2 = new Runnable() { |
| 958 |
@Override |
| 959 |
public void run() { |
| 960 |
just.onNext(1); |
| 961 |
} |
| 962 |
}; |
| 963 |
|
| 964 |
TestHelper.race(r1, r2); |
| 965 |
|
| 966 |
assertTrue(errors.toString(), errors.isEmpty()); |
| 967 |
} finally { |
| 968 |
RxJavaPlugins.reset(); |
| 969 |
} |
| 970 |
} |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
@Test |
| 975 |
public void iterableMapperFunctionReturnsNull() { |
| 976 |
Flowable.just(1) |
| 977 |
.flatMapIterable(new Function<Integer, Iterable<Object>>() { |
| 978 |
@Override |
| 979 |
public Iterable<Object> apply(Integer v) throws Exception { |
| 980 |
return null; |
| 981 |
} |
| 982 |
}, new BiFunction<Integer, Object, Object>() { |
| 983 |
@Override |
| 984 |
public Object apply(Integer v, Object w) throws Exception { |
| 985 |
return v; |
| 986 |
} |
| 987 |
}) |
| 988 |
.to(TestHelper.<Object>testConsumer()) |
| 989 |
.assertFailureAndMessage(NullPointerException.class, "The mapper returned a null Iterable"); |
| 990 |
} |
| 991 |
|
| 992 |
@Test |
| 993 |
public void combinerMapperFunctionReturnsNull() { |
| 994 |
Flowable.just(1) |
| 995 |
.flatMap(new Function<Integer, Publisher<Object>>() { |
| 996 |
@Override |
| 997 |
public Publisher<Object> apply(Integer v) throws Exception { |
| 998 |
return null; |
| 999 |
} |
| 1000 |
}, new BiFunction<Integer, Object, Object>() { |
| 1001 |
@Override |
| 1002 |
public Object apply(Integer v, Object w) throws Exception { |
| 1003 |
return v; |
| 1004 |
} |
| 1005 |
}) |
| 1006 |
.to(TestHelper.<Object>testConsumer()) |
| 1007 |
.assertFailureAndMessage(NullPointerException.class, "The mapper returned a null Publisher"); |
| 1008 |
} |
| 1009 |
|
| 1010 |
@Test |
| 1011 |
public void failingFusedInnerCancelsSource() { |
| 1012 |
final AtomicInteger counter = new AtomicInteger(); |
| 1013 |
Flowable.range(1, 5) |
| 1014 |
.doOnNext(new Consumer<Integer>() { |
| 1015 |
@Override |
| 1016 |
public void accept(Integer v) throws Exception { |
| 1017 |
counter.getAndIncrement(); |
| 1018 |
} |
| 1019 |
}) |
| 1020 |
.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 1021 |
@Override |
| 1022 |
public Publisher<Integer> apply(Integer v) |
| 1023 |
throws Exception { |
| 1024 |
return Flowable.<Integer>fromIterable(new Iterable<Integer>() { |
| 1025 |
@Override |
| 1026 |
public Iterator<Integer> iterator() { |
| 1027 |
return new Iterator<Integer>() { |
| 1028 |
@Override |
| 1029 |
public boolean hasNext() { |
| 1030 |
return true; |
| 1031 |
} |
| 1032 |
|
| 1033 |
@Override |
| 1034 |
public Integer next() { |
| 1035 |
throw new TestException(); |
| 1036 |
} |
| 1037 |
|
| 1038 |
@Override |
| 1039 |
public void remove() { |
| 1040 |
throw new UnsupportedOperationException(); |
| 1041 |
} |
| 1042 |
}; |
| 1043 |
} |
| 1044 |
}); |
| 1045 |
} |
| 1046 |
}) |
| 1047 |
.test() |
| 1048 |
.assertFailure(TestException.class); |
| 1049 |
|
| 1050 |
assertEquals(1, counter.get()); |
| 1051 |
} |
| 1052 |
|
| 1053 |
@Test |
| 1054 |
public void maxConcurrencySustained() { |
| 1055 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1056 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1057 |
PublishProcessor<Integer> pp3 = PublishProcessor.create(); |
| 1058 |
PublishProcessor<Integer> pp4 = PublishProcessor.create(); |
| 1059 |
|
| 1060 |
TestSubscriber<Integer> ts = Flowable.just(pp1, pp2, pp3, pp4) |
| 1061 |
.flatMap(new Function<PublishProcessor<Integer>, Flowable<Integer>>() { |
| 1062 |
@Override |
| 1063 |
public Flowable<Integer> apply(PublishProcessor<Integer> v) throws Exception { |
| 1064 |
return v; |
| 1065 |
} |
| 1066 |
}, 2) |
| 1067 |
.doOnNext(new Consumer<Integer>() { |
| 1068 |
@Override |
| 1069 |
public void accept(Integer v) throws Exception { |
| 1070 |
if (v == 1) { |
| 1071 |
// this will make sure the drain loop detects two completed |
| 1072 |
// inner sources and replaces them with fresh ones |
| 1073 |
pp1.onComplete(); |
| 1074 |
pp2.onComplete(); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
}) |
| 1078 |
.test(); |
| 1079 |
|
| 1080 |
pp1.onNext(1); |
| 1081 |
|
| 1082 |
assertFalse(pp1.hasSubscribers()); |
| 1083 |
assertFalse(pp2.hasSubscribers()); |
| 1084 |
assertTrue(pp3.hasSubscribers()); |
| 1085 |
assertTrue(pp4.hasSubscribers()); |
| 1086 |
|
| 1087 |
ts.cancel(); |
| 1088 |
|
| 1089 |
assertFalse(pp3.hasSubscribers()); |
| 1090 |
assertFalse(pp4.hasSubscribers()); |
| 1091 |
} |
| 1092 |
|
| 1093 |
@Test |
| 1094 |
public void undeliverableUponCancel() { |
| 1095 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 1096 |
@Override |
| 1097 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 1098 |
return upstream.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 1099 |
@Override |
| 1100 |
public Publisher<Integer> apply(Integer v) throws Throwable { |
| 1101 |
return Flowable.just(v).hide(); |
| 1102 |
} |
| 1103 |
}); |
| 1104 |
} |
| 1105 |
}); |
| 1106 |
} |
| 1107 |
|
| 1108 |
@Test |
| 1109 |
public void undeliverableUponCancelDelayError() { |
| 1110 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 1111 |
@Override |
| 1112 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 1113 |
return upstream.flatMap(new Function<Integer, Publisher<Integer>>() { |
| 1114 |
@Override |
| 1115 |
public Publisher<Integer> apply(Integer v) throws Throwable { |
| 1116 |
return Flowable.just(v).hide(); |
| 1117 |
} |
| 1118 |
}, true); |
| 1119 |
} |
| 1120 |
}); |
| 1121 |
} |
| 1122 |
|
| 1123 |
@Test |
| 1124 |
public void mainErrorsInnerCancelled() { |
| 1125 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1126 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1127 |
|
| 1128 |
pp1 |
| 1129 |
.flatMap(v -> pp2) |
| 1130 |
.test(); |
| 1131 |
|
| 1132 |
pp1.onNext(1); |
| 1133 |
assertTrue("No subscribers?", pp2.hasSubscribers()); |
| 1134 |
|
| 1135 |
pp1.onError(new TestException()); |
| 1136 |
|
| 1137 |
assertFalse("Has subscribers?", pp2.hasSubscribers()); |
| 1138 |
} |
| 1139 |
|
| 1140 |
@Test |
| 1141 |
public void innerErrorsMainCancelled() { |
| 1142 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 1143 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 1144 |
|
| 1145 |
pp1 |
| 1146 |
.flatMap(v -> pp2) |
| 1147 |
.test(); |
| 1148 |
|
| 1149 |
pp1.onNext(1); |
| 1150 |
assertTrue("No subscribers?", pp2.hasSubscribers()); |
| 1151 |
|
| 1152 |
pp2.onError(new TestException()); |
| 1153 |
|
| 1154 |
assertFalse("Has subscribers?", pp1.hasSubscribers()); |
| 1155 |
} |
| 1156 |
|
| 1157 |
@Test |
| 1158 |
public void innerIsDisposed() { |
| 1159 |
FlowableFlatMap.InnerSubscriber<Integer, Integer> inner = new FlowableFlatMap.InnerSubscriber<>(null, 10, 0L); |
| 1160 |
|
| 1161 |
assertFalse(inner.isDisposed()); |
| 1162 |
|
| 1163 |
inner.dispose(); |
| 1164 |
|
| 1165 |
assertTrue(inner.isDisposed()); |
| 1166 |
} |
| 1167 |
|
| 1168 |
@Test |
| 1169 |
public void badRequest() { |
| 1170 |
TestHelper.assertBadRequestReported(Flowable.never().flatMap(v -> Flowable.never())); |
| 1171 |
} |
| 1172 |
|
| 1173 |
@Test |
| 1174 |
public void signalsAfterMapperCrash() throws Throwable { |
| 1175 |
TestHelper.withErrorTracking(errors -> { |
| 1176 |
new Flowable<Integer>() { |
| 1177 |
@Override |
| 1178 |
protected void subscribeActual(@NonNull Subscriber<? super @NonNull Integer> s) { |
| 1179 |
s.onSubscribe(new BooleanSubscription()); |
| 1180 |
s.onNext(1); |
| 1181 |
s.onNext(2); |
| 1182 |
s.onComplete(); |
| 1183 |
s.onError(new IOException()); |
| 1184 |
} |
| 1185 |
} |
| 1186 |
.flatMap(v -> { |
| 1187 |
throw new TestException(); |
| 1188 |
}) |
| 1189 |
.test() |
| 1190 |
.assertFailure(TestException.class); |
| 1191 |
|
| 1192 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 1193 |
}); |
| 1194 |
} |
| 1195 |
|
| 1196 |
@Test |
| 1197 |
public void scalarQueueTerminate() { |
| 1198 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1199 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1200 |
|
| 1201 |
pp |
| 1202 |
.flatMap(v -> Flowable.just(v)) |
| 1203 |
.doOnNext(v -> { |
| 1204 |
if (v == 1) { |
| 1205 |
pp.onNext(2); |
| 1206 |
pp.onNext(3); |
| 1207 |
} |
| 1208 |
}) |
| 1209 |
.take(2) |
| 1210 |
.subscribe(ts); |
| 1211 |
|
| 1212 |
pp.onNext(1); |
| 1213 |
|
| 1214 |
ts.assertResult(1, 2); |
| 1215 |
} |
| 1216 |
|
| 1217 |
@Test |
| 1218 |
public void scalarQueueCompleteMain() throws Exception { |
| 1219 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1220 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1221 |
CountDownLatch cdl = new CountDownLatch(1); |
| 1222 |
pp |
| 1223 |
.flatMap(v -> Flowable.just(v)) |
| 1224 |
.doOnNext(v -> { |
| 1225 |
if (v == 1) { |
| 1226 |
pp.onNext(2); |
| 1227 |
TestHelper.raceOther(() -> pp.onComplete(), cdl); |
| 1228 |
} |
| 1229 |
}) |
| 1230 |
.subscribe(ts); |
| 1231 |
|
| 1232 |
pp.onNext(1); |
| 1233 |
|
| 1234 |
cdl.await(); |
| 1235 |
ts.assertResult(1, 2); |
| 1236 |
} |
| 1237 |
|
| 1238 |
@Test |
| 1239 |
public void fusedInnerCrash() { |
| 1240 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1241 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1242 |
|
| 1243 |
TestSubscriber<Integer> ts = Flowable.just( |
| 1244 |
pp, |
| 1245 |
up.map(v -> { |
| 1246 |
if (v == 10) { |
| 1247 |
throw new TestException(); |
| 1248 |
} |
| 1249 |
return v; |
| 1250 |
}) |
| 1251 |
.compose(TestHelper.flowableStripBoundary()) |
| 1252 |
) |
| 1253 |
.flatMap(v -> v, true) |
| 1254 |
.doOnNext(v -> { |
| 1255 |
if (v == 1) { |
| 1256 |
pp.onNext(2); |
| 1257 |
up.onNext(10); |
| 1258 |
} |
| 1259 |
}) |
| 1260 |
.test(); |
| 1261 |
|
| 1262 |
pp.onNext(1); |
| 1263 |
pp.onComplete(); |
| 1264 |
|
| 1265 |
ts.assertFailure(TestException.class, 1, 2); |
| 1266 |
} |
| 1267 |
|
| 1268 |
@Test |
| 1269 |
public void fusedInnerCrash2() { |
| 1270 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1271 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1272 |
|
| 1273 |
TestSubscriber<Integer> ts = Flowable.just( |
| 1274 |
up.map(v -> { |
| 1275 |
if (v == 10) { |
| 1276 |
throw new TestException(); |
| 1277 |
} |
| 1278 |
return v; |
| 1279 |
}) |
| 1280 |
.compose(TestHelper.flowableStripBoundary()) |
| 1281 |
, pp |
| 1282 |
) |
| 1283 |
.flatMap(v -> v, true) |
| 1284 |
.doOnNext(v -> { |
| 1285 |
if (v == 1) { |
| 1286 |
pp.onNext(2); |
| 1287 |
up.onNext(10); |
| 1288 |
} |
| 1289 |
}) |
| 1290 |
.test(); |
| 1291 |
|
| 1292 |
pp.onNext(1); |
| 1293 |
pp.onComplete(); |
| 1294 |
|
| 1295 |
ts.assertFailure(TestException.class, 1, 2); |
| 1296 |
} |
| 1297 |
|
| 1298 |
@Test |
| 1299 |
public void doubleOnSubscribe() { |
| 1300 |
TestHelper.checkDoubleOnSubscribeFlowable(f -> f.flatMap(v -> Flowable.never())); |
| 1301 |
} |
| 1302 |
|
| 1303 |
@Test |
| 1304 |
public void allConcurrency() { |
| 1305 |
Flowable.just(1) |
| 1306 |
.hide() |
| 1307 |
.flatMap(v -> Flowable.just(2).hide(), Integer.MAX_VALUE) |
| 1308 |
.test() |
| 1309 |
.assertResult(2); |
| 1310 |
} |
| 1311 |
|
| 1312 |
@Test |
| 1313 |
public void allConcurrencyScalarInner() { |
| 1314 |
Flowable.just(1) |
| 1315 |
.hide() |
| 1316 |
.flatMap(v -> Flowable.just(2), Integer.MAX_VALUE) |
| 1317 |
.test() |
| 1318 |
.assertResult(2); |
| 1319 |
} |
| 1320 |
|
| 1321 |
@Test |
| 1322 |
public void allConcurrencyScalarInnerEmpty() { |
| 1323 |
Flowable.just(1) |
| 1324 |
.hide() |
| 1325 |
.flatMap(v -> Flowable.empty(), Integer.MAX_VALUE) |
| 1326 |
.test() |
| 1327 |
.assertResult(); |
| 1328 |
} |
| 1329 |
|
| 1330 |
static final class ScalarEmptyCancel extends Flowable<Integer> implements Supplier<Integer> { |
| 1331 |
final TestSubscriber<?> ts; |
| 1332 |
|
| 1333 |
ScalarEmptyCancel(TestSubscriber<?> ts) { |
| 1334 |
this.ts = ts; |
| 1335 |
} |
| 1336 |
|
| 1337 |
@Override |
| 1338 |
public @NonNull Integer get() throws Throwable { |
| 1339 |
ts.cancel(); |
| 1340 |
return null; |
| 1341 |
} |
| 1342 |
|
| 1343 |
@Override |
| 1344 |
protected void subscribeActual(@NonNull Subscriber<@NonNull ? super @NonNull Integer> subscriber) { |
| 1345 |
EmptySubscription.complete(subscriber); |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
@Test |
| 1350 |
public void someConcurrencyScalarInnerCancel() { |
| 1351 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1352 |
|
| 1353 |
Flowable.just(1) |
| 1354 |
.hide() |
| 1355 |
.flatMap(v -> new ScalarEmptyCancel(ts)) |
| 1356 |
.subscribeWith(ts) |
| 1357 |
.assertEmpty(); |
| 1358 |
} |
| 1359 |
|
| 1360 |
@Test |
| 1361 |
public void allConcurrencyBackpressured() { |
| 1362 |
Flowable.just(1) |
| 1363 |
.hide() |
| 1364 |
.flatMap(v -> Flowable.just(2), Integer.MAX_VALUE) |
| 1365 |
.test(0L) |
| 1366 |
.assertEmpty() |
| 1367 |
.requestMore(1) |
| 1368 |
.assertResult(2); |
| 1369 |
} |
| 1370 |
|
| 1371 |
@Test |
| 1372 |
public void someConcurrencyInnerScalarCancel() { |
| 1373 |
Flowable.just(1) |
| 1374 |
.hide() |
| 1375 |
.flatMap(v -> Flowable.just(2), 2) |
| 1376 |
.takeUntil(v -> true) |
| 1377 |
.test() |
| 1378 |
.assertResult(2); |
| 1379 |
} |
| 1380 |
|
| 1381 |
@Test |
| 1382 |
public void scalarInnerOuterOverflow() { |
| 1383 |
new Flowable<Integer>() { |
| 1384 |
@Override |
| 1385 |
protected void subscribeActual(@NonNull Subscriber<@NonNull ? super @NonNull Integer> subscriber) { |
| 1386 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1387 |
subscriber.onNext(1); |
| 1388 |
subscriber.onNext(2); |
| 1389 |
subscriber.onNext(3); |
| 1390 |
} |
| 1391 |
} |
| 1392 |
.flatMap(v -> Flowable.just(v), 1) |
| 1393 |
.test(0L) |
| 1394 |
.assertFailure(MissingBackpressureException.class); |
| 1395 |
} |
| 1396 |
|
| 1397 |
@Test |
| 1398 |
public void scalarInnerOuterOverflowSlowPath() { |
| 1399 |
AtomicReference<Subscriber<? super Integer>> ref = new AtomicReference<>(); |
| 1400 |
new Flowable<Integer>() { |
| 1401 |
@Override |
| 1402 |
protected void subscribeActual(@NonNull Subscriber<@NonNull ? super @NonNull Integer> subscriber) { |
| 1403 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1404 |
ref.set(subscriber); |
| 1405 |
subscriber.onNext(1); |
| 1406 |
} |
| 1407 |
} |
| 1408 |
.flatMap(v -> Flowable.just(v), 1) |
| 1409 |
.doOnNext(v -> { |
| 1410 |
if (v == 1) { |
| 1411 |
ref.get().onNext(2); |
| 1412 |
ref.get().onNext(3); |
| 1413 |
} |
| 1414 |
}) |
| 1415 |
.test() |
| 1416 |
.assertFailure(MissingBackpressureException.class, 1); |
| 1417 |
} |
| 1418 |
|
| 1419 |
@Test |
| 1420 |
public void innerFastPathEmitOverflow() { |
| 1421 |
Flowable.just(1) |
| 1422 |
.hide() |
| 1423 |
.flatMap(v -> new Flowable<Integer>() { |
| 1424 |
@Override |
| 1425 |
protected void subscribeActual(@NonNull Subscriber<@NonNull ? super @NonNull Integer> subscriber) { |
| 1426 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1427 |
subscriber.onNext(1); |
| 1428 |
subscriber.onNext(2); |
| 1429 |
subscriber.onNext(3); |
| 1430 |
} |
| 1431 |
}, false, 1, 1) |
| 1432 |
.test(0L) |
| 1433 |
.assertFailure(MissingBackpressureException.class); |
| 1434 |
} |
| 1435 |
|
| 1436 |
@Test |
| 1437 |
public void takeFromScalarQueue() { |
| 1438 |
Flowable.just(1) |
| 1439 |
.hide() |
| 1440 |
.flatMap(v -> Flowable.just(2), 2) |
| 1441 |
.takeUntil(v -> true) |
| 1442 |
.test(0L) |
| 1443 |
.requestMore(2) |
| 1444 |
.assertResult(2); |
| 1445 |
} |
| 1446 |
|
| 1447 |
@Test |
| 1448 |
public void scalarInnerQueueEmpty() { |
| 1449 |
Flowable.just(1) |
| 1450 |
.concatWith(Flowable.never()) |
| 1451 |
.hide() |
| 1452 |
.flatMap(v -> Flowable.just(2), 2) |
| 1453 |
.test(0L) |
| 1454 |
.requestMore(2) |
| 1455 |
.assertValuesOnly(2); |
| 1456 |
} |
| 1457 |
|
| 1458 |
@Test |
| 1459 |
public void innerCompletesAfterOnNextInDrainThenCancels() { |
| 1460 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1461 |
|
| 1462 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 1463 |
|
| 1464 |
Flowable.just(1) |
| 1465 |
.hide() |
| 1466 |
.flatMap(v -> pp) |
| 1467 |
.doOnNext(v -> { |
| 1468 |
if (v == 1) { |
| 1469 |
pp.onComplete(); |
| 1470 |
ts.cancel(); |
| 1471 |
} |
| 1472 |
}) |
| 1473 |
.subscribe(ts); |
| 1474 |
|
| 1475 |
pp.onNext(1); |
| 1476 |
|
| 1477 |
ts |
| 1478 |
.requestMore(1) |
| 1479 |
.assertValuesOnly(1); |
| 1480 |
} |
| 1481 |
|
| 1482 |
@Test(timeout = 5000) |
| 1483 |
public void mixedScalarAsync() { |
| 1484 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1485 |
Flowable |
| 1486 |
.range(0, 20) |
| 1487 |
.flatMap( |
| 1488 |
integer -> { |
| 1489 |
if (integer % 5 != 0) { |
| 1490 |
return Flowable |
| 1491 |
.just(integer); |
| 1492 |
} |
| 1493 |
|
| 1494 |
return Flowable |
| 1495 |
.just(-integer) |
| 1496 |
.observeOn(Schedulers.computation()); |
| 1497 |
}, |
| 1498 |
false, |
| 1499 |
1 |
| 1500 |
) |
| 1501 |
.ignoreElements() |
| 1502 |
.blockingAwait(); |
| 1503 |
} |
| 1504 |
} |
| 1505 |
} |