| 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.lang.management.ManagementFactory; |
| 22 |
import java.util.*; |
| 23 |
import java.util.concurrent.*; |
| 24 |
import java.util.concurrent.atomic.*; |
| 25 |
|
| 26 |
import org.junit.Test; |
| 27 |
import org.mockito.InOrder; |
| 28 |
import org.reactivestreams.*; |
| 29 |
|
| 30 |
import io.reactivex.rxjava3.core.*; |
| 31 |
import io.reactivex.rxjava3.disposables.*; |
| 32 |
import io.reactivex.rxjava3.exceptions.*; |
| 33 |
import io.reactivex.rxjava3.flowables.ConnectableFlowable; |
| 34 |
import io.reactivex.rxjava3.functions.*; |
| 35 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 36 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableRefCount.RefConnection; |
| 37 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 38 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 39 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 40 |
import io.reactivex.rxjava3.processors.*; |
| 41 |
import io.reactivex.rxjava3.schedulers.*; |
| 42 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 43 |
import io.reactivex.rxjava3.testsupport.*; |
| 44 |
|
| 45 |
public class FlowableRefCountTest extends RxJavaTest { |
| 46 |
|
| 47 |
@Test |
| 48 |
public void refCountAsync() throws InterruptedException { |
| 49 |
// Flaky |
| 50 |
for (int i = 0; i < 10; i++) { |
| 51 |
try { |
| 52 |
refCountAsyncActual(); |
| 53 |
return; |
| 54 |
} catch (AssertionError ex) { |
| 55 |
if (i == 9) { |
| 56 |
throw ex; |
| 57 |
} |
| 58 |
Thread.sleep((int)(200 * (Math.random() * 10 + 1))); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Tries to coordinate async counting but it is flaky due to the low 10s of milliseconds. |
| 65 |
*/ |
| 66 |
void refCountAsyncActual() { |
| 67 |
final AtomicInteger subscribeCount = new AtomicInteger(); |
| 68 |
final AtomicInteger nextCount = new AtomicInteger(); |
| 69 |
Flowable<Long> r = Flowable.interval(0, 20, TimeUnit.MILLISECONDS) |
| 70 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 71 |
@Override |
| 72 |
public void accept(Subscription s) { |
| 73 |
subscribeCount.incrementAndGet(); |
| 74 |
} |
| 75 |
}) |
| 76 |
.doOnNext(new Consumer<Long>() { |
| 77 |
@Override |
| 78 |
public void accept(Long l) { |
| 79 |
nextCount.incrementAndGet(); |
| 80 |
} |
| 81 |
}) |
| 82 |
.publish().refCount(); |
| 83 |
|
| 84 |
final AtomicInteger receivedCount = new AtomicInteger(); |
| 85 |
Disposable d1 = r.subscribe(new Consumer<Long>() { |
| 86 |
@Override |
| 87 |
public void accept(Long l) { |
| 88 |
receivedCount.incrementAndGet(); |
| 89 |
} |
| 90 |
}); |
| 91 |
|
| 92 |
Disposable d2 = r.subscribe(); |
| 93 |
|
| 94 |
try { |
| 95 |
Thread.sleep(10); |
| 96 |
} catch (InterruptedException e) { |
| 97 |
} |
| 98 |
|
| 99 |
for (;;) { |
| 100 |
int a = nextCount.get(); |
| 101 |
int b = receivedCount.get(); |
| 102 |
if (a > 10 && a < 20 && a == b) { |
| 103 |
break; |
| 104 |
} |
| 105 |
if (a >= 20) { |
| 106 |
break; |
| 107 |
} |
| 108 |
try { |
| 109 |
Thread.sleep(20); |
| 110 |
} catch (InterruptedException e) { |
| 111 |
} |
| 112 |
} |
| 113 |
// give time to emit |
| 114 |
|
| 115 |
// now unsubscribe |
| 116 |
d2.dispose(); // unsubscribe s2 first as we're counting in 1 and there can be a race between unsubscribe and one subscriber getting a value but not the other |
| 117 |
d1.dispose(); |
| 118 |
|
| 119 |
System.out.println("onNext: " + nextCount.get()); |
| 120 |
|
| 121 |
// should emit once for both subscribers |
| 122 |
assertEquals(nextCount.get(), receivedCount.get()); |
| 123 |
// only 1 subscribe |
| 124 |
assertEquals(1, subscribeCount.get()); |
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void refCountSynchronous() { |
| 129 |
final AtomicInteger subscribeCount = new AtomicInteger(); |
| 130 |
final AtomicInteger nextCount = new AtomicInteger(); |
| 131 |
Flowable<Integer> r = Flowable.just(1, 2, 3, 4, 5, 6, 7, 8, 9) |
| 132 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 133 |
@Override |
| 134 |
public void accept(Subscription s) { |
| 135 |
subscribeCount.incrementAndGet(); |
| 136 |
} |
| 137 |
}) |
| 138 |
.doOnNext(new Consumer<Integer>() { |
| 139 |
@Override |
| 140 |
public void accept(Integer l) { |
| 141 |
nextCount.incrementAndGet(); |
| 142 |
} |
| 143 |
}) |
| 144 |
.publish().refCount(); |
| 145 |
|
| 146 |
final AtomicInteger receivedCount = new AtomicInteger(); |
| 147 |
Disposable d1 = r.subscribe(new Consumer<Integer>() { |
| 148 |
@Override |
| 149 |
public void accept(Integer l) { |
| 150 |
receivedCount.incrementAndGet(); |
| 151 |
} |
| 152 |
}); |
| 153 |
|
| 154 |
Disposable d2 = r.subscribe(); |
| 155 |
|
| 156 |
// give time to emit |
| 157 |
try { |
| 158 |
Thread.sleep(50); |
| 159 |
} catch (InterruptedException e) { |
| 160 |
} |
| 161 |
|
| 162 |
// now unsubscribe |
| 163 |
d2.dispose(); // unsubscribe s2 first as we're counting in 1 and there can be a race between unsubscribe and one subscriber getting a value but not the other |
| 164 |
d1.dispose(); |
| 165 |
|
| 166 |
System.out.println("onNext Count: " + nextCount.get()); |
| 167 |
|
| 168 |
// it will emit twice because it is synchronous |
| 169 |
assertEquals(nextCount.get(), receivedCount.get() * 2); |
| 170 |
// it will subscribe twice because it is synchronous |
| 171 |
assertEquals(2, subscribeCount.get()); |
| 172 |
} |
| 173 |
|
| 174 |
@Test |
| 175 |
public void refCountSynchronousTake() { |
| 176 |
final AtomicInteger nextCount = new AtomicInteger(); |
| 177 |
Flowable<Integer> r = Flowable.just(1, 2, 3, 4, 5, 6, 7, 8, 9) |
| 178 |
.doOnNext(new Consumer<Integer>() { |
| 179 |
@Override |
| 180 |
public void accept(Integer l) { |
| 181 |
System.out.println("onNext --------> " + l); |
| 182 |
nextCount.incrementAndGet(); |
| 183 |
} |
| 184 |
}) |
| 185 |
.take(4) |
| 186 |
.publish().refCount(); |
| 187 |
|
| 188 |
final AtomicInteger receivedCount = new AtomicInteger(); |
| 189 |
r.subscribe(new Consumer<Integer>() { |
| 190 |
@Override |
| 191 |
public void accept(Integer l) { |
| 192 |
receivedCount.incrementAndGet(); |
| 193 |
} |
| 194 |
}); |
| 195 |
|
| 196 |
System.out.println("onNext: " + nextCount.get()); |
| 197 |
|
| 198 |
assertEquals(4, receivedCount.get()); |
| 199 |
assertEquals(4, receivedCount.get()); |
| 200 |
} |
| 201 |
|
| 202 |
@Test |
| 203 |
public void repeat() { |
| 204 |
final AtomicInteger subscribeCount = new AtomicInteger(); |
| 205 |
final AtomicInteger unsubscribeCount = new AtomicInteger(); |
| 206 |
Flowable<Long> r = Flowable.interval(0, 1, TimeUnit.MILLISECONDS) |
| 207 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 208 |
@Override |
| 209 |
public void accept(Subscription s) { |
| 210 |
System.out.println("******************************* Subscribe received"); |
| 211 |
// when we are subscribed |
| 212 |
subscribeCount.incrementAndGet(); |
| 213 |
} |
| 214 |
}) |
| 215 |
.doOnCancel(new Action() { |
| 216 |
@Override |
| 217 |
public void run() { |
| 218 |
System.out.println("******************************* Unsubscribe received"); |
| 219 |
// when we are unsubscribed |
| 220 |
unsubscribeCount.incrementAndGet(); |
| 221 |
} |
| 222 |
}) |
| 223 |
.publish().refCount(); |
| 224 |
|
| 225 |
for (int i = 0; i < 10; i++) { |
| 226 |
TestSubscriber<Long> ts1 = new TestSubscriber<>(); |
| 227 |
TestSubscriber<Long> ts2 = new TestSubscriber<>(); |
| 228 |
r.subscribe(ts1); |
| 229 |
r.subscribe(ts2); |
| 230 |
try { |
| 231 |
Thread.sleep(50); |
| 232 |
} catch (InterruptedException e) { |
| 233 |
} |
| 234 |
ts1.cancel(); |
| 235 |
ts2.cancel(); |
| 236 |
ts1.assertNoErrors(); |
| 237 |
ts2.assertNoErrors(); |
| 238 |
assertTrue(ts1.values().size() > 0); |
| 239 |
assertTrue(ts2.values().size() > 0); |
| 240 |
} |
| 241 |
|
| 242 |
assertEquals(10, subscribeCount.get()); |
| 243 |
assertEquals(10, unsubscribeCount.get()); |
| 244 |
} |
| 245 |
|
| 246 |
@Test |
| 247 |
public void connectUnsubscribe() throws InterruptedException { |
| 248 |
final CountDownLatch unsubscribeLatch = new CountDownLatch(1); |
| 249 |
final CountDownLatch subscribeLatch = new CountDownLatch(1); |
| 250 |
|
| 251 |
Flowable<Long> f = synchronousInterval() |
| 252 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 253 |
@Override |
| 254 |
public void accept(Subscription s) { |
| 255 |
System.out.println("******************************* Subscribe received"); |
| 256 |
// when we are subscribed |
| 257 |
subscribeLatch.countDown(); |
| 258 |
} |
| 259 |
}) |
| 260 |
.doOnCancel(new Action() { |
| 261 |
@Override |
| 262 |
public void run() { |
| 263 |
System.out.println("******************************* Unsubscribe received"); |
| 264 |
// when we are unsubscribed |
| 265 |
unsubscribeLatch.countDown(); |
| 266 |
} |
| 267 |
}); |
| 268 |
|
| 269 |
TestSubscriberEx<Long> s = new TestSubscriberEx<>(); |
| 270 |
f.publish().refCount().subscribeOn(Schedulers.newThread()).subscribe(s); |
| 271 |
System.out.println("send unsubscribe"); |
| 272 |
// wait until connected |
| 273 |
subscribeLatch.await(); |
| 274 |
// now unsubscribe |
| 275 |
s.cancel(); |
| 276 |
System.out.println("DONE sending unsubscribe ... now waiting"); |
| 277 |
if (!unsubscribeLatch.await(3000, TimeUnit.MILLISECONDS)) { |
| 278 |
System.out.println("Errors: " + s.errors()); |
| 279 |
if (s.errors().size() > 0) { |
| 280 |
s.errors().get(0).printStackTrace(); |
| 281 |
} |
| 282 |
fail("timed out waiting for unsubscribe"); |
| 283 |
} |
| 284 |
s.assertNoErrors(); |
| 285 |
} |
| 286 |
|
| 287 |
@Test |
| 288 |
public void connectUnsubscribeRaceConditionLoop() throws InterruptedException { |
| 289 |
for (int i = 0; i < 100; i++) { |
| 290 |
connectUnsubscribeRaceCondition(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
@Test |
| 295 |
public void connectUnsubscribeRaceCondition() throws InterruptedException { |
| 296 |
final AtomicInteger subUnsubCount = new AtomicInteger(); |
| 297 |
Flowable<Long> f = synchronousInterval() |
| 298 |
.doOnCancel(new Action() { |
| 299 |
@Override |
| 300 |
public void run() { |
| 301 |
System.out.println("******************************* Unsubscribe received"); |
| 302 |
// when we are unsubscribed |
| 303 |
subUnsubCount.decrementAndGet(); |
| 304 |
} |
| 305 |
}) |
| 306 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 307 |
@Override |
| 308 |
public void accept(Subscription s) { |
| 309 |
System.out.println("******************************* SUBSCRIBE received"); |
| 310 |
subUnsubCount.incrementAndGet(); |
| 311 |
} |
| 312 |
}); |
| 313 |
|
| 314 |
TestSubscriberEx<Long> s = new TestSubscriberEx<>(); |
| 315 |
|
| 316 |
f.publish().refCount().subscribeOn(Schedulers.computation()).subscribe(s); |
| 317 |
System.out.println("send unsubscribe"); |
| 318 |
// now immediately unsubscribe while subscribeOn is racing to subscribe |
| 319 |
s.cancel(); |
| 320 |
// this generally will mean it won't even subscribe as it is already unsubscribed by the time connect() gets scheduled |
| 321 |
// give time to the counter to update |
| 322 |
Thread.sleep(10); |
| 323 |
// either we subscribed and then unsubscribed, or we didn't ever even subscribe |
| 324 |
assertEquals(0, subUnsubCount.get()); |
| 325 |
|
| 326 |
System.out.println("DONE sending unsubscribe ... now waiting"); |
| 327 |
System.out.println("Errors: " + s.errors()); |
| 328 |
if (s.errors().size() > 0) { |
| 329 |
s.errors().get(0).printStackTrace(); |
| 330 |
} |
| 331 |
s.assertNoErrors(); |
| 332 |
} |
| 333 |
|
| 334 |
private Flowable<Long> synchronousInterval() { |
| 335 |
return Flowable.unsafeCreate(new Publisher<Long>() { |
| 336 |
@Override |
| 337 |
public void subscribe(Subscriber<? super Long> subscriber) { |
| 338 |
final AtomicBoolean cancel = new AtomicBoolean(); |
| 339 |
subscriber.onSubscribe(new Subscription() { |
| 340 |
@Override |
| 341 |
public void request(long n) { |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
public void cancel() { |
| 347 |
cancel.set(true); |
| 348 |
} |
| 349 |
|
| 350 |
}); |
| 351 |
for (;;) { |
| 352 |
if (cancel.get()) { |
| 353 |
break; |
| 354 |
} |
| 355 |
try { |
| 356 |
Thread.sleep(100); |
| 357 |
} catch (InterruptedException e) { |
| 358 |
} |
| 359 |
subscriber.onNext(1L); |
| 360 |
} |
| 361 |
} |
| 362 |
}); |
| 363 |
} |
| 364 |
|
| 365 |
@Test |
| 366 |
public void onlyFirstShouldSubscribeAndLastUnsubscribe() { |
| 367 |
final AtomicInteger subscriptionCount = new AtomicInteger(); |
| 368 |
final AtomicInteger unsubscriptionCount = new AtomicInteger(); |
| 369 |
Flowable<Integer> flowable = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 370 |
@Override |
| 371 |
public void subscribe(Subscriber<? super Integer> subscriber) { |
| 372 |
subscriptionCount.incrementAndGet(); |
| 373 |
subscriber.onSubscribe(new Subscription() { |
| 374 |
@Override |
| 375 |
public void request(long n) { |
| 376 |
|
| 377 |
} |
| 378 |
|
| 379 |
@Override |
| 380 |
public void cancel() { |
| 381 |
unsubscriptionCount.incrementAndGet(); |
| 382 |
} |
| 383 |
}); |
| 384 |
} |
| 385 |
}); |
| 386 |
Flowable<Integer> refCounted = flowable.publish().refCount(); |
| 387 |
|
| 388 |
Disposable first = refCounted.subscribe(); |
| 389 |
assertEquals(1, subscriptionCount.get()); |
| 390 |
|
| 391 |
Disposable second = refCounted.subscribe(); |
| 392 |
assertEquals(1, subscriptionCount.get()); |
| 393 |
|
| 394 |
first.dispose(); |
| 395 |
assertEquals(0, unsubscriptionCount.get()); |
| 396 |
|
| 397 |
second.dispose(); |
| 398 |
assertEquals(1, unsubscriptionCount.get()); |
| 399 |
} |
| 400 |
|
| 401 |
@Test |
| 402 |
public void refCount() { |
| 403 |
TestScheduler s = new TestScheduler(); |
| 404 |
Flowable<Long> interval = Flowable.interval(100, TimeUnit.MILLISECONDS, s).publish().refCount(); |
| 405 |
|
| 406 |
// subscribe list1 |
| 407 |
final List<Long> list1 = new ArrayList<>(); |
| 408 |
Disposable d1 = interval.subscribe(new Consumer<Long>() { |
| 409 |
@Override |
| 410 |
public void accept(Long t1) { |
| 411 |
list1.add(t1); |
| 412 |
} |
| 413 |
}); |
| 414 |
|
| 415 |
s.advanceTimeBy(200, TimeUnit.MILLISECONDS); |
| 416 |
|
| 417 |
assertEquals(2, list1.size()); |
| 418 |
assertEquals(0L, list1.get(0).longValue()); |
| 419 |
assertEquals(1L, list1.get(1).longValue()); |
| 420 |
|
| 421 |
// subscribe list2 |
| 422 |
final List<Long> list2 = new ArrayList<>(); |
| 423 |
Disposable d2 = interval.subscribe(new Consumer<Long>() { |
| 424 |
@Override |
| 425 |
public void accept(Long t1) { |
| 426 |
list2.add(t1); |
| 427 |
} |
| 428 |
}); |
| 429 |
|
| 430 |
s.advanceTimeBy(300, TimeUnit.MILLISECONDS); |
| 431 |
|
| 432 |
// list 1 should have 5 items |
| 433 |
assertEquals(5, list1.size()); |
| 434 |
assertEquals(2L, list1.get(2).longValue()); |
| 435 |
assertEquals(3L, list1.get(3).longValue()); |
| 436 |
assertEquals(4L, list1.get(4).longValue()); |
| 437 |
|
| 438 |
// list 2 should only have 3 items |
| 439 |
assertEquals(3, list2.size()); |
| 440 |
assertEquals(2L, list2.get(0).longValue()); |
| 441 |
assertEquals(3L, list2.get(1).longValue()); |
| 442 |
assertEquals(4L, list2.get(2).longValue()); |
| 443 |
|
| 444 |
// unsubscribe list1 |
| 445 |
d1.dispose(); |
| 446 |
|
| 447 |
// advance further |
| 448 |
s.advanceTimeBy(300, TimeUnit.MILLISECONDS); |
| 449 |
|
| 450 |
// list 1 should still have 5 items |
| 451 |
assertEquals(5, list1.size()); |
| 452 |
|
| 453 |
// list 2 should have 6 items |
| 454 |
assertEquals(6, list2.size()); |
| 455 |
assertEquals(5L, list2.get(3).longValue()); |
| 456 |
assertEquals(6L, list2.get(4).longValue()); |
| 457 |
assertEquals(7L, list2.get(5).longValue()); |
| 458 |
|
| 459 |
// unsubscribe list2 |
| 460 |
d2.dispose(); |
| 461 |
|
| 462 |
// advance further |
| 463 |
s.advanceTimeBy(1000, TimeUnit.MILLISECONDS); |
| 464 |
|
| 465 |
// subscribing a new one should start over because the source should have been unsubscribed |
| 466 |
// subscribe list3 |
| 467 |
final List<Long> list3 = new ArrayList<>(); |
| 468 |
interval.subscribe(new Consumer<Long>() { |
| 469 |
@Override |
| 470 |
public void accept(Long t1) { |
| 471 |
list3.add(t1); |
| 472 |
} |
| 473 |
}); |
| 474 |
|
| 475 |
s.advanceTimeBy(200, TimeUnit.MILLISECONDS); |
| 476 |
|
| 477 |
assertEquals(2, list3.size()); |
| 478 |
assertEquals(0L, list3.get(0).longValue()); |
| 479 |
assertEquals(1L, list3.get(1).longValue()); |
| 480 |
|
| 481 |
} |
| 482 |
|
| 483 |
@Test |
| 484 |
public void alreadyUnsubscribedClient() { |
| 485 |
Subscriber<Integer> done = CancelledSubscriber.INSTANCE; |
| 486 |
|
| 487 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 488 |
|
| 489 |
Flowable<Integer> result = Flowable.just(1).publish().refCount(); |
| 490 |
|
| 491 |
result.subscribe(done); |
| 492 |
|
| 493 |
result.subscribe(subscriber); |
| 494 |
|
| 495 |
verify(subscriber).onNext(1); |
| 496 |
verify(subscriber).onComplete(); |
| 497 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 498 |
} |
| 499 |
|
| 500 |
@Test |
| 501 |
public void alreadyUnsubscribedInterleavesWithClient() { |
| 502 |
ReplayProcessor<Integer> source = ReplayProcessor.create(); |
| 503 |
|
| 504 |
Subscriber<Integer> done = CancelledSubscriber.INSTANCE; |
| 505 |
|
| 506 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 507 |
InOrder inOrder = inOrder(subscriber); |
| 508 |
|
| 509 |
Flowable<Integer> result = source.publish().refCount(); |
| 510 |
|
| 511 |
result.subscribe(subscriber); |
| 512 |
|
| 513 |
source.onNext(1); |
| 514 |
|
| 515 |
result.subscribe(done); |
| 516 |
|
| 517 |
source.onNext(2); |
| 518 |
source.onComplete(); |
| 519 |
|
| 520 |
inOrder.verify(subscriber).onNext(1); |
| 521 |
inOrder.verify(subscriber).onNext(2); |
| 522 |
inOrder.verify(subscriber).onComplete(); |
| 523 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 524 |
} |
| 525 |
|
| 526 |
@Test |
| 527 |
public void connectDisconnectConnectAndSubjectState() { |
| 528 |
Flowable<Integer> f1 = Flowable.just(10); |
| 529 |
Flowable<Integer> f2 = Flowable.just(20); |
| 530 |
Flowable<Integer> combined = Flowable.combineLatest(f1, f2, new BiFunction<Integer, Integer, Integer>() { |
| 531 |
@Override |
| 532 |
public Integer apply(Integer t1, Integer t2) { |
| 533 |
return t1 + t2; |
| 534 |
} |
| 535 |
}) |
| 536 |
.publish().refCount(); |
| 537 |
|
| 538 |
TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>(); |
| 539 |
TestSubscriberEx<Integer> ts2 = new TestSubscriberEx<>(); |
| 540 |
|
| 541 |
combined.subscribe(ts1); |
| 542 |
combined.subscribe(ts2); |
| 543 |
|
| 544 |
ts1.assertTerminated(); |
| 545 |
ts1.assertNoErrors(); |
| 546 |
ts1.assertValue(30); |
| 547 |
|
| 548 |
ts2.assertTerminated(); |
| 549 |
ts2.assertNoErrors(); |
| 550 |
ts2.assertValue(30); |
| 551 |
} |
| 552 |
|
| 553 |
@Test |
| 554 |
public void upstreamErrorAllowsRetry() throws InterruptedException { |
| 555 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 556 |
try { |
| 557 |
final AtomicInteger intervalSubscribed = new AtomicInteger(); |
| 558 |
Flowable<String> interval = |
| 559 |
Flowable.interval(200, TimeUnit.MILLISECONDS) |
| 560 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 561 |
@Override |
| 562 |
public void accept(Subscription s) { |
| 563 |
System.out.println("Subscribing to interval " + intervalSubscribed.incrementAndGet()); |
| 564 |
} |
| 565 |
} |
| 566 |
) |
| 567 |
.flatMap(new Function<Long, Publisher<String>>() { |
| 568 |
@Override |
| 569 |
public Publisher<String> apply(Long t1) { |
| 570 |
return Flowable.defer(new Supplier<Publisher<String>>() { |
| 571 |
@Override |
| 572 |
public Publisher<String> get() { |
| 573 |
return Flowable.<String>error(new TestException("Some exception")); |
| 574 |
} |
| 575 |
}); |
| 576 |
} |
| 577 |
}) |
| 578 |
.onErrorResumeNext(new Function<Throwable, Publisher<String>>() { |
| 579 |
@Override |
| 580 |
public Publisher<String> apply(Throwable t1) { |
| 581 |
return Flowable.error(t1); |
| 582 |
} |
| 583 |
}) |
| 584 |
.publish() |
| 585 |
.refCount(); |
| 586 |
|
| 587 |
interval |
| 588 |
.doOnError(new Consumer<Throwable>() { |
| 589 |
@Override |
| 590 |
public void accept(Throwable t1) { |
| 591 |
System.out.println("Subscriber 1 onError: " + t1); |
| 592 |
} |
| 593 |
}) |
| 594 |
.retry(5) |
| 595 |
.subscribe(new Consumer<String>() { |
| 596 |
@Override |
| 597 |
public void accept(String t1) { |
| 598 |
System.out.println("Subscriber 1: " + t1); |
| 599 |
} |
| 600 |
}); |
| 601 |
Thread.sleep(100); |
| 602 |
interval |
| 603 |
.doOnError(new Consumer<Throwable>() { |
| 604 |
@Override |
| 605 |
public void accept(Throwable t1) { |
| 606 |
System.out.println("Subscriber 2 onError: " + t1); |
| 607 |
} |
| 608 |
}) |
| 609 |
.retry(5) |
| 610 |
.subscribe(new Consumer<String>() { |
| 611 |
@Override |
| 612 |
public void accept(String t1) { |
| 613 |
System.out.println("Subscriber 2: " + t1); |
| 614 |
} |
| 615 |
}); |
| 616 |
|
| 617 |
Thread.sleep(1300); |
| 618 |
|
| 619 |
System.out.println(intervalSubscribed.get()); |
| 620 |
assertEquals(6, intervalSubscribed.get()); |
| 621 |
|
| 622 |
TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class); |
| 623 |
} finally { |
| 624 |
RxJavaPlugins.reset(); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
private enum CancelledSubscriber implements FlowableSubscriber<Integer> { |
| 629 |
INSTANCE; |
| 630 |
|
| 631 |
@Override public void onSubscribe(Subscription s) { |
| 632 |
s.cancel(); |
| 633 |
} |
| 634 |
|
| 635 |
@Override public void onNext(Integer o) { |
| 636 |
} |
| 637 |
|
| 638 |
@Override public void onError(Throwable t) { |
| 639 |
} |
| 640 |
|
| 641 |
@Override public void onComplete() { |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
@Test |
| 646 |
public void disposed() { |
| 647 |
TestHelper.checkDisposed(Flowable.just(1).publish().refCount()); |
| 648 |
} |
| 649 |
|
| 650 |
@Test |
| 651 |
public void noOpConnect() { |
| 652 |
final int[] calls = { 0 }; |
| 653 |
Flowable<Integer> f = new ConnectableFlowable<Integer>() { |
| 654 |
@Override |
| 655 |
public void connect(Consumer<? super Disposable> connection) { |
| 656 |
calls[0]++; |
| 657 |
} |
| 658 |
|
| 659 |
@Override |
| 660 |
public void reset() { |
| 661 |
// nothing to do in this test |
| 662 |
} |
| 663 |
|
| 664 |
@Override |
| 665 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 666 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 667 |
} |
| 668 |
}.refCount(); |
| 669 |
|
| 670 |
f.test(); |
| 671 |
f.test(); |
| 672 |
|
| 673 |
assertEquals(1, calls[0]); |
| 674 |
} |
| 675 |
|
| 676 |
Flowable<Object> source; |
| 677 |
|
| 678 |
@Test |
| 679 |
public void replayNoLeak() throws Exception { |
| 680 |
Thread.sleep(100); |
| 681 |
System.gc(); |
| 682 |
Thread.sleep(100); |
| 683 |
|
| 684 |
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 685 |
|
| 686 |
source = Flowable.fromCallable(new Callable<Object>() { |
| 687 |
@Override |
| 688 |
public Object call() throws Exception { |
| 689 |
return new byte[100 * 1000 * 1000]; |
| 690 |
} |
| 691 |
}) |
| 692 |
.replay(1) |
| 693 |
.refCount(); |
| 694 |
|
| 695 |
source.subscribe(); |
| 696 |
|
| 697 |
long after = TestHelper.awaitGC(GC_SLEEP_TIME, 20, start + 20 * 1000 * 1000); |
| 698 |
|
| 699 |
source = null; |
| 700 |
assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 701 |
} |
| 702 |
|
| 703 |
@Test |
| 704 |
public void replayNoLeak2() throws Exception { |
| 705 |
Thread.sleep(100); |
| 706 |
System.gc(); |
| 707 |
Thread.sleep(100); |
| 708 |
|
| 709 |
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 710 |
|
| 711 |
source = Flowable.fromCallable(new Callable<Object>() { |
| 712 |
@Override |
| 713 |
public Object call() throws Exception { |
| 714 |
return new byte[100 * 1000 * 1000]; |
| 715 |
} |
| 716 |
}).concatWith(Flowable.never()) |
| 717 |
.replay(1) |
| 718 |
.refCount(); |
| 719 |
|
| 720 |
Disposable d1 = source.subscribe(); |
| 721 |
Disposable d2 = source.subscribe(); |
| 722 |
|
| 723 |
d1.dispose(); |
| 724 |
d2.dispose(); |
| 725 |
|
| 726 |
d1 = null; |
| 727 |
d2 = null; |
| 728 |
|
| 729 |
long after = TestHelper.awaitGC(GC_SLEEP_TIME, 20, start + 20 * 1000 * 1000); |
| 730 |
|
| 731 |
source = null; |
| 732 |
assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 733 |
} |
| 734 |
|
| 735 |
static final class ExceptionData extends Exception { |
| 736 |
private static final long serialVersionUID = -6763898015338136119L; |
| 737 |
|
| 738 |
public final Object data; |
| 739 |
|
| 740 |
ExceptionData(Object data) { |
| 741 |
this.data = data; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
static final int GC_SLEEP_TIME = 250; |
| 746 |
|
| 747 |
@Test |
| 748 |
public void publishNoLeak() throws Exception { |
| 749 |
System.gc(); |
| 750 |
Thread.sleep(GC_SLEEP_TIME); |
| 751 |
|
| 752 |
source = Flowable.fromCallable(new Callable<Object>() { |
| 753 |
@Override |
| 754 |
public Object call() throws Exception { |
| 755 |
throw new ExceptionData(new byte[100 * 1000 * 1000]); |
| 756 |
} |
| 757 |
}) |
| 758 |
.publish() |
| 759 |
.refCount(); |
| 760 |
|
| 761 |
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 762 |
|
| 763 |
source.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer()); |
| 764 |
|
| 765 |
long after = TestHelper.awaitGC(GC_SLEEP_TIME, 20, start + 20 * 1000 * 1000); |
| 766 |
|
| 767 |
source = null; |
| 768 |
|
| 769 |
assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 770 |
} |
| 771 |
|
| 772 |
@Test |
| 773 |
public void publishNoLeak2() throws Exception { |
| 774 |
System.gc(); |
| 775 |
Thread.sleep(GC_SLEEP_TIME); |
| 776 |
|
| 777 |
long start = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed(); |
| 778 |
|
| 779 |
source = Flowable.fromCallable(new Callable<Object>() { |
| 780 |
@Override |
| 781 |
public Object call() throws Exception { |
| 782 |
return new byte[100 * 1000 * 1000]; |
| 783 |
} |
| 784 |
}).concatWith(Flowable.never()) |
| 785 |
.publish() |
| 786 |
.refCount(); |
| 787 |
|
| 788 |
TestSubscriber<Object> d1 = source.test(); |
| 789 |
TestSubscriber<Object> d2 = source.test(); |
| 790 |
|
| 791 |
d1.cancel(); |
| 792 |
d2.cancel(); |
| 793 |
|
| 794 |
d1 = null; |
| 795 |
d2 = null; |
| 796 |
|
| 797 |
long after = TestHelper.awaitGC(GC_SLEEP_TIME, 20, start + 20 * 1000 * 1000); |
| 798 |
|
| 799 |
source = null; |
| 800 |
assertTrue(String.format("%,3d -> %,3d%n", start, after), start + 20 * 1000 * 1000 > after); |
| 801 |
} |
| 802 |
|
| 803 |
@Test |
| 804 |
public void replayIsUnsubscribed() { |
| 805 |
ConnectableFlowable<Integer> cf = Flowable.just(1) |
| 806 |
.replay(); |
| 807 |
|
| 808 |
if (cf instanceof Disposable) { |
| 809 |
assertTrue(((Disposable)cf).isDisposed()); |
| 810 |
|
| 811 |
Disposable connection = cf.connect(); |
| 812 |
|
| 813 |
assertFalse(((Disposable)cf).isDisposed()); |
| 814 |
|
| 815 |
connection.dispose(); |
| 816 |
|
| 817 |
assertTrue(((Disposable)cf).isDisposed()); |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
static final class BadFlowableSubscribe extends ConnectableFlowable<Object> { |
| 822 |
|
| 823 |
@Override |
| 824 |
public void connect(Consumer<? super Disposable> connection) { |
| 825 |
try { |
| 826 |
connection.accept(Disposable.empty()); |
| 827 |
} catch (Throwable ex) { |
| 828 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
@Override |
| 833 |
public void reset() { |
| 834 |
// nothing to do in this test |
| 835 |
} |
| 836 |
|
| 837 |
@Override |
| 838 |
protected void subscribeActual(Subscriber<? super Object> subscriber) { |
| 839 |
throw new TestException("subscribeActual"); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
static final class BadFlowableDispose extends ConnectableFlowable<Object> { |
| 844 |
|
| 845 |
@Override |
| 846 |
public void connect(Consumer<? super Disposable> connection) { |
| 847 |
try { |
| 848 |
connection.accept(Disposable.empty()); |
| 849 |
} catch (Throwable ex) { |
| 850 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
@Override |
| 855 |
public void reset() { |
| 856 |
throw new TestException("dispose"); |
| 857 |
} |
| 858 |
|
| 859 |
@Override |
| 860 |
protected void subscribeActual(Subscriber<? super Object> subscriber) { |
| 861 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
static final class BadFlowableConnect extends ConnectableFlowable<Object> { |
| 866 |
|
| 867 |
@Override |
| 868 |
public void connect(Consumer<? super Disposable> connection) { |
| 869 |
throw new TestException("connect"); |
| 870 |
} |
| 871 |
|
| 872 |
@Override |
| 873 |
public void reset() { |
| 874 |
// nothing to do in this test |
| 875 |
} |
| 876 |
|
| 877 |
@Override |
| 878 |
protected void subscribeActual(Subscriber<? super Object> subscriber) { |
| 879 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
@Test |
| 884 |
public void badSourceSubscribe() { |
| 885 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 886 |
try { |
| 887 |
BadFlowableSubscribe bo = new BadFlowableSubscribe(); |
| 888 |
|
| 889 |
try { |
| 890 |
bo.refCount() |
| 891 |
.test(); |
| 892 |
fail("Should have thrown"); |
| 893 |
} catch (NullPointerException ex) { |
| 894 |
assertTrue(ex.getCause() instanceof TestException); |
| 895 |
} |
| 896 |
|
| 897 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 898 |
} finally { |
| 899 |
RxJavaPlugins.reset(); |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
@Test |
| 904 |
public void badSourceDispose() { |
| 905 |
BadFlowableDispose bf = new BadFlowableDispose(); |
| 906 |
|
| 907 |
try { |
| 908 |
bf.refCount() |
| 909 |
.test() |
| 910 |
.cancel(); |
| 911 |
fail("Should have thrown"); |
| 912 |
} catch (TestException expected) { |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
@Test |
| 917 |
public void badSourceConnect() { |
| 918 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 919 |
try { |
| 920 |
BadFlowableConnect bf = new BadFlowableConnect(); |
| 921 |
|
| 922 |
try { |
| 923 |
bf.refCount() |
| 924 |
.test(); |
| 925 |
fail("Should have thrown"); |
| 926 |
} catch (NullPointerException ex) { |
| 927 |
assertTrue(ex.getCause() instanceof TestException); |
| 928 |
} |
| 929 |
|
| 930 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 931 |
} finally { |
| 932 |
RxJavaPlugins.reset(); |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
static final class BadFlowableSubscribe2 extends ConnectableFlowable<Object> { |
| 937 |
|
| 938 |
int count; |
| 939 |
|
| 940 |
@Override |
| 941 |
public void connect(Consumer<? super Disposable> connection) { |
| 942 |
try { |
| 943 |
connection.accept(Disposable.empty()); |
| 944 |
} catch (Throwable ex) { |
| 945 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
@Override |
| 950 |
public void reset() { |
| 951 |
// nothing to do in this test |
| 952 |
} |
| 953 |
|
| 954 |
@Override |
| 955 |
protected void subscribeActual(Subscriber<? super Object> subscriber) { |
| 956 |
if (++count == 1) { |
| 957 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 958 |
} else { |
| 959 |
throw new TestException("subscribeActual"); |
| 960 |
} |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
@Test |
| 965 |
public void badSourceSubscribe2() { |
| 966 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 967 |
try { |
| 968 |
BadFlowableSubscribe2 bf = new BadFlowableSubscribe2(); |
| 969 |
|
| 970 |
Flowable<Object> f = bf.refCount(); |
| 971 |
f.test(); |
| 972 |
try { |
| 973 |
f.test(); |
| 974 |
fail("Should have thrown"); |
| 975 |
} catch (NullPointerException ex) { |
| 976 |
assertTrue(ex.getCause() instanceof TestException); |
| 977 |
} |
| 978 |
|
| 979 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 980 |
} finally { |
| 981 |
RxJavaPlugins.reset(); |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
static final class BadFlowableConnect2 extends ConnectableFlowable<Object> { |
| 986 |
|
| 987 |
@Override |
| 988 |
public void connect(Consumer<? super Disposable> connection) { |
| 989 |
try { |
| 990 |
connection.accept(Disposable.empty()); |
| 991 |
} catch (Throwable ex) { |
| 992 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
@Override |
| 997 |
public void reset() { |
| 998 |
throw new TestException("dispose"); |
| 999 |
} |
| 1000 |
|
| 1001 |
@Override |
| 1002 |
protected void subscribeActual(Subscriber<? super Object> subscriber) { |
| 1003 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1004 |
subscriber.onComplete(); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
@Test |
| 1009 |
public void badSourceCompleteDisconnect() { |
| 1010 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1011 |
try { |
| 1012 |
BadFlowableConnect2 bf = new BadFlowableConnect2(); |
| 1013 |
|
| 1014 |
try { |
| 1015 |
bf.refCount() |
| 1016 |
.test(); |
| 1017 |
fail("Should have thrown"); |
| 1018 |
} catch (NullPointerException ex) { |
| 1019 |
assertTrue(ex.getCause() instanceof TestException); |
| 1020 |
} |
| 1021 |
|
| 1022 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1023 |
} finally { |
| 1024 |
RxJavaPlugins.reset(); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
@Test |
| 1029 |
public void blockingSourceAsnycCancel() throws Exception { |
| 1030 |
BehaviorProcessor<Integer> bp = BehaviorProcessor.createDefault(1); |
| 1031 |
|
| 1032 |
Flowable<Integer> f = bp |
| 1033 |
.replay(1) |
| 1034 |
.refCount(); |
| 1035 |
|
| 1036 |
f.subscribe(); |
| 1037 |
|
| 1038 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 1039 |
|
| 1040 |
f.switchMap(new Function<Integer, Publisher<? extends Object>>() { |
| 1041 |
@Override |
| 1042 |
public Publisher<? extends Object> apply(Integer v) throws Exception { |
| 1043 |
return Flowable.create(new FlowableOnSubscribe<Object>() { |
| 1044 |
@Override |
| 1045 |
public void subscribe(FlowableEmitter<Object> emitter) throws Exception { |
| 1046 |
while (!emitter.isCancelled()) { |
| 1047 |
Thread.sleep(100); |
| 1048 |
} |
| 1049 |
interrupted.set(true); |
| 1050 |
} |
| 1051 |
}, BackpressureStrategy.MISSING); |
| 1052 |
} |
| 1053 |
}) |
| 1054 |
.takeUntil(Flowable.timer(500, TimeUnit.MILLISECONDS)) |
| 1055 |
.test() |
| 1056 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1057 |
.assertResult(); |
| 1058 |
|
| 1059 |
assertTrue(interrupted.get()); |
| 1060 |
} |
| 1061 |
|
| 1062 |
@Test |
| 1063 |
public void byCount() { |
| 1064 |
final int[] subscriptions = { 0 }; |
| 1065 |
|
| 1066 |
Flowable<Integer> source = Flowable.range(1, 5) |
| 1067 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 1068 |
@Override |
| 1069 |
public void accept(Subscription s) throws Exception { |
| 1070 |
subscriptions[0]++; |
| 1071 |
} |
| 1072 |
}) |
| 1073 |
.publish() |
| 1074 |
.refCount(2); |
| 1075 |
|
| 1076 |
for (int i = 0; i < 3; i++) { |
| 1077 |
TestSubscriber<Integer> ts1 = source.test(); |
| 1078 |
|
| 1079 |
ts1.assertEmpty(); |
| 1080 |
|
| 1081 |
TestSubscriber<Integer> ts2 = source.test(); |
| 1082 |
|
| 1083 |
ts1.assertResult(1, 2, 3, 4, 5); |
| 1084 |
ts2.assertResult(1, 2, 3, 4, 5); |
| 1085 |
} |
| 1086 |
|
| 1087 |
assertEquals(3, subscriptions[0]); |
| 1088 |
} |
| 1089 |
|
| 1090 |
@Test |
| 1091 |
public void resubscribeBeforeTimeout() throws Exception { |
| 1092 |
final int[] subscriptions = { 0 }; |
| 1093 |
|
| 1094 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1095 |
|
| 1096 |
Flowable<Integer> source = pp |
| 1097 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 1098 |
@Override |
| 1099 |
public void accept(Subscription s) throws Exception { |
| 1100 |
subscriptions[0]++; |
| 1101 |
} |
| 1102 |
}) |
| 1103 |
.publish() |
| 1104 |
.refCount(500, TimeUnit.MILLISECONDS); |
| 1105 |
|
| 1106 |
TestSubscriber<Integer> ts1 = source.test(0); |
| 1107 |
|
| 1108 |
assertEquals(1, subscriptions[0]); |
| 1109 |
|
| 1110 |
ts1.cancel(); |
| 1111 |
|
| 1112 |
Thread.sleep(100); |
| 1113 |
|
| 1114 |
ts1 = source.test(0); |
| 1115 |
|
| 1116 |
assertEquals(1, subscriptions[0]); |
| 1117 |
|
| 1118 |
Thread.sleep(500); |
| 1119 |
|
| 1120 |
assertEquals(1, subscriptions[0]); |
| 1121 |
|
| 1122 |
pp.onNext(1); |
| 1123 |
pp.onNext(2); |
| 1124 |
pp.onNext(3); |
| 1125 |
pp.onNext(4); |
| 1126 |
pp.onNext(5); |
| 1127 |
pp.onComplete(); |
| 1128 |
|
| 1129 |
ts1.requestMore(5) |
| 1130 |
.assertResult(1, 2, 3, 4, 5); |
| 1131 |
} |
| 1132 |
|
| 1133 |
@Test |
| 1134 |
public void letitTimeout() throws Exception { |
| 1135 |
final int[] subscriptions = { 0 }; |
| 1136 |
|
| 1137 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1138 |
|
| 1139 |
Flowable<Integer> source = pp |
| 1140 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 1141 |
@Override |
| 1142 |
public void accept(Subscription s) throws Exception { |
| 1143 |
subscriptions[0]++; |
| 1144 |
} |
| 1145 |
}) |
| 1146 |
.publish() |
| 1147 |
.refCount(1, 100, TimeUnit.MILLISECONDS); |
| 1148 |
|
| 1149 |
TestSubscriber<Integer> ts1 = source.test(0); |
| 1150 |
|
| 1151 |
assertEquals(1, subscriptions[0]); |
| 1152 |
|
| 1153 |
ts1.cancel(); |
| 1154 |
|
| 1155 |
assertTrue(pp.hasSubscribers()); |
| 1156 |
|
| 1157 |
Thread.sleep(200); |
| 1158 |
|
| 1159 |
assertFalse(pp.hasSubscribers()); |
| 1160 |
} |
| 1161 |
|
| 1162 |
@Test |
| 1163 |
public void error() { |
| 1164 |
Flowable.<Integer>error(new IOException()) |
| 1165 |
.publish() |
| 1166 |
.refCount(500, TimeUnit.MILLISECONDS) |
| 1167 |
.test() |
| 1168 |
.assertFailure(IOException.class); |
| 1169 |
} |
| 1170 |
|
| 1171 |
@Test |
| 1172 |
public void comeAndGo() { |
| 1173 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1174 |
|
| 1175 |
Flowable<Integer> source = pp |
| 1176 |
.publish() |
| 1177 |
.refCount(1); |
| 1178 |
|
| 1179 |
TestSubscriber<Integer> ts1 = source.test(0); |
| 1180 |
|
| 1181 |
assertTrue(pp.hasSubscribers()); |
| 1182 |
|
| 1183 |
for (int i = 0; i < 3; i++) { |
| 1184 |
TestSubscriber<Integer> ts2 = source.test(); |
| 1185 |
ts1.cancel(); |
| 1186 |
ts1 = ts2; |
| 1187 |
} |
| 1188 |
|
| 1189 |
ts1.cancel(); |
| 1190 |
|
| 1191 |
assertFalse(pp.hasSubscribers()); |
| 1192 |
} |
| 1193 |
|
| 1194 |
@Test |
| 1195 |
public void unsubscribeSubscribeRace() { |
| 1196 |
for (int i = 0; i < 1000; i++) { |
| 1197 |
|
| 1198 |
final Flowable<Integer> source = Flowable.range(1, 5) |
| 1199 |
.replay() |
| 1200 |
.refCount(1) |
| 1201 |
; |
| 1202 |
|
| 1203 |
final TestSubscriber<Integer> ts1 = source.test(0); |
| 1204 |
|
| 1205 |
final TestSubscriber<Integer> ts2 = new TestSubscriber<>(0); |
| 1206 |
|
| 1207 |
Runnable r1 = new Runnable() { |
| 1208 |
@Override |
| 1209 |
public void run() { |
| 1210 |
ts1.cancel(); |
| 1211 |
} |
| 1212 |
}; |
| 1213 |
|
| 1214 |
Runnable r2 = new Runnable() { |
| 1215 |
@Override |
| 1216 |
public void run() { |
| 1217 |
source.subscribe(ts2); |
| 1218 |
} |
| 1219 |
}; |
| 1220 |
|
| 1221 |
TestHelper.race(r1, r2, Schedulers.single()); |
| 1222 |
|
| 1223 |
ts2.requestMore(6) // FIXME RxJava replay() doesn't issue onComplete without request |
| 1224 |
.withTag("Round: " + i) |
| 1225 |
.assertResult(1, 2, 3, 4, 5); |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
static final class BadFlowableDoubleOnX extends ConnectableFlowable<Object> |
| 1230 |
implements Disposable { |
| 1231 |
|
| 1232 |
@Override |
| 1233 |
public void connect(Consumer<? super Disposable> connection) { |
| 1234 |
try { |
| 1235 |
connection.accept(Disposable.empty()); |
| 1236 |
} catch (Throwable ex) { |
| 1237 |
throw ExceptionHelper.wrapOrThrow(ex); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
@Override |
| 1242 |
public void reset() { |
| 1243 |
// nothing to do in this test |
| 1244 |
} |
| 1245 |
|
| 1246 |
@Override |
| 1247 |
protected void subscribeActual(Subscriber<? super Object> subscriber) { |
| 1248 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1249 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1250 |
subscriber.onComplete(); |
| 1251 |
subscriber.onComplete(); |
| 1252 |
subscriber.onError(new TestException()); |
| 1253 |
} |
| 1254 |
|
| 1255 |
@Override |
| 1256 |
public void dispose() { |
| 1257 |
} |
| 1258 |
|
| 1259 |
@Override |
| 1260 |
public boolean isDisposed() { |
| 1261 |
return false; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
@Test |
| 1266 |
public void doubleOnX() { |
| 1267 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1268 |
try { |
| 1269 |
new BadFlowableDoubleOnX() |
| 1270 |
.refCount() |
| 1271 |
.test() |
| 1272 |
.assertResult(); |
| 1273 |
|
| 1274 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 1275 |
TestHelper.assertUndeliverable(errors, 1, TestException.class); |
| 1276 |
} finally { |
| 1277 |
RxJavaPlugins.reset(); |
| 1278 |
} |
| 1279 |
} |
| 1280 |
|
| 1281 |
@Test |
| 1282 |
public void doubleOnXCount() { |
| 1283 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1284 |
try { |
| 1285 |
new BadFlowableDoubleOnX() |
| 1286 |
.refCount(1) |
| 1287 |
.test() |
| 1288 |
.assertResult(); |
| 1289 |
|
| 1290 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 1291 |
TestHelper.assertUndeliverable(errors, 1, TestException.class); |
| 1292 |
} finally { |
| 1293 |
RxJavaPlugins.reset(); |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
@Test |
| 1298 |
public void doubleOnXTime() { |
| 1299 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1300 |
try { |
| 1301 |
new BadFlowableDoubleOnX() |
| 1302 |
.refCount(5, TimeUnit.SECONDS, Schedulers.single()) |
| 1303 |
.test() |
| 1304 |
.assertResult(); |
| 1305 |
|
| 1306 |
TestHelper.assertError(errors, 0, ProtocolViolationException.class); |
| 1307 |
TestHelper.assertUndeliverable(errors, 1, TestException.class); |
| 1308 |
} finally { |
| 1309 |
RxJavaPlugins.reset(); |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
@Test |
| 1314 |
public void cancelTerminateStateExclusion() { |
| 1315 |
FlowableRefCount<Object> o = (FlowableRefCount<Object>)PublishProcessor.create() |
| 1316 |
.publish() |
| 1317 |
.refCount(); |
| 1318 |
|
| 1319 |
o.cancel(null); |
| 1320 |
|
| 1321 |
RefConnection rc = new RefConnection(o); |
| 1322 |
o.connection = null; |
| 1323 |
rc.subscriberCount = 0; |
| 1324 |
o.timeout(rc); |
| 1325 |
|
| 1326 |
rc.subscriberCount = 1; |
| 1327 |
o.timeout(rc); |
| 1328 |
|
| 1329 |
o.connection = rc; |
| 1330 |
o.timeout(rc); |
| 1331 |
|
| 1332 |
rc.subscriberCount = 0; |
| 1333 |
o.timeout(rc); |
| 1334 |
|
| 1335 |
// ------------------- |
| 1336 |
|
| 1337 |
rc.subscriberCount = 2; |
| 1338 |
rc.connected = false; |
| 1339 |
o.connection = rc; |
| 1340 |
o.cancel(rc); |
| 1341 |
|
| 1342 |
rc.subscriberCount = 1; |
| 1343 |
rc.connected = false; |
| 1344 |
o.connection = rc; |
| 1345 |
o.cancel(rc); |
| 1346 |
|
| 1347 |
rc.subscriberCount = 2; |
| 1348 |
rc.connected = true; |
| 1349 |
o.connection = rc; |
| 1350 |
o.cancel(rc); |
| 1351 |
|
| 1352 |
rc.subscriberCount = 1; |
| 1353 |
rc.connected = true; |
| 1354 |
o.connection = rc; |
| 1355 |
rc.set(null); |
| 1356 |
o.cancel(rc); |
| 1357 |
|
| 1358 |
o.connection = rc; |
| 1359 |
o.cancel(new RefConnection(o)); |
| 1360 |
} |
| 1361 |
|
| 1362 |
@Test |
| 1363 |
public void replayRefCountShallBeThreadSafe() { |
| 1364 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 1365 |
Flowable<Integer> flowable = Flowable.just(1).replay(1).refCount(); |
| 1366 |
|
| 1367 |
TestSubscriber<Integer> ts1 = flowable |
| 1368 |
.subscribeOn(Schedulers.io()) |
| 1369 |
.test(); |
| 1370 |
|
| 1371 |
TestSubscriber<Integer> ts2 = flowable |
| 1372 |
.subscribeOn(Schedulers.io()) |
| 1373 |
.test(); |
| 1374 |
|
| 1375 |
ts1 |
| 1376 |
.withTag("" + i) |
| 1377 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1378 |
.assertResult(1); |
| 1379 |
|
| 1380 |
ts2 |
| 1381 |
.withTag("" + i) |
| 1382 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1383 |
.assertResult(1); |
| 1384 |
} |
| 1385 |
} |
| 1386 |
|
| 1387 |
static final class TestConnectableFlowable<T> extends ConnectableFlowable<T> { |
| 1388 |
|
| 1389 |
volatile boolean reset; |
| 1390 |
|
| 1391 |
@Override |
| 1392 |
public void reset() { |
| 1393 |
reset = true; |
| 1394 |
} |
| 1395 |
|
| 1396 |
@Override |
| 1397 |
public void connect(Consumer<? super Disposable> connection) { |
| 1398 |
// not relevant |
| 1399 |
} |
| 1400 |
|
| 1401 |
@Override |
| 1402 |
protected void subscribeActual(Subscriber<? super T> subscriber) { |
| 1403 |
// not relevant |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
@Test |
| 1408 |
public void timeoutResetsSource() { |
| 1409 |
TestConnectableFlowable<Object> tcf = new TestConnectableFlowable<>(); |
| 1410 |
FlowableRefCount<Object> o = (FlowableRefCount<Object>)tcf.refCount(); |
| 1411 |
|
| 1412 |
RefConnection rc = new RefConnection(o); |
| 1413 |
rc.set(Disposable.empty()); |
| 1414 |
o.connection = rc; |
| 1415 |
|
| 1416 |
o.timeout(rc); |
| 1417 |
|
| 1418 |
assertTrue(tcf.reset); |
| 1419 |
} |
| 1420 |
|
| 1421 |
@Test |
| 1422 |
public void disconnectBeforeConnect() { |
| 1423 |
BehaviorProcessor<Integer> processor = BehaviorProcessor.create(); |
| 1424 |
|
| 1425 |
Flowable<Integer> flowable = processor |
| 1426 |
.replay(1) |
| 1427 |
.refCount(); |
| 1428 |
|
| 1429 |
flowable.takeUntil(Flowable.just(1)).test(); |
| 1430 |
|
| 1431 |
processor.onNext(2); |
| 1432 |
|
| 1433 |
flowable.take(1).test().assertResult(2); |
| 1434 |
} |
| 1435 |
|
| 1436 |
@Test |
| 1437 |
public void publishRefCountShallBeThreadSafe() { |
| 1438 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 1439 |
Flowable<Integer> flowable = Flowable.just(1).publish().refCount(); |
| 1440 |
|
| 1441 |
TestSubscriber<Integer> subscriber1 = flowable |
| 1442 |
.subscribeOn(Schedulers.io()) |
| 1443 |
.test(); |
| 1444 |
|
| 1445 |
TestSubscriber<Integer> subscriber2 = flowable |
| 1446 |
.subscribeOn(Schedulers.io()) |
| 1447 |
.test(); |
| 1448 |
|
| 1449 |
subscriber1 |
| 1450 |
.withTag("subscriber1 " + i) |
| 1451 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1452 |
.assertNoErrors() |
| 1453 |
.assertComplete(); |
| 1454 |
|
| 1455 |
subscriber2 |
| 1456 |
.withTag("subscriber2 " + i) |
| 1457 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1458 |
.assertNoErrors() |
| 1459 |
.assertComplete(); |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
@Test |
| 1464 |
public void upstreamTerminationTriggersAnotherCancel() throws Exception { |
| 1465 |
ReplayProcessor<Integer> rp = ReplayProcessor.create(); |
| 1466 |
rp.onNext(1); |
| 1467 |
rp.onComplete(); |
| 1468 |
|
| 1469 |
Flowable<Integer> shared = rp.share(); |
| 1470 |
|
| 1471 |
shared |
| 1472 |
.buffer(shared.debounce(5, TimeUnit.SECONDS)) |
| 1473 |
.test() |
| 1474 |
.assertValueCount(2); |
| 1475 |
|
| 1476 |
shared |
| 1477 |
.buffer(shared.debounce(5, TimeUnit.SECONDS)) |
| 1478 |
.test() |
| 1479 |
.assertValueCount(2); |
| 1480 |
}} |