| 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.Mockito.*; |
| 18 |
|
| 19 |
import java.util.*; |
| 20 |
import java.util.concurrent.*; |
| 21 |
import java.util.concurrent.atomic.AtomicInteger; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 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.subscriptions.BooleanSubscription; |
| 30 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 31 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 32 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 33 |
import io.reactivex.rxjava3.testsupport.*; |
| 34 |
|
| 35 |
public class FlowableCacheTest extends RxJavaTest { |
| 36 |
@Test |
| 37 |
public void coldReplayNoBackpressure() { |
| 38 |
FlowableCache<Integer> source = new FlowableCache<>(Flowable.range(0, 1000), 16); |
| 39 |
|
| 40 |
assertFalse("Source is connected!", source.isConnected()); |
| 41 |
|
| 42 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 43 |
|
| 44 |
source.subscribe(ts); |
| 45 |
|
| 46 |
assertTrue("Source is not connected!", source.isConnected()); |
| 47 |
assertFalse("Subscribers retained!", source.hasSubscribers()); |
| 48 |
|
| 49 |
ts.assertNoErrors(); |
| 50 |
ts.assertTerminated(); |
| 51 |
List<Integer> onNextEvents = ts.values(); |
| 52 |
assertEquals(1000, onNextEvents.size()); |
| 53 |
|
| 54 |
for (int i = 0; i < 1000; i++) { |
| 55 |
assertEquals((Integer)i, onNextEvents.get(i)); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void coldReplayBackpressure() { |
| 61 |
FlowableCache<Integer> source = new FlowableCache<>(Flowable.range(0, 1000), 16); |
| 62 |
|
| 63 |
assertFalse("Source is connected!", source.isConnected()); |
| 64 |
|
| 65 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 66 |
ts.request(10); |
| 67 |
|
| 68 |
source.subscribe(ts); |
| 69 |
|
| 70 |
assertTrue("Source is not connected!", source.isConnected()); |
| 71 |
assertFalse("Subscribers retained!", source.hasSubscribers()); |
| 72 |
|
| 73 |
ts.assertNoErrors(); |
| 74 |
ts.assertNotComplete(); |
| 75 |
List<Integer> onNextEvents = ts.values(); |
| 76 |
assertEquals(10, onNextEvents.size()); |
| 77 |
|
| 78 |
for (int i = 0; i < 10; i++) { |
| 79 |
assertEquals((Integer)i, onNextEvents.get(i)); |
| 80 |
} |
| 81 |
|
| 82 |
ts.cancel(); |
| 83 |
assertFalse("Subscribers retained!", source.hasSubscribers()); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void cache() throws InterruptedException { |
| 88 |
final AtomicInteger counter = new AtomicInteger(); |
| 89 |
Flowable<String> f = Flowable.unsafeCreate(new Publisher<String>() { |
| 90 |
|
| 91 |
@Override |
| 92 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 93 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 94 |
new Thread(new Runnable() { |
| 95 |
|
| 96 |
@Override |
| 97 |
public void run() { |
| 98 |
counter.incrementAndGet(); |
| 99 |
System.out.println("published observable being executed"); |
| 100 |
subscriber.onNext("one"); |
| 101 |
subscriber.onComplete(); |
| 102 |
} |
| 103 |
}).start(); |
| 104 |
} |
| 105 |
}).cache(); |
| 106 |
|
| 107 |
// we then expect the following 2 subscriptions to get that same value |
| 108 |
final CountDownLatch latch = new CountDownLatch(2); |
| 109 |
|
| 110 |
// subscribe once |
| 111 |
f.subscribe(new Consumer<String>() { |
| 112 |
@Override |
| 113 |
public void accept(String v) { |
| 114 |
assertEquals("one", v); |
| 115 |
System.out.println("v: " + v); |
| 116 |
latch.countDown(); |
| 117 |
} |
| 118 |
}); |
| 119 |
|
| 120 |
// subscribe again |
| 121 |
f.subscribe(new Consumer<String>() { |
| 122 |
@Override |
| 123 |
public void accept(String v) { |
| 124 |
assertEquals("one", v); |
| 125 |
System.out.println("v: " + v); |
| 126 |
latch.countDown(); |
| 127 |
} |
| 128 |
}); |
| 129 |
|
| 130 |
if (!latch.await(1000, TimeUnit.MILLISECONDS)) { |
| 131 |
fail("subscriptions did not receive values"); |
| 132 |
} |
| 133 |
assertEquals(1, counter.get()); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void unsubscribeSource() throws Throwable { |
| 138 |
Action unsubscribe = mock(Action.class); |
| 139 |
Flowable<Integer> f = Flowable.just(1).doOnCancel(unsubscribe).cache(); |
| 140 |
f.subscribe(); |
| 141 |
f.subscribe(); |
| 142 |
f.subscribe(); |
| 143 |
verify(unsubscribe, never()).run(); |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void take() { |
| 148 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 149 |
|
| 150 |
FlowableCache<Integer> cached = new FlowableCache<>(Flowable.range(1, 100), 16); |
| 151 |
cached.take(10).subscribe(ts); |
| 152 |
|
| 153 |
ts.assertNoErrors(); |
| 154 |
ts.assertComplete(); |
| 155 |
ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 156 |
assertFalse(cached.hasSubscribers()); |
| 157 |
} |
| 158 |
|
| 159 |
@Test |
| 160 |
public void async() { |
| 161 |
Flowable<Integer> source = Flowable.range(1, 10000); |
| 162 |
for (int i = 0; i < 100; i++) { |
| 163 |
TestSubscriber<Integer> ts1 = new TestSubscriber<>(); |
| 164 |
|
| 165 |
FlowableCache<Integer> cached = new FlowableCache<>(source, 16); |
| 166 |
|
| 167 |
cached.observeOn(Schedulers.computation()).subscribe(ts1); |
| 168 |
|
| 169 |
ts1.awaitDone(2, TimeUnit.SECONDS); |
| 170 |
ts1.assertNoErrors(); |
| 171 |
ts1.assertComplete(); |
| 172 |
assertEquals(10000, ts1.values().size()); |
| 173 |
|
| 174 |
TestSubscriber<Integer> ts2 = new TestSubscriber<>(); |
| 175 |
cached.observeOn(Schedulers.computation()).subscribe(ts2); |
| 176 |
|
| 177 |
ts2.awaitDone(2, TimeUnit.SECONDS); |
| 178 |
ts2.assertNoErrors(); |
| 179 |
ts2.assertComplete(); |
| 180 |
assertEquals(10000, ts2.values().size()); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void asyncComeAndGo() { |
| 186 |
Flowable<Long> source = Flowable.interval(1, 1, TimeUnit.MILLISECONDS) |
| 187 |
.take(1000) |
| 188 |
.subscribeOn(Schedulers.io()); |
| 189 |
FlowableCache<Long> cached = new FlowableCache<>(source, 16); |
| 190 |
|
| 191 |
Flowable<Long> output = cached.observeOn(Schedulers.computation()); |
| 192 |
|
| 193 |
List<TestSubscriber<Long>> list = new ArrayList<>(100); |
| 194 |
for (int i = 0; i < 100; i++) { |
| 195 |
TestSubscriber<Long> ts = new TestSubscriber<>(); |
| 196 |
list.add(ts); |
| 197 |
output.skip(i * 10).take(10).subscribe(ts); |
| 198 |
} |
| 199 |
|
| 200 |
List<Long> expected = new ArrayList<>(); |
| 201 |
for (int i = 0; i < 10; i++) { |
| 202 |
expected.add((long)(i - 10)); |
| 203 |
} |
| 204 |
int j = 0; |
| 205 |
for (TestSubscriber<Long> ts : list) { |
| 206 |
ts.awaitDone(3, TimeUnit.SECONDS); |
| 207 |
ts.assertNoErrors(); |
| 208 |
ts.assertComplete(); |
| 209 |
|
| 210 |
for (int i = j * 10; i < j * 10 + 10; i++) { |
| 211 |
expected.set(i - j * 10, (long)i); |
| 212 |
} |
| 213 |
|
| 214 |
ts.assertValueSequence(expected); |
| 215 |
|
| 216 |
j++; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
@Test |
| 221 |
public void noMissingBackpressureException() { |
| 222 |
final int m = 4 * 1000 * 1000; |
| 223 |
Flowable<Integer> firehose = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 224 |
@Override |
| 225 |
public void subscribe(Subscriber<? super Integer> t) { |
| 226 |
t.onSubscribe(new BooleanSubscription()); |
| 227 |
for (int i = 0; i < m; i++) { |
| 228 |
t.onNext(i); |
| 229 |
} |
| 230 |
t.onComplete(); |
| 231 |
} |
| 232 |
}); |
| 233 |
|
| 234 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 235 |
firehose.cache().observeOn(Schedulers.computation()).takeLast(100).subscribe(ts); |
| 236 |
|
| 237 |
ts.awaitDone(3, TimeUnit.SECONDS); |
| 238 |
ts.assertNoErrors(); |
| 239 |
ts.assertComplete(); |
| 240 |
|
| 241 |
assertEquals(100, ts.values().size()); |
| 242 |
} |
| 243 |
|
| 244 |
@Test |
| 245 |
public void valuesAndThenError() { |
| 246 |
Flowable<Integer> source = Flowable.range(1, 10) |
| 247 |
.concatWith(Flowable.<Integer>error(new TestException())) |
| 248 |
.cache(); |
| 249 |
|
| 250 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 251 |
source.subscribe(ts); |
| 252 |
|
| 253 |
ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 254 |
ts.assertNotComplete(); |
| 255 |
ts.assertError(TestException.class); |
| 256 |
|
| 257 |
TestSubscriber<Integer> ts2 = new TestSubscriber<>(); |
| 258 |
source.subscribe(ts2); |
| 259 |
|
| 260 |
ts2.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 261 |
ts2.assertNotComplete(); |
| 262 |
ts2.assertError(TestException.class); |
| 263 |
} |
| 264 |
|
| 265 |
@Test |
| 266 |
public void take2() { |
| 267 |
Flowable<Integer> cache = Flowable.range(1, 5).cache(); |
| 268 |
|
| 269 |
cache.take(2).test().assertResult(1, 2); |
| 270 |
cache.take(3).test().assertResult(1, 2, 3); |
| 271 |
} |
| 272 |
|
| 273 |
@Test |
| 274 |
public void dispose() { |
| 275 |
TestHelper.checkDisposed(Flowable.range(1, 5).cache()); |
| 276 |
} |
| 277 |
|
| 278 |
@Test |
| 279 |
public void disposeOnArrival2() { |
| 280 |
Flowable<Integer> f = PublishProcessor.<Integer>create().cache(); |
| 281 |
|
| 282 |
f.test(); |
| 283 |
|
| 284 |
f.test(0L, true) |
| 285 |
.assertEmpty(); |
| 286 |
} |
| 287 |
|
| 288 |
@Test |
| 289 |
public void subscribeEmitRace() { |
| 290 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 291 |
final PublishProcessor<Integer> pp = PublishProcessor.<Integer>create(); |
| 292 |
|
| 293 |
final Flowable<Integer> cache = pp.cache(); |
| 294 |
|
| 295 |
cache.test(); |
| 296 |
|
| 297 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 298 |
|
| 299 |
Runnable r1 = new Runnable() { |
| 300 |
@Override |
| 301 |
public void run() { |
| 302 |
cache.subscribe(ts); |
| 303 |
} |
| 304 |
}; |
| 305 |
|
| 306 |
Runnable r2 = new Runnable() { |
| 307 |
@Override |
| 308 |
public void run() { |
| 309 |
for (int j = 0; j < 500; j++) { |
| 310 |
pp.onNext(j); |
| 311 |
} |
| 312 |
pp.onComplete(); |
| 313 |
} |
| 314 |
}; |
| 315 |
|
| 316 |
TestHelper.race(r1, r2); |
| 317 |
|
| 318 |
ts |
| 319 |
.awaitDone(5, TimeUnit.SECONDS) |
| 320 |
.assertSubscribed().assertValueCount(500).assertComplete().assertNoErrors(); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
@Test |
| 325 |
public void observers() { |
| 326 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 327 |
FlowableCache<Integer> cache = (FlowableCache<Integer>)Flowable.range(1, 5).concatWith(pp).cache(); |
| 328 |
|
| 329 |
assertFalse(cache.hasSubscribers()); |
| 330 |
|
| 331 |
assertEquals(0, cache.cachedEventCount()); |
| 332 |
|
| 333 |
TestSubscriber<Integer> ts = cache.test(); |
| 334 |
|
| 335 |
assertTrue(cache.hasSubscribers()); |
| 336 |
|
| 337 |
assertEquals(5, cache.cachedEventCount()); |
| 338 |
|
| 339 |
pp.onComplete(); |
| 340 |
|
| 341 |
ts.assertResult(1, 2, 3, 4, 5); |
| 342 |
} |
| 343 |
|
| 344 |
@Test |
| 345 |
public void disposeOnArrival() { |
| 346 |
Flowable.range(1, 5).cache() |
| 347 |
.test(0L, true) |
| 348 |
.assertEmpty(); |
| 349 |
} |
| 350 |
|
| 351 |
@Test |
| 352 |
public void badSource() { |
| 353 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Object>, Object>() { |
| 354 |
@Override |
| 355 |
public Object apply(Flowable<Object> f) throws Exception { |
| 356 |
return f.cache(); |
| 357 |
} |
| 358 |
}, false, 1, 1, 1); |
| 359 |
} |
| 360 |
|
| 361 |
@Test |
| 362 |
public void badRequest() { |
| 363 |
TestHelper.assertBadRequestReported(Flowable.never().cache()); |
| 364 |
} |
| 365 |
|
| 366 |
@Test |
| 367 |
public void take1() { |
| 368 |
Flowable<Integer> cache = Flowable.just(1, 2) |
| 369 |
.cache(); |
| 370 |
|
| 371 |
cache.test(); |
| 372 |
|
| 373 |
cache |
| 374 |
.take(1) |
| 375 |
.test() |
| 376 |
.assertResult(1); |
| 377 |
} |
| 378 |
|
| 379 |
@Test |
| 380 |
public void empty() { |
| 381 |
Flowable.empty() |
| 382 |
.cache() |
| 383 |
.test(0L) |
| 384 |
.assertResult(); |
| 385 |
} |
| 386 |
|
| 387 |
@Test |
| 388 |
public void error() { |
| 389 |
Flowable.error(new TestException()) |
| 390 |
.cache() |
| 391 |
.test(0L) |
| 392 |
.assertFailure(TestException.class); |
| 393 |
} |
| 394 |
|
| 395 |
@Test |
| 396 |
public void cancelledUpFrontConnectAnyway() { |
| 397 |
final AtomicInteger call = new AtomicInteger(); |
| 398 |
Flowable.fromCallable(new Callable<Object>() { |
| 399 |
@Override |
| 400 |
public Object call() throws Exception { |
| 401 |
return call.incrementAndGet(); |
| 402 |
} |
| 403 |
}) |
| 404 |
.cache() |
| 405 |
.test(1L, true) |
| 406 |
.assertNoValues(); |
| 407 |
|
| 408 |
assertEquals(1, call.get()); |
| 409 |
} |
| 410 |
|
| 411 |
@Test |
| 412 |
public void cancelledUpFront() { |
| 413 |
final AtomicInteger call = new AtomicInteger(); |
| 414 |
Flowable<Object> f = Flowable.fromCallable(new Callable<Object>() { |
| 415 |
@Override |
| 416 |
public Object call() throws Exception { |
| 417 |
return call.incrementAndGet(); |
| 418 |
} |
| 419 |
}).concatWith(Flowable.never()) |
| 420 |
.cache(); |
| 421 |
|
| 422 |
f.test().assertValuesOnly(1); |
| 423 |
|
| 424 |
f.test(1L, true) |
| 425 |
.assertEmpty(); |
| 426 |
|
| 427 |
assertEquals(1, call.get()); |
| 428 |
} |
| 429 |
|
| 430 |
@Test |
| 431 |
public void subscribeSubscribeRace() { |
| 432 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 433 |
final Flowable<Integer> cache = Flowable.range(1, 500).cache(); |
| 434 |
|
| 435 |
final TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>(); |
| 436 |
final TestSubscriberEx<Integer> ts2 = new TestSubscriberEx<>(); |
| 437 |
|
| 438 |
Runnable r1 = new Runnable() { |
| 439 |
@Override |
| 440 |
public void run() { |
| 441 |
cache.subscribe(ts1); |
| 442 |
} |
| 443 |
}; |
| 444 |
|
| 445 |
Runnable r2 = new Runnable() { |
| 446 |
@Override |
| 447 |
public void run() { |
| 448 |
cache.subscribe(ts2); |
| 449 |
} |
| 450 |
}; |
| 451 |
|
| 452 |
TestHelper.race(r1, r2); |
| 453 |
|
| 454 |
ts1 |
| 455 |
.awaitDone(5, TimeUnit.SECONDS) |
| 456 |
.assertSubscribed() |
| 457 |
.assertValueCount(500) |
| 458 |
.assertComplete() |
| 459 |
.assertNoErrors(); |
| 460 |
|
| 461 |
ts2 |
| 462 |
.awaitDone(5, TimeUnit.SECONDS) |
| 463 |
.assertSubscribed() |
| 464 |
.assertValueCount(500) |
| 465 |
.assertComplete() |
| 466 |
.assertNoErrors(); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
@Test |
| 471 |
public void subscribeCompleteRace() { |
| 472 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 473 |
final PublishProcessor<Integer> pp = PublishProcessor.<Integer>create(); |
| 474 |
|
| 475 |
final Flowable<Integer> cache = pp.cache(); |
| 476 |
|
| 477 |
cache.test(); |
| 478 |
|
| 479 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 480 |
|
| 481 |
Runnable r1 = new Runnable() { |
| 482 |
@Override |
| 483 |
public void run() { |
| 484 |
cache.subscribe(ts); |
| 485 |
} |
| 486 |
}; |
| 487 |
|
| 488 |
Runnable r2 = new Runnable() { |
| 489 |
@Override |
| 490 |
public void run() { |
| 491 |
pp.onComplete(); |
| 492 |
} |
| 493 |
}; |
| 494 |
|
| 495 |
TestHelper.race(r1, r2); |
| 496 |
|
| 497 |
ts |
| 498 |
.awaitDone(5, TimeUnit.SECONDS) |
| 499 |
.assertResult(); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
@Test |
| 504 |
public void backpressure() { |
| 505 |
Flowable.range(1, 5) |
| 506 |
.cache() |
| 507 |
.test(0) |
| 508 |
.assertEmpty() |
| 509 |
.requestMore(2) |
| 510 |
.assertValuesOnly(1, 2) |
| 511 |
.requestMore(3) |
| 512 |
.assertResult(1, 2, 3, 4, 5); |
| 513 |
} |
| 514 |
|
| 515 |
@Test |
| 516 |
public void addRemoveRace() { |
| 517 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 518 |
Flowable<Object> f = Flowable.never().cache(); |
| 519 |
|
| 520 |
TestSubscriber<Object> ts = f.test(); |
| 521 |
|
| 522 |
TestHelper.race( |
| 523 |
() -> ts.cancel(), |
| 524 |
() -> f.test() |
| 525 |
); |
| 526 |
} |
| 527 |
} |
| 528 |
} |