| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.flowable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.TimeUnit; |
| 22 |
import java.util.concurrent.atomic.AtomicInteger; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.reactivestreams.*; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.Flowable; |
| 28 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 29 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 30 |
import io.reactivex.rxjava3.functions.*; |
| 31 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 32 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 33 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 34 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 35 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 36 |
import io.reactivex.rxjava3.testsupport.*; |
| 37 |
|
| 38 |
public class FlowableRepeatTest { |
| 39 |
|
| 40 |
@Test |
| 41 |
public void repetition() { |
| 42 |
int num = 10; |
| 43 |
final AtomicInteger count = new AtomicInteger(); |
| 44 |
int value = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 45 |
|
| 46 |
@Override |
| 47 |
public void subscribe(final Subscriber<? super Integer> subscriber) { |
| 48 |
subscriber.onNext(count.incrementAndGet()); |
| 49 |
subscriber.onComplete(); |
| 50 |
} |
| 51 |
}).repeat().subscribeOn(Schedulers.computation()) |
| 52 |
.take(num).blockingLast(); |
| 53 |
|
| 54 |
assertEquals(num, value); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void repeatTake() { |
| 59 |
Flowable<Integer> xs = Flowable.just(1, 2); |
| 60 |
Object[] ys = xs.repeat().subscribeOn(Schedulers.newThread()).take(4).toList().blockingGet().toArray(); |
| 61 |
assertArrayEquals(new Object[] { 1, 2, 1, 2 }, ys); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void noStackOverFlow() { |
| 66 |
Flowable.just(1).repeat().subscribeOn(Schedulers.newThread()).take(100000).blockingLast(); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void repeatTakeWithSubscribeOn() throws InterruptedException { |
| 71 |
|
| 72 |
final AtomicInteger counter = new AtomicInteger(); |
| 73 |
Flowable<Integer> oi = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 74 |
|
| 75 |
@Override |
| 76 |
public void subscribe(Subscriber<? super Integer> sub) { |
| 77 |
sub.onSubscribe(new BooleanSubscription()); |
| 78 |
counter.incrementAndGet(); |
| 79 |
sub.onNext(1); |
| 80 |
sub.onNext(2); |
| 81 |
sub.onComplete(); |
| 82 |
} |
| 83 |
}).subscribeOn(Schedulers.newThread()); |
| 84 |
|
| 85 |
Object[] ys = oi.repeat().subscribeOn(Schedulers.newThread()).map(new Function<Integer, Integer>() { |
| 86 |
|
| 87 |
@Override |
| 88 |
public Integer apply(Integer t1) { |
| 89 |
try { |
| 90 |
Thread.sleep(50); |
| 91 |
} catch (InterruptedException e) { |
| 92 |
e.printStackTrace(); |
| 93 |
} |
| 94 |
return t1; |
| 95 |
} |
| 96 |
|
| 97 |
}).take(4).toList().blockingGet().toArray(); |
| 98 |
|
| 99 |
assertEquals(2, counter.get()); |
| 100 |
assertArrayEquals(new Object[] { 1, 2, 1, 2 }, ys); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void repeatAndTake() { |
| 105 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 106 |
|
| 107 |
Flowable.just(1).repeat().take(10).subscribe(subscriber); |
| 108 |
|
| 109 |
verify(subscriber, times(10)).onNext(1); |
| 110 |
verify(subscriber).onComplete(); |
| 111 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void repeatLimited() { |
| 116 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 117 |
|
| 118 |
Flowable.just(1).repeat(10).subscribe(subscriber); |
| 119 |
|
| 120 |
verify(subscriber, times(10)).onNext(1); |
| 121 |
verify(subscriber).onComplete(); |
| 122 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void repeatError() { |
| 127 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 128 |
|
| 129 |
Flowable.error(new TestException()).repeat(10).subscribe(subscriber); |
| 130 |
|
| 131 |
verify(subscriber).onError(any(TestException.class)); |
| 132 |
verify(subscriber, never()).onNext(any()); |
| 133 |
verify(subscriber, never()).onComplete(); |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void repeatZero() { |
| 139 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 140 |
|
| 141 |
Flowable.just(1).repeat(0).subscribe(subscriber); |
| 142 |
|
| 143 |
verify(subscriber).onComplete(); |
| 144 |
verify(subscriber, never()).onNext(any()); |
| 145 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void repeatOne() { |
| 150 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 151 |
|
| 152 |
Flowable.just(1).repeat(1).subscribe(subscriber); |
| 153 |
|
| 154 |
verify(subscriber).onComplete(); |
| 155 |
verify(subscriber, times(1)).onNext(any()); |
| 156 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 157 |
} |
| 158 |
|
| 159 |
/** Issue #2587. */ |
| 160 |
@Test |
| 161 |
public void repeatAndDistinctUnbounded() { |
| 162 |
Flowable<Integer> src = Flowable.fromIterable(Arrays.asList(1, 2, 3, 4, 5)) |
| 163 |
.take(3) |
| 164 |
.repeat(3) |
| 165 |
.distinct(); |
| 166 |
|
| 167 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 168 |
|
| 169 |
src.subscribe(ts); |
| 170 |
|
| 171 |
ts.assertNoErrors(); |
| 172 |
ts.assertTerminated(); |
| 173 |
ts.assertValues(1, 2, 3); |
| 174 |
} |
| 175 |
|
| 176 |
/** Issue #2844: wrong target of request. */ |
| 177 |
@Test |
| 178 |
public void repeatRetarget() { |
| 179 |
final List<Integer> concatBase = new ArrayList<>(); |
| 180 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 181 |
Flowable.just(1, 2) |
| 182 |
.repeat(5) |
| 183 |
.concatMap(new Function<Integer, Flowable<Integer>>() { |
| 184 |
@Override |
| 185 |
public Flowable<Integer> apply(Integer x) { |
| 186 |
System.out.println("testRepeatRetarget -> " + x); |
| 187 |
concatBase.add(x); |
| 188 |
return Flowable.<Integer>empty() |
| 189 |
.delay(200, TimeUnit.MILLISECONDS); |
| 190 |
} |
| 191 |
}) |
| 192 |
.subscribe(ts); |
| 193 |
|
| 194 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 195 |
ts.assertNoErrors(); |
| 196 |
ts.assertNoValues(); |
| 197 |
|
| 198 |
assertEquals(Arrays.asList(1, 2, 1, 2, 1, 2, 1, 2, 1, 2), concatBase); |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void repeatScheduled() { |
| 203 |
|
| 204 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 205 |
|
| 206 |
Flowable.just(1).subscribeOn(Schedulers.computation()).repeat(5).subscribe(ts); |
| 207 |
|
| 208 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 209 |
ts.assertValues(1, 1, 1, 1, 1); |
| 210 |
ts.assertNoErrors(); |
| 211 |
ts.assertComplete(); |
| 212 |
} |
| 213 |
|
| 214 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 215 |
@Test |
| 216 |
public void repeatWhenDefaultScheduler() { |
| 217 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 218 |
|
| 219 |
Flowable.just(1).repeatWhen((Function)new Function<Flowable, Flowable>() { |
| 220 |
@Override |
| 221 |
public Flowable apply(Flowable f) { |
| 222 |
return f.take(2); |
| 223 |
} |
| 224 |
}).subscribe(ts); |
| 225 |
|
| 226 |
ts.assertValues(1, 1); |
| 227 |
ts.assertNoErrors(); |
| 228 |
ts.assertComplete(); |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 233 |
@Test |
| 234 |
public void repeatWhenTrampolineScheduler() { |
| 235 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 236 |
|
| 237 |
Flowable.just(1).subscribeOn(Schedulers.trampoline()) |
| 238 |
.repeatWhen((Function)new Function<Flowable, Flowable>() { |
| 239 |
@Override |
| 240 |
public Flowable apply(Flowable f) { |
| 241 |
return f.take(2); |
| 242 |
} |
| 243 |
}).subscribe(ts); |
| 244 |
|
| 245 |
ts.assertValues(1, 1); |
| 246 |
ts.assertNoErrors(); |
| 247 |
ts.assertComplete(); |
| 248 |
} |
| 249 |
|
| 250 |
@Test |
| 251 |
public void repeatUntil() { |
| 252 |
Flowable.just(1) |
| 253 |
.repeatUntil(new BooleanSupplier() { |
| 254 |
@Override |
| 255 |
public boolean getAsBoolean() throws Exception { |
| 256 |
return false; |
| 257 |
} |
| 258 |
}) |
| 259 |
.take(5) |
| 260 |
.test() |
| 261 |
.assertResult(1, 1, 1, 1, 1); |
| 262 |
} |
| 263 |
|
| 264 |
@Test |
| 265 |
public void repeatUntilCancel() { |
| 266 |
Flowable.just(1) |
| 267 |
.repeatUntil(new BooleanSupplier() { |
| 268 |
@Override |
| 269 |
public boolean getAsBoolean() throws Exception { |
| 270 |
return true; |
| 271 |
} |
| 272 |
}) |
| 273 |
.test(2L, true) |
| 274 |
.assertEmpty(); |
| 275 |
} |
| 276 |
|
| 277 |
@Test |
| 278 |
public void repeatLongPredicateInvalid() { |
| 279 |
try { |
| 280 |
Flowable.just(1).repeat(-99); |
| 281 |
fail("Should have thrown"); |
| 282 |
} catch (IllegalArgumentException ex) { |
| 283 |
assertEquals("times >= 0 required but it was -99", ex.getMessage()); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
@Test |
| 288 |
public void repeatUntilError() { |
| 289 |
Flowable.error(new TestException()) |
| 290 |
.repeatUntil(new BooleanSupplier() { |
| 291 |
@Override |
| 292 |
public boolean getAsBoolean() throws Exception { |
| 293 |
return true; |
| 294 |
} |
| 295 |
}) |
| 296 |
.test() |
| 297 |
.assertFailure(TestException.class); |
| 298 |
} |
| 299 |
|
| 300 |
@Test |
| 301 |
public void repeatUntilFalse() { |
| 302 |
Flowable.just(1) |
| 303 |
.repeatUntil(new BooleanSupplier() { |
| 304 |
@Override |
| 305 |
public boolean getAsBoolean() throws Exception { |
| 306 |
return true; |
| 307 |
} |
| 308 |
}) |
| 309 |
.test() |
| 310 |
.assertResult(1); |
| 311 |
} |
| 312 |
|
| 313 |
@Test |
| 314 |
public void repeatUntilSupplierCrash() { |
| 315 |
Flowable.just(1) |
| 316 |
.repeatUntil(new BooleanSupplier() { |
| 317 |
@Override |
| 318 |
public boolean getAsBoolean() throws Exception { |
| 319 |
throw new TestException(); |
| 320 |
} |
| 321 |
}) |
| 322 |
.test() |
| 323 |
.assertFailure(TestException.class, 1); |
| 324 |
} |
| 325 |
|
| 326 |
@Test |
| 327 |
public void shouldDisposeInnerObservable() { |
| 328 |
final PublishProcessor<Object> processor = PublishProcessor.create(); |
| 329 |
final Disposable disposable = Flowable.just("Leak") |
| 330 |
.repeatWhen(new Function<Flowable<Object>, Flowable<Object>>() { |
| 331 |
@Override |
| 332 |
public Flowable<Object> apply(Flowable<Object> completions) throws Exception { |
| 333 |
return completions.switchMap(new Function<Object, Flowable<Object>>() { |
| 334 |
@Override |
| 335 |
public Flowable<Object> apply(Object ignore) throws Exception { |
| 336 |
return processor; |
| 337 |
} |
| 338 |
}); |
| 339 |
} |
| 340 |
}) |
| 341 |
.subscribe(); |
| 342 |
|
| 343 |
assertTrue(processor.hasSubscribers()); |
| 344 |
disposable.dispose(); |
| 345 |
assertFalse(processor.hasSubscribers()); |
| 346 |
} |
| 347 |
|
| 348 |
@Test |
| 349 |
public void repeatWhen() { |
| 350 |
Flowable.error(new TestException()) |
| 351 |
.repeatWhen(new Function<Flowable<Object>, Flowable<Object>>() { |
| 352 |
@Override |
| 353 |
public Flowable<Object> apply(Flowable<Object> v) throws Exception { |
| 354 |
return v.delay(10, TimeUnit.SECONDS); |
| 355 |
} |
| 356 |
}) |
| 357 |
.test() |
| 358 |
.awaitDone(5, TimeUnit.SECONDS) |
| 359 |
.assertFailure(TestException.class); |
| 360 |
} |
| 361 |
|
| 362 |
@Test |
| 363 |
public void whenTake() { |
| 364 |
Flowable.range(1, 3).repeatWhen(new Function<Flowable<Object>, Flowable<Object>>() { |
| 365 |
@Override |
| 366 |
public Flowable<Object> apply(Flowable<Object> handler) throws Exception { |
| 367 |
return handler.take(2); |
| 368 |
} |
| 369 |
}) |
| 370 |
.test() |
| 371 |
.assertResult(1, 2, 3, 1, 2, 3); |
| 372 |
} |
| 373 |
|
| 374 |
@Test |
| 375 |
public void noCancelPreviousRepeat() { |
| 376 |
final AtomicInteger counter = new AtomicInteger(); |
| 377 |
|
| 378 |
Flowable<Integer> source = Flowable.just(1).doOnCancel(new Action() { |
| 379 |
@Override |
| 380 |
public void run() throws Exception { |
| 381 |
counter.getAndIncrement(); |
| 382 |
} |
| 383 |
}); |
| 384 |
|
| 385 |
source.repeat(5) |
| 386 |
.test() |
| 387 |
.assertResult(1, 1, 1, 1, 1); |
| 388 |
|
| 389 |
assertEquals(0, counter.get()); |
| 390 |
} |
| 391 |
|
| 392 |
@Test |
| 393 |
public void noCancelPreviousRepeatUntil() { |
| 394 |
final AtomicInteger counter = new AtomicInteger(); |
| 395 |
|
| 396 |
Flowable<Integer> source = Flowable.just(1).doOnCancel(new Action() { |
| 397 |
@Override |
| 398 |
public void run() throws Exception { |
| 399 |
counter.getAndIncrement(); |
| 400 |
} |
| 401 |
}); |
| 402 |
|
| 403 |
final AtomicInteger times = new AtomicInteger(); |
| 404 |
|
| 405 |
source.repeatUntil(new BooleanSupplier() { |
| 406 |
@Override |
| 407 |
public boolean getAsBoolean() throws Exception { |
| 408 |
return times.getAndIncrement() == 4; |
| 409 |
} |
| 410 |
}) |
| 411 |
.test() |
| 412 |
.assertResult(1, 1, 1, 1, 1); |
| 413 |
|
| 414 |
assertEquals(0, counter.get()); |
| 415 |
} |
| 416 |
|
| 417 |
@Test |
| 418 |
public void noCancelPreviousRepeatWhen() { |
| 419 |
final AtomicInteger counter = new AtomicInteger(); |
| 420 |
|
| 421 |
Flowable<Integer> source = Flowable.just(1).doOnCancel(new Action() { |
| 422 |
@Override |
| 423 |
public void run() throws Exception { |
| 424 |
counter.getAndIncrement(); |
| 425 |
} |
| 426 |
}); |
| 427 |
|
| 428 |
final AtomicInteger times = new AtomicInteger(); |
| 429 |
|
| 430 |
source.repeatWhen(new Function<Flowable<Object>, Flowable<?>>() { |
| 431 |
@Override |
| 432 |
public Flowable<?> apply(Flowable<Object> e) throws Exception { |
| 433 |
return e.takeWhile(new Predicate<Object>() { |
| 434 |
@Override |
| 435 |
public boolean test(Object v) throws Exception { |
| 436 |
return times.getAndIncrement() < 4; |
| 437 |
} |
| 438 |
}); |
| 439 |
} |
| 440 |
}) |
| 441 |
.test() |
| 442 |
.assertResult(1, 1, 1, 1, 1); |
| 443 |
|
| 444 |
assertEquals(0, counter.get()); |
| 445 |
} |
| 446 |
|
| 447 |
@Test |
| 448 |
public void repeatFloodNoSubscriptionError() { |
| 449 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 450 |
|
| 451 |
try { |
| 452 |
final PublishProcessor<Integer> source = PublishProcessor.create(); |
| 453 |
final PublishProcessor<Integer> signaller = PublishProcessor.create(); |
| 454 |
|
| 455 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 456 |
|
| 457 |
TestSubscriber<Integer> ts = source.take(1) |
| 458 |
.repeatWhen(new Function<Flowable<Object>, Flowable<Integer>>() { |
| 459 |
@Override |
| 460 |
public Flowable<Integer> apply(Flowable<Object> v) |
| 461 |
throws Exception { |
| 462 |
return signaller; |
| 463 |
} |
| 464 |
}).test(); |
| 465 |
|
| 466 |
Runnable r1 = new Runnable() { |
| 467 |
@Override |
| 468 |
public void run() { |
| 469 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 470 |
source.onNext(1); |
| 471 |
} |
| 472 |
} |
| 473 |
}; |
| 474 |
Runnable r2 = new Runnable() { |
| 475 |
@Override |
| 476 |
public void run() { |
| 477 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 478 |
signaller.offer(1); |
| 479 |
} |
| 480 |
} |
| 481 |
}; |
| 482 |
|
| 483 |
TestHelper.race(r1, r2); |
| 484 |
|
| 485 |
ts.cancel(); |
| 486 |
} |
| 487 |
|
| 488 |
if (!errors.isEmpty()) { |
| 489 |
for (Throwable e : errors) { |
| 490 |
e.printStackTrace(); |
| 491 |
} |
| 492 |
fail(errors + ""); |
| 493 |
} |
| 494 |
} finally { |
| 495 |
RxJavaPlugins.reset(); |
| 496 |
} |
| 497 |
} |
| 498 |
} |