| 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.List; |
| 21 |
import java.util.concurrent.atomic.AtomicInteger; |
| 22 |
|
| 23 |
import org.junit.*; |
| 24 |
import org.reactivestreams.*; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.internal.fuseable.HasUpstreamPublisher; |
| 30 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 31 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 32 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 33 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 34 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 35 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 36 |
|
| 37 |
public class FlowableReduceTest extends RxJavaTest { |
| 38 |
Subscriber<Object> subscriber; |
| 39 |
|
| 40 |
SingleObserver<Object> singleObserver; |
| 41 |
|
| 42 |
@Before |
| 43 |
public void before() { |
| 44 |
subscriber = TestHelper.mockSubscriber(); |
| 45 |
singleObserver = TestHelper.mockSingleObserver(); |
| 46 |
} |
| 47 |
|
| 48 |
BiFunction<Integer, Integer, Integer> sum = new BiFunction<Integer, Integer, Integer>() { |
| 49 |
@Override |
| 50 |
public Integer apply(Integer t1, Integer t2) { |
| 51 |
return t1 + t2; |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
@Test |
| 56 |
public void aggregateAsIntSumFlowable() { |
| 57 |
|
| 58 |
Flowable<Integer> result = Flowable.just(1, 2, 3, 4, 5).reduce(0, sum).toFlowable() |
| 59 |
.map(new Function<Integer, Integer>() { |
| 60 |
@Override |
| 61 |
public Integer apply(Integer v) { |
| 62 |
return v; |
| 63 |
} |
| 64 |
}); |
| 65 |
|
| 66 |
result.subscribe(subscriber); |
| 67 |
|
| 68 |
verify(subscriber).onNext(1 + 2 + 3 + 4 + 5); |
| 69 |
verify(subscriber).onComplete(); |
| 70 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void aggregateAsIntSumSourceThrowsFlowable() { |
| 75 |
Flowable<Integer> result = Flowable.concat(Flowable.just(1, 2, 3, 4, 5), |
| 76 |
Flowable.<Integer> error(new TestException())) |
| 77 |
.reduce(0, sum).toFlowable().map(new Function<Integer, Integer>() { |
| 78 |
@Override |
| 79 |
public Integer apply(Integer v) { |
| 80 |
return v; |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
result.subscribe(subscriber); |
| 85 |
|
| 86 |
verify(subscriber, never()).onNext(any()); |
| 87 |
verify(subscriber, never()).onComplete(); |
| 88 |
verify(subscriber, times(1)).onError(any(TestException.class)); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void aggregateAsIntSumAccumulatorThrowsFlowable() { |
| 93 |
BiFunction<Integer, Integer, Integer> sumErr = new BiFunction<Integer, Integer, Integer>() { |
| 94 |
@Override |
| 95 |
public Integer apply(Integer t1, Integer t2) { |
| 96 |
throw new TestException(); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
Flowable<Integer> result = Flowable.just(1, 2, 3, 4, 5) |
| 101 |
.reduce(0, sumErr).toFlowable().map(new Function<Integer, Integer>() { |
| 102 |
@Override |
| 103 |
public Integer apply(Integer v) { |
| 104 |
return v; |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
result.subscribe(subscriber); |
| 109 |
|
| 110 |
verify(subscriber, never()).onNext(any()); |
| 111 |
verify(subscriber, never()).onComplete(); |
| 112 |
verify(subscriber, times(1)).onError(any(TestException.class)); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void aggregateAsIntSumResultSelectorThrowsFlowable() { |
| 117 |
|
| 118 |
Function<Integer, Integer> error = new Function<Integer, Integer>() { |
| 119 |
|
| 120 |
@Override |
| 121 |
public Integer apply(Integer t1) { |
| 122 |
throw new TestException(); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
Flowable<Integer> result = Flowable.just(1, 2, 3, 4, 5) |
| 127 |
.reduce(0, sum).toFlowable().map(error); |
| 128 |
|
| 129 |
result.subscribe(subscriber); |
| 130 |
|
| 131 |
verify(subscriber, never()).onNext(any()); |
| 132 |
verify(subscriber, never()).onComplete(); |
| 133 |
verify(subscriber, times(1)).onError(any(TestException.class)); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void backpressureWithInitialValueFlowable() throws InterruptedException { |
| 138 |
Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6); |
| 139 |
Flowable<Integer> reduced = source.reduce(0, sum).toFlowable(); |
| 140 |
|
| 141 |
Integer r = reduced.blockingFirst(); |
| 142 |
assertEquals(21, r.intValue()); |
| 143 |
} |
| 144 |
|
| 145 |
@Test |
| 146 |
public void aggregateAsIntSum() { |
| 147 |
|
| 148 |
Single<Integer> result = Flowable.just(1, 2, 3, 4, 5).reduce(0, sum) |
| 149 |
.map(new Function<Integer, Integer>() { |
| 150 |
@Override |
| 151 |
public Integer apply(Integer v) { |
| 152 |
return v; |
| 153 |
} |
| 154 |
}); |
| 155 |
|
| 156 |
result.subscribe(singleObserver); |
| 157 |
|
| 158 |
verify(singleObserver).onSuccess(1 + 2 + 3 + 4 + 5); |
| 159 |
verify(singleObserver, never()).onError(any(Throwable.class)); |
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void aggregateAsIntSumSourceThrows() { |
| 164 |
Single<Integer> result = Flowable.concat(Flowable.just(1, 2, 3, 4, 5), |
| 165 |
Flowable.<Integer> error(new TestException())) |
| 166 |
.reduce(0, sum).map(new Function<Integer, Integer>() { |
| 167 |
@Override |
| 168 |
public Integer apply(Integer v) { |
| 169 |
return v; |
| 170 |
} |
| 171 |
}); |
| 172 |
|
| 173 |
result.subscribe(singleObserver); |
| 174 |
|
| 175 |
verify(singleObserver, never()).onSuccess(any()); |
| 176 |
verify(singleObserver, times(1)).onError(any(TestException.class)); |
| 177 |
} |
| 178 |
|
| 179 |
@Test |
| 180 |
public void aggregateAsIntSumAccumulatorThrows() { |
| 181 |
BiFunction<Integer, Integer, Integer> sumErr = new BiFunction<Integer, Integer, Integer>() { |
| 182 |
@Override |
| 183 |
public Integer apply(Integer t1, Integer t2) { |
| 184 |
throw new TestException(); |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
Single<Integer> result = Flowable.just(1, 2, 3, 4, 5) |
| 189 |
.reduce(0, sumErr).map(new Function<Integer, Integer>() { |
| 190 |
@Override |
| 191 |
public Integer apply(Integer v) { |
| 192 |
return v; |
| 193 |
} |
| 194 |
}); |
| 195 |
|
| 196 |
result.subscribe(singleObserver); |
| 197 |
|
| 198 |
verify(singleObserver, never()).onSuccess(any()); |
| 199 |
verify(singleObserver, times(1)).onError(any(TestException.class)); |
| 200 |
} |
| 201 |
|
| 202 |
@Test |
| 203 |
public void aggregateAsIntSumResultSelectorThrows() { |
| 204 |
|
| 205 |
Function<Integer, Integer> error = new Function<Integer, Integer>() { |
| 206 |
|
| 207 |
@Override |
| 208 |
public Integer apply(Integer t1) { |
| 209 |
throw new TestException(); |
| 210 |
} |
| 211 |
}; |
| 212 |
|
| 213 |
Single<Integer> result = Flowable.just(1, 2, 3, 4, 5) |
| 214 |
.reduce(0, sum).map(error); |
| 215 |
|
| 216 |
result.subscribe(singleObserver); |
| 217 |
|
| 218 |
verify(singleObserver, never()).onSuccess(any()); |
| 219 |
verify(singleObserver, times(1)).onError(any(TestException.class)); |
| 220 |
} |
| 221 |
|
| 222 |
@Test |
| 223 |
public void backpressureWithNoInitialValue() throws InterruptedException { |
| 224 |
Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6); |
| 225 |
Maybe<Integer> reduced = source.reduce(sum); |
| 226 |
|
| 227 |
Integer r = reduced.blockingGet(); |
| 228 |
assertEquals(21, r.intValue()); |
| 229 |
} |
| 230 |
|
| 231 |
@Test |
| 232 |
public void backpressureWithInitialValue() throws InterruptedException { |
| 233 |
Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6); |
| 234 |
Single<Integer> reduced = source.reduce(0, sum); |
| 235 |
|
| 236 |
Integer r = reduced.blockingGet(); |
| 237 |
assertEquals(21, r.intValue()); |
| 238 |
} |
| 239 |
|
| 240 |
@Test |
| 241 |
public void reducerCrashSuppressOnError() { |
| 242 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 243 |
|
| 244 |
try { |
| 245 |
Flowable.<Integer>fromPublisher(new Publisher<Integer>() { |
| 246 |
@Override |
| 247 |
public void subscribe(Subscriber<? super Integer> s) { |
| 248 |
s.onSubscribe(new BooleanSubscription()); |
| 249 |
s.onNext(1); |
| 250 |
s.onNext(1); |
| 251 |
s.onError(new TestException("Source")); |
| 252 |
s.onComplete(); |
| 253 |
} |
| 254 |
}) |
| 255 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 256 |
@Override |
| 257 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 258 |
throw new TestException("Reducer"); |
| 259 |
} |
| 260 |
}) |
| 261 |
.toFlowable() |
| 262 |
.to(TestHelper.<Integer>testConsumer()) |
| 263 |
.assertFailureAndMessage(TestException.class, "Reducer"); |
| 264 |
|
| 265 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Source"); |
| 266 |
} finally { |
| 267 |
RxJavaPlugins.reset(); |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
@Test |
| 273 |
public void cancel() { |
| 274 |
|
| 275 |
TestSubscriber<Integer> ts = Flowable.just(1) |
| 276 |
.concatWith(Flowable.<Integer>never()) |
| 277 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 278 |
@Override |
| 279 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 280 |
return a + b; |
| 281 |
} |
| 282 |
}).toFlowable() |
| 283 |
.test(); |
| 284 |
|
| 285 |
ts.assertEmpty(); |
| 286 |
|
| 287 |
ts.cancel(); |
| 288 |
|
| 289 |
ts.assertEmpty(); |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
@Test |
| 294 |
public void backpressureWithNoInitialValueObservable() throws InterruptedException { |
| 295 |
Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6); |
| 296 |
Flowable<Integer> reduced = source.reduce(sum).toFlowable(); |
| 297 |
|
| 298 |
Integer r = reduced.blockingFirst(); |
| 299 |
assertEquals(21, r.intValue()); |
| 300 |
} |
| 301 |
|
| 302 |
@Test |
| 303 |
public void source() { |
| 304 |
Flowable<Integer> source = Flowable.just(1); |
| 305 |
|
| 306 |
assertSame(source, (((HasUpstreamPublisher<?>)source.reduce(sum))).source()); |
| 307 |
} |
| 308 |
|
| 309 |
@Test |
| 310 |
public void dispose() { |
| 311 |
TestHelper.checkDisposed(Flowable.range(1, 2).reduce(sum)); |
| 312 |
} |
| 313 |
|
| 314 |
@Test |
| 315 |
public void doubleOnSubscribe() { |
| 316 |
TestHelper.checkDoubleOnSubscribeFlowableToMaybe(new Function<Flowable<Integer>, MaybeSource<Integer>>() { |
| 317 |
@Override |
| 318 |
public MaybeSource<Integer> apply(Flowable<Integer> f) throws Exception { |
| 319 |
return f.reduce(sum); |
| 320 |
} |
| 321 |
}); |
| 322 |
} |
| 323 |
|
| 324 |
@Test |
| 325 |
public void error() { |
| 326 |
Flowable.<Integer>error(new TestException()) |
| 327 |
.reduce(sum) |
| 328 |
.test() |
| 329 |
.assertFailure(TestException.class); |
| 330 |
} |
| 331 |
|
| 332 |
@Test |
| 333 |
public void errorFlowable() { |
| 334 |
Flowable.<Integer>error(new TestException()) |
| 335 |
.reduce(sum) |
| 336 |
.toFlowable() |
| 337 |
.test() |
| 338 |
.assertFailure(TestException.class); |
| 339 |
} |
| 340 |
|
| 341 |
@Test |
| 342 |
public void empty() { |
| 343 |
Flowable.<Integer>empty() |
| 344 |
.reduce(sum) |
| 345 |
.test() |
| 346 |
.assertResult(); |
| 347 |
} |
| 348 |
|
| 349 |
@Test |
| 350 |
public void emptyFlowable() { |
| 351 |
Flowable.<Integer>empty() |
| 352 |
.reduce(sum) |
| 353 |
.toFlowable() |
| 354 |
.test() |
| 355 |
.assertResult(); |
| 356 |
} |
| 357 |
|
| 358 |
@Test |
| 359 |
public void badSource() { |
| 360 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 361 |
@Override |
| 362 |
public Object apply(Flowable<Integer> f) throws Exception { |
| 363 |
return f.reduce(sum); |
| 364 |
} |
| 365 |
}, false, 1, 1, 1); |
| 366 |
} |
| 367 |
|
| 368 |
@Test |
| 369 |
public void badSourceFlowable() { |
| 370 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 371 |
@Override |
| 372 |
public Object apply(Flowable<Integer> f) throws Exception { |
| 373 |
return f.reduce(sum).toFlowable(); |
| 374 |
} |
| 375 |
}, false, 1, 1, 1); |
| 376 |
} |
| 377 |
|
| 378 |
@Test |
| 379 |
public void reducerThrows() { |
| 380 |
Flowable.just(1, 2) |
| 381 |
.reduce(new BiFunction<Integer, Integer, Integer>() { |
| 382 |
@Override |
| 383 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 384 |
throw new TestException(); |
| 385 |
} |
| 386 |
}) |
| 387 |
.test() |
| 388 |
.assertFailure(TestException.class); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Make sure an asynchronous reduce with flatMap works. |
| 393 |
* Original Reactor-Core test case: https://gist.github.com/jurna/353a2bd8ff83f0b24f0b5bc772077d61 |
| 394 |
*/ |
| 395 |
@Test |
| 396 |
public void shouldReduceTo10Events() { |
| 397 |
final AtomicInteger count = new AtomicInteger(); |
| 398 |
|
| 399 |
Flowable.range(0, 10).flatMap(new Function<Integer, Publisher<String>>() { |
| 400 |
@Override |
| 401 |
public Publisher<String> apply(final Integer x) throws Exception { |
| 402 |
return Flowable.range(0, 2) |
| 403 |
.map(new Function<Integer, String>() { |
| 404 |
@Override |
| 405 |
public String apply(Integer y) throws Exception { |
| 406 |
return blockingOp(x, y); |
| 407 |
} |
| 408 |
}).subscribeOn(Schedulers.io()) |
| 409 |
.reduce(new BiFunction<String, String, String>() { |
| 410 |
@Override |
| 411 |
public String apply(String l, String r) throws Exception { |
| 412 |
return l + "_" + r; |
| 413 |
} |
| 414 |
}) |
| 415 |
.doOnSuccess(new Consumer<String>() { |
| 416 |
@Override |
| 417 |
public void accept(String s) throws Exception { |
| 418 |
count.incrementAndGet(); |
| 419 |
System.out.println("Completed with " + s); |
| 420 |
} |
| 421 |
}) |
| 422 |
.toFlowable(); |
| 423 |
} |
| 424 |
} |
| 425 |
).blockingLast(); |
| 426 |
|
| 427 |
assertEquals(10, count.get()); |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Make sure an asynchronous reduce with flatMap works. |
| 432 |
* Original Reactor-Core test case: https://gist.github.com/jurna/353a2bd8ff83f0b24f0b5bc772077d61 |
| 433 |
*/ |
| 434 |
@Test |
| 435 |
public void shouldReduceTo10EventsFlowable() { |
| 436 |
final AtomicInteger count = new AtomicInteger(); |
| 437 |
|
| 438 |
Flowable.range(0, 10).flatMap(new Function<Integer, Publisher<String>>() { |
| 439 |
@Override |
| 440 |
public Publisher<String> apply(final Integer x) throws Exception { |
| 441 |
return Flowable.range(0, 2) |
| 442 |
.map(new Function<Integer, String>() { |
| 443 |
@Override |
| 444 |
public String apply(Integer y) throws Exception { |
| 445 |
return blockingOp(x, y); |
| 446 |
} |
| 447 |
}).subscribeOn(Schedulers.io()) |
| 448 |
.reduce(new BiFunction<String, String, String>() { |
| 449 |
@Override |
| 450 |
public String apply(String l, String r) throws Exception { |
| 451 |
return l + "_" + r; |
| 452 |
} |
| 453 |
}) |
| 454 |
.toFlowable() |
| 455 |
.doOnNext(new Consumer<String>() { |
| 456 |
@Override |
| 457 |
public void accept(String s) throws Exception { |
| 458 |
count.incrementAndGet(); |
| 459 |
System.out.println("Completed with " + s); |
| 460 |
} |
| 461 |
}) |
| 462 |
; |
| 463 |
} |
| 464 |
} |
| 465 |
).blockingLast(); |
| 466 |
|
| 467 |
assertEquals(10, count.get()); |
| 468 |
} |
| 469 |
|
| 470 |
static String blockingOp(Integer x, Integer y) { |
| 471 |
try { |
| 472 |
Thread.sleep(100); |
| 473 |
} catch (InterruptedException e) { |
| 474 |
e.printStackTrace(); |
| 475 |
} |
| 476 |
return "x" + x + "y" + y; |
| 477 |
} |
| 478 |
|
| 479 |
@Test |
| 480 |
public void seedDoubleOnSubscribe() { |
| 481 |
TestHelper.checkDoubleOnSubscribeFlowableToSingle(new Function<Flowable<Integer>, SingleSource<Integer>>() { |
| 482 |
@Override |
| 483 |
public SingleSource<Integer> apply(Flowable<Integer> f) |
| 484 |
throws Exception { |
| 485 |
return f.reduce(0, new BiFunction<Integer, Integer, Integer>() { |
| 486 |
@Override |
| 487 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 488 |
return a; |
| 489 |
} |
| 490 |
}); |
| 491 |
} |
| 492 |
}); |
| 493 |
} |
| 494 |
|
| 495 |
@Test |
| 496 |
public void seedDisposed() { |
| 497 |
TestHelper.checkDisposed(PublishProcessor.<Integer>create().reduce(0, new BiFunction<Integer, Integer, Integer>() { |
| 498 |
@Override |
| 499 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 500 |
return a; |
| 501 |
} |
| 502 |
})); |
| 503 |
} |
| 504 |
|
| 505 |
@Test |
| 506 |
public void seedBadSource() { |
| 507 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 508 |
try { |
| 509 |
new Flowable<Integer>() { |
| 510 |
@Override |
| 511 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 512 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 513 |
subscriber.onComplete(); |
| 514 |
subscriber.onNext(1); |
| 515 |
subscriber.onError(new TestException()); |
| 516 |
subscriber.onComplete(); |
| 517 |
} |
| 518 |
} |
| 519 |
.reduce(0, new BiFunction<Integer, Integer, Integer>() { |
| 520 |
@Override |
| 521 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 522 |
return a; |
| 523 |
} |
| 524 |
}) |
| 525 |
.test() |
| 526 |
.assertResult(0); |
| 527 |
|
| 528 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 529 |
} finally { |
| 530 |
RxJavaPlugins.reset(); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
@Test |
| 535 |
public void doubleOnSubscribeFlowable() { |
| 536 |
TestHelper.checkDoubleOnSubscribeFlowable(f -> f.reduce((a, b) -> a).toFlowable()); |
| 537 |
} |
| 538 |
} |