| 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.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.CopyOnWriteArrayList; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.*; |
| 25 |
import org.reactivestreams.*; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.exceptions.*; |
| 29 |
import io.reactivex.rxjava3.flowable.*; |
| 30 |
import io.reactivex.rxjava3.flowable.FlowableEventStream.Event; |
| 31 |
import io.reactivex.rxjava3.functions.*; |
| 32 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 33 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 34 |
import io.reactivex.rxjava3.subscribers.*; |
| 35 |
import io.reactivex.rxjava3.testsupport.*; |
| 36 |
|
| 37 |
public class FlowableScanTest extends RxJavaTest { |
| 38 |
|
| 39 |
@Test |
| 40 |
public void scanIntegersWithInitialValue() { |
| 41 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 42 |
|
| 43 |
Flowable<Integer> flowable = Flowable.just(1, 2, 3); |
| 44 |
|
| 45 |
Flowable<String> m = flowable.scan("", new BiFunction<String, Integer, String>() { |
| 46 |
|
| 47 |
@Override |
| 48 |
public String apply(String s, Integer n) { |
| 49 |
return s + n.toString(); |
| 50 |
} |
| 51 |
|
| 52 |
}); |
| 53 |
m.subscribe(subscriber); |
| 54 |
|
| 55 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 56 |
verify(subscriber, times(1)).onNext(""); |
| 57 |
verify(subscriber, times(1)).onNext("1"); |
| 58 |
verify(subscriber, times(1)).onNext("12"); |
| 59 |
verify(subscriber, times(1)).onNext("123"); |
| 60 |
verify(subscriber, times(4)).onNext(anyString()); |
| 61 |
verify(subscriber, times(1)).onComplete(); |
| 62 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void scanIntegersWithoutInitialValue() { |
| 67 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 68 |
|
| 69 |
Flowable<Integer> flowable = Flowable.just(1, 2, 3); |
| 70 |
|
| 71 |
Flowable<Integer> m = flowable.scan(new BiFunction<Integer, Integer, Integer>() { |
| 72 |
|
| 73 |
@Override |
| 74 |
public Integer apply(Integer t1, Integer t2) { |
| 75 |
return t1 + t2; |
| 76 |
} |
| 77 |
|
| 78 |
}); |
| 79 |
m.subscribe(subscriber); |
| 80 |
|
| 81 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 82 |
verify(subscriber, never()).onNext(0); |
| 83 |
verify(subscriber, times(1)).onNext(1); |
| 84 |
verify(subscriber, times(1)).onNext(3); |
| 85 |
verify(subscriber, times(1)).onNext(6); |
| 86 |
verify(subscriber, times(3)).onNext(anyInt()); |
| 87 |
verify(subscriber, times(1)).onComplete(); |
| 88 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void scanIntegersWithoutInitialValueAndOnlyOneValue() { |
| 93 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 94 |
|
| 95 |
Flowable<Integer> flowable = Flowable.just(1); |
| 96 |
|
| 97 |
Flowable<Integer> m = flowable.scan(new BiFunction<Integer, Integer, Integer>() { |
| 98 |
|
| 99 |
@Override |
| 100 |
public Integer apply(Integer t1, Integer t2) { |
| 101 |
return t1 + t2; |
| 102 |
} |
| 103 |
|
| 104 |
}); |
| 105 |
m.subscribe(subscriber); |
| 106 |
|
| 107 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 108 |
verify(subscriber, never()).onNext(0); |
| 109 |
verify(subscriber, times(1)).onNext(1); |
| 110 |
verify(subscriber, times(1)).onNext(anyInt()); |
| 111 |
verify(subscriber, times(1)).onComplete(); |
| 112 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void shouldNotEmitUntilAfterSubscription() { |
| 117 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 118 |
Flowable.range(1, 100).scan(0, new BiFunction<Integer, Integer, Integer>() { |
| 119 |
|
| 120 |
@Override |
| 121 |
public Integer apply(Integer t1, Integer t2) { |
| 122 |
return t1 + t2; |
| 123 |
} |
| 124 |
|
| 125 |
}).filter(new Predicate<Integer>() { |
| 126 |
|
| 127 |
@Override |
| 128 |
public boolean test(Integer t1) { |
| 129 |
// this will cause request(1) when 0 is emitted |
| 130 |
return t1 > 0; |
| 131 |
} |
| 132 |
|
| 133 |
}).subscribe(ts); |
| 134 |
|
| 135 |
assertEquals(100, ts.values().size()); |
| 136 |
} |
| 137 |
|
| 138 |
@Test |
| 139 |
public void backpressureWithInitialValue() { |
| 140 |
final AtomicInteger count = new AtomicInteger(); |
| 141 |
Flowable.range(1, 100) |
| 142 |
.scan(0, new BiFunction<Integer, Integer, Integer>() { |
| 143 |
|
| 144 |
@Override |
| 145 |
public Integer apply(Integer t1, Integer t2) { |
| 146 |
return t1 + t2; |
| 147 |
} |
| 148 |
|
| 149 |
}) |
| 150 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 151 |
|
| 152 |
@Override |
| 153 |
public void onStart() { |
| 154 |
request(10); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void onComplete() { |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public void onError(Throwable e) { |
| 164 |
Assert.fail(e.getMessage()); |
| 165 |
e.printStackTrace(); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public void onNext(Integer t) { |
| 170 |
count.incrementAndGet(); |
| 171 |
} |
| 172 |
|
| 173 |
}); |
| 174 |
|
| 175 |
// we only expect to receive 10 since we request(10) |
| 176 |
assertEquals(10, count.get()); |
| 177 |
} |
| 178 |
|
| 179 |
@Test |
| 180 |
public void backpressureWithoutInitialValue() { |
| 181 |
final AtomicInteger count = new AtomicInteger(); |
| 182 |
Flowable.range(1, 100) |
| 183 |
.scan(new BiFunction<Integer, Integer, Integer>() { |
| 184 |
|
| 185 |
@Override |
| 186 |
public Integer apply(Integer t1, Integer t2) { |
| 187 |
return t1 + t2; |
| 188 |
} |
| 189 |
|
| 190 |
}) |
| 191 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 192 |
|
| 193 |
@Override |
| 194 |
public void onStart() { |
| 195 |
request(10); |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public void onComplete() { |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public void onError(Throwable e) { |
| 205 |
Assert.fail(e.getMessage()); |
| 206 |
e.printStackTrace(); |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public void onNext(Integer t) { |
| 211 |
count.incrementAndGet(); |
| 212 |
} |
| 213 |
|
| 214 |
}); |
| 215 |
|
| 216 |
// we only expect to receive 10 since we request(10) |
| 217 |
assertEquals(10, count.get()); |
| 218 |
} |
| 219 |
|
| 220 |
@Test |
| 221 |
public void noBackpressureWithInitialValue() { |
| 222 |
final AtomicInteger count = new AtomicInteger(); |
| 223 |
Flowable.range(1, 100) |
| 224 |
.scan(0, new BiFunction<Integer, Integer, Integer>() { |
| 225 |
|
| 226 |
@Override |
| 227 |
public Integer apply(Integer t1, Integer t2) { |
| 228 |
return t1 + t2; |
| 229 |
} |
| 230 |
|
| 231 |
}) |
| 232 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 233 |
|
| 234 |
@Override |
| 235 |
public void onComplete() { |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public void onError(Throwable e) { |
| 241 |
Assert.fail(e.getMessage()); |
| 242 |
e.printStackTrace(); |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public void onNext(Integer t) { |
| 247 |
count.incrementAndGet(); |
| 248 |
} |
| 249 |
|
| 250 |
}); |
| 251 |
|
| 252 |
// we only expect to receive 101 as we'll receive all 100 + the initial value |
| 253 |
assertEquals(101, count.get()); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* This uses the public API collect which uses scan under the covers. |
| 258 |
*/ |
| 259 |
@Test |
| 260 |
public void seedFactory() { |
| 261 |
Single<List<Integer>> o = Flowable.range(1, 10) |
| 262 |
.collect(new Supplier<List<Integer>>() { |
| 263 |
|
| 264 |
@Override |
| 265 |
public List<Integer> get() { |
| 266 |
return new ArrayList<>(); |
| 267 |
} |
| 268 |
|
| 269 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 270 |
|
| 271 |
@Override |
| 272 |
public void accept(List<Integer> list, Integer t2) { |
| 273 |
list.add(t2); |
| 274 |
} |
| 275 |
|
| 276 |
}); |
| 277 |
|
| 278 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), o.blockingGet()); |
| 279 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), o.blockingGet()); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* This uses the public API collect which uses scan under the covers. |
| 284 |
*/ |
| 285 |
@Test |
| 286 |
public void seedFactoryFlowable() { |
| 287 |
Flowable<List<Integer>> f = Flowable.range(1, 10) |
| 288 |
.collect(new Supplier<List<Integer>>() { |
| 289 |
|
| 290 |
@Override |
| 291 |
public List<Integer> get() { |
| 292 |
return new ArrayList<>(); |
| 293 |
} |
| 294 |
|
| 295 |
}, new BiConsumer<List<Integer>, Integer>() { |
| 296 |
|
| 297 |
@Override |
| 298 |
public void accept(List<Integer> list, Integer t2) { |
| 299 |
list.add(t2); |
| 300 |
} |
| 301 |
|
| 302 |
}).toFlowable().takeLast(1); |
| 303 |
|
| 304 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), f.blockingSingle()); |
| 305 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), f.blockingSingle()); |
| 306 |
} |
| 307 |
|
| 308 |
@Test |
| 309 |
public void scanWithRequestOne() { |
| 310 |
Flowable<Integer> f = Flowable.just(1, 2).scan(0, new BiFunction<Integer, Integer, Integer>() { |
| 311 |
|
| 312 |
@Override |
| 313 |
public Integer apply(Integer t1, Integer t2) { |
| 314 |
return t1 + t2; |
| 315 |
} |
| 316 |
|
| 317 |
}).take(1); |
| 318 |
TestSubscriberEx<Integer> subscriber = new TestSubscriberEx<>(); |
| 319 |
f.subscribe(subscriber); |
| 320 |
subscriber.assertValue(0); |
| 321 |
subscriber.assertTerminated(); |
| 322 |
subscriber.assertNoErrors(); |
| 323 |
} |
| 324 |
|
| 325 |
@Test |
| 326 |
public void scanShouldNotRequestZero() { |
| 327 |
final AtomicReference<Subscription> producer = new AtomicReference<>(); |
| 328 |
Flowable<Integer> f = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 329 |
@Override |
| 330 |
public void subscribe(final Subscriber<? super Integer> subscriber) { |
| 331 |
Subscription p = spy(new Subscription() { |
| 332 |
|
| 333 |
private AtomicBoolean requested = new AtomicBoolean(false); |
| 334 |
|
| 335 |
@Override |
| 336 |
public void request(long n) { |
| 337 |
if (requested.compareAndSet(false, true)) { |
| 338 |
subscriber.onNext(1); |
| 339 |
subscriber.onComplete(); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
@Override |
| 344 |
public void cancel() { |
| 345 |
|
| 346 |
} |
| 347 |
}); |
| 348 |
producer.set(p); |
| 349 |
subscriber.onSubscribe(p); |
| 350 |
} |
| 351 |
}).scan(100, new BiFunction<Integer, Integer, Integer>() { |
| 352 |
|
| 353 |
@Override |
| 354 |
public Integer apply(Integer t1, Integer t2) { |
| 355 |
return t1 + t2; |
| 356 |
} |
| 357 |
|
| 358 |
}); |
| 359 |
|
| 360 |
f.subscribe(new TestSubscriber<Integer>(1L) { |
| 361 |
|
| 362 |
@Override |
| 363 |
public void onNext(Integer integer) { |
| 364 |
request(1); |
| 365 |
} |
| 366 |
}); |
| 367 |
|
| 368 |
verify(producer.get(), never()).request(0); |
| 369 |
verify(producer.get(), times(1)).request(Flowable.bufferSize() - 1); |
| 370 |
} |
| 371 |
|
| 372 |
@Test |
| 373 |
public void dispose() { |
| 374 |
TestHelper.checkDisposed(PublishProcessor.create().scan(new BiFunction<Object, Object, Object>() { |
| 375 |
@Override |
| 376 |
public Object apply(Object a, Object b) throws Exception { |
| 377 |
return a; |
| 378 |
} |
| 379 |
})); |
| 380 |
|
| 381 |
TestHelper.checkDisposed(PublishProcessor.<Integer>create().scan(0, new BiFunction<Integer, Integer, Integer>() { |
| 382 |
@Override |
| 383 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 384 |
return a + b; |
| 385 |
} |
| 386 |
})); |
| 387 |
} |
| 388 |
|
| 389 |
@Test |
| 390 |
public void doubleOnSubscribe() { |
| 391 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 392 |
@Override |
| 393 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 394 |
return f.scan(new BiFunction<Object, Object, Object>() { |
| 395 |
@Override |
| 396 |
public Object apply(Object a, Object b) throws Exception { |
| 397 |
return a; |
| 398 |
} |
| 399 |
}); |
| 400 |
} |
| 401 |
}); |
| 402 |
|
| 403 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 404 |
@Override |
| 405 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 406 |
return f.scan(0, new BiFunction<Object, Object, Object>() { |
| 407 |
@Override |
| 408 |
public Object apply(Object a, Object b) throws Exception { |
| 409 |
return a; |
| 410 |
} |
| 411 |
}); |
| 412 |
} |
| 413 |
}); |
| 414 |
} |
| 415 |
|
| 416 |
@Test |
| 417 |
public void error() { |
| 418 |
Flowable.error(new TestException()) |
| 419 |
.scan(new BiFunction<Object, Object, Object>() { |
| 420 |
@Override |
| 421 |
public Object apply(Object a, Object b) throws Exception { |
| 422 |
return a; |
| 423 |
} |
| 424 |
}) |
| 425 |
.test() |
| 426 |
.assertFailure(TestException.class); |
| 427 |
} |
| 428 |
|
| 429 |
@Test |
| 430 |
public void neverSource() { |
| 431 |
Flowable.<Integer>never() |
| 432 |
.scan(0, new BiFunction<Integer, Integer, Integer>() { |
| 433 |
@Override |
| 434 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 435 |
return a + b; |
| 436 |
} |
| 437 |
}) |
| 438 |
.test() |
| 439 |
.assertValue(0) |
| 440 |
.assertNoErrors() |
| 441 |
.assertNotComplete(); |
| 442 |
} |
| 443 |
|
| 444 |
@Test |
| 445 |
public void unsubscribeScan() { |
| 446 |
|
| 447 |
FlowableEventStream.getEventStream("HTTP-ClusterB", 20) |
| 448 |
.scan(new HashMap<>(), new BiFunction<HashMap<String, String>, Event, HashMap<String, String>>() { |
| 449 |
@Override |
| 450 |
public HashMap<String, String> apply(HashMap<String, String> accum, Event perInstanceEvent) { |
| 451 |
accum.put("instance", perInstanceEvent.instanceId); |
| 452 |
return accum; |
| 453 |
} |
| 454 |
}) |
| 455 |
.take(10) |
| 456 |
.blockingForEach(new Consumer<HashMap<String, String>>() { |
| 457 |
@Override |
| 458 |
public void accept(HashMap<String, String> v) { |
| 459 |
System.out.println(v); |
| 460 |
} |
| 461 |
}); |
| 462 |
} |
| 463 |
|
| 464 |
@Test |
| 465 |
public void scanWithSeedDoesNotEmitErrorTwiceIfScanFunctionThrows() { |
| 466 |
final List<Throwable> list = new CopyOnWriteArrayList<>(); |
| 467 |
Consumer<Throwable> errorConsumer = new Consumer<Throwable>() { |
| 468 |
@Override |
| 469 |
public void accept(Throwable t) throws Exception { |
| 470 |
list.add(t); |
| 471 |
}}; |
| 472 |
try { |
| 473 |
RxJavaPlugins.setErrorHandler(errorConsumer); |
| 474 |
final RuntimeException e = new RuntimeException(); |
| 475 |
final RuntimeException e2 = new RuntimeException(); |
| 476 |
Burst.items(1).error(e2) |
| 477 |
.scan(0, throwingBiFunction(e)) |
| 478 |
.test() |
| 479 |
.assertValues(0) |
| 480 |
.assertError(e); |
| 481 |
|
| 482 |
assertEquals("" + list, 1, list.size()); |
| 483 |
assertTrue("" + list, list.get(0) instanceof UndeliverableException); |
| 484 |
assertEquals(e2, list.get(0).getCause()); |
| 485 |
} finally { |
| 486 |
RxJavaPlugins.reset(); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
@Test |
| 491 |
public void scanWithSeedDoesNotEmitTerminalEventTwiceIfScanFunctionThrows() { |
| 492 |
final RuntimeException e = new RuntimeException(); |
| 493 |
Burst.item(1).create() |
| 494 |
.scan(0, throwingBiFunction(e)) |
| 495 |
.test() |
| 496 |
.assertValue(0) |
| 497 |
.assertError(e); |
| 498 |
} |
| 499 |
|
| 500 |
@Test |
| 501 |
public void scanWithSeedDoesNotProcessOnNextAfterTerminalEventIfScanFunctionThrows() { |
| 502 |
final RuntimeException e = new RuntimeException(); |
| 503 |
final AtomicInteger count = new AtomicInteger(); |
| 504 |
Burst.items(1, 2).create().scan(0, new BiFunction<Integer, Integer, Integer>() { |
| 505 |
|
| 506 |
@Override |
| 507 |
public Integer apply(Integer n1, Integer n2) throws Exception { |
| 508 |
count.incrementAndGet(); |
| 509 |
throw e; |
| 510 |
}}) |
| 511 |
.test() |
| 512 |
.assertValues(0) |
| 513 |
.assertError(e); |
| 514 |
assertEquals(1, count.get()); |
| 515 |
} |
| 516 |
|
| 517 |
@Test |
| 518 |
public void scanWithSeedCompletesNormally() { |
| 519 |
Flowable.just(1, 2, 3).scan(0, SUM) |
| 520 |
.test() |
| 521 |
.assertValues(0, 1, 3, 6) |
| 522 |
.assertComplete(); |
| 523 |
} |
| 524 |
|
| 525 |
@Test |
| 526 |
public void scanWithSeedWhenScanSeedProviderThrows() { |
| 527 |
final RuntimeException e = new RuntimeException(); |
| 528 |
Flowable.just(1, 2, 3).scanWith(throwingSupplier(e), |
| 529 |
SUM) |
| 530 |
.test() |
| 531 |
.assertError(e) |
| 532 |
.assertNoValues(); |
| 533 |
} |
| 534 |
|
| 535 |
@Test |
| 536 |
public void scanNoSeed() { |
| 537 |
Flowable.just(1, 2, 3) |
| 538 |
.scan(SUM) |
| 539 |
.test() |
| 540 |
.assertValues(1, 3, 6) |
| 541 |
.assertComplete(); |
| 542 |
} |
| 543 |
|
| 544 |
@Test |
| 545 |
public void scanNoSeedDoesNotEmitErrorTwiceIfScanFunctionThrows() { |
| 546 |
final List<Throwable> list = new CopyOnWriteArrayList<>(); |
| 547 |
Consumer<Throwable> errorConsumer = new Consumer<Throwable>() { |
| 548 |
@Override |
| 549 |
public void accept(Throwable t) throws Exception { |
| 550 |
list.add(t); |
| 551 |
}}; |
| 552 |
try { |
| 553 |
RxJavaPlugins.setErrorHandler(errorConsumer); |
| 554 |
final RuntimeException e = new RuntimeException(); |
| 555 |
final RuntimeException e2 = new RuntimeException(); |
| 556 |
Burst.items(1, 2).error(e2) |
| 557 |
.scan(throwingBiFunction(e)) |
| 558 |
.test() |
| 559 |
.assertValue(1) |
| 560 |
.assertError(e); |
| 561 |
|
| 562 |
assertEquals("" + list, 1, list.size()); |
| 563 |
assertTrue("" + list, list.get(0) instanceof UndeliverableException); |
| 564 |
assertEquals(e2, list.get(0).getCause()); |
| 565 |
} finally { |
| 566 |
RxJavaPlugins.reset(); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
@Test |
| 571 |
public void scanNoSeedDoesNotEmitTerminalEventTwiceIfScanFunctionThrows() { |
| 572 |
final RuntimeException e = new RuntimeException(); |
| 573 |
Burst.items(1, 2).create() |
| 574 |
.scan(throwingBiFunction(e)) |
| 575 |
.test() |
| 576 |
.assertValue(1) |
| 577 |
.assertError(e); |
| 578 |
} |
| 579 |
|
| 580 |
@Test |
| 581 |
public void scanNoSeedDoesNotProcessOnNextAfterTerminalEventIfScanFunctionThrows() { |
| 582 |
final RuntimeException e = new RuntimeException(); |
| 583 |
final AtomicInteger count = new AtomicInteger(); |
| 584 |
Burst.items(1, 2, 3).create().scan(new BiFunction<Integer, Integer, Integer>() { |
| 585 |
|
| 586 |
@Override |
| 587 |
public Integer apply(Integer n1, Integer n2) throws Exception { |
| 588 |
count.incrementAndGet(); |
| 589 |
throw e; |
| 590 |
}}) |
| 591 |
.test() |
| 592 |
.assertValue(1) |
| 593 |
.assertError(e); |
| 594 |
assertEquals(1, count.get()); |
| 595 |
} |
| 596 |
|
| 597 |
private static BiFunction<Integer, Integer, Integer> throwingBiFunction(final RuntimeException e) { |
| 598 |
return new BiFunction<Integer, Integer, Integer>() { |
| 599 |
@Override |
| 600 |
public Integer apply(Integer n1, Integer n2) throws Exception { |
| 601 |
throw e; |
| 602 |
} |
| 603 |
}; |
| 604 |
} |
| 605 |
|
| 606 |
private static final BiFunction<Integer, Integer, Integer> SUM = new BiFunction<Integer, Integer, Integer>() { |
| 607 |
|
| 608 |
@Override |
| 609 |
public Integer apply(Integer t1, Integer t2) throws Exception { |
| 610 |
return t1 + t2; |
| 611 |
} |
| 612 |
}; |
| 613 |
|
| 614 |
private static Supplier<Integer> throwingSupplier(final RuntimeException e) { |
| 615 |
return new Supplier<Integer>() { |
| 616 |
@Override |
| 617 |
public Integer get() throws Exception { |
| 618 |
throw e; |
| 619 |
} |
| 620 |
}; |
| 621 |
} |
| 622 |
|
| 623 |
@Test |
| 624 |
public void scanEmptyBackpressured() { |
| 625 |
Flowable.<Integer>empty() |
| 626 |
.scan(0, SUM) |
| 627 |
.test(1) |
| 628 |
.assertResult(0); |
| 629 |
} |
| 630 |
|
| 631 |
@Test |
| 632 |
public void scanErrorBackpressured() { |
| 633 |
Flowable.<Integer>error(new TestException()) |
| 634 |
.scan(0, SUM) |
| 635 |
.test(0) |
| 636 |
.assertFailure(TestException.class); |
| 637 |
} |
| 638 |
|
| 639 |
@Test |
| 640 |
public void scanTake() { |
| 641 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 642 |
@Override |
| 643 |
public void onNext(Integer t) { |
| 644 |
super.onNext(t); |
| 645 |
onComplete(); |
| 646 |
cancel(); |
| 647 |
} |
| 648 |
}; |
| 649 |
|
| 650 |
Flowable.range(1, 10) |
| 651 |
.scan(0, SUM) |
| 652 |
.subscribe(ts) |
| 653 |
; |
| 654 |
|
| 655 |
ts.assertResult(0); |
| 656 |
} |
| 657 |
|
| 658 |
@Test |
| 659 |
public void scanLong() { |
| 660 |
int n = 2 * Flowable.bufferSize(); |
| 661 |
|
| 662 |
for (int b = 1; b <= n; b *= 2) { |
| 663 |
List<Integer> list = Flowable.range(1, n) |
| 664 |
.scan(0, new BiFunction<Integer, Integer, Integer>() { |
| 665 |
@Override |
| 666 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 667 |
return b; |
| 668 |
} |
| 669 |
}) |
| 670 |
.rebatchRequests(b) |
| 671 |
.toList() |
| 672 |
.blockingGet(); |
| 673 |
|
| 674 |
for (int i = 0; i <= n; i++) { |
| 675 |
assertEquals(i, list.get(i).intValue()); |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
@Test |
| 681 |
public void badRequest() { |
| 682 |
TestHelper.assertBadRequestReported(Flowable.<Integer>never().scanWith(() -> 1, (a, b) -> a + b)); |
| 683 |
} |
| 684 |
|
| 685 |
@Test |
| 686 |
public void drainMoreWork() { |
| 687 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 688 |
|
| 689 |
TestSubscriber<Integer> ts = pp.scanWith(() -> 0, (a, b) -> a + b) |
| 690 |
.doOnNext(v -> { |
| 691 |
if (v == 1) { |
| 692 |
pp.onNext(2); |
| 693 |
pp.onComplete(); |
| 694 |
} |
| 695 |
}) |
| 696 |
.test(); |
| 697 |
|
| 698 |
pp.onNext(1); |
| 699 |
|
| 700 |
ts.assertResult(0, 1, 3); |
| 701 |
} |
| 702 |
} |