| 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.processors; |
| 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.Arrays; |
| 22 |
import java.util.concurrent.*; |
| 23 |
import java.util.concurrent.atomic.*; |
| 24 |
|
| 25 |
import org.junit.*; |
| 26 |
import org.mockito.*; |
| 27 |
import org.reactivestreams.*; |
| 28 |
|
| 29 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 30 |
import io.reactivex.rxjava3.core.Flowable; |
| 31 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 32 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 33 |
import io.reactivex.rxjava3.functions.*; |
| 34 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 35 |
import io.reactivex.rxjava3.processors.ReplayProcessor.*; |
| 36 |
import io.reactivex.rxjava3.schedulers.*; |
| 37 |
import io.reactivex.rxjava3.subscribers.*; |
| 38 |
import io.reactivex.rxjava3.testsupport.*; |
| 39 |
|
| 40 |
public class ReplayProcessorTest extends FlowableProcessorTest<Object> { |
| 41 |
|
| 42 |
private final Throwable testException = new Throwable(); |
| 43 |
|
| 44 |
@Override |
| 45 |
protected FlowableProcessor<Object> create() { |
| 46 |
return ReplayProcessor.create(); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
@SuppressUndeliverable |
| 51 |
public void completed() { |
| 52 |
ReplayProcessor<String> processor = ReplayProcessor.create(); |
| 53 |
|
| 54 |
Subscriber<String> subscriber1 = TestHelper.mockSubscriber(); |
| 55 |
processor.subscribe(subscriber1); |
| 56 |
|
| 57 |
processor.onNext("one"); |
| 58 |
processor.onNext("two"); |
| 59 |
processor.onNext("three"); |
| 60 |
processor.onComplete(); |
| 61 |
|
| 62 |
processor.onNext("four"); |
| 63 |
processor.onComplete(); |
| 64 |
processor.onError(new Throwable()); |
| 65 |
|
| 66 |
assertCompletedSubscriber(subscriber1); |
| 67 |
|
| 68 |
// assert that subscribing a 2nd time gets the same data |
| 69 |
Subscriber<String> subscriber2 = TestHelper.mockSubscriber(); |
| 70 |
processor.subscribe(subscriber2); |
| 71 |
assertCompletedSubscriber(subscriber2); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
@SuppressUndeliverable |
| 76 |
public void completedStopsEmittingData() { |
| 77 |
ReplayProcessor<Integer> channel = ReplayProcessor.create(); |
| 78 |
Subscriber<Object> observerA = TestHelper.mockSubscriber(); |
| 79 |
Subscriber<Object> observerB = TestHelper.mockSubscriber(); |
| 80 |
Subscriber<Object> observerC = TestHelper.mockSubscriber(); |
| 81 |
Subscriber<Object> observerD = TestHelper.mockSubscriber(); |
| 82 |
TestSubscriber<Object> ts = new TestSubscriber<>(observerA); |
| 83 |
|
| 84 |
channel.subscribe(ts); |
| 85 |
channel.subscribe(observerB); |
| 86 |
|
| 87 |
InOrder inOrderA = inOrder(observerA); |
| 88 |
InOrder inOrderB = inOrder(observerB); |
| 89 |
InOrder inOrderC = inOrder(observerC); |
| 90 |
InOrder inOrderD = inOrder(observerD); |
| 91 |
|
| 92 |
channel.onNext(42); |
| 93 |
|
| 94 |
// both A and B should have received 42 from before subscription |
| 95 |
inOrderA.verify(observerA).onNext(42); |
| 96 |
inOrderB.verify(observerB).onNext(42); |
| 97 |
|
| 98 |
ts.cancel(); |
| 99 |
|
| 100 |
// a should receive no more |
| 101 |
inOrderA.verifyNoMoreInteractions(); |
| 102 |
|
| 103 |
channel.onNext(4711); |
| 104 |
|
| 105 |
// only be should receive 4711 at this point |
| 106 |
inOrderB.verify(observerB).onNext(4711); |
| 107 |
|
| 108 |
channel.onComplete(); |
| 109 |
|
| 110 |
// B is subscribed so should receive onComplete |
| 111 |
inOrderB.verify(observerB).onComplete(); |
| 112 |
|
| 113 |
channel.subscribe(observerC); |
| 114 |
|
| 115 |
// when C subscribes it should receive 42, 4711, onComplete |
| 116 |
inOrderC.verify(observerC).onNext(42); |
| 117 |
inOrderC.verify(observerC).onNext(4711); |
| 118 |
inOrderC.verify(observerC).onComplete(); |
| 119 |
|
| 120 |
// if further events are propagated they should be ignored |
| 121 |
channel.onNext(13); |
| 122 |
channel.onNext(14); |
| 123 |
channel.onNext(15); |
| 124 |
channel.onError(new RuntimeException()); |
| 125 |
|
| 126 |
// a new subscription should only receive what was emitted prior to terminal state onComplete |
| 127 |
channel.subscribe(observerD); |
| 128 |
|
| 129 |
inOrderD.verify(observerD).onNext(42); |
| 130 |
inOrderD.verify(observerD).onNext(4711); |
| 131 |
inOrderD.verify(observerD).onComplete(); |
| 132 |
|
| 133 |
verify(observerA).onSubscribe((Subscription)notNull()); |
| 134 |
verify(observerB).onSubscribe((Subscription)notNull()); |
| 135 |
verify(observerC).onSubscribe((Subscription)notNull()); |
| 136 |
verify(observerD).onSubscribe((Subscription)notNull()); |
| 137 |
Mockito.verifyNoMoreInteractions(observerA); |
| 138 |
Mockito.verifyNoMoreInteractions(observerB); |
| 139 |
Mockito.verifyNoMoreInteractions(observerC); |
| 140 |
Mockito.verifyNoMoreInteractions(observerD); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
@SuppressUndeliverable |
| 146 |
public void completedAfterError() { |
| 147 |
ReplayProcessor<String> processor = ReplayProcessor.create(); |
| 148 |
|
| 149 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 150 |
|
| 151 |
processor.onNext("one"); |
| 152 |
processor.onError(testException); |
| 153 |
processor.onNext("two"); |
| 154 |
processor.onComplete(); |
| 155 |
processor.onError(new RuntimeException()); |
| 156 |
|
| 157 |
processor.subscribe(subscriber); |
| 158 |
verify(subscriber).onSubscribe((Subscription)notNull()); |
| 159 |
verify(subscriber, times(1)).onNext("one"); |
| 160 |
verify(subscriber, times(1)).onError(testException); |
| 161 |
verifyNoMoreInteractions(subscriber); |
| 162 |
} |
| 163 |
|
| 164 |
private void assertCompletedSubscriber(Subscriber<String> subscriber) { |
| 165 |
InOrder inOrder = inOrder(subscriber); |
| 166 |
|
| 167 |
inOrder.verify(subscriber, times(1)).onNext("one"); |
| 168 |
inOrder.verify(subscriber, times(1)).onNext("two"); |
| 169 |
inOrder.verify(subscriber, times(1)).onNext("three"); |
| 170 |
inOrder.verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 171 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 172 |
inOrder.verifyNoMoreInteractions(); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
@SuppressUndeliverable |
| 177 |
public void error() { |
| 178 |
ReplayProcessor<String> processor = ReplayProcessor.create(); |
| 179 |
|
| 180 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 181 |
processor.subscribe(subscriber); |
| 182 |
|
| 183 |
processor.onNext("one"); |
| 184 |
processor.onNext("two"); |
| 185 |
processor.onNext("three"); |
| 186 |
processor.onError(testException); |
| 187 |
|
| 188 |
processor.onNext("four"); |
| 189 |
processor.onError(new Throwable()); |
| 190 |
processor.onComplete(); |
| 191 |
|
| 192 |
assertErrorSubscriber(subscriber); |
| 193 |
|
| 194 |
subscriber = TestHelper.mockSubscriber(); |
| 195 |
processor.subscribe(subscriber); |
| 196 |
assertErrorSubscriber(subscriber); |
| 197 |
} |
| 198 |
|
| 199 |
private void assertErrorSubscriber(Subscriber<String> subscriber) { |
| 200 |
verify(subscriber, times(1)).onNext("one"); |
| 201 |
verify(subscriber, times(1)).onNext("two"); |
| 202 |
verify(subscriber, times(1)).onNext("three"); |
| 203 |
verify(subscriber, times(1)).onError(testException); |
| 204 |
verify(subscriber, Mockito.never()).onComplete(); |
| 205 |
} |
| 206 |
|
| 207 |
@Test |
| 208 |
public void subscribeMidSequence() { |
| 209 |
ReplayProcessor<String> processor = ReplayProcessor.create(); |
| 210 |
|
| 211 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 212 |
processor.subscribe(subscriber); |
| 213 |
|
| 214 |
processor.onNext("one"); |
| 215 |
processor.onNext("two"); |
| 216 |
|
| 217 |
assertObservedUntilTwo(subscriber); |
| 218 |
|
| 219 |
Subscriber<String> anotherSubscriber = TestHelper.mockSubscriber(); |
| 220 |
processor.subscribe(anotherSubscriber); |
| 221 |
assertObservedUntilTwo(anotherSubscriber); |
| 222 |
|
| 223 |
processor.onNext("three"); |
| 224 |
processor.onComplete(); |
| 225 |
|
| 226 |
assertCompletedSubscriber(subscriber); |
| 227 |
assertCompletedSubscriber(anotherSubscriber); |
| 228 |
} |
| 229 |
|
| 230 |
@Test |
| 231 |
public void unsubscribeFirstSubscriber() { |
| 232 |
ReplayProcessor<String> processor = ReplayProcessor.create(); |
| 233 |
|
| 234 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 235 |
TestSubscriber<String> ts = new TestSubscriber<>(subscriber); |
| 236 |
processor.subscribe(ts); |
| 237 |
|
| 238 |
processor.onNext("one"); |
| 239 |
processor.onNext("two"); |
| 240 |
|
| 241 |
ts.cancel(); |
| 242 |
assertObservedUntilTwo(subscriber); |
| 243 |
|
| 244 |
Subscriber<String> anotherSubscriber = TestHelper.mockSubscriber(); |
| 245 |
processor.subscribe(anotherSubscriber); |
| 246 |
assertObservedUntilTwo(anotherSubscriber); |
| 247 |
|
| 248 |
processor.onNext("three"); |
| 249 |
processor.onComplete(); |
| 250 |
|
| 251 |
assertObservedUntilTwo(subscriber); |
| 252 |
assertCompletedSubscriber(anotherSubscriber); |
| 253 |
} |
| 254 |
|
| 255 |
private void assertObservedUntilTwo(Subscriber<String> subscriber) { |
| 256 |
verify(subscriber, times(1)).onNext("one"); |
| 257 |
verify(subscriber, times(1)).onNext("two"); |
| 258 |
verify(subscriber, Mockito.never()).onNext("three"); |
| 259 |
verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 260 |
verify(subscriber, Mockito.never()).onComplete(); |
| 261 |
} |
| 262 |
|
| 263 |
@Test |
| 264 |
public void newSubscriberDoesntBlockExisting() throws InterruptedException { |
| 265 |
|
| 266 |
final AtomicReference<String> lastValueForSubscriber1 = new AtomicReference<>(); |
| 267 |
Subscriber<String> subscriber1 = new DefaultSubscriber<String>() { |
| 268 |
|
| 269 |
@Override |
| 270 |
public void onComplete() { |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
@Override |
| 275 |
public void onError(Throwable e) { |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
@Override |
| 280 |
public void onNext(String v) { |
| 281 |
System.out.println("observer1: " + v); |
| 282 |
lastValueForSubscriber1.set(v); |
| 283 |
} |
| 284 |
|
| 285 |
}; |
| 286 |
|
| 287 |
final AtomicReference<String> lastValueForSubscriber2 = new AtomicReference<>(); |
| 288 |
final CountDownLatch oneReceived = new CountDownLatch(1); |
| 289 |
final CountDownLatch makeSlow = new CountDownLatch(1); |
| 290 |
final CountDownLatch completed = new CountDownLatch(1); |
| 291 |
Subscriber<String> subscriber2 = new DefaultSubscriber<String>() { |
| 292 |
|
| 293 |
@Override |
| 294 |
public void onComplete() { |
| 295 |
completed.countDown(); |
| 296 |
} |
| 297 |
|
| 298 |
@Override |
| 299 |
public void onError(Throwable e) { |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public void onNext(String v) { |
| 305 |
System.out.println("observer2: " + v); |
| 306 |
if (v.equals("one")) { |
| 307 |
oneReceived.countDown(); |
| 308 |
} else { |
| 309 |
try { |
| 310 |
makeSlow.await(); |
| 311 |
} catch (InterruptedException e) { |
| 312 |
e.printStackTrace(); |
| 313 |
} |
| 314 |
lastValueForSubscriber2.set(v); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
}; |
| 319 |
|
| 320 |
ReplayProcessor<String> processor = ReplayProcessor.create(); |
| 321 |
processor.subscribe(subscriber1); |
| 322 |
processor.onNext("one"); |
| 323 |
assertEquals("one", lastValueForSubscriber1.get()); |
| 324 |
processor.onNext("two"); |
| 325 |
assertEquals("two", lastValueForSubscriber1.get()); |
| 326 |
|
| 327 |
// use subscribeOn to make this async otherwise we deadlock as we are using CountDownLatches |
| 328 |
processor.subscribeOn(Schedulers.newThread()).subscribe(subscriber2); |
| 329 |
|
| 330 |
System.out.println("before waiting for one"); |
| 331 |
|
| 332 |
// wait until observer2 starts having replay occur |
| 333 |
oneReceived.await(); |
| 334 |
|
| 335 |
System.out.println("after waiting for one"); |
| 336 |
|
| 337 |
processor.onNext("three"); |
| 338 |
|
| 339 |
System.out.println("sent three"); |
| 340 |
|
| 341 |
// if subscription blocked existing subscribers then 'makeSlow' would cause this to not be there yet |
| 342 |
assertEquals("three", lastValueForSubscriber1.get()); |
| 343 |
|
| 344 |
System.out.println("about to send onComplete"); |
| 345 |
|
| 346 |
processor.onComplete(); |
| 347 |
|
| 348 |
System.out.println("completed processor"); |
| 349 |
|
| 350 |
// release |
| 351 |
makeSlow.countDown(); |
| 352 |
|
| 353 |
System.out.println("makeSlow released"); |
| 354 |
|
| 355 |
completed.await(); |
| 356 |
// all of them should be emitted with the last being "three" |
| 357 |
assertEquals("three", lastValueForSubscriber2.get()); |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
@Test |
| 362 |
public void subscriptionLeak() { |
| 363 |
ReplayProcessor<Object> replaySubject = ReplayProcessor.create(); |
| 364 |
|
| 365 |
Disposable connection = replaySubject.subscribe(); |
| 366 |
|
| 367 |
assertEquals(1, replaySubject.subscriberCount()); |
| 368 |
|
| 369 |
connection.dispose(); |
| 370 |
|
| 371 |
assertEquals(0, replaySubject.subscriberCount()); |
| 372 |
} |
| 373 |
|
| 374 |
@Test |
| 375 |
public void unsubscriptionCase() { |
| 376 |
ReplayProcessor<String> src = ReplayProcessor.create(); |
| 377 |
|
| 378 |
for (int i = 0; i < 10; i++) { |
| 379 |
final Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 380 |
InOrder inOrder = inOrder(subscriber); |
| 381 |
String v = "" + i; |
| 382 |
src.onNext(v); |
| 383 |
System.out.printf("Turn: %d%n", i); |
| 384 |
src.firstElement().toFlowable() |
| 385 |
.flatMap(new Function<String, Flowable<String>>() { |
| 386 |
|
| 387 |
@Override |
| 388 |
public Flowable<String> apply(String t1) { |
| 389 |
return Flowable.just(t1 + ", " + t1); |
| 390 |
} |
| 391 |
}) |
| 392 |
.subscribe(new DefaultSubscriber<String>() { |
| 393 |
@Override |
| 394 |
public void onNext(String t) { |
| 395 |
System.out.println(t); |
| 396 |
subscriber.onNext(t); |
| 397 |
} |
| 398 |
|
| 399 |
@Override |
| 400 |
public void onError(Throwable e) { |
| 401 |
subscriber.onError(e); |
| 402 |
} |
| 403 |
|
| 404 |
@Override |
| 405 |
public void onComplete() { |
| 406 |
subscriber.onComplete(); |
| 407 |
} |
| 408 |
}); |
| 409 |
inOrder.verify(subscriber).onNext("0, 0"); |
| 410 |
inOrder.verify(subscriber).onComplete(); |
| 411 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
@Test |
| 416 |
public void terminateOnce() { |
| 417 |
ReplayProcessor<Integer> source = ReplayProcessor.create(); |
| 418 |
source.onNext(1); |
| 419 |
source.onNext(2); |
| 420 |
source.onComplete(); |
| 421 |
|
| 422 |
final Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 423 |
|
| 424 |
source.subscribe(new DefaultSubscriber<Integer>() { |
| 425 |
|
| 426 |
@Override |
| 427 |
public void onNext(Integer t) { |
| 428 |
subscriber.onNext(t); |
| 429 |
} |
| 430 |
|
| 431 |
@Override |
| 432 |
public void onError(Throwable e) { |
| 433 |
subscriber.onError(e); |
| 434 |
} |
| 435 |
|
| 436 |
@Override |
| 437 |
public void onComplete() { |
| 438 |
subscriber.onComplete(); |
| 439 |
} |
| 440 |
}); |
| 441 |
|
| 442 |
verify(subscriber).onNext(1); |
| 443 |
verify(subscriber).onNext(2); |
| 444 |
verify(subscriber).onComplete(); |
| 445 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 446 |
} |
| 447 |
|
| 448 |
@Test |
| 449 |
public void replay1AfterTermination() { |
| 450 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(1); |
| 451 |
|
| 452 |
source.onNext(1); |
| 453 |
source.onNext(2); |
| 454 |
source.onComplete(); |
| 455 |
|
| 456 |
for (int i = 0; i < 1; i++) { |
| 457 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 458 |
|
| 459 |
source.subscribe(subscriber); |
| 460 |
|
| 461 |
verify(subscriber, never()).onNext(1); |
| 462 |
verify(subscriber).onNext(2); |
| 463 |
verify(subscriber).onComplete(); |
| 464 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
@Test |
| 469 |
public void replay1Directly() { |
| 470 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(1); |
| 471 |
|
| 472 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 473 |
|
| 474 |
source.onNext(1); |
| 475 |
source.onNext(2); |
| 476 |
|
| 477 |
source.subscribe(subscriber); |
| 478 |
|
| 479 |
source.onNext(3); |
| 480 |
source.onComplete(); |
| 481 |
|
| 482 |
verify(subscriber, never()).onNext(1); |
| 483 |
verify(subscriber).onNext(2); |
| 484 |
verify(subscriber).onNext(3); |
| 485 |
verify(subscriber).onComplete(); |
| 486 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 487 |
} |
| 488 |
|
| 489 |
@Test |
| 490 |
public void replayTimestampedAfterTermination() { |
| 491 |
TestScheduler scheduler = new TestScheduler(); |
| 492 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.SECONDS, scheduler); |
| 493 |
|
| 494 |
source.onNext(1); |
| 495 |
|
| 496 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 497 |
|
| 498 |
source.onNext(2); |
| 499 |
|
| 500 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 501 |
|
| 502 |
source.onNext(3); |
| 503 |
source.onComplete(); |
| 504 |
|
| 505 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 506 |
|
| 507 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 508 |
|
| 509 |
source.subscribe(subscriber); |
| 510 |
|
| 511 |
verify(subscriber, never()).onNext(1); |
| 512 |
verify(subscriber, never()).onNext(2); |
| 513 |
verify(subscriber, never()).onNext(3); |
| 514 |
verify(subscriber).onComplete(); |
| 515 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 516 |
} |
| 517 |
|
| 518 |
@Test |
| 519 |
public void replayTimestampedDirectly() { |
| 520 |
TestScheduler scheduler = new TestScheduler(); |
| 521 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.SECONDS, scheduler); |
| 522 |
|
| 523 |
source.onNext(1); |
| 524 |
|
| 525 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 526 |
|
| 527 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 528 |
|
| 529 |
source.subscribe(subscriber); |
| 530 |
|
| 531 |
source.onNext(2); |
| 532 |
|
| 533 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 534 |
|
| 535 |
source.onNext(3); |
| 536 |
|
| 537 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 538 |
|
| 539 |
source.onComplete(); |
| 540 |
|
| 541 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 542 |
|
| 543 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 544 |
verify(subscriber, never()).onNext(1); |
| 545 |
verify(subscriber).onNext(2); |
| 546 |
verify(subscriber).onNext(3); |
| 547 |
verify(subscriber).onComplete(); |
| 548 |
} |
| 549 |
|
| 550 |
@Test |
| 551 |
public void currentStateMethodsNormal() { |
| 552 |
ReplayProcessor<Object> as = ReplayProcessor.create(); |
| 553 |
|
| 554 |
assertFalse(as.hasThrowable()); |
| 555 |
assertFalse(as.hasComplete()); |
| 556 |
assertNull(as.getThrowable()); |
| 557 |
|
| 558 |
as.onNext(1); |
| 559 |
|
| 560 |
assertFalse(as.hasThrowable()); |
| 561 |
assertFalse(as.hasComplete()); |
| 562 |
assertNull(as.getThrowable()); |
| 563 |
|
| 564 |
as.onComplete(); |
| 565 |
|
| 566 |
assertFalse(as.hasThrowable()); |
| 567 |
assertTrue(as.hasComplete()); |
| 568 |
assertNull(as.getThrowable()); |
| 569 |
} |
| 570 |
|
| 571 |
@Test |
| 572 |
public void currentStateMethodsEmpty() { |
| 573 |
ReplayProcessor<Object> as = ReplayProcessor.create(); |
| 574 |
|
| 575 |
assertFalse(as.hasThrowable()); |
| 576 |
assertFalse(as.hasComplete()); |
| 577 |
assertNull(as.getThrowable()); |
| 578 |
|
| 579 |
as.onComplete(); |
| 580 |
|
| 581 |
assertFalse(as.hasThrowable()); |
| 582 |
assertTrue(as.hasComplete()); |
| 583 |
assertNull(as.getThrowable()); |
| 584 |
} |
| 585 |
|
| 586 |
@Test |
| 587 |
public void currentStateMethodsError() { |
| 588 |
ReplayProcessor<Object> as = ReplayProcessor.create(); |
| 589 |
|
| 590 |
assertFalse(as.hasThrowable()); |
| 591 |
assertFalse(as.hasComplete()); |
| 592 |
assertNull(as.getThrowable()); |
| 593 |
|
| 594 |
as.onError(new TestException()); |
| 595 |
|
| 596 |
assertTrue(as.hasThrowable()); |
| 597 |
assertFalse(as.hasComplete()); |
| 598 |
assertTrue(as.getThrowable() instanceof TestException); |
| 599 |
} |
| 600 |
|
| 601 |
@Test |
| 602 |
public void sizeAndHasAnyValueUnbounded() { |
| 603 |
ReplayProcessor<Object> rs = ReplayProcessor.create(); |
| 604 |
|
| 605 |
assertEquals(0, rs.size()); |
| 606 |
assertFalse(rs.hasValue()); |
| 607 |
|
| 608 |
rs.onNext(1); |
| 609 |
|
| 610 |
assertEquals(1, rs.size()); |
| 611 |
assertTrue(rs.hasValue()); |
| 612 |
|
| 613 |
rs.onNext(1); |
| 614 |
|
| 615 |
assertEquals(2, rs.size()); |
| 616 |
assertTrue(rs.hasValue()); |
| 617 |
|
| 618 |
rs.onComplete(); |
| 619 |
|
| 620 |
assertEquals(2, rs.size()); |
| 621 |
assertTrue(rs.hasValue()); |
| 622 |
} |
| 623 |
|
| 624 |
@Test |
| 625 |
public void sizeAndHasAnyValueEffectivelyUnbounded() { |
| 626 |
ReplayProcessor<Object> rs = ReplayProcessor.createUnbounded(); |
| 627 |
|
| 628 |
assertEquals(0, rs.size()); |
| 629 |
assertFalse(rs.hasValue()); |
| 630 |
|
| 631 |
rs.onNext(1); |
| 632 |
|
| 633 |
assertEquals(1, rs.size()); |
| 634 |
assertTrue(rs.hasValue()); |
| 635 |
|
| 636 |
rs.onNext(1); |
| 637 |
|
| 638 |
assertEquals(2, rs.size()); |
| 639 |
assertTrue(rs.hasValue()); |
| 640 |
|
| 641 |
rs.onComplete(); |
| 642 |
|
| 643 |
assertEquals(2, rs.size()); |
| 644 |
assertTrue(rs.hasValue()); |
| 645 |
} |
| 646 |
|
| 647 |
@Test |
| 648 |
public void sizeAndHasAnyValueUnboundedError() { |
| 649 |
ReplayProcessor<Object> rs = ReplayProcessor.create(); |
| 650 |
|
| 651 |
assertEquals(0, rs.size()); |
| 652 |
assertFalse(rs.hasValue()); |
| 653 |
|
| 654 |
rs.onNext(1); |
| 655 |
|
| 656 |
assertEquals(1, rs.size()); |
| 657 |
assertTrue(rs.hasValue()); |
| 658 |
|
| 659 |
rs.onNext(1); |
| 660 |
|
| 661 |
assertEquals(2, rs.size()); |
| 662 |
assertTrue(rs.hasValue()); |
| 663 |
|
| 664 |
rs.onError(new TestException()); |
| 665 |
|
| 666 |
assertEquals(2, rs.size()); |
| 667 |
assertTrue(rs.hasValue()); |
| 668 |
} |
| 669 |
|
| 670 |
@Test |
| 671 |
public void sizeAndHasAnyValueEffectivelyUnboundedError() { |
| 672 |
ReplayProcessor<Object> rs = ReplayProcessor.createUnbounded(); |
| 673 |
|
| 674 |
assertEquals(0, rs.size()); |
| 675 |
assertFalse(rs.hasValue()); |
| 676 |
|
| 677 |
rs.onNext(1); |
| 678 |
|
| 679 |
assertEquals(1, rs.size()); |
| 680 |
assertTrue(rs.hasValue()); |
| 681 |
|
| 682 |
rs.onNext(1); |
| 683 |
|
| 684 |
assertEquals(2, rs.size()); |
| 685 |
assertTrue(rs.hasValue()); |
| 686 |
|
| 687 |
rs.onError(new TestException()); |
| 688 |
|
| 689 |
assertEquals(2, rs.size()); |
| 690 |
assertTrue(rs.hasValue()); |
| 691 |
} |
| 692 |
|
| 693 |
@Test |
| 694 |
public void sizeAndHasAnyValueUnboundedEmptyError() { |
| 695 |
ReplayProcessor<Object> rs = ReplayProcessor.create(); |
| 696 |
|
| 697 |
rs.onError(new TestException()); |
| 698 |
|
| 699 |
assertEquals(0, rs.size()); |
| 700 |
assertFalse(rs.hasValue()); |
| 701 |
} |
| 702 |
|
| 703 |
@Test |
| 704 |
public void sizeAndHasAnyValueEffectivelyUnboundedEmptyError() { |
| 705 |
ReplayProcessor<Object> rs = ReplayProcessor.createUnbounded(); |
| 706 |
|
| 707 |
rs.onError(new TestException()); |
| 708 |
|
| 709 |
assertEquals(0, rs.size()); |
| 710 |
assertFalse(rs.hasValue()); |
| 711 |
} |
| 712 |
|
| 713 |
@Test |
| 714 |
public void sizeAndHasAnyValueUnboundedEmptyCompleted() { |
| 715 |
ReplayProcessor<Object> rs = ReplayProcessor.create(); |
| 716 |
|
| 717 |
rs.onComplete(); |
| 718 |
|
| 719 |
assertEquals(0, rs.size()); |
| 720 |
assertFalse(rs.hasValue()); |
| 721 |
} |
| 722 |
|
| 723 |
@Test |
| 724 |
public void sizeAndHasAnyValueEffectivelyUnboundedEmptyCompleted() { |
| 725 |
ReplayProcessor<Object> rs = ReplayProcessor.createUnbounded(); |
| 726 |
|
| 727 |
rs.onComplete(); |
| 728 |
|
| 729 |
assertEquals(0, rs.size()); |
| 730 |
assertFalse(rs.hasValue()); |
| 731 |
} |
| 732 |
|
| 733 |
@Test |
| 734 |
public void sizeAndHasAnyValueSizeBounded() { |
| 735 |
ReplayProcessor<Object> rs = ReplayProcessor.createWithSize(1); |
| 736 |
|
| 737 |
assertEquals(0, rs.size()); |
| 738 |
assertFalse(rs.hasValue()); |
| 739 |
|
| 740 |
for (int i = 0; i < 1000; i++) { |
| 741 |
rs.onNext(i); |
| 742 |
|
| 743 |
assertEquals(1, rs.size()); |
| 744 |
assertTrue(rs.hasValue()); |
| 745 |
} |
| 746 |
|
| 747 |
rs.onComplete(); |
| 748 |
|
| 749 |
assertEquals(1, rs.size()); |
| 750 |
assertTrue(rs.hasValue()); |
| 751 |
} |
| 752 |
|
| 753 |
@Test |
| 754 |
public void sizeAndHasAnyValueTimeBounded() { |
| 755 |
TestScheduler ts = new TestScheduler(); |
| 756 |
ReplayProcessor<Object> rs = ReplayProcessor.createWithTime(1, TimeUnit.SECONDS, ts); |
| 757 |
|
| 758 |
assertEquals(0, rs.size()); |
| 759 |
assertFalse(rs.hasValue()); |
| 760 |
|
| 761 |
for (int i = 0; i < 1000; i++) { |
| 762 |
rs.onNext(i); |
| 763 |
assertEquals(1, rs.size()); |
| 764 |
assertTrue(rs.hasValue()); |
| 765 |
ts.advanceTimeBy(2, TimeUnit.SECONDS); |
| 766 |
assertEquals(0, rs.size()); |
| 767 |
assertFalse(rs.hasValue()); |
| 768 |
} |
| 769 |
|
| 770 |
rs.onComplete(); |
| 771 |
|
| 772 |
assertEquals(0, rs.size()); |
| 773 |
assertFalse(rs.hasValue()); |
| 774 |
} |
| 775 |
|
| 776 |
@Test |
| 777 |
public void getValues() { |
| 778 |
ReplayProcessor<Object> rs = ReplayProcessor.create(); |
| 779 |
Object[] expected = new Object[10]; |
| 780 |
for (int i = 0; i < expected.length; i++) { |
| 781 |
expected[i] = i; |
| 782 |
rs.onNext(i); |
| 783 |
assertArrayEquals(Arrays.copyOf(expected, i + 1), rs.getValues()); |
| 784 |
} |
| 785 |
rs.onComplete(); |
| 786 |
|
| 787 |
assertArrayEquals(expected, rs.getValues()); |
| 788 |
|
| 789 |
} |
| 790 |
|
| 791 |
@Test |
| 792 |
public void getValuesUnbounded() { |
| 793 |
ReplayProcessor<Object> rs = ReplayProcessor.createUnbounded(); |
| 794 |
Object[] expected = new Object[10]; |
| 795 |
for (int i = 0; i < expected.length; i++) { |
| 796 |
expected[i] = i; |
| 797 |
rs.onNext(i); |
| 798 |
assertArrayEquals(Arrays.copyOf(expected, i + 1), rs.getValues()); |
| 799 |
} |
| 800 |
rs.onComplete(); |
| 801 |
|
| 802 |
assertArrayEquals(expected, rs.getValues()); |
| 803 |
|
| 804 |
} |
| 805 |
|
| 806 |
@Test |
| 807 |
public void backpressureHonored() { |
| 808 |
ReplayProcessor<Integer> rs = ReplayProcessor.create(); |
| 809 |
rs.onNext(1); |
| 810 |
rs.onNext(2); |
| 811 |
rs.onNext(3); |
| 812 |
rs.onComplete(); |
| 813 |
|
| 814 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 815 |
|
| 816 |
rs.subscribe(ts); |
| 817 |
|
| 818 |
ts.request(1); |
| 819 |
ts.assertValue(1); |
| 820 |
ts.assertNotComplete(); |
| 821 |
ts.assertNoErrors(); |
| 822 |
|
| 823 |
ts.request(1); |
| 824 |
ts.assertValues(1, 2); |
| 825 |
ts.assertNotComplete(); |
| 826 |
ts.assertNoErrors(); |
| 827 |
|
| 828 |
ts.request(1); |
| 829 |
ts.assertValues(1, 2, 3); |
| 830 |
ts.assertComplete(); |
| 831 |
ts.assertNoErrors(); |
| 832 |
} |
| 833 |
|
| 834 |
@Test |
| 835 |
public void backpressureHonoredSizeBound() { |
| 836 |
ReplayProcessor<Integer> rs = ReplayProcessor.createWithSize(100); |
| 837 |
rs.onNext(1); |
| 838 |
rs.onNext(2); |
| 839 |
rs.onNext(3); |
| 840 |
rs.onComplete(); |
| 841 |
|
| 842 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 843 |
|
| 844 |
rs.subscribe(ts); |
| 845 |
|
| 846 |
ts.request(1); |
| 847 |
ts.assertValue(1); |
| 848 |
ts.assertNotComplete(); |
| 849 |
ts.assertNoErrors(); |
| 850 |
|
| 851 |
ts.request(1); |
| 852 |
ts.assertValues(1, 2); |
| 853 |
ts.assertNotComplete(); |
| 854 |
ts.assertNoErrors(); |
| 855 |
|
| 856 |
ts.request(1); |
| 857 |
ts.assertValues(1, 2, 3); |
| 858 |
ts.assertComplete(); |
| 859 |
ts.assertNoErrors(); |
| 860 |
} |
| 861 |
|
| 862 |
@Test |
| 863 |
public void backpressureHonoredTimeBound() { |
| 864 |
ReplayProcessor<Integer> rs = ReplayProcessor.createWithTime(1, TimeUnit.DAYS, Schedulers.trampoline()); |
| 865 |
rs.onNext(1); |
| 866 |
rs.onNext(2); |
| 867 |
rs.onNext(3); |
| 868 |
rs.onComplete(); |
| 869 |
|
| 870 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 871 |
|
| 872 |
rs.subscribe(ts); |
| 873 |
|
| 874 |
ts.request(1); |
| 875 |
ts.assertValue(1); |
| 876 |
ts.assertNotComplete(); |
| 877 |
ts.assertNoErrors(); |
| 878 |
|
| 879 |
ts.request(1); |
| 880 |
ts.assertValues(1, 2); |
| 881 |
ts.assertNotComplete(); |
| 882 |
ts.assertNoErrors(); |
| 883 |
|
| 884 |
ts.request(1); |
| 885 |
ts.assertValues(1, 2, 3); |
| 886 |
ts.assertComplete(); |
| 887 |
ts.assertNoErrors(); |
| 888 |
} |
| 889 |
|
| 890 |
@Test |
| 891 |
public void createInvalidCapacity() { |
| 892 |
try { |
| 893 |
ReplayProcessor.create(-99); |
| 894 |
fail("Didn't throw IllegalArgumentException"); |
| 895 |
} catch (IllegalArgumentException ex) { |
| 896 |
assertEquals("capacityHint > 0 required but it was -99", ex.getMessage()); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
@Test |
| 901 |
public void createWithSizeInvalidCapacity() { |
| 902 |
try { |
| 903 |
ReplayProcessor.createWithSize(-99); |
| 904 |
fail("Didn't throw IllegalArgumentException"); |
| 905 |
} catch (IllegalArgumentException ex) { |
| 906 |
assertEquals("maxSize > 0 required but it was -99", ex.getMessage()); |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
@Test |
| 911 |
public void createWithTimeAndSizeInvalidCapacity() { |
| 912 |
try { |
| 913 |
ReplayProcessor.createWithTimeAndSize(1, TimeUnit.DAYS, Schedulers.computation(), -99); |
| 914 |
fail("Didn't throw IllegalArgumentException"); |
| 915 |
} catch (IllegalArgumentException ex) { |
| 916 |
assertEquals("maxSize > 0 required but it was -99", ex.getMessage()); |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
@Test |
| 921 |
public void hasSubscribers() { |
| 922 |
ReplayProcessor<Integer> rp = ReplayProcessor.create(); |
| 923 |
|
| 924 |
assertFalse(rp.hasSubscribers()); |
| 925 |
|
| 926 |
TestSubscriber<Integer> ts = rp.test(); |
| 927 |
|
| 928 |
assertTrue(rp.hasSubscribers()); |
| 929 |
|
| 930 |
ts.cancel(); |
| 931 |
|
| 932 |
assertFalse(rp.hasSubscribers()); |
| 933 |
} |
| 934 |
|
| 935 |
@Test |
| 936 |
public void peekStateUnbounded() { |
| 937 |
ReplayProcessor<Integer> rp = ReplayProcessor.create(); |
| 938 |
|
| 939 |
rp.onNext(1); |
| 940 |
|
| 941 |
assertEquals((Integer)1, rp.getValue()); |
| 942 |
|
| 943 |
assertEquals(1, rp.getValues()[0]); |
| 944 |
} |
| 945 |
|
| 946 |
@Test |
| 947 |
public void peekStateTimeAndSize() { |
| 948 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.DAYS, Schedulers.computation(), 1); |
| 949 |
|
| 950 |
rp.onNext(1); |
| 951 |
|
| 952 |
assertEquals((Integer)1, rp.getValue()); |
| 953 |
|
| 954 |
assertEquals(1, rp.getValues()[0]); |
| 955 |
|
| 956 |
rp.onNext(2); |
| 957 |
|
| 958 |
assertEquals((Integer)2, rp.getValue()); |
| 959 |
|
| 960 |
assertEquals(2, rp.getValues()[0]); |
| 961 |
|
| 962 |
assertEquals((Integer)2, rp.getValues(new Integer[0])[0]); |
| 963 |
|
| 964 |
assertEquals((Integer)2, rp.getValues(new Integer[1])[0]); |
| 965 |
|
| 966 |
Integer[] a = new Integer[2]; |
| 967 |
assertEquals((Integer)2, rp.getValues(a)[0]); |
| 968 |
assertNull(a[1]); |
| 969 |
} |
| 970 |
|
| 971 |
@Test |
| 972 |
public void peekStateTimeAndSizeValue() { |
| 973 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.DAYS, Schedulers.computation(), 1); |
| 974 |
|
| 975 |
assertNull(rp.getValue()); |
| 976 |
|
| 977 |
assertEquals(0, rp.getValues().length); |
| 978 |
|
| 979 |
assertNull(rp.getValues(new Integer[2])[0]); |
| 980 |
|
| 981 |
rp.onComplete(); |
| 982 |
|
| 983 |
assertNull(rp.getValue()); |
| 984 |
|
| 985 |
assertEquals(0, rp.getValues().length); |
| 986 |
|
| 987 |
assertNull(rp.getValues(new Integer[2])[0]); |
| 988 |
|
| 989 |
rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.DAYS, Schedulers.computation(), 1); |
| 990 |
rp.onError(new TestException()); |
| 991 |
|
| 992 |
assertNull(rp.getValue()); |
| 993 |
|
| 994 |
assertEquals(0, rp.getValues().length); |
| 995 |
|
| 996 |
assertNull(rp.getValues(new Integer[2])[0]); |
| 997 |
} |
| 998 |
|
| 999 |
@Test |
| 1000 |
public void peekStateTimeAndSizeValueExpired() { |
| 1001 |
TestScheduler scheduler = new TestScheduler(); |
| 1002 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTime(1, TimeUnit.DAYS, scheduler); |
| 1003 |
|
| 1004 |
assertNull(rp.getValue()); |
| 1005 |
assertNull(rp.getValues(new Integer[2])[0]); |
| 1006 |
|
| 1007 |
rp.onNext(2); |
| 1008 |
|
| 1009 |
assertEquals((Integer)2, rp.getValue()); |
| 1010 |
assertEquals(2, rp.getValues()[0]); |
| 1011 |
|
| 1012 |
scheduler.advanceTimeBy(2, TimeUnit.DAYS); |
| 1013 |
|
| 1014 |
assertNull(rp.getValue()); |
| 1015 |
assertEquals(0, rp.getValues().length); |
| 1016 |
assertNull(rp.getValues(new Integer[2])[0]); |
| 1017 |
} |
| 1018 |
|
| 1019 |
@Test |
| 1020 |
public void capacityHint() { |
| 1021 |
ReplayProcessor<Integer> rp = ReplayProcessor.create(8); |
| 1022 |
|
| 1023 |
for (int i = 0; i < 15; i++) { |
| 1024 |
rp.onNext(i); |
| 1025 |
} |
| 1026 |
rp.onComplete(); |
| 1027 |
|
| 1028 |
rp.test().assertResult(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14); |
| 1029 |
} |
| 1030 |
|
| 1031 |
@Test |
| 1032 |
public void subscribeCancelRace() { |
| 1033 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1034 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 1035 |
|
| 1036 |
final ReplayProcessor<Integer> rp = ReplayProcessor.create(); |
| 1037 |
|
| 1038 |
Runnable r1 = new Runnable() { |
| 1039 |
@Override |
| 1040 |
public void run() { |
| 1041 |
rp.subscribe(ts); |
| 1042 |
} |
| 1043 |
}; |
| 1044 |
|
| 1045 |
Runnable r2 = new Runnable() { |
| 1046 |
@Override |
| 1047 |
public void run() { |
| 1048 |
ts.cancel(); |
| 1049 |
} |
| 1050 |
}; |
| 1051 |
|
| 1052 |
TestHelper.race(r1, r2); |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
@Test |
| 1057 |
public void subscribeAfterDone() { |
| 1058 |
ReplayProcessor<Integer> rp = ReplayProcessor.create(); |
| 1059 |
rp.onComplete(); |
| 1060 |
|
| 1061 |
BooleanSubscription bs = new BooleanSubscription(); |
| 1062 |
|
| 1063 |
rp.onSubscribe(bs); |
| 1064 |
|
| 1065 |
assertTrue(bs.isCancelled()); |
| 1066 |
} |
| 1067 |
|
| 1068 |
@Test |
| 1069 |
public void subscribeRace() { |
| 1070 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1071 |
final ReplayProcessor<Integer> rp = ReplayProcessor.create(); |
| 1072 |
|
| 1073 |
Runnable r1 = new Runnable() { |
| 1074 |
@Override |
| 1075 |
public void run() { |
| 1076 |
rp.test(); |
| 1077 |
} |
| 1078 |
}; |
| 1079 |
|
| 1080 |
TestHelper.race(r1, r1); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
@Test |
| 1085 |
public void cancelUpfront() { |
| 1086 |
ReplayProcessor<Integer> rp = ReplayProcessor.create(); |
| 1087 |
rp.test(); |
| 1088 |
rp.test(); |
| 1089 |
|
| 1090 |
TestSubscriber<Integer> ts = rp.test(0L, true); |
| 1091 |
|
| 1092 |
assertEquals(2, rp.subscriberCount()); |
| 1093 |
|
| 1094 |
ts.assertEmpty(); |
| 1095 |
} |
| 1096 |
|
| 1097 |
@Test |
| 1098 |
public void cancelRace() { |
| 1099 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1100 |
|
| 1101 |
final ReplayProcessor<Integer> rp = ReplayProcessor.create(); |
| 1102 |
final TestSubscriber<Integer> ts1 = rp.test(); |
| 1103 |
final TestSubscriber<Integer> ts2 = rp.test(); |
| 1104 |
|
| 1105 |
Runnable r1 = new Runnable() { |
| 1106 |
@Override |
| 1107 |
public void run() { |
| 1108 |
ts1.cancel(); |
| 1109 |
} |
| 1110 |
}; |
| 1111 |
|
| 1112 |
Runnable r2 = new Runnable() { |
| 1113 |
@Override |
| 1114 |
public void run() { |
| 1115 |
ts2.cancel(); |
| 1116 |
} |
| 1117 |
}; |
| 1118 |
|
| 1119 |
TestHelper.race(r1, r2); |
| 1120 |
|
| 1121 |
assertFalse(rp.hasSubscribers()); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
@Test |
| 1126 |
public void sizeboundReplayError() { |
| 1127 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithSize(2); |
| 1128 |
|
| 1129 |
rp.onNext(1); |
| 1130 |
rp.onNext(2); |
| 1131 |
rp.onNext(3); |
| 1132 |
rp.onNext(4); |
| 1133 |
rp.onError(new TestException()); |
| 1134 |
|
| 1135 |
rp.test() |
| 1136 |
.assertFailure(TestException.class, 3, 4); |
| 1137 |
} |
| 1138 |
|
| 1139 |
@Test |
| 1140 |
public void sizeAndTimeBoundReplayError() { |
| 1141 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.DAYS, Schedulers.single(), 2); |
| 1142 |
|
| 1143 |
rp.onNext(1); |
| 1144 |
rp.onNext(2); |
| 1145 |
rp.onNext(3); |
| 1146 |
rp.onNext(4); |
| 1147 |
rp.onError(new TestException()); |
| 1148 |
|
| 1149 |
rp.test() |
| 1150 |
.assertFailure(TestException.class, 3, 4); |
| 1151 |
} |
| 1152 |
|
| 1153 |
@Test |
| 1154 |
public void replayRequestRace() { |
| 1155 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1156 |
|
| 1157 |
final ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.DAYS, Schedulers.single(), 2); |
| 1158 |
final TestSubscriber<Integer> ts = rp.test(0L); |
| 1159 |
|
| 1160 |
Runnable r1 = new Runnable() { |
| 1161 |
@Override |
| 1162 |
public void run() { |
| 1163 |
ts.request(1); |
| 1164 |
} |
| 1165 |
}; |
| 1166 |
|
| 1167 |
Runnable r2 = new Runnable() { |
| 1168 |
@Override |
| 1169 |
public void run() { |
| 1170 |
rp.onNext(1); |
| 1171 |
} |
| 1172 |
}; |
| 1173 |
|
| 1174 |
TestHelper.race(r1, r2); |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
@Test |
| 1179 |
public void timedSkipOld() { |
| 1180 |
TestScheduler scheduler = new TestScheduler(); |
| 1181 |
|
| 1182 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2); |
| 1183 |
|
| 1184 |
rp.onNext(1); |
| 1185 |
scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 1186 |
|
| 1187 |
rp.test() |
| 1188 |
.assertEmpty(); |
| 1189 |
} |
| 1190 |
|
| 1191 |
@Test |
| 1192 |
public void takeSizeAndTime() { |
| 1193 |
TestScheduler scheduler = new TestScheduler(); |
| 1194 |
|
| 1195 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2); |
| 1196 |
|
| 1197 |
rp.onNext(1); |
| 1198 |
rp.onNext(2); |
| 1199 |
rp.onNext(3); |
| 1200 |
|
| 1201 |
rp |
| 1202 |
.take(1) |
| 1203 |
.test() |
| 1204 |
.assertResult(2); |
| 1205 |
} |
| 1206 |
|
| 1207 |
@Test |
| 1208 |
public void takeSizeAndTime2() { |
| 1209 |
TestScheduler scheduler = new TestScheduler(); |
| 1210 |
|
| 1211 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2); |
| 1212 |
|
| 1213 |
rp.onNext(1); |
| 1214 |
rp.onNext(2); |
| 1215 |
rp.onNext(3); |
| 1216 |
|
| 1217 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1218 |
@Override |
| 1219 |
public void onNext(@NonNull Integer t) { |
| 1220 |
super.onNext(t); |
| 1221 |
cancel(); |
| 1222 |
onComplete(); |
| 1223 |
} |
| 1224 |
}; |
| 1225 |
|
| 1226 |
rp |
| 1227 |
.subscribeWith(ts) |
| 1228 |
.assertResult(2); |
| 1229 |
} |
| 1230 |
|
| 1231 |
@Test |
| 1232 |
public void takeSize() { |
| 1233 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithSize(2); |
| 1234 |
|
| 1235 |
rp.onNext(1); |
| 1236 |
rp.onNext(2); |
| 1237 |
rp.onNext(3); |
| 1238 |
|
| 1239 |
rp |
| 1240 |
.take(1) |
| 1241 |
.test() |
| 1242 |
.assertResult(2); |
| 1243 |
} |
| 1244 |
|
| 1245 |
@Test |
| 1246 |
public void takeSize2() { |
| 1247 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithSize(2); |
| 1248 |
|
| 1249 |
rp.onNext(1); |
| 1250 |
rp.onNext(2); |
| 1251 |
rp.onNext(3); |
| 1252 |
|
| 1253 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1254 |
@Override |
| 1255 |
public void onNext(@NonNull Integer t) { |
| 1256 |
super.onNext(t); |
| 1257 |
cancel(); |
| 1258 |
onComplete(); |
| 1259 |
} |
| 1260 |
}; |
| 1261 |
|
| 1262 |
rp |
| 1263 |
.subscribeWith(ts) |
| 1264 |
.assertResult(2); |
| 1265 |
} |
| 1266 |
|
| 1267 |
@Test |
| 1268 |
public void reentrantDrain() { |
| 1269 |
TestScheduler scheduler = new TestScheduler(); |
| 1270 |
|
| 1271 |
final ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2); |
| 1272 |
|
| 1273 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 1274 |
@Override |
| 1275 |
public void onNext(Integer t) { |
| 1276 |
if (t == 1) { |
| 1277 |
rp.onNext(2); |
| 1278 |
} |
| 1279 |
super.onNext(t); |
| 1280 |
} |
| 1281 |
}; |
| 1282 |
|
| 1283 |
rp.subscribe(ts); |
| 1284 |
|
| 1285 |
rp.onNext(1); |
| 1286 |
rp.onComplete(); |
| 1287 |
|
| 1288 |
ts.assertResult(1, 2); |
| 1289 |
} |
| 1290 |
|
| 1291 |
@Test |
| 1292 |
public void reentrantDrainBackpressured() { |
| 1293 |
TestScheduler scheduler = new TestScheduler(); |
| 1294 |
|
| 1295 |
final ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2); |
| 1296 |
|
| 1297 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1L) { |
| 1298 |
@Override |
| 1299 |
public void onNext(Integer t) { |
| 1300 |
if (t == 1) { |
| 1301 |
rp.onNext(2); |
| 1302 |
} |
| 1303 |
super.onNext(t); |
| 1304 |
} |
| 1305 |
}; |
| 1306 |
|
| 1307 |
rp.subscribe(ts); |
| 1308 |
|
| 1309 |
rp.onNext(1); |
| 1310 |
rp.onComplete(); |
| 1311 |
|
| 1312 |
ts.request(1); |
| 1313 |
|
| 1314 |
ts.assertResult(1, 2); |
| 1315 |
} |
| 1316 |
|
| 1317 |
@Test |
| 1318 |
public void timedNoOutdatedData() { |
| 1319 |
TestScheduler scheduler = new TestScheduler(); |
| 1320 |
|
| 1321 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(2, TimeUnit.SECONDS, scheduler); |
| 1322 |
source.onNext(1); |
| 1323 |
source.onComplete(); |
| 1324 |
|
| 1325 |
source.test().assertResult(1); |
| 1326 |
|
| 1327 |
source.test().assertResult(1); |
| 1328 |
|
| 1329 |
scheduler.advanceTimeBy(3, TimeUnit.SECONDS); |
| 1330 |
|
| 1331 |
source.test().assertResult(); |
| 1332 |
} |
| 1333 |
|
| 1334 |
@Test |
| 1335 |
public void unboundedRequestCompleteRace() { |
| 1336 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 1337 |
final ReplayProcessor<Integer> source = ReplayProcessor.create(); |
| 1338 |
|
| 1339 |
final TestSubscriber<Integer> ts = source.test(0); |
| 1340 |
|
| 1341 |
Runnable r1 = new Runnable() { |
| 1342 |
@Override |
| 1343 |
public void run() { |
| 1344 |
source.onComplete(); |
| 1345 |
} |
| 1346 |
}; |
| 1347 |
|
| 1348 |
Runnable r2 = new Runnable() { |
| 1349 |
@Override |
| 1350 |
public void run() { |
| 1351 |
ts.request(1); |
| 1352 |
} |
| 1353 |
}; |
| 1354 |
|
| 1355 |
TestHelper.race(r1, r2); |
| 1356 |
|
| 1357 |
ts.assertResult(); |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
@Test |
| 1362 |
public void sizeRequestCompleteRace() { |
| 1363 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 1364 |
final ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(10); |
| 1365 |
|
| 1366 |
final TestSubscriber<Integer> ts = source.test(0); |
| 1367 |
|
| 1368 |
Runnable r1 = new Runnable() { |
| 1369 |
@Override |
| 1370 |
public void run() { |
| 1371 |
source.onComplete(); |
| 1372 |
} |
| 1373 |
}; |
| 1374 |
|
| 1375 |
Runnable r2 = new Runnable() { |
| 1376 |
@Override |
| 1377 |
public void run() { |
| 1378 |
ts.request(1); |
| 1379 |
} |
| 1380 |
}; |
| 1381 |
|
| 1382 |
TestHelper.race(r1, r2); |
| 1383 |
|
| 1384 |
ts.assertResult(); |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
@Test |
| 1389 |
public void timedRequestCompleteRace() { |
| 1390 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 1391 |
final ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(2, TimeUnit.HOURS, Schedulers.single()); |
| 1392 |
|
| 1393 |
final TestSubscriber<Integer> ts = source.test(0); |
| 1394 |
|
| 1395 |
Runnable r1 = new Runnable() { |
| 1396 |
@Override |
| 1397 |
public void run() { |
| 1398 |
source.onComplete(); |
| 1399 |
} |
| 1400 |
}; |
| 1401 |
|
| 1402 |
Runnable r2 = new Runnable() { |
| 1403 |
@Override |
| 1404 |
public void run() { |
| 1405 |
ts.request(1); |
| 1406 |
} |
| 1407 |
}; |
| 1408 |
|
| 1409 |
TestHelper.race(r1, r2); |
| 1410 |
|
| 1411 |
ts.assertResult(); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
@Test |
| 1416 |
public void timeAndSizeRequestCompleteRace() { |
| 1417 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 1418 |
final ReplayProcessor<Integer> source = ReplayProcessor.createWithTimeAndSize(2, TimeUnit.HOURS, Schedulers.single(), 100); |
| 1419 |
|
| 1420 |
final TestSubscriber<Integer> ts = source.test(0); |
| 1421 |
|
| 1422 |
Runnable r1 = new Runnable() { |
| 1423 |
@Override |
| 1424 |
public void run() { |
| 1425 |
source.onComplete(); |
| 1426 |
} |
| 1427 |
}; |
| 1428 |
|
| 1429 |
Runnable r2 = new Runnable() { |
| 1430 |
@Override |
| 1431 |
public void run() { |
| 1432 |
ts.request(1); |
| 1433 |
} |
| 1434 |
}; |
| 1435 |
|
| 1436 |
TestHelper.race(r1, r2); |
| 1437 |
|
| 1438 |
ts.assertResult(); |
| 1439 |
} |
| 1440 |
} |
| 1441 |
|
| 1442 |
@Test |
| 1443 |
public void unboundedZeroRequestComplete() { |
| 1444 |
final ReplayProcessor<Integer> source = ReplayProcessor.create(); |
| 1445 |
|
| 1446 |
source.onComplete(); |
| 1447 |
|
| 1448 |
source.test(0).assertResult(); |
| 1449 |
} |
| 1450 |
|
| 1451 |
@Test |
| 1452 |
public void unboundedZeroRequestError() { |
| 1453 |
final ReplayProcessor<Integer> source = ReplayProcessor.create(); |
| 1454 |
|
| 1455 |
source.onError(new TestException()); |
| 1456 |
|
| 1457 |
source.test(0).assertFailure(TestException.class); |
| 1458 |
} |
| 1459 |
|
| 1460 |
@Test |
| 1461 |
public void sizeBoundZeroRequestComplete() { |
| 1462 |
final ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(16); |
| 1463 |
|
| 1464 |
source.onComplete(); |
| 1465 |
|
| 1466 |
source.test(0).assertResult(); |
| 1467 |
} |
| 1468 |
|
| 1469 |
@Test |
| 1470 |
public void sizeBoundZeroRequestError() { |
| 1471 |
final ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(16); |
| 1472 |
|
| 1473 |
source.onError(new TestException()); |
| 1474 |
|
| 1475 |
source.test(0).assertFailure(TestException.class); |
| 1476 |
} |
| 1477 |
|
| 1478 |
@Test |
| 1479 |
public void timeBoundZeroRequestComplete() { |
| 1480 |
final ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.MINUTES, Schedulers.single()); |
| 1481 |
|
| 1482 |
source.onComplete(); |
| 1483 |
|
| 1484 |
source.test(0).assertResult(); |
| 1485 |
} |
| 1486 |
|
| 1487 |
@Test |
| 1488 |
public void timeBoundZeroRequestError() { |
| 1489 |
final ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.MINUTES, Schedulers.single()); |
| 1490 |
|
| 1491 |
source.onError(new TestException()); |
| 1492 |
|
| 1493 |
source.test(0).assertFailure(TestException.class); |
| 1494 |
} |
| 1495 |
|
| 1496 |
@Test |
| 1497 |
public void timeAndSizeBoundZeroRequestComplete() { |
| 1498 |
final ReplayProcessor<Integer> source = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.MINUTES, Schedulers.single(), 16); |
| 1499 |
|
| 1500 |
source.onComplete(); |
| 1501 |
|
| 1502 |
source.test(0).assertResult(); |
| 1503 |
} |
| 1504 |
|
| 1505 |
@Test |
| 1506 |
public void timeAndSizeBoundZeroRequestError() { |
| 1507 |
final ReplayProcessor<Integer> source = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.MINUTES, Schedulers.single(), 16); |
| 1508 |
|
| 1509 |
source.onError(new TestException()); |
| 1510 |
|
| 1511 |
source.test(0).assertFailure(TestException.class); |
| 1512 |
} |
| 1513 |
|
| 1514 |
TestSubscriber<Integer> take1AndCancel() { |
| 1515 |
return new TestSubscriber<Integer>(1) { |
| 1516 |
@Override |
| 1517 |
public void onNext(Integer t) { |
| 1518 |
super.onNext(t); |
| 1519 |
cancel(); |
| 1520 |
onComplete(); |
| 1521 |
} |
| 1522 |
}; |
| 1523 |
} |
| 1524 |
|
| 1525 |
@Test |
| 1526 |
public void unboundedCancelAfterOne() { |
| 1527 |
ReplayProcessor<Integer> source = ReplayProcessor.create(); |
| 1528 |
source.onNext(1); |
| 1529 |
|
| 1530 |
source.subscribeWith(take1AndCancel()) |
| 1531 |
.assertResult(1); |
| 1532 |
} |
| 1533 |
|
| 1534 |
@Test |
| 1535 |
public void sizeBoundCancelAfterOne() { |
| 1536 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(16); |
| 1537 |
source.onNext(1); |
| 1538 |
|
| 1539 |
source.subscribeWith(take1AndCancel()) |
| 1540 |
.assertResult(1); |
| 1541 |
} |
| 1542 |
|
| 1543 |
@Test |
| 1544 |
public void timeBoundCancelAfterOne() { |
| 1545 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.MINUTES, Schedulers.single()); |
| 1546 |
source.onNext(1); |
| 1547 |
|
| 1548 |
source.subscribeWith(take1AndCancel()) |
| 1549 |
.assertResult(1); |
| 1550 |
} |
| 1551 |
|
| 1552 |
@Test |
| 1553 |
public void timeAndSizeBoundCancelAfterOne() { |
| 1554 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.MINUTES, Schedulers.single(), 16); |
| 1555 |
source.onNext(1); |
| 1556 |
|
| 1557 |
source.subscribeWith(take1AndCancel()) |
| 1558 |
.assertResult(1); |
| 1559 |
} |
| 1560 |
|
| 1561 |
@Test |
| 1562 |
public void noHeadRetentionCompleteSize() { |
| 1563 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(1); |
| 1564 |
|
| 1565 |
source.onNext(1); |
| 1566 |
source.onNext(2); |
| 1567 |
source.onComplete(); |
| 1568 |
|
| 1569 |
SizeBoundReplayBuffer<Integer> buf = (SizeBoundReplayBuffer<Integer>)source.buffer; |
| 1570 |
|
| 1571 |
assertNull(buf.head.value); |
| 1572 |
|
| 1573 |
Object o = buf.head; |
| 1574 |
|
| 1575 |
source.cleanupBuffer(); |
| 1576 |
|
| 1577 |
assertSame(o, buf.head); |
| 1578 |
} |
| 1579 |
|
| 1580 |
@Test |
| 1581 |
public void noHeadRetentionErrorSize() { |
| 1582 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(1); |
| 1583 |
|
| 1584 |
source.onNext(1); |
| 1585 |
source.onNext(2); |
| 1586 |
source.onError(new TestException()); |
| 1587 |
|
| 1588 |
SizeBoundReplayBuffer<Integer> buf = (SizeBoundReplayBuffer<Integer>)source.buffer; |
| 1589 |
|
| 1590 |
assertNull(buf.head.value); |
| 1591 |
|
| 1592 |
Object o = buf.head; |
| 1593 |
|
| 1594 |
source.cleanupBuffer(); |
| 1595 |
|
| 1596 |
assertSame(o, buf.head); |
| 1597 |
} |
| 1598 |
|
| 1599 |
@Test |
| 1600 |
public void unboundedCleanupBufferNoOp() { |
| 1601 |
ReplayProcessor<Integer> source = ReplayProcessor.create(1); |
| 1602 |
|
| 1603 |
source.onNext(1); |
| 1604 |
source.onNext(2); |
| 1605 |
|
| 1606 |
source.cleanupBuffer(); |
| 1607 |
|
| 1608 |
source.test().assertValuesOnly(1, 2); |
| 1609 |
} |
| 1610 |
|
| 1611 |
@Test |
| 1612 |
public void noHeadRetentionSize() { |
| 1613 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(1); |
| 1614 |
|
| 1615 |
source.onNext(1); |
| 1616 |
source.onNext(2); |
| 1617 |
|
| 1618 |
SizeBoundReplayBuffer<Integer> buf = (SizeBoundReplayBuffer<Integer>)source.buffer; |
| 1619 |
|
| 1620 |
assertNotNull(buf.head.value); |
| 1621 |
|
| 1622 |
source.cleanupBuffer(); |
| 1623 |
|
| 1624 |
assertNull(buf.head.value); |
| 1625 |
|
| 1626 |
Object o = buf.head; |
| 1627 |
|
| 1628 |
source.cleanupBuffer(); |
| 1629 |
|
| 1630 |
assertSame(o, buf.head); |
| 1631 |
} |
| 1632 |
|
| 1633 |
@Test |
| 1634 |
public void noHeadRetentionCompleteTime() { |
| 1635 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.MINUTES, Schedulers.computation()); |
| 1636 |
|
| 1637 |
source.onNext(1); |
| 1638 |
source.onNext(2); |
| 1639 |
source.onComplete(); |
| 1640 |
|
| 1641 |
SizeAndTimeBoundReplayBuffer<Integer> buf = (SizeAndTimeBoundReplayBuffer<Integer>)source.buffer; |
| 1642 |
|
| 1643 |
assertNull(buf.head.value); |
| 1644 |
|
| 1645 |
Object o = buf.head; |
| 1646 |
|
| 1647 |
source.cleanupBuffer(); |
| 1648 |
|
| 1649 |
assertSame(o, buf.head); |
| 1650 |
} |
| 1651 |
|
| 1652 |
@Test |
| 1653 |
public void noHeadRetentionErrorTime() { |
| 1654 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.MINUTES, Schedulers.computation()); |
| 1655 |
|
| 1656 |
source.onNext(1); |
| 1657 |
source.onNext(2); |
| 1658 |
source.onError(new TestException()); |
| 1659 |
|
| 1660 |
SizeAndTimeBoundReplayBuffer<Integer> buf = (SizeAndTimeBoundReplayBuffer<Integer>)source.buffer; |
| 1661 |
|
| 1662 |
assertNull(buf.head.value); |
| 1663 |
|
| 1664 |
Object o = buf.head; |
| 1665 |
|
| 1666 |
source.cleanupBuffer(); |
| 1667 |
|
| 1668 |
assertSame(o, buf.head); |
| 1669 |
} |
| 1670 |
|
| 1671 |
@Test |
| 1672 |
public void noHeadRetentionTime() { |
| 1673 |
TestScheduler sch = new TestScheduler(); |
| 1674 |
|
| 1675 |
ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.MILLISECONDS, sch); |
| 1676 |
|
| 1677 |
source.onNext(1); |
| 1678 |
|
| 1679 |
sch.advanceTimeBy(2, TimeUnit.MILLISECONDS); |
| 1680 |
|
| 1681 |
source.onNext(2); |
| 1682 |
|
| 1683 |
SizeAndTimeBoundReplayBuffer<Integer> buf = (SizeAndTimeBoundReplayBuffer<Integer>)source.buffer; |
| 1684 |
|
| 1685 |
assertNotNull(buf.head.value); |
| 1686 |
|
| 1687 |
source.cleanupBuffer(); |
| 1688 |
|
| 1689 |
assertNull(buf.head.value); |
| 1690 |
|
| 1691 |
Object o = buf.head; |
| 1692 |
|
| 1693 |
source.cleanupBuffer(); |
| 1694 |
|
| 1695 |
assertSame(o, buf.head); |
| 1696 |
} |
| 1697 |
|
| 1698 |
@Test |
| 1699 |
public void invalidRequest() { |
| 1700 |
TestHelper.assertBadRequestReported(ReplayProcessor.create()); |
| 1701 |
} |
| 1702 |
|
| 1703 |
@Test |
| 1704 |
public void noBoundedRetentionViaThreadLocal() throws Exception { |
| 1705 |
final ReplayProcessor<byte[]> rp = ReplayProcessor.createWithSize(1); |
| 1706 |
|
| 1707 |
Flowable<byte[]> source = rp.take(1) |
| 1708 |
.concatMap(new Function<byte[], Publisher<byte[]>>() { |
| 1709 |
@Override |
| 1710 |
public Publisher<byte[]> apply(byte[] v) throws Exception { |
| 1711 |
return rp; |
| 1712 |
} |
| 1713 |
}) |
| 1714 |
.takeLast(1) |
| 1715 |
; |
| 1716 |
|
| 1717 |
System.out.println("Bounded Replay Leak check: Wait before GC"); |
| 1718 |
Thread.sleep(1000); |
| 1719 |
|
| 1720 |
System.out.println("Bounded Replay Leak check: GC"); |
| 1721 |
System.gc(); |
| 1722 |
|
| 1723 |
Thread.sleep(500); |
| 1724 |
|
| 1725 |
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); |
| 1726 |
MemoryUsage memHeap = memoryMXBean.getHeapMemoryUsage(); |
| 1727 |
long initial = memHeap.getUsed(); |
| 1728 |
|
| 1729 |
System.out.printf("Bounded Replay Leak check: Starting: %.3f MB%n", initial / 1024.0 / 1024.0); |
| 1730 |
|
| 1731 |
final AtomicLong after = new AtomicLong(); |
| 1732 |
|
| 1733 |
source.subscribe(new Consumer<byte[]>() { |
| 1734 |
@Override |
| 1735 |
public void accept(byte[] v) throws Exception { |
| 1736 |
System.out.println("Bounded Replay Leak check: Wait before GC 2"); |
| 1737 |
Thread.sleep(1000); |
| 1738 |
|
| 1739 |
System.out.println("Bounded Replay Leak check: GC 2"); |
| 1740 |
System.gc(); |
| 1741 |
|
| 1742 |
Thread.sleep(500); |
| 1743 |
|
| 1744 |
after.set(memoryMXBean.getHeapMemoryUsage().getUsed()); |
| 1745 |
} |
| 1746 |
}); |
| 1747 |
|
| 1748 |
for (int i = 0; i < 200; i++) { |
| 1749 |
rp.onNext(new byte[1024 * 1024]); |
| 1750 |
} |
| 1751 |
rp.onComplete(); |
| 1752 |
|
| 1753 |
System.out.printf("Bounded Replay Leak check: After: %.3f MB%n", after.get() / 1024.0 / 1024.0); |
| 1754 |
|
| 1755 |
if (initial + 100 * 1024 * 1024 < after.get()) { |
| 1756 |
Assert.fail("Bounded Replay Leak check: Memory leak detected: " + (initial / 1024.0 / 1024.0) |
| 1757 |
+ " -> " + after.get() / 1024.0 / 1024.0); |
| 1758 |
} |
| 1759 |
} |
| 1760 |
|
| 1761 |
@Test |
| 1762 |
public void timeAndSizeNoTerminalTruncationOnTimechange() { |
| 1763 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, new TimesteppingScheduler(), 1); |
| 1764 |
|
| 1765 |
TestSubscriber<Integer> ts = rp.test(); |
| 1766 |
|
| 1767 |
rp.onNext(1); |
| 1768 |
rp.cleanupBuffer(); |
| 1769 |
rp.onComplete(); |
| 1770 |
|
| 1771 |
ts.assertNoErrors() |
| 1772 |
.assertComplete(); |
| 1773 |
} |
| 1774 |
|
| 1775 |
@Test |
| 1776 |
public void timeAndSizeNoTerminalTruncationOnTimechange2() { |
| 1777 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, new TimesteppingScheduler(), 1); |
| 1778 |
|
| 1779 |
TestSubscriber<Integer> ts = rp.test(); |
| 1780 |
|
| 1781 |
rp.onNext(1); |
| 1782 |
rp.cleanupBuffer(); |
| 1783 |
rp.onNext(2); |
| 1784 |
rp.cleanupBuffer(); |
| 1785 |
rp.onComplete(); |
| 1786 |
|
| 1787 |
ts.assertNoErrors() |
| 1788 |
.assertComplete(); |
| 1789 |
} |
| 1790 |
|
| 1791 |
@Test |
| 1792 |
public void timeAndSizeNoTerminalTruncationOnTimechange3() { |
| 1793 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, new TimesteppingScheduler(), 1); |
| 1794 |
|
| 1795 |
TestSubscriber<Integer> ts = rp.test(); |
| 1796 |
|
| 1797 |
rp.onNext(1); |
| 1798 |
rp.onNext(2); |
| 1799 |
rp.onComplete(); |
| 1800 |
|
| 1801 |
ts.assertNoErrors() |
| 1802 |
.assertComplete(); |
| 1803 |
} |
| 1804 |
|
| 1805 |
@Test |
| 1806 |
public void timeAndSizeNoTerminalTruncationOnTimechange4() { |
| 1807 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, new TimesteppingScheduler(), 10); |
| 1808 |
|
| 1809 |
TestSubscriber<Integer> ts = rp.test(); |
| 1810 |
|
| 1811 |
rp.onNext(1); |
| 1812 |
rp.onNext(2); |
| 1813 |
rp.onComplete(); |
| 1814 |
|
| 1815 |
ts.assertNoErrors() |
| 1816 |
.assertComplete(); |
| 1817 |
} |
| 1818 |
|
| 1819 |
@Test |
| 1820 |
public void timeAndSizeRemoveCorrectNumberOfOld() { |
| 1821 |
TestScheduler scheduler = new TestScheduler(); |
| 1822 |
ReplayProcessor<Integer> rp = ReplayProcessor.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2); |
| 1823 |
|
| 1824 |
rp.onNext(1); |
| 1825 |
rp.onNext(2); |
| 1826 |
rp.onNext(3); |
| 1827 |
|
| 1828 |
scheduler.advanceTimeBy(2, TimeUnit.SECONDS); |
| 1829 |
|
| 1830 |
rp.onNext(4); |
| 1831 |
rp.onNext(5); |
| 1832 |
|
| 1833 |
rp.test().assertValuesOnly(4, 5); |
| 1834 |
} |
| 1835 |
} |