| 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.subjects; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.concurrent.TimeUnit; |
| 21 |
import java.util.concurrent.atomic.AtomicReference; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.mockito.*; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.Observer; |
| 27 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 28 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 29 |
import io.reactivex.rxjava3.functions.Consumer; |
| 30 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 31 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 32 |
import io.reactivex.rxjava3.testsupport.*; |
| 33 |
|
| 34 |
public class AsyncSubjectTest extends SubjectTest<Integer> { |
| 35 |
|
| 36 |
private final Throwable testException = new Throwable(); |
| 37 |
|
| 38 |
@Override |
| 39 |
protected Subject<Integer> create() { |
| 40 |
return AsyncSubject.create(); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void neverCompleted() { |
| 45 |
AsyncSubject<String> subject = AsyncSubject.create(); |
| 46 |
|
| 47 |
Observer<String> observer = TestHelper.mockObserver(); |
| 48 |
subject.subscribe(observer); |
| 49 |
|
| 50 |
subject.onNext("one"); |
| 51 |
subject.onNext("two"); |
| 52 |
subject.onNext("three"); |
| 53 |
|
| 54 |
verify(observer, Mockito.never()).onNext(anyString()); |
| 55 |
verify(observer, Mockito.never()).onError(testException); |
| 56 |
verify(observer, Mockito.never()).onComplete(); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void completed() { |
| 61 |
AsyncSubject<String> subject = AsyncSubject.create(); |
| 62 |
|
| 63 |
Observer<String> observer = TestHelper.mockObserver(); |
| 64 |
subject.subscribe(observer); |
| 65 |
|
| 66 |
subject.onNext("one"); |
| 67 |
subject.onNext("two"); |
| 68 |
subject.onNext("three"); |
| 69 |
subject.onComplete(); |
| 70 |
|
| 71 |
verify(observer, times(1)).onNext("three"); |
| 72 |
verify(observer, Mockito.never()).onError(any(Throwable.class)); |
| 73 |
verify(observer, times(1)).onComplete(); |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
public void subscribeAfterCompleted() { |
| 78 |
AsyncSubject<String> subject = AsyncSubject.create(); |
| 79 |
|
| 80 |
Observer<String> observer = TestHelper.mockObserver(); |
| 81 |
|
| 82 |
subject.onNext("one"); |
| 83 |
subject.onNext("two"); |
| 84 |
subject.onNext("three"); |
| 85 |
subject.onComplete(); |
| 86 |
|
| 87 |
subject.subscribe(observer); |
| 88 |
|
| 89 |
verify(observer, times(1)).onNext("three"); |
| 90 |
verify(observer, Mockito.never()).onError(any(Throwable.class)); |
| 91 |
verify(observer, times(1)).onComplete(); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void subscribeAfterError() { |
| 96 |
AsyncSubject<String> subject = AsyncSubject.create(); |
| 97 |
|
| 98 |
Observer<String> observer = TestHelper.mockObserver(); |
| 99 |
|
| 100 |
subject.onNext("one"); |
| 101 |
subject.onNext("two"); |
| 102 |
subject.onNext("three"); |
| 103 |
|
| 104 |
RuntimeException re = new RuntimeException("failed"); |
| 105 |
subject.onError(re); |
| 106 |
|
| 107 |
subject.subscribe(observer); |
| 108 |
|
| 109 |
verify(observer, times(1)).onError(re); |
| 110 |
verify(observer, Mockito.never()).onNext(any(String.class)); |
| 111 |
verify(observer, Mockito.never()).onComplete(); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
@SuppressUndeliverable |
| 116 |
public void error() { |
| 117 |
AsyncSubject<String> subject = AsyncSubject.create(); |
| 118 |
|
| 119 |
Observer<String> observer = TestHelper.mockObserver(); |
| 120 |
subject.subscribe(observer); |
| 121 |
|
| 122 |
subject.onNext("one"); |
| 123 |
subject.onNext("two"); |
| 124 |
subject.onNext("three"); |
| 125 |
subject.onError(testException); |
| 126 |
subject.onNext("four"); |
| 127 |
subject.onError(new Throwable()); |
| 128 |
subject.onComplete(); |
| 129 |
|
| 130 |
verify(observer, Mockito.never()).onNext(anyString()); |
| 131 |
verify(observer, times(1)).onError(testException); |
| 132 |
verify(observer, Mockito.never()).onComplete(); |
| 133 |
} |
| 134 |
|
| 135 |
@Test |
| 136 |
public void unsubscribeBeforeCompleted() { |
| 137 |
AsyncSubject<String> subject = AsyncSubject.create(); |
| 138 |
|
| 139 |
Observer<String> observer = TestHelper.mockObserver(); |
| 140 |
TestObserver<String> to = new TestObserver<>(observer); |
| 141 |
subject.subscribe(to); |
| 142 |
|
| 143 |
subject.onNext("one"); |
| 144 |
subject.onNext("two"); |
| 145 |
|
| 146 |
to.dispose(); |
| 147 |
|
| 148 |
verify(observer, Mockito.never()).onNext(anyString()); |
| 149 |
verify(observer, Mockito.never()).onError(any(Throwable.class)); |
| 150 |
verify(observer, Mockito.never()).onComplete(); |
| 151 |
|
| 152 |
subject.onNext("three"); |
| 153 |
subject.onComplete(); |
| 154 |
|
| 155 |
verify(observer, Mockito.never()).onNext(anyString()); |
| 156 |
verify(observer, Mockito.never()).onError(any(Throwable.class)); |
| 157 |
verify(observer, Mockito.never()).onComplete(); |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
public void emptySubjectCompleted() { |
| 162 |
AsyncSubject<String> subject = AsyncSubject.create(); |
| 163 |
|
| 164 |
Observer<String> observer = TestHelper.mockObserver(); |
| 165 |
subject.subscribe(observer); |
| 166 |
|
| 167 |
subject.onComplete(); |
| 168 |
|
| 169 |
InOrder inOrder = inOrder(observer); |
| 170 |
inOrder.verify(observer, never()).onNext(null); |
| 171 |
inOrder.verify(observer, never()).onNext(any(String.class)); |
| 172 |
inOrder.verify(observer, times(1)).onComplete(); |
| 173 |
inOrder.verifyNoMoreInteractions(); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Can receive timeout if subscribe never receives an onError/onComplete ... which reveals a race condition. |
| 178 |
*/ |
| 179 |
@Test |
| 180 |
public void subscribeCompletionRaceCondition() { |
| 181 |
/* |
| 182 |
* With non-threadsafe code this fails most of the time on my dev laptop and is non-deterministic enough |
| 183 |
* to act as a unit test to the race conditions. |
| 184 |
* |
| 185 |
* With the synchronization code in place I can not get this to fail on my laptop. |
| 186 |
*/ |
| 187 |
for (int i = 0; i < 50; i++) { |
| 188 |
final AsyncSubject<String> subject = AsyncSubject.create(); |
| 189 |
final AtomicReference<String> value1 = new AtomicReference<>(); |
| 190 |
|
| 191 |
subject.subscribe(new Consumer<String>() { |
| 192 |
|
| 193 |
@Override |
| 194 |
public void accept(String t1) { |
| 195 |
try { |
| 196 |
// simulate a slow observer |
| 197 |
Thread.sleep(50); |
| 198 |
} catch (InterruptedException e) { |
| 199 |
e.printStackTrace(); |
| 200 |
} |
| 201 |
value1.set(t1); |
| 202 |
} |
| 203 |
|
| 204 |
}); |
| 205 |
|
| 206 |
Thread t1 = new Thread(new Runnable() { |
| 207 |
|
| 208 |
@Override |
| 209 |
public void run() { |
| 210 |
subject.onNext("value"); |
| 211 |
subject.onComplete(); |
| 212 |
} |
| 213 |
}); |
| 214 |
|
| 215 |
SubjectSubscriberThread t2 = new SubjectSubscriberThread(subject); |
| 216 |
SubjectSubscriberThread t3 = new SubjectSubscriberThread(subject); |
| 217 |
SubjectSubscriberThread t4 = new SubjectSubscriberThread(subject); |
| 218 |
SubjectSubscriberThread t5 = new SubjectSubscriberThread(subject); |
| 219 |
|
| 220 |
t2.start(); |
| 221 |
t3.start(); |
| 222 |
t1.start(); |
| 223 |
t4.start(); |
| 224 |
t5.start(); |
| 225 |
try { |
| 226 |
t1.join(); |
| 227 |
t2.join(); |
| 228 |
t3.join(); |
| 229 |
t4.join(); |
| 230 |
t5.join(); |
| 231 |
} catch (InterruptedException e) { |
| 232 |
throw new RuntimeException(e); |
| 233 |
} |
| 234 |
|
| 235 |
assertEquals("value", value1.get()); |
| 236 |
assertEquals("value", t2.value.get()); |
| 237 |
assertEquals("value", t3.value.get()); |
| 238 |
assertEquals("value", t4.value.get()); |
| 239 |
assertEquals("value", t5.value.get()); |
| 240 |
} |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
private static class SubjectSubscriberThread extends Thread { |
| 245 |
|
| 246 |
private final AsyncSubject<String> subject; |
| 247 |
private final AtomicReference<String> value = new AtomicReference<>(); |
| 248 |
|
| 249 |
SubjectSubscriberThread(AsyncSubject<String> subject) { |
| 250 |
this.subject = subject; |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
public void run() { |
| 255 |
try { |
| 256 |
// a timeout exception will happen if we don't get a terminal state |
| 257 |
String v = subject.timeout(2000, TimeUnit.MILLISECONDS).blockingSingle(); |
| 258 |
value.set(v); |
| 259 |
} catch (Exception e) { |
| 260 |
e.printStackTrace(); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
@Test |
| 266 |
public void currentStateMethodsNormal() { |
| 267 |
AsyncSubject<Object> as = AsyncSubject.create(); |
| 268 |
|
| 269 |
assertFalse(as.hasValue()); |
| 270 |
assertFalse(as.hasThrowable()); |
| 271 |
assertFalse(as.hasComplete()); |
| 272 |
assertNull(as.getValue()); |
| 273 |
assertNull(as.getThrowable()); |
| 274 |
|
| 275 |
as.onNext(1); |
| 276 |
|
| 277 |
assertFalse(as.hasValue()); // AS no longer reports a value until it has completed |
| 278 |
assertFalse(as.hasThrowable()); |
| 279 |
assertFalse(as.hasComplete()); |
| 280 |
assertNull(as.getValue()); // AS no longer reports a value until it has completed |
| 281 |
assertNull(as.getThrowable()); |
| 282 |
|
| 283 |
as.onComplete(); |
| 284 |
assertTrue(as.hasValue()); |
| 285 |
assertFalse(as.hasThrowable()); |
| 286 |
assertTrue(as.hasComplete()); |
| 287 |
assertEquals(1, as.getValue()); |
| 288 |
assertNull(as.getThrowable()); |
| 289 |
} |
| 290 |
|
| 291 |
@Test |
| 292 |
public void currentStateMethodsEmpty() { |
| 293 |
AsyncSubject<Object> as = AsyncSubject.create(); |
| 294 |
|
| 295 |
assertFalse(as.hasValue()); |
| 296 |
assertFalse(as.hasThrowable()); |
| 297 |
assertFalse(as.hasComplete()); |
| 298 |
assertNull(as.getValue()); |
| 299 |
assertNull(as.getThrowable()); |
| 300 |
|
| 301 |
as.onComplete(); |
| 302 |
|
| 303 |
assertFalse(as.hasValue()); |
| 304 |
assertFalse(as.hasThrowable()); |
| 305 |
assertTrue(as.hasComplete()); |
| 306 |
assertNull(as.getValue()); |
| 307 |
assertNull(as.getThrowable()); |
| 308 |
} |
| 309 |
|
| 310 |
@Test |
| 311 |
public void currentStateMethodsError() { |
| 312 |
AsyncSubject<Object> as = AsyncSubject.create(); |
| 313 |
|
| 314 |
assertFalse(as.hasValue()); |
| 315 |
assertFalse(as.hasThrowable()); |
| 316 |
assertFalse(as.hasComplete()); |
| 317 |
assertNull(as.getValue()); |
| 318 |
assertNull(as.getThrowable()); |
| 319 |
|
| 320 |
as.onError(new TestException()); |
| 321 |
|
| 322 |
assertFalse(as.hasValue()); |
| 323 |
assertTrue(as.hasThrowable()); |
| 324 |
assertFalse(as.hasComplete()); |
| 325 |
assertNull(as.getValue()); |
| 326 |
assertTrue(as.getThrowable() instanceof TestException); |
| 327 |
} |
| 328 |
|
| 329 |
@Test |
| 330 |
public void fusionLive() { |
| 331 |
AsyncSubject<Integer> ap = new AsyncSubject<>(); |
| 332 |
|
| 333 |
TestObserverEx<Integer> to = ap.to(TestHelper.<Integer>testConsumer(false, QueueFuseable.ANY)); |
| 334 |
|
| 335 |
to.assertFuseable() |
| 336 |
.assertFusionMode(QueueFuseable.ASYNC); |
| 337 |
|
| 338 |
to.assertNoValues().assertNoErrors().assertNotComplete(); |
| 339 |
|
| 340 |
ap.onNext(1); |
| 341 |
|
| 342 |
to.assertNoValues().assertNoErrors().assertNotComplete(); |
| 343 |
|
| 344 |
ap.onComplete(); |
| 345 |
|
| 346 |
to.assertResult(1); |
| 347 |
} |
| 348 |
|
| 349 |
@Test |
| 350 |
public void fusionOfflie() { |
| 351 |
AsyncSubject<Integer> ap = new AsyncSubject<>(); |
| 352 |
ap.onNext(1); |
| 353 |
ap.onComplete(); |
| 354 |
|
| 355 |
TestObserverEx<Integer> to = ap.to(TestHelper.<Integer>testConsumer(false, QueueFuseable.ANY)); |
| 356 |
|
| 357 |
to.assertFuseable() |
| 358 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 359 |
.assertResult(1); |
| 360 |
} |
| 361 |
|
| 362 |
@Test |
| 363 |
public void onSubscribeAfterDone() { |
| 364 |
AsyncSubject<Object> p = AsyncSubject.create(); |
| 365 |
|
| 366 |
Disposable bs = Disposable.empty(); |
| 367 |
p.onSubscribe(bs); |
| 368 |
|
| 369 |
assertFalse(bs.isDisposed()); |
| 370 |
|
| 371 |
p.onComplete(); |
| 372 |
|
| 373 |
bs = Disposable.empty(); |
| 374 |
p.onSubscribe(bs); |
| 375 |
|
| 376 |
assertTrue(bs.isDisposed()); |
| 377 |
|
| 378 |
p.test().assertResult(); |
| 379 |
} |
| 380 |
|
| 381 |
@Test |
| 382 |
public void cancelUpfront() { |
| 383 |
AsyncSubject<Object> p = AsyncSubject.create(); |
| 384 |
|
| 385 |
assertFalse(p.hasObservers()); |
| 386 |
|
| 387 |
p.test().assertEmpty(); |
| 388 |
p.test().assertEmpty(); |
| 389 |
|
| 390 |
p.test(true).assertEmpty(); |
| 391 |
|
| 392 |
assertTrue(p.hasObservers()); |
| 393 |
} |
| 394 |
|
| 395 |
@Test |
| 396 |
public void cancelRace() { |
| 397 |
AsyncSubject<Object> p = AsyncSubject.create(); |
| 398 |
|
| 399 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 400 |
final TestObserver<Object> to1 = p.test(); |
| 401 |
final TestObserver<Object> to2 = p.test(); |
| 402 |
|
| 403 |
Runnable r1 = new Runnable() { |
| 404 |
@Override |
| 405 |
public void run() { |
| 406 |
to1.dispose(); |
| 407 |
} |
| 408 |
}; |
| 409 |
|
| 410 |
Runnable r2 = new Runnable() { |
| 411 |
@Override |
| 412 |
public void run() { |
| 413 |
to2.dispose(); |
| 414 |
} |
| 415 |
}; |
| 416 |
|
| 417 |
TestHelper.race(r1, r2); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
@Test |
| 422 |
@SuppressUndeliverable |
| 423 |
public void onErrorCancelRace() { |
| 424 |
|
| 425 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 426 |
final AsyncSubject<Object> p = AsyncSubject.create(); |
| 427 |
|
| 428 |
final TestObserverEx<Object> to1 = p.to(TestHelper.testConsumer()); |
| 429 |
|
| 430 |
Runnable r1 = new Runnable() { |
| 431 |
@Override |
| 432 |
public void run() { |
| 433 |
to1.dispose(); |
| 434 |
} |
| 435 |
}; |
| 436 |
|
| 437 |
final TestException ex = new TestException(); |
| 438 |
|
| 439 |
Runnable r2 = new Runnable() { |
| 440 |
@Override |
| 441 |
public void run() { |
| 442 |
p.onError(ex); |
| 443 |
} |
| 444 |
}; |
| 445 |
|
| 446 |
TestHelper.race(r1, r2); |
| 447 |
|
| 448 |
if (to1.errors().size() != 0) { |
| 449 |
to1.assertFailure(TestException.class); |
| 450 |
} else { |
| 451 |
to1.assertEmpty(); |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
@Test |
| 457 |
public void onNextCrossCancel() { |
| 458 |
AsyncSubject<Object> p = AsyncSubject.create(); |
| 459 |
|
| 460 |
final TestObserver<Object> to2 = new TestObserver<>(); |
| 461 |
TestObserver<Object> to1 = new TestObserver<Object>() { |
| 462 |
@Override |
| 463 |
public void onNext(Object t) { |
| 464 |
to2.dispose(); |
| 465 |
super.onNext(t); |
| 466 |
} |
| 467 |
}; |
| 468 |
|
| 469 |
p.subscribe(to1); |
| 470 |
p.subscribe(to2); |
| 471 |
|
| 472 |
p.onNext(1); |
| 473 |
p.onComplete(); |
| 474 |
|
| 475 |
to1.assertResult(1); |
| 476 |
to2.assertEmpty(); |
| 477 |
} |
| 478 |
|
| 479 |
@Test |
| 480 |
@SuppressUndeliverable |
| 481 |
public void onErrorCrossCancel() { |
| 482 |
AsyncSubject<Object> p = AsyncSubject.create(); |
| 483 |
|
| 484 |
final TestObserver<Object> to2 = new TestObserver<>(); |
| 485 |
TestObserver<Object> to1 = new TestObserver<Object>() { |
| 486 |
@Override |
| 487 |
public void onError(Throwable t) { |
| 488 |
to2.dispose(); |
| 489 |
super.onError(t); |
| 490 |
} |
| 491 |
}; |
| 492 |
|
| 493 |
p.subscribe(to1); |
| 494 |
p.subscribe(to2); |
| 495 |
|
| 496 |
p.onError(new TestException()); |
| 497 |
|
| 498 |
to1.assertFailure(TestException.class); |
| 499 |
to2.assertEmpty(); |
| 500 |
} |
| 501 |
|
| 502 |
@Test |
| 503 |
public void onCompleteCrossCancel() { |
| 504 |
AsyncSubject<Object> p = AsyncSubject.create(); |
| 505 |
|
| 506 |
final TestObserver<Object> to2 = new TestObserver<>(); |
| 507 |
TestObserver<Object> to1 = new TestObserver<Object>() { |
| 508 |
@Override |
| 509 |
public void onComplete() { |
| 510 |
to2.dispose(); |
| 511 |
super.onComplete(); |
| 512 |
} |
| 513 |
}; |
| 514 |
|
| 515 |
p.subscribe(to1); |
| 516 |
p.subscribe(to2); |
| 517 |
|
| 518 |
p.onComplete(); |
| 519 |
|
| 520 |
to1.assertResult(); |
| 521 |
to2.assertEmpty(); |
| 522 |
} |
| 523 |
|
| 524 |
@Test |
| 525 |
public void dispose() { |
| 526 |
TestHelper.checkDisposed(AsyncSubject.create()); |
| 527 |
} |
| 528 |
} |