| 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.lang.management.*; |
| 21 |
import java.util.*; |
| 22 |
import java.util.concurrent.*; |
| 23 |
import java.util.concurrent.atomic.*; |
| 24 |
|
| 25 |
import org.junit.*; |
| 26 |
import org.mockito.InOrder; |
| 27 |
import org.reactivestreams.*; |
| 28 |
|
| 29 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 30 |
import io.reactivex.rxjava3.core.*; |
| 31 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 32 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 33 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 34 |
import io.reactivex.rxjava3.flowables.ConnectableFlowable; |
| 35 |
import io.reactivex.rxjava3.functions.*; |
| 36 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 37 |
import io.reactivex.rxjava3.internal.fuseable.HasUpstreamPublisher; |
| 38 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableReplay.*; |
| 39 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 40 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 41 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 42 |
import io.reactivex.rxjava3.schedulers.*; |
| 43 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 44 |
import io.reactivex.rxjava3.testsupport.*; |
| 45 |
|
| 46 |
public class FlowableReplayEagerTruncateTest extends RxJavaTest { |
| 47 |
@Test |
| 48 |
public void bufferedReplay() { |
| 49 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 50 |
|
| 51 |
ConnectableFlowable<Integer> cf = source.replay(3, true); |
| 52 |
cf.connect(); |
| 53 |
|
| 54 |
{ |
| 55 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 56 |
InOrder inOrder = inOrder(subscriber1); |
| 57 |
|
| 58 |
cf.subscribe(subscriber1); |
| 59 |
|
| 60 |
source.onNext(1); |
| 61 |
source.onNext(2); |
| 62 |
source.onNext(3); |
| 63 |
|
| 64 |
inOrder.verify(subscriber1, times(1)).onNext(1); |
| 65 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 66 |
inOrder.verify(subscriber1, times(1)).onNext(3); |
| 67 |
|
| 68 |
source.onNext(4); |
| 69 |
source.onComplete(); |
| 70 |
inOrder.verify(subscriber1, times(1)).onNext(4); |
| 71 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 72 |
inOrder.verifyNoMoreInteractions(); |
| 73 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
{ |
| 78 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 79 |
InOrder inOrder = inOrder(subscriber1); |
| 80 |
|
| 81 |
cf.subscribe(subscriber1); |
| 82 |
|
| 83 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 84 |
inOrder.verify(subscriber1, times(1)).onNext(3); |
| 85 |
inOrder.verify(subscriber1, times(1)).onNext(4); |
| 86 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 87 |
inOrder.verifyNoMoreInteractions(); |
| 88 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void bufferedWindowReplay() { |
| 94 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 95 |
TestScheduler scheduler = new TestScheduler(); |
| 96 |
ConnectableFlowable<Integer> cf = source.replay(3, 100, TimeUnit.MILLISECONDS, scheduler, true); |
| 97 |
cf.connect(); |
| 98 |
|
| 99 |
{ |
| 100 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 101 |
InOrder inOrder = inOrder(subscriber1); |
| 102 |
|
| 103 |
cf.subscribe(subscriber1); |
| 104 |
|
| 105 |
source.onNext(1); |
| 106 |
scheduler.advanceTimeBy(10, TimeUnit.MILLISECONDS); |
| 107 |
source.onNext(2); |
| 108 |
scheduler.advanceTimeBy(10, TimeUnit.MILLISECONDS); |
| 109 |
source.onNext(3); |
| 110 |
scheduler.advanceTimeBy(10, TimeUnit.MILLISECONDS); |
| 111 |
|
| 112 |
inOrder.verify(subscriber1, times(1)).onNext(1); |
| 113 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 114 |
inOrder.verify(subscriber1, times(1)).onNext(3); |
| 115 |
|
| 116 |
source.onNext(4); |
| 117 |
source.onNext(5); |
| 118 |
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS); |
| 119 |
|
| 120 |
inOrder.verify(subscriber1, times(1)).onNext(4); |
| 121 |
|
| 122 |
inOrder.verify(subscriber1, times(1)).onNext(5); |
| 123 |
|
| 124 |
inOrder.verifyNoMoreInteractions(); |
| 125 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
{ |
| 130 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 131 |
InOrder inOrder = inOrder(subscriber1); |
| 132 |
|
| 133 |
cf.subscribe(subscriber1); |
| 134 |
|
| 135 |
inOrder.verify(subscriber1, times(1)).onNext(4); |
| 136 |
inOrder.verify(subscriber1, times(1)).onNext(5); |
| 137 |
inOrder.verifyNoMoreInteractions(); |
| 138 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void windowedReplay() { |
| 144 |
TestScheduler scheduler = new TestScheduler(); |
| 145 |
|
| 146 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 147 |
|
| 148 |
ConnectableFlowable<Integer> cf = source.replay(100, TimeUnit.MILLISECONDS, scheduler, true); |
| 149 |
cf.connect(); |
| 150 |
|
| 151 |
{ |
| 152 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 153 |
InOrder inOrder = inOrder(subscriber1); |
| 154 |
|
| 155 |
cf.subscribe(subscriber1); |
| 156 |
|
| 157 |
source.onNext(1); |
| 158 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 159 |
source.onNext(2); |
| 160 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 161 |
source.onNext(3); |
| 162 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 163 |
source.onComplete(); |
| 164 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 165 |
|
| 166 |
inOrder.verify(subscriber1, times(1)).onNext(1); |
| 167 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 168 |
inOrder.verify(subscriber1, times(1)).onNext(3); |
| 169 |
|
| 170 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 171 |
inOrder.verifyNoMoreInteractions(); |
| 172 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 173 |
|
| 174 |
} |
| 175 |
{ |
| 176 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 177 |
InOrder inOrder = inOrder(subscriber1); |
| 178 |
|
| 179 |
cf.subscribe(subscriber1); |
| 180 |
inOrder.verify(subscriber1, never()).onNext(3); |
| 181 |
|
| 182 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 183 |
inOrder.verifyNoMoreInteractions(); |
| 184 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
@Test |
| 189 |
public void replaySelector() { |
| 190 |
final Function<Integer, Integer> dbl = new Function<Integer, Integer>() { |
| 191 |
|
| 192 |
@Override |
| 193 |
public Integer apply(Integer t1) { |
| 194 |
return t1 * 2; |
| 195 |
} |
| 196 |
|
| 197 |
}; |
| 198 |
|
| 199 |
Function<Flowable<Integer>, Flowable<Integer>> selector = new Function<Flowable<Integer>, Flowable<Integer>>() { |
| 200 |
|
| 201 |
@Override |
| 202 |
public Flowable<Integer> apply(Flowable<Integer> t1) { |
| 203 |
return t1.map(dbl); |
| 204 |
} |
| 205 |
|
| 206 |
}; |
| 207 |
|
| 208 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 209 |
|
| 210 |
Flowable<Integer> co = source.replay(selector); |
| 211 |
|
| 212 |
{ |
| 213 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 214 |
InOrder inOrder = inOrder(subscriber1); |
| 215 |
|
| 216 |
co.subscribe(subscriber1); |
| 217 |
|
| 218 |
source.onNext(1); |
| 219 |
source.onNext(2); |
| 220 |
source.onNext(3); |
| 221 |
|
| 222 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 223 |
inOrder.verify(subscriber1, times(1)).onNext(4); |
| 224 |
inOrder.verify(subscriber1, times(1)).onNext(6); |
| 225 |
|
| 226 |
source.onNext(4); |
| 227 |
source.onComplete(); |
| 228 |
inOrder.verify(subscriber1, times(1)).onNext(8); |
| 229 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 230 |
inOrder.verifyNoMoreInteractions(); |
| 231 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
{ |
| 236 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 237 |
InOrder inOrder = inOrder(subscriber1); |
| 238 |
|
| 239 |
co.subscribe(subscriber1); |
| 240 |
|
| 241 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 242 |
inOrder.verifyNoMoreInteractions(); |
| 243 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
@Test |
| 250 |
public void bufferedReplaySelector() { |
| 251 |
|
| 252 |
final Function<Integer, Integer> dbl = new Function<Integer, Integer>() { |
| 253 |
|
| 254 |
@Override |
| 255 |
public Integer apply(Integer t1) { |
| 256 |
return t1 * 2; |
| 257 |
} |
| 258 |
|
| 259 |
}; |
| 260 |
|
| 261 |
Function<Flowable<Integer>, Flowable<Integer>> selector = new Function<Flowable<Integer>, Flowable<Integer>>() { |
| 262 |
|
| 263 |
@Override |
| 264 |
public Flowable<Integer> apply(Flowable<Integer> t1) { |
| 265 |
return t1.map(dbl); |
| 266 |
} |
| 267 |
|
| 268 |
}; |
| 269 |
|
| 270 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 271 |
|
| 272 |
Flowable<Integer> co = source.replay(selector, 3, true); |
| 273 |
|
| 274 |
{ |
| 275 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 276 |
InOrder inOrder = inOrder(subscriber1); |
| 277 |
|
| 278 |
co.subscribe(subscriber1); |
| 279 |
|
| 280 |
source.onNext(1); |
| 281 |
source.onNext(2); |
| 282 |
source.onNext(3); |
| 283 |
|
| 284 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 285 |
inOrder.verify(subscriber1, times(1)).onNext(4); |
| 286 |
inOrder.verify(subscriber1, times(1)).onNext(6); |
| 287 |
|
| 288 |
source.onNext(4); |
| 289 |
source.onComplete(); |
| 290 |
inOrder.verify(subscriber1, times(1)).onNext(8); |
| 291 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 292 |
inOrder.verifyNoMoreInteractions(); |
| 293 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
{ |
| 298 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 299 |
InOrder inOrder = inOrder(subscriber1); |
| 300 |
|
| 301 |
co.subscribe(subscriber1); |
| 302 |
|
| 303 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 304 |
inOrder.verifyNoMoreInteractions(); |
| 305 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
@Test |
| 310 |
public void windowedReplaySelector() { |
| 311 |
|
| 312 |
final Function<Integer, Integer> dbl = new Function<Integer, Integer>() { |
| 313 |
|
| 314 |
@Override |
| 315 |
public Integer apply(Integer t1) { |
| 316 |
return t1 * 2; |
| 317 |
} |
| 318 |
|
| 319 |
}; |
| 320 |
|
| 321 |
Function<Flowable<Integer>, Flowable<Integer>> selector = new Function<Flowable<Integer>, Flowable<Integer>>() { |
| 322 |
|
| 323 |
@Override |
| 324 |
public Flowable<Integer> apply(Flowable<Integer> t1) { |
| 325 |
return t1.map(dbl); |
| 326 |
} |
| 327 |
|
| 328 |
}; |
| 329 |
|
| 330 |
TestScheduler scheduler = new TestScheduler(); |
| 331 |
|
| 332 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 333 |
|
| 334 |
Flowable<Integer> co = source.replay(selector, 100, TimeUnit.MILLISECONDS, scheduler, true); |
| 335 |
|
| 336 |
{ |
| 337 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 338 |
InOrder inOrder = inOrder(subscriber1); |
| 339 |
|
| 340 |
co.subscribe(subscriber1); |
| 341 |
|
| 342 |
source.onNext(1); |
| 343 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 344 |
source.onNext(2); |
| 345 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 346 |
source.onNext(3); |
| 347 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 348 |
source.onComplete(); |
| 349 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 350 |
|
| 351 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 352 |
inOrder.verify(subscriber1, times(1)).onNext(4); |
| 353 |
inOrder.verify(subscriber1, times(1)).onNext(6); |
| 354 |
|
| 355 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 356 |
inOrder.verifyNoMoreInteractions(); |
| 357 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 358 |
|
| 359 |
} |
| 360 |
{ |
| 361 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 362 |
InOrder inOrder = inOrder(subscriber1); |
| 363 |
|
| 364 |
co.subscribe(subscriber1); |
| 365 |
|
| 366 |
inOrder.verify(subscriber1, times(1)).onComplete(); |
| 367 |
inOrder.verifyNoMoreInteractions(); |
| 368 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
@Test |
| 373 |
public void bufferedReplayError() { |
| 374 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 375 |
|
| 376 |
ConnectableFlowable<Integer> cf = source.replay(3, true); |
| 377 |
cf.connect(); |
| 378 |
|
| 379 |
{ |
| 380 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 381 |
InOrder inOrder = inOrder(subscriber1); |
| 382 |
|
| 383 |
cf.subscribe(subscriber1); |
| 384 |
|
| 385 |
source.onNext(1); |
| 386 |
source.onNext(2); |
| 387 |
source.onNext(3); |
| 388 |
|
| 389 |
inOrder.verify(subscriber1, times(1)).onNext(1); |
| 390 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 391 |
inOrder.verify(subscriber1, times(1)).onNext(3); |
| 392 |
|
| 393 |
source.onNext(4); |
| 394 |
source.onError(new RuntimeException("Forced failure")); |
| 395 |
|
| 396 |
inOrder.verify(subscriber1, times(1)).onNext(4); |
| 397 |
inOrder.verify(subscriber1, times(1)).onError(any(RuntimeException.class)); |
| 398 |
inOrder.verifyNoMoreInteractions(); |
| 399 |
verify(subscriber1, never()).onComplete(); |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
{ |
| 404 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 405 |
InOrder inOrder = inOrder(subscriber1); |
| 406 |
|
| 407 |
cf.subscribe(subscriber1); |
| 408 |
|
| 409 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 410 |
inOrder.verify(subscriber1, times(1)).onNext(3); |
| 411 |
inOrder.verify(subscriber1, times(1)).onNext(4); |
| 412 |
inOrder.verify(subscriber1, times(1)).onError(any(RuntimeException.class)); |
| 413 |
inOrder.verifyNoMoreInteractions(); |
| 414 |
verify(subscriber1, never()).onComplete(); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
@Test |
| 419 |
public void windowedReplayError() { |
| 420 |
TestScheduler scheduler = new TestScheduler(); |
| 421 |
|
| 422 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 423 |
|
| 424 |
ConnectableFlowable<Integer> cf = source.replay(100, TimeUnit.MILLISECONDS, scheduler, true); |
| 425 |
cf.connect(); |
| 426 |
|
| 427 |
{ |
| 428 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 429 |
InOrder inOrder = inOrder(subscriber1); |
| 430 |
|
| 431 |
cf.subscribe(subscriber1); |
| 432 |
|
| 433 |
source.onNext(1); |
| 434 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 435 |
source.onNext(2); |
| 436 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 437 |
source.onNext(3); |
| 438 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 439 |
source.onError(new RuntimeException("Forced failure")); |
| 440 |
scheduler.advanceTimeBy(60, TimeUnit.MILLISECONDS); |
| 441 |
|
| 442 |
inOrder.verify(subscriber1, times(1)).onNext(1); |
| 443 |
inOrder.verify(subscriber1, times(1)).onNext(2); |
| 444 |
inOrder.verify(subscriber1, times(1)).onNext(3); |
| 445 |
|
| 446 |
inOrder.verify(subscriber1, times(1)).onError(any(RuntimeException.class)); |
| 447 |
inOrder.verifyNoMoreInteractions(); |
| 448 |
verify(subscriber1, never()).onComplete(); |
| 449 |
|
| 450 |
} |
| 451 |
{ |
| 452 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 453 |
InOrder inOrder = inOrder(subscriber1); |
| 454 |
|
| 455 |
cf.subscribe(subscriber1); |
| 456 |
inOrder.verify(subscriber1, never()).onNext(3); |
| 457 |
|
| 458 |
inOrder.verify(subscriber1, times(1)).onError(any(RuntimeException.class)); |
| 459 |
inOrder.verifyNoMoreInteractions(); |
| 460 |
verify(subscriber1, never()).onComplete(); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
@Test |
| 465 |
public void synchronousDisconnect() { |
| 466 |
final AtomicInteger effectCounter = new AtomicInteger(); |
| 467 |
Flowable<Integer> source = Flowable.just(1, 2, 3, 4) |
| 468 |
.doOnNext(new Consumer<Integer>() { |
| 469 |
@Override |
| 470 |
public void accept(Integer v) { |
| 471 |
effectCounter.incrementAndGet(); |
| 472 |
System.out.println("Sideeffect #" + v); |
| 473 |
} |
| 474 |
}); |
| 475 |
|
| 476 |
Flowable<Integer> result = source.replay( |
| 477 |
new Function<Flowable<Integer>, Flowable<Integer>>() { |
| 478 |
@Override |
| 479 |
public Flowable<Integer> apply(Flowable<Integer> f) { |
| 480 |
return f.take(2); |
| 481 |
} |
| 482 |
}); |
| 483 |
|
| 484 |
for (int i = 1; i < 3; i++) { |
| 485 |
effectCounter.set(0); |
| 486 |
System.out.printf("- %d -%n", i); |
| 487 |
result.subscribe(new Consumer<Integer>() { |
| 488 |
|
| 489 |
@Override |
| 490 |
public void accept(Integer t1) { |
| 491 |
System.out.println(t1); |
| 492 |
} |
| 493 |
|
| 494 |
}, new Consumer<Throwable>() { |
| 495 |
|
| 496 |
@Override |
| 497 |
public void accept(Throwable t1) { |
| 498 |
t1.printStackTrace(); |
| 499 |
} |
| 500 |
}, |
| 501 |
new Action() { |
| 502 |
@Override |
| 503 |
public void run() { |
| 504 |
System.out.println("Done"); |
| 505 |
} |
| 506 |
}); |
| 507 |
assertEquals(2, effectCounter.get()); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
/* |
| 512 |
* test the basic expectation of OperatorMulticast via replay |
| 513 |
*/ |
| 514 |
@SuppressWarnings("unchecked") |
| 515 |
@Test |
| 516 |
public void issue2191_UnsubscribeSource() throws Throwable { |
| 517 |
// setup mocks |
| 518 |
Consumer<Integer> sourceNext = mock(Consumer.class); |
| 519 |
Action sourceCompleted = mock(Action.class); |
| 520 |
Action sourceUnsubscribed = mock(Action.class); |
| 521 |
Subscriber<Integer> spiedSubscriberBeforeConnect = TestHelper.mockSubscriber(); |
| 522 |
Subscriber<Integer> spiedSubscriberAfterConnect = TestHelper.mockSubscriber(); |
| 523 |
|
| 524 |
// Flowable under test |
| 525 |
Flowable<Integer> source = Flowable.just(1, 2); |
| 526 |
|
| 527 |
ConnectableFlowable<Integer> replay = source |
| 528 |
.doOnNext(sourceNext) |
| 529 |
.doOnCancel(sourceUnsubscribed) |
| 530 |
.doOnComplete(sourceCompleted) |
| 531 |
.replay(); |
| 532 |
|
| 533 |
replay.subscribe(spiedSubscriberBeforeConnect); |
| 534 |
replay.subscribe(spiedSubscriberBeforeConnect); |
| 535 |
replay.connect(); |
| 536 |
replay.subscribe(spiedSubscriberAfterConnect); |
| 537 |
replay.subscribe(spiedSubscriberAfterConnect); |
| 538 |
|
| 539 |
verify(spiedSubscriberBeforeConnect, times(2)).onSubscribe((Subscription)any()); |
| 540 |
verify(spiedSubscriberAfterConnect, times(2)).onSubscribe((Subscription)any()); |
| 541 |
|
| 542 |
// verify interactions |
| 543 |
verify(sourceNext, times(1)).accept(1); |
| 544 |
verify(sourceNext, times(1)).accept(2); |
| 545 |
verify(sourceCompleted, times(1)).run(); |
| 546 |
verifyObserverMock(spiedSubscriberBeforeConnect, 2, 4); |
| 547 |
verifyObserverMock(spiedSubscriberAfterConnect, 2, 4); |
| 548 |
|
| 549 |
verify(sourceUnsubscribed, never()).run(); |
| 550 |
|
| 551 |
verifyNoMoreInteractions(sourceNext); |
| 552 |
verifyNoMoreInteractions(sourceCompleted); |
| 553 |
verifyNoMoreInteractions(sourceUnsubscribed); |
| 554 |
verifyNoMoreInteractions(spiedSubscriberBeforeConnect); |
| 555 |
verifyNoMoreInteractions(spiedSubscriberAfterConnect); |
| 556 |
|
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Specifically test interaction with a Scheduler with subscribeOn. |
| 561 |
* |
| 562 |
* @throws Throwable functional interfaces declare throws Exception |
| 563 |
*/ |
| 564 |
@SuppressWarnings("unchecked") |
| 565 |
@Test |
| 566 |
public void issue2191_SchedulerUnsubscribe() throws Throwable { |
| 567 |
// setup mocks |
| 568 |
Consumer<Integer> sourceNext = mock(Consumer.class); |
| 569 |
Action sourceCompleted = mock(Action.class); |
| 570 |
Action sourceUnsubscribed = mock(Action.class); |
| 571 |
final Scheduler mockScheduler = mock(Scheduler.class); |
| 572 |
final Disposable mockSubscription = mock(Disposable.class); |
| 573 |
Worker spiedWorker = workerSpy(mockSubscription); |
| 574 |
Subscriber<Integer> mockObserverBeforeConnect = TestHelper.mockSubscriber(); |
| 575 |
Subscriber<Integer> mockObserverAfterConnect = TestHelper.mockSubscriber(); |
| 576 |
|
| 577 |
when(mockScheduler.createWorker()).thenReturn(spiedWorker); |
| 578 |
|
| 579 |
// Flowable under test |
| 580 |
ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3) |
| 581 |
.doOnNext(sourceNext) |
| 582 |
.doOnCancel(sourceUnsubscribed) |
| 583 |
.doOnComplete(sourceCompleted) |
| 584 |
.subscribeOn(mockScheduler).replay(); |
| 585 |
|
| 586 |
replay.subscribe(mockObserverBeforeConnect); |
| 587 |
replay.subscribe(mockObserverBeforeConnect); |
| 588 |
replay.connect(); |
| 589 |
replay.subscribe(mockObserverAfterConnect); |
| 590 |
replay.subscribe(mockObserverAfterConnect); |
| 591 |
|
| 592 |
verify(mockObserverBeforeConnect, times(2)).onSubscribe((Subscription)any()); |
| 593 |
verify(mockObserverAfterConnect, times(2)).onSubscribe((Subscription)any()); |
| 594 |
|
| 595 |
// verify interactions |
| 596 |
verify(sourceNext, times(1)).accept(1); |
| 597 |
verify(sourceNext, times(1)).accept(2); |
| 598 |
verify(sourceNext, times(1)).accept(3); |
| 599 |
verify(sourceCompleted, times(1)).run(); |
| 600 |
verify(mockScheduler, times(1)).createWorker(); |
| 601 |
verify(spiedWorker, times(1)).schedule((Runnable)notNull()); |
| 602 |
verifyObserverMock(mockObserverBeforeConnect, 2, 6); |
| 603 |
verifyObserverMock(mockObserverAfterConnect, 2, 6); |
| 604 |
|
| 605 |
// FIXME publish calls cancel too |
| 606 |
verify(spiedWorker, times(1)).dispose(); |
| 607 |
verify(sourceUnsubscribed, never()).run(); |
| 608 |
|
| 609 |
verifyNoMoreInteractions(sourceNext); |
| 610 |
verifyNoMoreInteractions(sourceCompleted); |
| 611 |
verifyNoMoreInteractions(sourceUnsubscribed); |
| 612 |
verifyNoMoreInteractions(spiedWorker); |
| 613 |
verifyNoMoreInteractions(mockSubscription); |
| 614 |
verifyNoMoreInteractions(mockScheduler); |
| 615 |
verifyNoMoreInteractions(mockObserverBeforeConnect); |
| 616 |
verifyNoMoreInteractions(mockObserverAfterConnect); |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Specifically test interaction with a Scheduler with subscribeOn. |
| 621 |
* |
| 622 |
* @throws Throwable functional interfaces declare throws Exception |
| 623 |
*/ |
| 624 |
@SuppressWarnings("unchecked") |
| 625 |
@Test |
| 626 |
public void issue2191_SchedulerUnsubscribeOnError() throws Throwable { |
| 627 |
// setup mocks |
| 628 |
Consumer<Integer> sourceNext = mock(Consumer.class); |
| 629 |
Action sourceCompleted = mock(Action.class); |
| 630 |
Consumer<Throwable> sourceError = mock(Consumer.class); |
| 631 |
Action sourceUnsubscribed = mock(Action.class); |
| 632 |
final Scheduler mockScheduler = mock(Scheduler.class); |
| 633 |
final Disposable mockSubscription = mock(Disposable.class); |
| 634 |
Worker spiedWorker = workerSpy(mockSubscription); |
| 635 |
Subscriber<Integer> mockObserverBeforeConnect = TestHelper.mockSubscriber(); |
| 636 |
Subscriber<Integer> mockObserverAfterConnect = TestHelper.mockSubscriber(); |
| 637 |
|
| 638 |
when(mockScheduler.createWorker()).thenReturn(spiedWorker); |
| 639 |
|
| 640 |
// Flowable under test |
| 641 |
Function<Integer, Integer> mockFunc = mock(Function.class); |
| 642 |
IllegalArgumentException illegalArgumentException = new IllegalArgumentException(); |
| 643 |
when(mockFunc.apply(1)).thenReturn(1); |
| 644 |
when(mockFunc.apply(2)).thenThrow(illegalArgumentException); |
| 645 |
ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3).map(mockFunc) |
| 646 |
.doOnNext(sourceNext) |
| 647 |
.doOnCancel(sourceUnsubscribed) |
| 648 |
.doOnComplete(sourceCompleted) |
| 649 |
.doOnError(sourceError) |
| 650 |
.subscribeOn(mockScheduler).replay(); |
| 651 |
|
| 652 |
replay.subscribe(mockObserverBeforeConnect); |
| 653 |
replay.subscribe(mockObserverBeforeConnect); |
| 654 |
replay.connect(); |
| 655 |
replay.subscribe(mockObserverAfterConnect); |
| 656 |
replay.subscribe(mockObserverAfterConnect); |
| 657 |
|
| 658 |
verify(mockObserverBeforeConnect, times(2)).onSubscribe((Subscription)any()); |
| 659 |
verify(mockObserverAfterConnect, times(2)).onSubscribe((Subscription)any()); |
| 660 |
|
| 661 |
// verify interactions |
| 662 |
verify(mockScheduler, times(1)).createWorker(); |
| 663 |
verify(spiedWorker, times(1)).schedule((Runnable)notNull()); |
| 664 |
verify(sourceNext, times(1)).accept(1); |
| 665 |
verify(sourceError, times(1)).accept(illegalArgumentException); |
| 666 |
verifyObserver(mockObserverBeforeConnect, 2, 2, illegalArgumentException); |
| 667 |
verifyObserver(mockObserverAfterConnect, 2, 2, illegalArgumentException); |
| 668 |
|
| 669 |
// FIXME publish also calls cancel |
| 670 |
verify(spiedWorker, times(1)).dispose(); |
| 671 |
verify(sourceUnsubscribed, never()).run(); |
| 672 |
|
| 673 |
verifyNoMoreInteractions(sourceNext); |
| 674 |
verifyNoMoreInteractions(sourceCompleted); |
| 675 |
verifyNoMoreInteractions(sourceError); |
| 676 |
verifyNoMoreInteractions(sourceUnsubscribed); |
| 677 |
verifyNoMoreInteractions(spiedWorker); |
| 678 |
verifyNoMoreInteractions(mockSubscription); |
| 679 |
verifyNoMoreInteractions(mockScheduler); |
| 680 |
verifyNoMoreInteractions(mockObserverBeforeConnect); |
| 681 |
verifyNoMoreInteractions(mockObserverAfterConnect); |
| 682 |
} |
| 683 |
|
| 684 |
private static void verifyObserverMock(Subscriber<Integer> mock, int numSubscriptions, int numItemsExpected) { |
| 685 |
verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); |
| 686 |
verify(mock, times(numSubscriptions)).onComplete(); |
| 687 |
verifyNoMoreInteractions(mock); |
| 688 |
} |
| 689 |
|
| 690 |
private static void verifyObserver(Subscriber<Integer> mock, int numSubscriptions, int numItemsExpected, Throwable error) { |
| 691 |
verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); |
| 692 |
verify(mock, times(numSubscriptions)).onError(error); |
| 693 |
verifyNoMoreInteractions(mock); |
| 694 |
} |
| 695 |
|
| 696 |
public static Worker workerSpy(final Disposable mockDisposable) { |
| 697 |
return spy(new InprocessWorker(mockDisposable)); |
| 698 |
} |
| 699 |
|
| 700 |
private static class InprocessWorker extends Worker { |
| 701 |
private final Disposable mockDisposable; |
| 702 |
public boolean unsubscribed; |
| 703 |
|
| 704 |
InprocessWorker(Disposable mockDisposable) { |
| 705 |
this.mockDisposable = mockDisposable; |
| 706 |
} |
| 707 |
|
| 708 |
@NonNull |
| 709 |
@Override |
| 710 |
public Disposable schedule(@NonNull Runnable action) { |
| 711 |
action.run(); |
| 712 |
return mockDisposable; // this subscription is returned but discarded |
| 713 |
} |
| 714 |
|
| 715 |
@NonNull |
| 716 |
@Override |
| 717 |
public Disposable schedule(@NonNull Runnable action, long delayTime, @NonNull TimeUnit unit) { |
| 718 |
action.run(); |
| 719 |
return mockDisposable; |
| 720 |
} |
| 721 |
|
| 722 |
@Override |
| 723 |
public void dispose() { |
| 724 |
unsubscribed = true; |
| 725 |
} |
| 726 |
|
| 727 |
@Override |
| 728 |
public boolean isDisposed() { |
| 729 |
return unsubscribed; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
@Test |
| 734 |
public void boundedReplayBuffer() { |
| 735 |
BoundedReplayBuffer<Integer> buf = new BoundedReplayBuffer<Integer>(true) { |
| 736 |
private static final long serialVersionUID = -9081211580719235896L; |
| 737 |
|
| 738 |
@Override |
| 739 |
void truncate() { |
| 740 |
} |
| 741 |
}; |
| 742 |
|
| 743 |
buf.addLast(new Node(1, 0)); |
| 744 |
buf.addLast(new Node(2, 1)); |
| 745 |
buf.addLast(new Node(3, 2)); |
| 746 |
buf.addLast(new Node(4, 3)); |
| 747 |
buf.addLast(new Node(5, 4)); |
| 748 |
|
| 749 |
List<Integer> values = new ArrayList<>(); |
| 750 |
buf.collect(values); |
| 751 |
|
| 752 |
Assert.assertEquals(Arrays.asList(1, 2, 3, 4, 5), values); |
| 753 |
|
| 754 |
buf.removeSome(2); |
| 755 |
buf.removeFirst(); |
| 756 |
buf.removeSome(2); |
| 757 |
|
| 758 |
values.clear(); |
| 759 |
buf.collect(values); |
| 760 |
Assert.assertTrue(values.isEmpty()); |
| 761 |
|
| 762 |
buf.addLast(new Node(5, 5)); |
| 763 |
buf.addLast(new Node(6, 6)); |
| 764 |
buf.collect(values); |
| 765 |
|
| 766 |
Assert.assertEquals(Arrays.asList(5, 6), values); |
| 767 |
} |
| 768 |
|
| 769 |
@Test |
| 770 |
public void timedAndSizedTruncation() { |
| 771 |
TestScheduler test = new TestScheduler(); |
| 772 |
SizeAndTimeBoundReplayBuffer<Integer> buf = new SizeAndTimeBoundReplayBuffer<>(2, 2000, TimeUnit.MILLISECONDS, test, true); |
| 773 |
List<Integer> values = new ArrayList<>(); |
| 774 |
|
| 775 |
buf.next(1); |
| 776 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 777 |
buf.next(2); |
| 778 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 779 |
buf.collect(values); |
| 780 |
Assert.assertEquals(Arrays.asList(2), values); |
| 781 |
|
| 782 |
buf.next(3); |
| 783 |
buf.next(4); |
| 784 |
values.clear(); |
| 785 |
buf.collect(values); |
| 786 |
Assert.assertEquals(Arrays.asList(3, 4), values); |
| 787 |
|
| 788 |
test.advanceTimeBy(2, TimeUnit.SECONDS); |
| 789 |
buf.next(5); |
| 790 |
|
| 791 |
values.clear(); |
| 792 |
buf.collect(values); |
| 793 |
Assert.assertEquals(Arrays.asList(5), values); |
| 794 |
|
| 795 |
test.advanceTimeBy(2, TimeUnit.SECONDS); |
| 796 |
buf.complete(); |
| 797 |
|
| 798 |
values.clear(); |
| 799 |
buf.collect(values); |
| 800 |
Assert.assertTrue(values.isEmpty()); |
| 801 |
|
| 802 |
Assert.assertEquals(1, buf.size); |
| 803 |
Assert.assertTrue(buf.hasCompleted()); |
| 804 |
} |
| 805 |
|
| 806 |
@Test |
| 807 |
public void backpressure() { |
| 808 |
final AtomicLong requested = new AtomicLong(); |
| 809 |
Flowable<Integer> source = Flowable.range(1, 1000) |
| 810 |
.doOnRequest(new LongConsumer() { |
| 811 |
@Override |
| 812 |
public void accept(long t) { |
| 813 |
requested.addAndGet(t); |
| 814 |
} |
| 815 |
}); |
| 816 |
ConnectableFlowable<Integer> cf = source.replay(); |
| 817 |
|
| 818 |
TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>(10L); |
| 819 |
TestSubscriberEx<Integer> ts2 = new TestSubscriberEx<>(90L); |
| 820 |
|
| 821 |
cf.subscribe(ts1); |
| 822 |
cf.subscribe(ts2); |
| 823 |
|
| 824 |
ts2.request(10); |
| 825 |
|
| 826 |
cf.connect(); |
| 827 |
|
| 828 |
ts1.assertValueCount(10); |
| 829 |
ts1.assertNotTerminated(); |
| 830 |
|
| 831 |
ts2.assertValueCount(100); |
| 832 |
ts2.assertNotTerminated(); |
| 833 |
|
| 834 |
Assert.assertEquals(100, requested.get()); |
| 835 |
} |
| 836 |
|
| 837 |
@Test |
| 838 |
public void backpressureBounded() { |
| 839 |
final AtomicLong requested = new AtomicLong(); |
| 840 |
Flowable<Integer> source = Flowable.range(1, 1000) |
| 841 |
.doOnRequest(new LongConsumer() { |
| 842 |
@Override |
| 843 |
public void accept(long t) { |
| 844 |
requested.addAndGet(t); |
| 845 |
} |
| 846 |
}); |
| 847 |
ConnectableFlowable<Integer> cf = source.replay(50, true); |
| 848 |
|
| 849 |
TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>(10L); |
| 850 |
TestSubscriberEx<Integer> ts2 = new TestSubscriberEx<>(90L); |
| 851 |
|
| 852 |
cf.subscribe(ts1); |
| 853 |
cf.subscribe(ts2); |
| 854 |
|
| 855 |
ts2.request(10); |
| 856 |
|
| 857 |
cf.connect(); |
| 858 |
|
| 859 |
ts1.assertValueCount(10); |
| 860 |
ts1.assertNotTerminated(); |
| 861 |
|
| 862 |
ts2.assertValueCount(100); |
| 863 |
ts2.assertNotTerminated(); |
| 864 |
|
| 865 |
Assert.assertEquals(100, requested.get()); |
| 866 |
} |
| 867 |
|
| 868 |
@Test |
| 869 |
public void coldReplayNoBackpressure() { |
| 870 |
Flowable<Integer> source = Flowable.range(0, 1000).replay().autoConnect(); |
| 871 |
|
| 872 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 873 |
|
| 874 |
source.subscribe(ts); |
| 875 |
|
| 876 |
ts.assertNoErrors(); |
| 877 |
ts.assertTerminated(); |
| 878 |
List<Integer> onNextEvents = ts.values(); |
| 879 |
assertEquals(1000, onNextEvents.size()); |
| 880 |
|
| 881 |
for (int i = 0; i < 1000; i++) { |
| 882 |
assertEquals((Integer)i, onNextEvents.get(i)); |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
@Test |
| 887 |
public void coldReplayBackpressure() { |
| 888 |
Flowable<Integer> source = Flowable.range(0, 1000).replay().autoConnect(); |
| 889 |
|
| 890 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 891 |
ts.request(10); |
| 892 |
|
| 893 |
source.subscribe(ts); |
| 894 |
|
| 895 |
ts.assertNoErrors(); |
| 896 |
ts.assertNotComplete(); |
| 897 |
List<Integer> onNextEvents = ts.values(); |
| 898 |
assertEquals(10, onNextEvents.size()); |
| 899 |
|
| 900 |
for (int i = 0; i < 10; i++) { |
| 901 |
assertEquals((Integer)i, onNextEvents.get(i)); |
| 902 |
} |
| 903 |
|
| 904 |
ts.cancel(); |
| 905 |
} |
| 906 |
|
| 907 |
@Test |
| 908 |
public void cache() throws InterruptedException { |
| 909 |
final AtomicInteger counter = new AtomicInteger(); |
| 910 |
Flowable<String> f = Flowable.unsafeCreate(new Publisher<String>() { |
| 911 |
|
| 912 |
@Override |
| 913 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 914 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 915 |
new Thread(new Runnable() { |
| 916 |
|
| 917 |
@Override |
| 918 |
public void run() { |
| 919 |
counter.incrementAndGet(); |
| 920 |
System.out.println("published observable being executed"); |
| 921 |
subscriber.onNext("one"); |
| 922 |
subscriber.onComplete(); |
| 923 |
} |
| 924 |
}).start(); |
| 925 |
} |
| 926 |
}).replay().autoConnect(); |
| 927 |
|
| 928 |
// we then expect the following 2 subscriptions to get that same value |
| 929 |
final CountDownLatch latch = new CountDownLatch(2); |
| 930 |
|
| 931 |
// subscribe once |
| 932 |
f.subscribe(new Consumer<String>() { |
| 933 |
|
| 934 |
@Override |
| 935 |
public void accept(String v) { |
| 936 |
assertEquals("one", v); |
| 937 |
System.out.println("v: " + v); |
| 938 |
latch.countDown(); |
| 939 |
} |
| 940 |
}); |
| 941 |
|
| 942 |
// subscribe again |
| 943 |
f.subscribe(new Consumer<String>() { |
| 944 |
|
| 945 |
@Override |
| 946 |
public void accept(String v) { |
| 947 |
assertEquals("one", v); |
| 948 |
System.out.println("v: " + v); |
| 949 |
latch.countDown(); |
| 950 |
} |
| 951 |
}); |
| 952 |
|
| 953 |
if (!latch.await(1000, TimeUnit.MILLISECONDS)) { |
| 954 |
fail("subscriptions did not receive values"); |
| 955 |
} |
| 956 |
assertEquals(1, counter.get()); |
| 957 |
} |
| 958 |
|
| 959 |
@Test |
| 960 |
public void unsubscribeSource() throws Throwable { |
| 961 |
Action unsubscribe = mock(Action.class); |
| 962 |
Flowable<Integer> f = Flowable.just(1).doOnCancel(unsubscribe).replay().autoConnect(); |
| 963 |
f.subscribe(); |
| 964 |
f.subscribe(); |
| 965 |
f.subscribe(); |
| 966 |
verify(unsubscribe, never()).run(); |
| 967 |
} |
| 968 |
|
| 969 |
@Test |
| 970 |
public void take() { |
| 971 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 972 |
|
| 973 |
Flowable<Integer> cached = Flowable.range(1, 100).replay().autoConnect(); |
| 974 |
cached.take(10).subscribe(ts); |
| 975 |
|
| 976 |
ts.assertNoErrors(); |
| 977 |
ts.assertTerminated(); |
| 978 |
ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 979 |
} |
| 980 |
|
| 981 |
@Test |
| 982 |
public void async() { |
| 983 |
Flowable<Integer> source = Flowable.range(1, 10000); |
| 984 |
for (int i = 0; i < 100; i++) { |
| 985 |
TestSubscriberEx<Integer> ts1 = new TestSubscriberEx<>(); |
| 986 |
|
| 987 |
Flowable<Integer> cached = source.replay().autoConnect(); |
| 988 |
|
| 989 |
cached.observeOn(Schedulers.computation()).subscribe(ts1); |
| 990 |
|
| 991 |
ts1.awaitDone(2, TimeUnit.SECONDS); |
| 992 |
ts1.assertNoErrors(); |
| 993 |
ts1.assertTerminated(); |
| 994 |
assertEquals(10000, ts1.values().size()); |
| 995 |
|
| 996 |
TestSubscriberEx<Integer> ts2 = new TestSubscriberEx<>(); |
| 997 |
cached.observeOn(Schedulers.computation()).subscribe(ts2); |
| 998 |
|
| 999 |
ts2.awaitDone(2, TimeUnit.SECONDS); |
| 1000 |
ts2.assertNoErrors(); |
| 1001 |
ts2.assertTerminated(); |
| 1002 |
assertEquals(10000, ts2.values().size()); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
@Test |
| 1007 |
public void asyncComeAndGo() { |
| 1008 |
Flowable<Long> source = Flowable.interval(1, 1, TimeUnit.MILLISECONDS) |
| 1009 |
.take(1000) |
| 1010 |
.subscribeOn(Schedulers.io()); |
| 1011 |
Flowable<Long> cached = source.replay().autoConnect(); |
| 1012 |
|
| 1013 |
Flowable<Long> output = cached.observeOn(Schedulers.computation(), false, 1024); |
| 1014 |
|
| 1015 |
List<TestSubscriberEx<Long>> list = new ArrayList<>(100); |
| 1016 |
for (int i = 0; i < 100; i++) { |
| 1017 |
TestSubscriberEx<Long> ts = new TestSubscriberEx<>(); |
| 1018 |
list.add(ts); |
| 1019 |
output.skip(i * 10).take(10).subscribe(ts); |
| 1020 |
} |
| 1021 |
|
| 1022 |
List<Long> expected = new ArrayList<>(); |
| 1023 |
for (int i = 0; i < 10; i++) { |
| 1024 |
expected.add((long)(i - 10)); |
| 1025 |
} |
| 1026 |
int j = 0; |
| 1027 |
for (TestSubscriberEx<Long> ts : list) { |
| 1028 |
ts.awaitDone(3, TimeUnit.SECONDS); |
| 1029 |
ts.assertNoErrors(); |
| 1030 |
ts.assertTerminated(); |
| 1031 |
|
| 1032 |
for (int i = j * 10; i < j * 10 + 10; i++) { |
| 1033 |
expected.set(i - j * 10, (long)i); |
| 1034 |
} |
| 1035 |
|
| 1036 |
ts.assertValueSequence(expected); |
| 1037 |
|
| 1038 |
j++; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
@Test |
| 1043 |
public void noMissingBackpressureException() { |
| 1044 |
final int m = 4 * 1000 * 1000; |
| 1045 |
Flowable<Integer> firehose = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 1046 |
@Override |
| 1047 |
public void subscribe(Subscriber<? super Integer> t) { |
| 1048 |
t.onSubscribe(new BooleanSubscription()); |
| 1049 |
for (int i = 0; i < m; i++) { |
| 1050 |
t.onNext(i); |
| 1051 |
} |
| 1052 |
t.onComplete(); |
| 1053 |
} |
| 1054 |
}); |
| 1055 |
|
| 1056 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 1057 |
firehose.replay().autoConnect().observeOn(Schedulers.computation()).takeLast(100).subscribe(ts); |
| 1058 |
|
| 1059 |
ts.awaitDone(3, TimeUnit.SECONDS); |
| 1060 |
ts.assertNoErrors(); |
| 1061 |
ts.assertTerminated(); |
| 1062 |
|
| 1063 |
assertEquals(100, ts.values().size()); |
| 1064 |
} |
| 1065 |
|
| 1066 |
@Test |
| 1067 |
public void valuesAndThenError() { |
| 1068 |
Flowable<Integer> source = Flowable.range(1, 10) |
| 1069 |
.concatWith(Flowable.<Integer>error(new TestException())) |
| 1070 |
.replay().autoConnect(); |
| 1071 |
|
| 1072 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 1073 |
source.subscribe(ts); |
| 1074 |
|
| 1075 |
ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 1076 |
ts.assertNotComplete(); |
| 1077 |
Assert.assertEquals(1, ts.errors().size()); |
| 1078 |
|
| 1079 |
TestSubscriberEx<Integer> ts2 = new TestSubscriberEx<>(); |
| 1080 |
source.subscribe(ts2); |
| 1081 |
|
| 1082 |
ts2.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 1083 |
ts2.assertNotComplete(); |
| 1084 |
Assert.assertEquals(1, ts2.errors().size()); |
| 1085 |
} |
| 1086 |
|
| 1087 |
@Test |
| 1088 |
public void unsafeChildThrows() { |
| 1089 |
final AtomicInteger count = new AtomicInteger(); |
| 1090 |
|
| 1091 |
Flowable<Integer> source = Flowable.range(1, 100) |
| 1092 |
.doOnNext(new Consumer<Integer>() { |
| 1093 |
@Override |
| 1094 |
public void accept(Integer t) { |
| 1095 |
count.getAndIncrement(); |
| 1096 |
} |
| 1097 |
}) |
| 1098 |
.replay().autoConnect(); |
| 1099 |
|
| 1100 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1101 |
@Override |
| 1102 |
public void onNext(Integer t) { |
| 1103 |
throw new TestException(); |
| 1104 |
} |
| 1105 |
}; |
| 1106 |
|
| 1107 |
source.subscribe(ts); |
| 1108 |
|
| 1109 |
Assert.assertEquals(100, count.get()); |
| 1110 |
|
| 1111 |
ts.assertNoValues(); |
| 1112 |
ts.assertNotComplete(); |
| 1113 |
ts.assertError(TestException.class); |
| 1114 |
} |
| 1115 |
|
| 1116 |
@Test |
| 1117 |
public void unboundedLeavesEarly() { |
| 1118 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 1119 |
|
| 1120 |
final List<Long> requests = new ArrayList<>(); |
| 1121 |
|
| 1122 |
Flowable<Integer> out = source |
| 1123 |
.doOnRequest(new LongConsumer() { |
| 1124 |
@Override |
| 1125 |
public void accept(long t) { |
| 1126 |
requests.add(t); |
| 1127 |
} |
| 1128 |
}).replay().autoConnect(); |
| 1129 |
|
| 1130 |
TestSubscriber<Integer> ts1 = new TestSubscriber<>(5L); |
| 1131 |
TestSubscriber<Integer> ts2 = new TestSubscriber<>(10L); |
| 1132 |
|
| 1133 |
out.subscribe(ts1); |
| 1134 |
out.subscribe(ts2); |
| 1135 |
ts2.cancel(); |
| 1136 |
|
| 1137 |
Assert.assertEquals(Arrays.asList(5L, 5L), requests); |
| 1138 |
} |
| 1139 |
|
| 1140 |
@Test |
| 1141 |
public void subscribersComeAndGoAtRequestBoundaries() { |
| 1142 |
ConnectableFlowable<Integer> source = Flowable.range(1, 10).replay(1, true); |
| 1143 |
source.connect(); |
| 1144 |
|
| 1145 |
TestSubscriber<Integer> ts1 = new TestSubscriber<>(2L); |
| 1146 |
|
| 1147 |
source.subscribe(ts1); |
| 1148 |
|
| 1149 |
ts1.assertValues(1, 2); |
| 1150 |
ts1.assertNoErrors(); |
| 1151 |
ts1.cancel(); |
| 1152 |
|
| 1153 |
TestSubscriber<Integer> ts2 = new TestSubscriber<>(2L); |
| 1154 |
|
| 1155 |
source.subscribe(ts2); |
| 1156 |
|
| 1157 |
ts2.assertValues(2, 3); |
| 1158 |
ts2.assertNoErrors(); |
| 1159 |
ts2.cancel(); |
| 1160 |
|
| 1161 |
TestSubscriber<Integer> ts21 = new TestSubscriber<>(1L); |
| 1162 |
|
| 1163 |
source.subscribe(ts21); |
| 1164 |
|
| 1165 |
ts21.assertValues(3); |
| 1166 |
ts21.assertNoErrors(); |
| 1167 |
ts21.cancel(); |
| 1168 |
|
| 1169 |
TestSubscriber<Integer> ts22 = new TestSubscriber<>(1L); |
| 1170 |
|
| 1171 |
source.subscribe(ts22); |
| 1172 |
|
| 1173 |
ts22.assertValues(3); |
| 1174 |
ts22.assertNoErrors(); |
| 1175 |
ts22.cancel(); |
| 1176 |
|
| 1177 |
TestSubscriber<Integer> ts3 = new TestSubscriber<>(); |
| 1178 |
|
| 1179 |
source.subscribe(ts3); |
| 1180 |
|
| 1181 |
ts3.assertNoErrors(); |
| 1182 |
System.out.println(ts3.values()); |
| 1183 |
ts3.assertValues(3, 4, 5, 6, 7, 8, 9, 10); |
| 1184 |
ts3.assertComplete(); |
| 1185 |
} |
| 1186 |
|
| 1187 |
@Test |
| 1188 |
public void subscribersComeAndGoAtRequestBoundaries2() { |
| 1189 |
ConnectableFlowable<Integer> source = Flowable.range(1, 10).replay(2, true); |
| 1190 |
source.connect(); |
| 1191 |
|
| 1192 |
TestSubscriber<Integer> ts1 = new TestSubscriber<>(2L); |
| 1193 |
|
| 1194 |
source.subscribe(ts1); |
| 1195 |
|
| 1196 |
ts1.assertValues(1, 2); |
| 1197 |
ts1.assertNoErrors(); |
| 1198 |
ts1.cancel(); |
| 1199 |
|
| 1200 |
TestSubscriber<Integer> ts11 = new TestSubscriber<>(2L); |
| 1201 |
|
| 1202 |
source.subscribe(ts11); |
| 1203 |
|
| 1204 |
ts11.assertValues(1, 2); |
| 1205 |
ts11.assertNoErrors(); |
| 1206 |
ts11.cancel(); |
| 1207 |
|
| 1208 |
TestSubscriber<Integer> ts2 = new TestSubscriber<>(3L); |
| 1209 |
|
| 1210 |
source.subscribe(ts2); |
| 1211 |
|
| 1212 |
ts2.assertValues(1, 2, 3); |
| 1213 |
ts2.assertNoErrors(); |
| 1214 |
ts2.cancel(); |
| 1215 |
|
| 1216 |
TestSubscriber<Integer> ts21 = new TestSubscriber<>(1L); |
| 1217 |
|
| 1218 |
source.subscribe(ts21); |
| 1219 |
|
| 1220 |
ts21.assertValues(2); |
| 1221 |
ts21.assertNoErrors(); |
| 1222 |
ts21.cancel(); |
| 1223 |
|
| 1224 |
TestSubscriber<Integer> ts22 = new TestSubscriber<>(1L); |
| 1225 |
|
| 1226 |
source.subscribe(ts22); |
| 1227 |
|
| 1228 |
ts22.assertValues(2); |
| 1229 |
ts22.assertNoErrors(); |
| 1230 |
ts22.cancel(); |
| 1231 |
|
| 1232 |
TestSubscriber<Integer> ts3 = new TestSubscriber<>(); |
| 1233 |
|
| 1234 |
source.subscribe(ts3); |
| 1235 |
|
| 1236 |
ts3.assertNoErrors(); |
| 1237 |
System.out.println(ts3.values()); |
| 1238 |
ts3.assertValues(2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 1239 |
ts3.assertComplete(); |
| 1240 |
} |
| 1241 |
|
| 1242 |
@Test |
| 1243 |
public void replayTime() { |
| 1244 |
Flowable.just(1).replay(1, TimeUnit.MINUTES, Schedulers.computation(), true) |
| 1245 |
.autoConnect() |
| 1246 |
.test() |
| 1247 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1248 |
.assertResult(1); |
| 1249 |
} |
| 1250 |
|
| 1251 |
@Test |
| 1252 |
public void replaySizeAndTime() { |
| 1253 |
Flowable.just(1).replay(1, 1, TimeUnit.MILLISECONDS, Schedulers.computation(), true) |
| 1254 |
.autoConnect() |
| 1255 |
.test() |
| 1256 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1257 |
.assertResult(1); |
| 1258 |
} |
| 1259 |
|
| 1260 |
@Test |
| 1261 |
public void replaySelectorTime() { |
| 1262 |
Flowable.just(1).replay(Functions.<Flowable<Integer>>identity(), 1, TimeUnit.MINUTES, Schedulers.computation(), true) |
| 1263 |
.test() |
| 1264 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1265 |
.assertResult(1); |
| 1266 |
} |
| 1267 |
|
| 1268 |
@Test |
| 1269 |
public void source() { |
| 1270 |
Flowable<Integer> source = Flowable.range(1, 3); |
| 1271 |
|
| 1272 |
assertSame(source, (((HasUpstreamPublisher<?>)source.replay())).source()); |
| 1273 |
} |
| 1274 |
|
| 1275 |
@Test |
| 1276 |
public void connectRace() { |
| 1277 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1278 |
final ConnectableFlowable<Integer> cf = Flowable.range(1, 3).replay(); |
| 1279 |
|
| 1280 |
Runnable r = new Runnable() { |
| 1281 |
@Override |
| 1282 |
public void run() { |
| 1283 |
cf.connect(); |
| 1284 |
} |
| 1285 |
}; |
| 1286 |
|
| 1287 |
TestHelper.race(r, r); |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
@Test |
| 1292 |
public void subscribeRace() { |
| 1293 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1294 |
final ConnectableFlowable<Integer> cf = Flowable.range(1, 3).replay(); |
| 1295 |
|
| 1296 |
final TestSubscriber<Integer> ts1 = new TestSubscriber<>(); |
| 1297 |
final TestSubscriber<Integer> ts2 = new TestSubscriber<>(); |
| 1298 |
|
| 1299 |
Runnable r1 = new Runnable() { |
| 1300 |
@Override |
| 1301 |
public void run() { |
| 1302 |
cf.subscribe(ts1); |
| 1303 |
} |
| 1304 |
}; |
| 1305 |
|
| 1306 |
Runnable r2 = new Runnable() { |
| 1307 |
@Override |
| 1308 |
public void run() { |
| 1309 |
cf.subscribe(ts2); |
| 1310 |
} |
| 1311 |
}; |
| 1312 |
|
| 1313 |
TestHelper.race(r1, r2); |
| 1314 |
} |
| 1315 |
} |
| 1316 |
|
| 1317 |
@Test |
| 1318 |
public void addRemoveRace() { |
| 1319 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1320 |
final ConnectableFlowable<Integer> cf = Flowable.range(1, 3).replay(); |
| 1321 |
|
| 1322 |
final TestSubscriber<Integer> ts1 = new TestSubscriber<>(); |
| 1323 |
final TestSubscriber<Integer> ts2 = new TestSubscriber<>(); |
| 1324 |
|
| 1325 |
cf.subscribe(ts1); |
| 1326 |
|
| 1327 |
Runnable r1 = new Runnable() { |
| 1328 |
@Override |
| 1329 |
public void run() { |
| 1330 |
ts1.cancel(); |
| 1331 |
} |
| 1332 |
}; |
| 1333 |
|
| 1334 |
Runnable r2 = new Runnable() { |
| 1335 |
@Override |
| 1336 |
public void run() { |
| 1337 |
cf.subscribe(ts2); |
| 1338 |
} |
| 1339 |
}; |
| 1340 |
|
| 1341 |
TestHelper.race(r1, r2); |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
@Test |
| 1346 |
public void cancelOnArrival() { |
| 1347 |
Flowable.range(1, 2) |
| 1348 |
.replay(Integer.MAX_VALUE, true) |
| 1349 |
.autoConnect() |
| 1350 |
.test(Long.MAX_VALUE, true) |
| 1351 |
.assertEmpty(); |
| 1352 |
} |
| 1353 |
|
| 1354 |
@Test |
| 1355 |
public void cancelOnArrival2() { |
| 1356 |
ConnectableFlowable<Integer> cf = PublishProcessor.<Integer>create() |
| 1357 |
.replay(Integer.MAX_VALUE, true); |
| 1358 |
|
| 1359 |
cf.test(); |
| 1360 |
|
| 1361 |
cf |
| 1362 |
.autoConnect() |
| 1363 |
.test(Long.MAX_VALUE, true) |
| 1364 |
.assertEmpty(); |
| 1365 |
} |
| 1366 |
|
| 1367 |
@Test |
| 1368 |
public void connectConsumerThrows() { |
| 1369 |
ConnectableFlowable<Integer> cf = Flowable.range(1, 2) |
| 1370 |
.replay(); |
| 1371 |
|
| 1372 |
try { |
| 1373 |
cf.connect(new Consumer<Disposable>() { |
| 1374 |
@Override |
| 1375 |
public void accept(Disposable t) throws Exception { |
| 1376 |
throw new TestException(); |
| 1377 |
} |
| 1378 |
}); |
| 1379 |
fail("Should have thrown"); |
| 1380 |
} catch (TestException ex) { |
| 1381 |
// expected |
| 1382 |
} |
| 1383 |
|
| 1384 |
cf.test().assertEmpty().cancel(); |
| 1385 |
|
| 1386 |
cf.connect(); |
| 1387 |
|
| 1388 |
cf.test().assertResult(1, 2); |
| 1389 |
} |
| 1390 |
|
| 1391 |
@Test |
| 1392 |
public void badSource() { |
| 1393 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1394 |
try { |
| 1395 |
new Flowable<Integer>() { |
| 1396 |
@Override |
| 1397 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 1398 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1399 |
subscriber.onError(new TestException("First")); |
| 1400 |
subscriber.onNext(1); |
| 1401 |
subscriber.onError(new TestException("Second")); |
| 1402 |
subscriber.onComplete(); |
| 1403 |
} |
| 1404 |
}.replay() |
| 1405 |
.autoConnect() |
| 1406 |
.to(TestHelper.<Integer>testConsumer()) |
| 1407 |
.assertFailureAndMessage(TestException.class, "First"); |
| 1408 |
|
| 1409 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second"); |
| 1410 |
} finally { |
| 1411 |
RxJavaPlugins.reset(); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
@Test |
| 1416 |
public void subscribeOnNextRace() { |
| 1417 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1418 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1419 |
|
| 1420 |
final ConnectableFlowable<Integer> cf = pp.replay(); |
| 1421 |
|
| 1422 |
final TestSubscriber<Integer> ts1 = new TestSubscriber<>(); |
| 1423 |
|
| 1424 |
Runnable r1 = new Runnable() { |
| 1425 |
@Override |
| 1426 |
public void run() { |
| 1427 |
cf.subscribe(ts1); |
| 1428 |
} |
| 1429 |
}; |
| 1430 |
|
| 1431 |
Runnable r2 = new Runnable() { |
| 1432 |
@Override |
| 1433 |
public void run() { |
| 1434 |
for (int j = 0; j < 1000; j++) { |
| 1435 |
pp.onNext(j); |
| 1436 |
} |
| 1437 |
} |
| 1438 |
}; |
| 1439 |
|
| 1440 |
TestHelper.race(r1, r2); |
| 1441 |
} |
| 1442 |
} |
| 1443 |
|
| 1444 |
@Test |
| 1445 |
public void unsubscribeOnNextRace() { |
| 1446 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1447 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1448 |
|
| 1449 |
final ConnectableFlowable<Integer> cf = pp.replay(); |
| 1450 |
|
| 1451 |
final TestSubscriber<Integer> ts1 = new TestSubscriber<>(); |
| 1452 |
|
| 1453 |
cf.subscribe(ts1); |
| 1454 |
|
| 1455 |
Runnable r1 = new Runnable() { |
| 1456 |
@Override |
| 1457 |
public void run() { |
| 1458 |
ts1.cancel(); |
| 1459 |
} |
| 1460 |
}; |
| 1461 |
|
| 1462 |
Runnable r2 = new Runnable() { |
| 1463 |
@Override |
| 1464 |
public void run() { |
| 1465 |
for (int j = 0; j < 1000; j++) { |
| 1466 |
pp.onNext(j); |
| 1467 |
} |
| 1468 |
} |
| 1469 |
}; |
| 1470 |
|
| 1471 |
TestHelper.race(r1, r2); |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
@Test |
| 1476 |
public void unsubscribeReplayRace() { |
| 1477 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1478 |
final ConnectableFlowable<Integer> cf = Flowable.range(1, 1000).replay(); |
| 1479 |
|
| 1480 |
final TestSubscriber<Integer> ts1 = new TestSubscriber<>(); |
| 1481 |
|
| 1482 |
cf.connect(); |
| 1483 |
|
| 1484 |
Runnable r1 = new Runnable() { |
| 1485 |
@Override |
| 1486 |
public void run() { |
| 1487 |
cf.subscribe(ts1); |
| 1488 |
} |
| 1489 |
}; |
| 1490 |
|
| 1491 |
Runnable r2 = new Runnable() { |
| 1492 |
@Override |
| 1493 |
public void run() { |
| 1494 |
ts1.cancel(); |
| 1495 |
} |
| 1496 |
}; |
| 1497 |
|
| 1498 |
TestHelper.race(r1, r2); |
| 1499 |
} |
| 1500 |
} |
| 1501 |
|
| 1502 |
@Test |
| 1503 |
public void reentrantOnNext() { |
| 1504 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1505 |
|
| 1506 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1507 |
@Override |
| 1508 |
public void onNext(Integer t) { |
| 1509 |
if (t == 1) { |
| 1510 |
pp.onNext(2); |
| 1511 |
pp.onComplete(); |
| 1512 |
} |
| 1513 |
super.onNext(t); |
| 1514 |
} |
| 1515 |
}; |
| 1516 |
|
| 1517 |
pp.replay().autoConnect().subscribe(ts); |
| 1518 |
|
| 1519 |
pp.onNext(1); |
| 1520 |
|
| 1521 |
ts.assertResult(1, 2); |
| 1522 |
} |
| 1523 |
|
| 1524 |
@Test |
| 1525 |
public void reentrantOnNextBound() { |
| 1526 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1527 |
|
| 1528 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1529 |
@Override |
| 1530 |
public void onNext(Integer t) { |
| 1531 |
if (t == 1) { |
| 1532 |
pp.onNext(2); |
| 1533 |
pp.onComplete(); |
| 1534 |
} |
| 1535 |
super.onNext(t); |
| 1536 |
} |
| 1537 |
}; |
| 1538 |
|
| 1539 |
pp.replay(10, true).autoConnect().subscribe(ts); |
| 1540 |
|
| 1541 |
pp.onNext(1); |
| 1542 |
|
| 1543 |
ts.assertResult(1, 2); |
| 1544 |
} |
| 1545 |
|
| 1546 |
@Test |
| 1547 |
public void reentrantOnNextCancel() { |
| 1548 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1549 |
|
| 1550 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1551 |
@Override |
| 1552 |
public void onNext(Integer t) { |
| 1553 |
if (t == 1) { |
| 1554 |
pp.onNext(2); |
| 1555 |
cancel(); |
| 1556 |
} |
| 1557 |
super.onNext(t); |
| 1558 |
} |
| 1559 |
}; |
| 1560 |
|
| 1561 |
pp.replay().autoConnect().subscribe(ts); |
| 1562 |
|
| 1563 |
pp.onNext(1); |
| 1564 |
|
| 1565 |
ts.assertValues(1); |
| 1566 |
} |
| 1567 |
|
| 1568 |
@Test |
| 1569 |
public void reentrantOnNextCancelBounded() { |
| 1570 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 1571 |
|
| 1572 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1573 |
@Override |
| 1574 |
public void onNext(Integer t) { |
| 1575 |
if (t == 1) { |
| 1576 |
pp.onNext(2); |
| 1577 |
cancel(); |
| 1578 |
} |
| 1579 |
super.onNext(t); |
| 1580 |
} |
| 1581 |
}; |
| 1582 |
|
| 1583 |
pp.replay(10, true).autoConnect().subscribe(ts); |
| 1584 |
|
| 1585 |
pp.onNext(1); |
| 1586 |
|
| 1587 |
ts.assertValues(1); |
| 1588 |
} |
| 1589 |
|
| 1590 |
@Test |
| 1591 |
public void replayMaxInt() { |
| 1592 |
Flowable.range(1, 2) |
| 1593 |
.replay(Integer.MAX_VALUE, true) |
| 1594 |
.autoConnect() |
| 1595 |
.test() |
| 1596 |
.assertResult(1, 2); |
| 1597 |
} |
| 1598 |
|
| 1599 |
@Test |
| 1600 |
public void timedAndSizedTruncationError() { |
| 1601 |
TestScheduler test = new TestScheduler(); |
| 1602 |
SizeAndTimeBoundReplayBuffer<Integer> buf = new SizeAndTimeBoundReplayBuffer<>(2, 2000, TimeUnit.MILLISECONDS, test, true); |
| 1603 |
|
| 1604 |
Assert.assertFalse(buf.hasCompleted()); |
| 1605 |
Assert.assertFalse(buf.hasError()); |
| 1606 |
|
| 1607 |
List<Integer> values = new ArrayList<>(); |
| 1608 |
|
| 1609 |
buf.next(1); |
| 1610 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 1611 |
buf.next(2); |
| 1612 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 1613 |
buf.collect(values); |
| 1614 |
Assert.assertEquals(Arrays.asList(2), values); |
| 1615 |
|
| 1616 |
buf.next(3); |
| 1617 |
buf.next(4); |
| 1618 |
values.clear(); |
| 1619 |
buf.collect(values); |
| 1620 |
Assert.assertEquals(Arrays.asList(3, 4), values); |
| 1621 |
|
| 1622 |
test.advanceTimeBy(2, TimeUnit.SECONDS); |
| 1623 |
buf.next(5); |
| 1624 |
|
| 1625 |
values.clear(); |
| 1626 |
buf.collect(values); |
| 1627 |
Assert.assertEquals(Arrays.asList(5), values); |
| 1628 |
Assert.assertFalse(buf.hasCompleted()); |
| 1629 |
Assert.assertFalse(buf.hasError()); |
| 1630 |
|
| 1631 |
test.advanceTimeBy(2, TimeUnit.SECONDS); |
| 1632 |
buf.error(new TestException()); |
| 1633 |
|
| 1634 |
values.clear(); |
| 1635 |
buf.collect(values); |
| 1636 |
Assert.assertTrue(values.isEmpty()); |
| 1637 |
|
| 1638 |
Assert.assertEquals(1, buf.size); |
| 1639 |
Assert.assertFalse(buf.hasCompleted()); |
| 1640 |
Assert.assertTrue(buf.hasError()); |
| 1641 |
} |
| 1642 |
|
| 1643 |
@Test |
| 1644 |
public void sizedTruncation() { |
| 1645 |
SizeBoundReplayBuffer<Integer> buf = new SizeBoundReplayBuffer<>(2, true); |
| 1646 |
List<Integer> values = new ArrayList<>(); |
| 1647 |
|
| 1648 |
buf.next(1); |
| 1649 |
buf.next(2); |
| 1650 |
buf.collect(values); |
| 1651 |
Assert.assertEquals(Arrays.asList(1, 2), values); |
| 1652 |
|
| 1653 |
buf.next(3); |
| 1654 |
buf.next(4); |
| 1655 |
values.clear(); |
| 1656 |
buf.collect(values); |
| 1657 |
Assert.assertEquals(Arrays.asList(3, 4), values); |
| 1658 |
|
| 1659 |
buf.next(5); |
| 1660 |
|
| 1661 |
values.clear(); |
| 1662 |
buf.collect(values); |
| 1663 |
Assert.assertEquals(Arrays.asList(4, 5), values); |
| 1664 |
Assert.assertFalse(buf.hasCompleted()); |
| 1665 |
|
| 1666 |
buf.complete(); |
| 1667 |
|
| 1668 |
values.clear(); |
| 1669 |
buf.collect(values); |
| 1670 |
Assert.assertEquals(Arrays.asList(4, 5), values); |
| 1671 |
|
| 1672 |
Assert.assertEquals(3, buf.size); |
| 1673 |
Assert.assertTrue(buf.hasCompleted()); |
| 1674 |
Assert.assertFalse(buf.hasError()); |
| 1675 |
} |
| 1676 |
|
| 1677 |
@Test |
| 1678 |
public void delayedUpstreamOnSubscribe() { |
| 1679 |
final Subscriber<?>[] sub = { null }; |
| 1680 |
|
| 1681 |
new Flowable<Integer>() { |
| 1682 |
@Override |
| 1683 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 1684 |
sub[0] = s; |
| 1685 |
} |
| 1686 |
} |
| 1687 |
.replay() |
| 1688 |
.connect() |
| 1689 |
.dispose(); |
| 1690 |
|
| 1691 |
BooleanSubscription bs = new BooleanSubscription(); |
| 1692 |
|
| 1693 |
sub[0].onSubscribe(bs); |
| 1694 |
|
| 1695 |
assertTrue(bs.isCancelled()); |
| 1696 |
} |
| 1697 |
|
| 1698 |
@Test |
| 1699 |
public void timedNoOutdatedData() { |
| 1700 |
TestScheduler scheduler = new TestScheduler(); |
| 1701 |
|
| 1702 |
Flowable<Integer> source = Flowable.just(1) |
| 1703 |
.replay(2, TimeUnit.SECONDS, scheduler, true) |
| 1704 |
.autoConnect(); |
| 1705 |
|
| 1706 |
source.test().assertResult(1); |
| 1707 |
|
| 1708 |
source.test().assertResult(1); |
| 1709 |
|
| 1710 |
scheduler.advanceTimeBy(3, TimeUnit.SECONDS); |
| 1711 |
|
| 1712 |
source.test().assertResult(); |
| 1713 |
} |
| 1714 |
|
| 1715 |
@Test |
| 1716 |
public void multicastSelectorCallableConnectableCrash() { |
| 1717 |
FlowableReplay.multicastSelector(new Supplier<ConnectableFlowable<Object>>() { |
| 1718 |
@Override |
| 1719 |
public ConnectableFlowable<Object> get() throws Exception { |
| 1720 |
throw new TestException(); |
| 1721 |
} |
| 1722 |
}, Functions.<Flowable<Object>>identity()) |
| 1723 |
.test() |
| 1724 |
.assertFailure(TestException.class); |
| 1725 |
} |
| 1726 |
|
| 1727 |
@Test |
| 1728 |
public void badRequest() { |
| 1729 |
TestHelper.assertBadRequestReported( |
| 1730 |
Flowable.never() |
| 1731 |
.replay() |
| 1732 |
); |
| 1733 |
} |
| 1734 |
|
| 1735 |
@Test |
| 1736 |
public void noHeadRetentionCompleteSize() { |
| 1737 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 1738 |
|
| 1739 |
FlowableReplay<Integer> co = (FlowableReplay<Integer>)source |
| 1740 |
.replay(1, true); |
| 1741 |
|
| 1742 |
// the backpressure coordination would not accept items from source otherwise |
| 1743 |
co.test(); |
| 1744 |
|
| 1745 |
co.connect(); |
| 1746 |
|
| 1747 |
BoundedReplayBuffer<Integer> buf = (BoundedReplayBuffer<Integer>)(co.current.get().buffer); |
| 1748 |
|
| 1749 |
source.onNext(1); |
| 1750 |
source.onNext(2); |
| 1751 |
source.onComplete(); |
| 1752 |
|
| 1753 |
assertNull(buf.get().value); |
| 1754 |
|
| 1755 |
Object o = buf.get(); |
| 1756 |
|
| 1757 |
buf.trimHead(); |
| 1758 |
|
| 1759 |
assertSame(o, buf.get()); |
| 1760 |
} |
| 1761 |
|
| 1762 |
@Test |
| 1763 |
public void noHeadRetentionErrorSize() { |
| 1764 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 1765 |
|
| 1766 |
FlowableReplay<Integer> co = (FlowableReplay<Integer>)source |
| 1767 |
.replay(1, true); |
| 1768 |
|
| 1769 |
co.test(); |
| 1770 |
|
| 1771 |
co.connect(); |
| 1772 |
|
| 1773 |
BoundedReplayBuffer<Integer> buf = (BoundedReplayBuffer<Integer>)(co.current.get().buffer); |
| 1774 |
|
| 1775 |
source.onNext(1); |
| 1776 |
source.onNext(2); |
| 1777 |
source.onError(new TestException()); |
| 1778 |
|
| 1779 |
assertNull(buf.get().value); |
| 1780 |
|
| 1781 |
Object o = buf.get(); |
| 1782 |
|
| 1783 |
buf.trimHead(); |
| 1784 |
|
| 1785 |
assertSame(o, buf.get()); |
| 1786 |
} |
| 1787 |
|
| 1788 |
@Test |
| 1789 |
public void noHeadRetentionSize() { |
| 1790 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 1791 |
|
| 1792 |
FlowableReplay<Integer> co = (FlowableReplay<Integer>)source |
| 1793 |
.replay(1, true); |
| 1794 |
|
| 1795 |
co.test(); |
| 1796 |
|
| 1797 |
co.connect(); |
| 1798 |
|
| 1799 |
BoundedReplayBuffer<Integer> buf = (BoundedReplayBuffer<Integer>)(co.current.get().buffer); |
| 1800 |
|
| 1801 |
source.onNext(1); |
| 1802 |
source.onNext(2); |
| 1803 |
|
| 1804 |
assertNull(buf.get().value); |
| 1805 |
|
| 1806 |
buf.trimHead(); |
| 1807 |
|
| 1808 |
assertNull(buf.get().value); |
| 1809 |
|
| 1810 |
Object o = buf.get(); |
| 1811 |
|
| 1812 |
buf.trimHead(); |
| 1813 |
|
| 1814 |
assertSame(o, buf.get()); |
| 1815 |
} |
| 1816 |
|
| 1817 |
@Test |
| 1818 |
public void noHeadRetentionCompleteTime() { |
| 1819 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 1820 |
|
| 1821 |
FlowableReplay<Integer> co = (FlowableReplay<Integer>)source |
| 1822 |
.replay(1, TimeUnit.MINUTES, Schedulers.computation(), true); |
| 1823 |
|
| 1824 |
co.test(); |
| 1825 |
|
| 1826 |
co.connect(); |
| 1827 |
|
| 1828 |
BoundedReplayBuffer<Integer> buf = (BoundedReplayBuffer<Integer>)(co.current.get().buffer); |
| 1829 |
|
| 1830 |
source.onNext(1); |
| 1831 |
source.onNext(2); |
| 1832 |
source.onComplete(); |
| 1833 |
|
| 1834 |
assertNull(buf.get().value); |
| 1835 |
|
| 1836 |
Object o = buf.get(); |
| 1837 |
|
| 1838 |
buf.trimHead(); |
| 1839 |
|
| 1840 |
assertSame(o, buf.get()); |
| 1841 |
} |
| 1842 |
|
| 1843 |
@Test |
| 1844 |
public void noHeadRetentionErrorTime() { |
| 1845 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 1846 |
|
| 1847 |
FlowableReplay<Integer> co = (FlowableReplay<Integer>)source |
| 1848 |
.replay(1, TimeUnit.MINUTES, Schedulers.computation(), true); |
| 1849 |
|
| 1850 |
co.test(); |
| 1851 |
|
| 1852 |
co.connect(); |
| 1853 |
|
| 1854 |
BoundedReplayBuffer<Integer> buf = (BoundedReplayBuffer<Integer>)(co.current.get().buffer); |
| 1855 |
|
| 1856 |
source.onNext(1); |
| 1857 |
source.onNext(2); |
| 1858 |
source.onError(new TestException()); |
| 1859 |
|
| 1860 |
assertNull(buf.get().value); |
| 1861 |
|
| 1862 |
Object o = buf.get(); |
| 1863 |
|
| 1864 |
buf.trimHead(); |
| 1865 |
|
| 1866 |
assertSame(o, buf.get()); |
| 1867 |
} |
| 1868 |
|
| 1869 |
@Test |
| 1870 |
public void noHeadRetentionTime() { |
| 1871 |
TestScheduler sch = new TestScheduler(); |
| 1872 |
|
| 1873 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 1874 |
|
| 1875 |
FlowableReplay<Integer> co = (FlowableReplay<Integer>)source |
| 1876 |
.replay(1, TimeUnit.MILLISECONDS, sch, true); |
| 1877 |
|
| 1878 |
co.test(); |
| 1879 |
|
| 1880 |
co.connect(); |
| 1881 |
|
| 1882 |
BoundedReplayBuffer<Integer> buf = (BoundedReplayBuffer<Integer>)(co.current.get().buffer); |
| 1883 |
|
| 1884 |
source.onNext(1); |
| 1885 |
|
| 1886 |
sch.advanceTimeBy(2, TimeUnit.MILLISECONDS); |
| 1887 |
|
| 1888 |
source.onNext(2); |
| 1889 |
|
| 1890 |
assertNull(buf.get().value); |
| 1891 |
|
| 1892 |
buf.trimHead(); |
| 1893 |
|
| 1894 |
assertNull(buf.get().value); |
| 1895 |
|
| 1896 |
Object o = buf.get(); |
| 1897 |
|
| 1898 |
buf.trimHead(); |
| 1899 |
|
| 1900 |
assertSame(o, buf.get()); |
| 1901 |
} |
| 1902 |
|
| 1903 |
@Test(expected = TestException.class) |
| 1904 |
public void createBufferFactoryCrash() { |
| 1905 |
FlowableReplay.create(Flowable.just(1), new Supplier<ReplayBuffer<Integer>>() { |
| 1906 |
@Override |
| 1907 |
public ReplayBuffer<Integer> get() throws Exception { |
| 1908 |
throw new TestException(); |
| 1909 |
} |
| 1910 |
}) |
| 1911 |
.connect(); |
| 1912 |
} |
| 1913 |
|
| 1914 |
@Test |
| 1915 |
public void createBufferFactoryCrashOnSubscribe() { |
| 1916 |
FlowableReplay.create(Flowable.just(1), new Supplier<ReplayBuffer<Integer>>() { |
| 1917 |
@Override |
| 1918 |
public ReplayBuffer<Integer> get() throws Exception { |
| 1919 |
throw new TestException(); |
| 1920 |
} |
| 1921 |
}) |
| 1922 |
.test() |
| 1923 |
.assertFailure(TestException.class); |
| 1924 |
} |
| 1925 |
|
| 1926 |
@Test |
| 1927 |
public void noBoundedRetentionViaThreadLocal() throws Exception { |
| 1928 |
Flowable<byte[]> source = Flowable.range(1, 200) |
| 1929 |
.map(new Function<Integer, byte[]>() { |
| 1930 |
@Override |
| 1931 |
public byte[] apply(Integer v) throws Exception { |
| 1932 |
return new byte[1024 * 1024]; |
| 1933 |
} |
| 1934 |
}) |
| 1935 |
.replay(new Function<Flowable<byte[]>, Publisher<byte[]>>() { |
| 1936 |
@Override |
| 1937 |
public Publisher<byte[]> apply(final Flowable<byte[]> f) throws Exception { |
| 1938 |
return f.take(1) |
| 1939 |
.concatMap(new Function<byte[], Publisher<byte[]>>() { |
| 1940 |
@Override |
| 1941 |
public Publisher<byte[]> apply(byte[] v) throws Exception { |
| 1942 |
return f; |
| 1943 |
} |
| 1944 |
}); |
| 1945 |
} |
| 1946 |
}, 1, true) |
| 1947 |
.takeLast(1) |
| 1948 |
; |
| 1949 |
|
| 1950 |
System.out.println("Bounded Replay Leak check: Wait before GC"); |
| 1951 |
Thread.sleep(1000); |
| 1952 |
|
| 1953 |
System.out.println("Bounded Replay Leak check: GC"); |
| 1954 |
System.gc(); |
| 1955 |
|
| 1956 |
Thread.sleep(500); |
| 1957 |
|
| 1958 |
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); |
| 1959 |
MemoryUsage memHeap = memoryMXBean.getHeapMemoryUsage(); |
| 1960 |
long initial = memHeap.getUsed(); |
| 1961 |
|
| 1962 |
System.out.printf("Bounded Replay Leak check: Starting: %.3f MB%n", initial / 1024.0 / 1024.0); |
| 1963 |
|
| 1964 |
final AtomicLong after = new AtomicLong(); |
| 1965 |
|
| 1966 |
source.subscribe(new Consumer<byte[]>() { |
| 1967 |
@Override |
| 1968 |
public void accept(byte[] v) throws Exception { |
| 1969 |
System.out.println("Bounded Replay Leak check: Wait before GC 2"); |
| 1970 |
Thread.sleep(1000); |
| 1971 |
|
| 1972 |
System.out.println("Bounded Replay Leak check: GC 2"); |
| 1973 |
System.gc(); |
| 1974 |
|
| 1975 |
Thread.sleep(500); |
| 1976 |
|
| 1977 |
after.set(memoryMXBean.getHeapMemoryUsage().getUsed()); |
| 1978 |
} |
| 1979 |
}); |
| 1980 |
|
| 1981 |
System.out.printf("Bounded Replay Leak check: After: %.3f MB%n", after.get() / 1024.0 / 1024.0); |
| 1982 |
|
| 1983 |
if (initial + 100 * 1024 * 1024 < after.get()) { |
| 1984 |
Assert.fail("Bounded Replay Leak check: Memory leak detected: " + (initial / 1024.0 / 1024.0) |
| 1985 |
+ " -> " + after.get() / 1024.0 / 1024.0); |
| 1986 |
} |
| 1987 |
} |
| 1988 |
|
| 1989 |
@Test |
| 1990 |
public void sizeBoundEagerTruncate() throws Exception { |
| 1991 |
|
| 1992 |
PublishProcessor<int[]> pp = PublishProcessor.create(); |
| 1993 |
|
| 1994 |
ConnectableFlowable<int[]> cf = pp.replay(1, true); |
| 1995 |
|
| 1996 |
TestSubscriber<int[]> ts = cf.test(); |
| 1997 |
|
| 1998 |
cf.connect(); |
| 1999 |
|
| 2000 |
Thread.sleep(200); |
| 2001 |
System.gc(); |
| 2002 |
Thread.sleep(200); |
| 2003 |
|
| 2004 |
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); |
| 2005 |
long initial = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2006 |
|
| 2007 |
System.out.printf("Bounded Replay Leak check: Starting: %.3f MB%n", initial / 1024.0 / 1024.0); |
| 2008 |
|
| 2009 |
pp.onNext(new int[100 * 1024 * 1024]); |
| 2010 |
|
| 2011 |
ts.assertValueCount(1); |
| 2012 |
ts.values().clear(); |
| 2013 |
|
| 2014 |
pp.onNext(new int[0]); |
| 2015 |
|
| 2016 |
Thread.sleep(200); |
| 2017 |
System.gc(); |
| 2018 |
Thread.sleep(200); |
| 2019 |
|
| 2020 |
long after = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2021 |
|
| 2022 |
ts.cancel(); |
| 2023 |
|
| 2024 |
System.out.printf("Bounded Replay Leak check: After: %.3f MB%n", after / 1024.0 / 1024.0); |
| 2025 |
|
| 2026 |
if (initial + 100 * 1024 * 1024 < after) { |
| 2027 |
Assert.fail("Bounded Replay Leak check: Memory leak detected: " + (initial / 1024.0 / 1024.0) |
| 2028 |
+ " -> " + after / 1024.0 / 1024.0); |
| 2029 |
} |
| 2030 |
} |
| 2031 |
|
| 2032 |
@Test |
| 2033 |
public void timeBoundEagerTruncate() throws Exception { |
| 2034 |
|
| 2035 |
PublishProcessor<int[]> pp = PublishProcessor.create(); |
| 2036 |
|
| 2037 |
TestScheduler scheduler = new TestScheduler(); |
| 2038 |
|
| 2039 |
ConnectableFlowable<int[]> cf = pp.replay(1, TimeUnit.SECONDS, scheduler, true); |
| 2040 |
|
| 2041 |
TestSubscriber<int[]> ts = cf.test(); |
| 2042 |
|
| 2043 |
cf.connect(); |
| 2044 |
|
| 2045 |
Thread.sleep(200); |
| 2046 |
System.gc(); |
| 2047 |
Thread.sleep(200); |
| 2048 |
|
| 2049 |
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); |
| 2050 |
long initial = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2051 |
|
| 2052 |
System.out.printf("Bounded Replay Leak check: Starting: %.3f MB%n", initial / 1024.0 / 1024.0); |
| 2053 |
|
| 2054 |
pp.onNext(new int[100 * 1024 * 1024]); |
| 2055 |
|
| 2056 |
ts.assertValueCount(1); |
| 2057 |
ts.values().clear(); |
| 2058 |
|
| 2059 |
scheduler.advanceTimeBy(2, TimeUnit.SECONDS); |
| 2060 |
|
| 2061 |
pp.onNext(new int[0]); |
| 2062 |
|
| 2063 |
Thread.sleep(200); |
| 2064 |
System.gc(); |
| 2065 |
Thread.sleep(200); |
| 2066 |
|
| 2067 |
long after = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2068 |
|
| 2069 |
ts.cancel(); |
| 2070 |
|
| 2071 |
System.out.printf("Bounded Replay Leak check: After: %.3f MB%n", after / 1024.0 / 1024.0); |
| 2072 |
|
| 2073 |
if (initial + 100 * 1024 * 1024 < after) { |
| 2074 |
Assert.fail("Bounded Replay Leak check: Memory leak detected: " + (initial / 1024.0 / 1024.0) |
| 2075 |
+ " -> " + after / 1024.0 / 1024.0); |
| 2076 |
} |
| 2077 |
} |
| 2078 |
|
| 2079 |
@Test |
| 2080 |
public void timeAndSizeBoundEagerTruncate() throws Exception { |
| 2081 |
|
| 2082 |
PublishProcessor<int[]> pp = PublishProcessor.create(); |
| 2083 |
|
| 2084 |
TestScheduler scheduler = new TestScheduler(); |
| 2085 |
|
| 2086 |
ConnectableFlowable<int[]> cf = pp.replay(1, 5, TimeUnit.SECONDS, scheduler, true); |
| 2087 |
|
| 2088 |
TestSubscriber<int[]> ts = cf.test(); |
| 2089 |
|
| 2090 |
cf.connect(); |
| 2091 |
|
| 2092 |
Thread.sleep(200); |
| 2093 |
System.gc(); |
| 2094 |
Thread.sleep(200); |
| 2095 |
|
| 2096 |
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); |
| 2097 |
long initial = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2098 |
|
| 2099 |
System.out.printf("Bounded Replay Leak check: Starting: %.3f MB%n", initial / 1024.0 / 1024.0); |
| 2100 |
|
| 2101 |
pp.onNext(new int[100 * 1024 * 1024]); |
| 2102 |
|
| 2103 |
ts.assertValueCount(1); |
| 2104 |
ts.values().clear(); |
| 2105 |
|
| 2106 |
scheduler.advanceTimeBy(2, TimeUnit.SECONDS); |
| 2107 |
|
| 2108 |
pp.onNext(new int[0]); |
| 2109 |
|
| 2110 |
Thread.sleep(200); |
| 2111 |
System.gc(); |
| 2112 |
Thread.sleep(200); |
| 2113 |
|
| 2114 |
long after = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2115 |
|
| 2116 |
ts.cancel(); |
| 2117 |
|
| 2118 |
System.out.printf("Bounded Replay Leak check: After: %.3f MB%n", after / 1024.0 / 1024.0); |
| 2119 |
|
| 2120 |
if (initial + 100 * 1024 * 1024 < after) { |
| 2121 |
Assert.fail("Bounded Replay Leak check: Memory leak detected: " + (initial / 1024.0 / 1024.0) |
| 2122 |
+ " -> " + after / 1024.0 / 1024.0); |
| 2123 |
} |
| 2124 |
} |
| 2125 |
|
| 2126 |
@Test |
| 2127 |
public void sizeBoundSelectorEagerTruncate() throws Exception { |
| 2128 |
|
| 2129 |
PublishProcessor<int[]> pp = PublishProcessor.create(); |
| 2130 |
|
| 2131 |
Flowable<int[]> cf = pp.replay(Functions.<Flowable<int[]>>identity(), 1, true); |
| 2132 |
|
| 2133 |
TestSubscriber<int[]> ts = cf.test(); |
| 2134 |
|
| 2135 |
Thread.sleep(200); |
| 2136 |
System.gc(); |
| 2137 |
Thread.sleep(200); |
| 2138 |
|
| 2139 |
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); |
| 2140 |
long initial = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2141 |
|
| 2142 |
System.out.printf("Bounded Replay Leak check: Starting: %.3f MB%n", initial / 1024.0 / 1024.0); |
| 2143 |
|
| 2144 |
pp.onNext(new int[100 * 1024 * 1024]); |
| 2145 |
|
| 2146 |
ts.assertValueCount(1); |
| 2147 |
ts.values().clear(); |
| 2148 |
|
| 2149 |
pp.onNext(new int[0]); |
| 2150 |
|
| 2151 |
Thread.sleep(200); |
| 2152 |
System.gc(); |
| 2153 |
Thread.sleep(200); |
| 2154 |
|
| 2155 |
long after = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2156 |
|
| 2157 |
ts.cancel(); |
| 2158 |
|
| 2159 |
System.out.printf("Bounded Replay Leak check: After: %.3f MB%n", after / 1024.0 / 1024.0); |
| 2160 |
|
| 2161 |
if (initial + 100 * 1024 * 1024 < after) { |
| 2162 |
Assert.fail("Bounded Replay Leak check: Memory leak detected: " + (initial / 1024.0 / 1024.0) |
| 2163 |
+ " -> " + after / 1024.0 / 1024.0); |
| 2164 |
} |
| 2165 |
} |
| 2166 |
|
| 2167 |
@Test |
| 2168 |
public void timeBoundSelectorEagerTruncate() throws Exception { |
| 2169 |
|
| 2170 |
PublishProcessor<int[]> pp = PublishProcessor.create(); |
| 2171 |
|
| 2172 |
TestScheduler scheduler = new TestScheduler(); |
| 2173 |
|
| 2174 |
Flowable<int[]> cf = pp.replay(Functions.<Flowable<int[]>>identity(), 1, TimeUnit.SECONDS, scheduler, true); |
| 2175 |
|
| 2176 |
TestSubscriber<int[]> ts = cf.test(); |
| 2177 |
|
| 2178 |
Thread.sleep(200); |
| 2179 |
System.gc(); |
| 2180 |
Thread.sleep(200); |
| 2181 |
|
| 2182 |
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); |
| 2183 |
long initial = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2184 |
|
| 2185 |
System.out.printf("Bounded Replay Leak check: Starting: %.3f MB%n", initial / 1024.0 / 1024.0); |
| 2186 |
|
| 2187 |
pp.onNext(new int[100 * 1024 * 1024]); |
| 2188 |
|
| 2189 |
ts.assertValueCount(1); |
| 2190 |
ts.values().clear(); |
| 2191 |
|
| 2192 |
scheduler.advanceTimeBy(2, TimeUnit.SECONDS); |
| 2193 |
|
| 2194 |
pp.onNext(new int[0]); |
| 2195 |
|
| 2196 |
Thread.sleep(200); |
| 2197 |
System.gc(); |
| 2198 |
Thread.sleep(200); |
| 2199 |
|
| 2200 |
long after = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2201 |
|
| 2202 |
ts.cancel(); |
| 2203 |
|
| 2204 |
System.out.printf("Bounded Replay Leak check: After: %.3f MB%n", after / 1024.0 / 1024.0); |
| 2205 |
|
| 2206 |
if (initial + 100 * 1024 * 1024 < after) { |
| 2207 |
Assert.fail("Bounded Replay Leak check: Memory leak detected: " + (initial / 1024.0 / 1024.0) |
| 2208 |
+ " -> " + after / 1024.0 / 1024.0); |
| 2209 |
} |
| 2210 |
} |
| 2211 |
|
| 2212 |
@Test |
| 2213 |
public void timeAndSizeBoundSelectorEagerTruncate() throws Exception { |
| 2214 |
|
| 2215 |
PublishProcessor<int[]> pp = PublishProcessor.create(); |
| 2216 |
|
| 2217 |
TestScheduler scheduler = new TestScheduler(); |
| 2218 |
|
| 2219 |
Flowable<int[]> cf = pp.replay(Functions.<Flowable<int[]>>identity(), 1, 5, TimeUnit.SECONDS, scheduler, true); |
| 2220 |
|
| 2221 |
TestSubscriber<int[]> ts = cf.test(); |
| 2222 |
|
| 2223 |
Thread.sleep(200); |
| 2224 |
System.gc(); |
| 2225 |
Thread.sleep(200); |
| 2226 |
|
| 2227 |
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); |
| 2228 |
long initial = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2229 |
|
| 2230 |
System.out.printf("Bounded Replay Leak check: Starting: %.3f MB%n", initial / 1024.0 / 1024.0); |
| 2231 |
|
| 2232 |
pp.onNext(new int[100 * 1024 * 1024]); |
| 2233 |
|
| 2234 |
ts.assertValueCount(1); |
| 2235 |
ts.values().clear(); |
| 2236 |
|
| 2237 |
scheduler.advanceTimeBy(2, TimeUnit.SECONDS); |
| 2238 |
|
| 2239 |
pp.onNext(new int[0]); |
| 2240 |
|
| 2241 |
Thread.sleep(200); |
| 2242 |
System.gc(); |
| 2243 |
Thread.sleep(200); |
| 2244 |
|
| 2245 |
long after = memoryMXBean.getHeapMemoryUsage().getUsed(); |
| 2246 |
|
| 2247 |
ts.cancel(); |
| 2248 |
|
| 2249 |
System.out.printf("Bounded Replay Leak check: After: %.3f MB%n", after / 1024.0 / 1024.0); |
| 2250 |
|
| 2251 |
if (initial + 100 * 1024 * 1024 < after) { |
| 2252 |
Assert.fail("Bounded Replay Leak check: Memory leak detected: " + (initial / 1024.0 / 1024.0) |
| 2253 |
+ " -> " + after / 1024.0 / 1024.0); |
| 2254 |
} |
| 2255 |
} |
| 2256 |
|
| 2257 |
@Test |
| 2258 |
public void timeAndSizeNoTerminalTruncationOnTimechange() { |
| 2259 |
Flowable.just(1).replay(1, 1, TimeUnit.SECONDS, new TimesteppingScheduler(), true) |
| 2260 |
.autoConnect() |
| 2261 |
.test() |
| 2262 |
.assertComplete() |
| 2263 |
.assertNoErrors(); |
| 2264 |
} |
| 2265 |
|
| 2266 |
@Test |
| 2267 |
public void disposeNoNeedForResetSizeBound() { |
| 2268 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2269 |
|
| 2270 |
ConnectableFlowable<Integer> cf = pp.replay(10, true); |
| 2271 |
|
| 2272 |
TestSubscriber<Integer> ts = cf.test(); |
| 2273 |
|
| 2274 |
Disposable d = cf.connect(); |
| 2275 |
|
| 2276 |
pp.onNext(1); |
| 2277 |
|
| 2278 |
d.dispose(); |
| 2279 |
|
| 2280 |
ts = cf.test(); |
| 2281 |
|
| 2282 |
ts.assertEmpty(); |
| 2283 |
|
| 2284 |
cf.connect(); |
| 2285 |
|
| 2286 |
ts.assertEmpty(); |
| 2287 |
|
| 2288 |
pp.onNext(2); |
| 2289 |
|
| 2290 |
ts.assertValuesOnly(2); |
| 2291 |
} |
| 2292 |
|
| 2293 |
@Test |
| 2294 |
public void disposeNoNeedForResetTimeBound() { |
| 2295 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2296 |
|
| 2297 |
ConnectableFlowable<Integer> cf = pp.replay(10, TimeUnit.MINUTES, Schedulers.single(), true); |
| 2298 |
|
| 2299 |
TestSubscriber<Integer> ts = cf.test(); |
| 2300 |
|
| 2301 |
Disposable d = cf.connect(); |
| 2302 |
|
| 2303 |
pp.onNext(1); |
| 2304 |
|
| 2305 |
d.dispose(); |
| 2306 |
|
| 2307 |
ts = cf.test(); |
| 2308 |
|
| 2309 |
ts.assertEmpty(); |
| 2310 |
|
| 2311 |
cf.connect(); |
| 2312 |
|
| 2313 |
ts.assertEmpty(); |
| 2314 |
|
| 2315 |
pp.onNext(2); |
| 2316 |
|
| 2317 |
ts.assertValuesOnly(2); |
| 2318 |
} |
| 2319 |
|
| 2320 |
@Test |
| 2321 |
public void disposeNoNeedForResetTimeAndSIzeBound() { |
| 2322 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2323 |
|
| 2324 |
ConnectableFlowable<Integer> cf = pp.replay(10, 10, TimeUnit.MINUTES, Schedulers.single(), true); |
| 2325 |
|
| 2326 |
TestSubscriber<Integer> ts = cf.test(); |
| 2327 |
|
| 2328 |
Disposable d = cf.connect(); |
| 2329 |
|
| 2330 |
pp.onNext(1); |
| 2331 |
|
| 2332 |
d.dispose(); |
| 2333 |
|
| 2334 |
ts = cf.test(); |
| 2335 |
|
| 2336 |
ts.assertEmpty(); |
| 2337 |
|
| 2338 |
cf.connect(); |
| 2339 |
|
| 2340 |
ts.assertEmpty(); |
| 2341 |
|
| 2342 |
pp.onNext(2); |
| 2343 |
|
| 2344 |
ts.assertValuesOnly(2); |
| 2345 |
} |
| 2346 |
} |