| 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.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.mockito.InOrder; |
| 26 |
import org.mockito.invocation.InvocationOnMock; |
| 27 |
import org.mockito.stubbing.Answer; |
| 28 |
|
| 29 |
import io.reactivex.rxjava3.core.*; |
| 30 |
import io.reactivex.rxjava3.core.Observable; |
| 31 |
import io.reactivex.rxjava3.core.Observer; |
| 32 |
import io.reactivex.rxjava3.disposables.*; |
| 33 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 34 |
import io.reactivex.rxjava3.functions.*; |
| 35 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 36 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 37 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 38 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 39 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 40 |
import io.reactivex.rxjava3.testsupport.*; |
| 41 |
|
| 42 |
public class ObservableTimeoutWithSelectorTest extends RxJavaTest { |
| 43 |
|
| 44 |
@Test |
| 45 |
public void timeoutSelectorNormal1() { |
| 46 |
PublishSubject<Integer> source = PublishSubject.create(); |
| 47 |
final PublishSubject<Integer> timeout = PublishSubject.create(); |
| 48 |
|
| 49 |
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() { |
| 50 |
@Override |
| 51 |
public Observable<Integer> apply(Integer t1) { |
| 52 |
return timeout; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100)); |
| 57 |
|
| 58 |
Observer<Object> o = TestHelper.mockObserver(); |
| 59 |
InOrder inOrder = inOrder(o); |
| 60 |
|
| 61 |
source.timeout(timeout, timeoutFunc, other).subscribe(o); |
| 62 |
|
| 63 |
source.onNext(1); |
| 64 |
source.onNext(2); |
| 65 |
source.onNext(3); |
| 66 |
timeout.onNext(1); |
| 67 |
|
| 68 |
inOrder.verify(o).onNext(1); |
| 69 |
inOrder.verify(o).onNext(2); |
| 70 |
inOrder.verify(o).onNext(3); |
| 71 |
inOrder.verify(o).onNext(100); |
| 72 |
inOrder.verify(o).onComplete(); |
| 73 |
verify(o, never()).onError(any(Throwable.class)); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void timeoutSelectorTimeoutFirst() throws InterruptedException { |
| 79 |
Observable<Integer> source = Observable.<Integer>never(); |
| 80 |
final PublishSubject<Integer> timeout = PublishSubject.create(); |
| 81 |
|
| 82 |
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() { |
| 83 |
@Override |
| 84 |
public Observable<Integer> apply(Integer t1) { |
| 85 |
return timeout; |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100)); |
| 90 |
|
| 91 |
Observer<Object> o = TestHelper.mockObserver(); |
| 92 |
InOrder inOrder = inOrder(o); |
| 93 |
|
| 94 |
source.timeout(timeout, timeoutFunc, other).subscribe(o); |
| 95 |
|
| 96 |
timeout.onNext(1); |
| 97 |
|
| 98 |
inOrder.verify(o).onNext(100); |
| 99 |
inOrder.verify(o).onComplete(); |
| 100 |
verify(o, never()).onError(any(Throwable.class)); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void timeoutSelectorFirstThrows() { |
| 106 |
Observable<Integer> source = Observable.<Integer>never(); |
| 107 |
final PublishSubject<Integer> timeout = PublishSubject.create(); |
| 108 |
|
| 109 |
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() { |
| 110 |
@Override |
| 111 |
public Observable<Integer> apply(Integer t1) { |
| 112 |
return timeout; |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
Supplier<Observable<Integer>> firstTimeoutFunc = new Supplier<Observable<Integer>>() { |
| 117 |
@Override |
| 118 |
public Observable<Integer> get() { |
| 119 |
throw new TestException(); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100)); |
| 124 |
|
| 125 |
Observer<Object> o = TestHelper.mockObserver(); |
| 126 |
|
| 127 |
source.timeout(Observable.defer(firstTimeoutFunc), timeoutFunc, other).subscribe(o); |
| 128 |
|
| 129 |
verify(o).onError(any(TestException.class)); |
| 130 |
verify(o, never()).onNext(any()); |
| 131 |
verify(o, never()).onComplete(); |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
@Test |
| 136 |
public void timeoutSelectorSubsequentThrows() { |
| 137 |
PublishSubject<Integer> source = PublishSubject.create(); |
| 138 |
final PublishSubject<Integer> timeout = PublishSubject.create(); |
| 139 |
|
| 140 |
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() { |
| 141 |
@Override |
| 142 |
public Observable<Integer> apply(Integer t1) { |
| 143 |
throw new TestException(); |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100)); |
| 148 |
|
| 149 |
Observer<Object> o = TestHelper.mockObserver(); |
| 150 |
InOrder inOrder = inOrder(o); |
| 151 |
|
| 152 |
source.timeout(timeout, timeoutFunc, other).subscribe(o); |
| 153 |
|
| 154 |
source.onNext(1); |
| 155 |
|
| 156 |
inOrder.verify(o).onNext(1); |
| 157 |
inOrder.verify(o).onError(any(TestException.class)); |
| 158 |
verify(o, never()).onComplete(); |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void timeoutSelectorFirstObservableThrows() { |
| 164 |
PublishSubject<Integer> source = PublishSubject.create(); |
| 165 |
final PublishSubject<Integer> timeout = PublishSubject.create(); |
| 166 |
|
| 167 |
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() { |
| 168 |
@Override |
| 169 |
public Observable<Integer> apply(Integer t1) { |
| 170 |
return timeout; |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100)); |
| 175 |
|
| 176 |
Observer<Object> o = TestHelper.mockObserver(); |
| 177 |
|
| 178 |
source.timeout(Observable.<Integer> error(new TestException()), timeoutFunc, other).subscribe(o); |
| 179 |
|
| 180 |
verify(o).onError(any(TestException.class)); |
| 181 |
verify(o, never()).onNext(any()); |
| 182 |
verify(o, never()).onComplete(); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
@Test |
| 187 |
public void timeoutSelectorSubsequentObservableThrows() { |
| 188 |
PublishSubject<Integer> source = PublishSubject.create(); |
| 189 |
final PublishSubject<Integer> timeout = PublishSubject.create(); |
| 190 |
|
| 191 |
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() { |
| 192 |
@Override |
| 193 |
public Observable<Integer> apply(Integer t1) { |
| 194 |
return Observable.<Integer> error(new TestException()); |
| 195 |
} |
| 196 |
}; |
| 197 |
|
| 198 |
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100)); |
| 199 |
|
| 200 |
Observer<Object> o = TestHelper.mockObserver(); |
| 201 |
InOrder inOrder = inOrder(o); |
| 202 |
|
| 203 |
source.timeout(timeout, timeoutFunc, other).subscribe(o); |
| 204 |
|
| 205 |
source.onNext(1); |
| 206 |
|
| 207 |
inOrder.verify(o).onNext(1); |
| 208 |
inOrder.verify(o).onError(any(TestException.class)); |
| 209 |
verify(o, never()).onComplete(); |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
@Test |
| 214 |
public void timeoutSelectorWithFirstTimeoutFirstAndNoOtherObservable() { |
| 215 |
PublishSubject<Integer> source = PublishSubject.create(); |
| 216 |
final PublishSubject<Integer> timeout = PublishSubject.create(); |
| 217 |
|
| 218 |
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() { |
| 219 |
@Override |
| 220 |
public Observable<Integer> apply(Integer t1) { |
| 221 |
return PublishSubject.create(); |
| 222 |
} |
| 223 |
}; |
| 224 |
|
| 225 |
Observer<Object> o = TestHelper.mockObserver(); |
| 226 |
source.timeout(timeout, timeoutFunc).subscribe(o); |
| 227 |
|
| 228 |
timeout.onNext(1); |
| 229 |
|
| 230 |
InOrder inOrder = inOrder(o); |
| 231 |
inOrder.verify(o).onError(isA(TimeoutException.class)); |
| 232 |
inOrder.verifyNoMoreInteractions(); |
| 233 |
} |
| 234 |
|
| 235 |
@Test |
| 236 |
public void timeoutSelectorWithTimeoutFirstAndNoOtherObservable() { |
| 237 |
PublishSubject<Integer> source = PublishSubject.create(); |
| 238 |
final PublishSubject<Integer> timeout = PublishSubject.create(); |
| 239 |
|
| 240 |
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() { |
| 241 |
@Override |
| 242 |
public Observable<Integer> apply(Integer t1) { |
| 243 |
return timeout; |
| 244 |
} |
| 245 |
}; |
| 246 |
|
| 247 |
Observer<Object> o = TestHelper.mockObserver(); |
| 248 |
source.timeout(PublishSubject.create(), timeoutFunc).subscribe(o); |
| 249 |
source.onNext(1); |
| 250 |
|
| 251 |
timeout.onNext(1); |
| 252 |
|
| 253 |
InOrder inOrder = inOrder(o); |
| 254 |
inOrder.verify(o).onNext(1); |
| 255 |
inOrder.verify(o).onError(isA(TimeoutException.class)); |
| 256 |
inOrder.verifyNoMoreInteractions(); |
| 257 |
} |
| 258 |
|
| 259 |
@Test |
| 260 |
public void timeoutSelectorWithTimeoutAndOnNextRaceCondition() throws InterruptedException { |
| 261 |
// Thread 1 Thread 2 |
| 262 |
// |
| 263 |
// observer.onNext(1) |
| 264 |
// start timeout |
| 265 |
// unsubscribe timeout in thread 2 start to do some long-time work in "unsubscribe" |
| 266 |
// observer.onNext(2) |
| 267 |
// timeout.onNext(1) |
| 268 |
// "unsubscribe" done |
| 269 |
// |
| 270 |
// |
| 271 |
// In the above case, the timeout operator should ignore "timeout.onNext(1)" |
| 272 |
// since "observer" has already seen 2. |
| 273 |
final CountDownLatch observerReceivedTwo = new CountDownLatch(1); |
| 274 |
final CountDownLatch timeoutEmittedOne = new CountDownLatch(1); |
| 275 |
final CountDownLatch observerCompleted = new CountDownLatch(1); |
| 276 |
final CountDownLatch enteredTimeoutOne = new CountDownLatch(1); |
| 277 |
final AtomicBoolean latchTimeout = new AtomicBoolean(false); |
| 278 |
|
| 279 |
final Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() { |
| 280 |
@Override |
| 281 |
public Observable<Integer> apply(Integer t1) { |
| 282 |
if (t1 == 1) { |
| 283 |
// Force "unsubscribe" run on another thread |
| 284 |
return Observable.unsafeCreate(new ObservableSource<Integer>() { |
| 285 |
@Override |
| 286 |
public void subscribe(Observer<? super Integer> observer) { |
| 287 |
observer.onSubscribe(Disposable.empty()); |
| 288 |
enteredTimeoutOne.countDown(); |
| 289 |
// force the timeout message be sent after observer.onNext(2) |
| 290 |
while (true) { |
| 291 |
try { |
| 292 |
if (!observerReceivedTwo.await(30, TimeUnit.SECONDS)) { |
| 293 |
// CountDownLatch timeout |
| 294 |
// There should be something wrong |
| 295 |
latchTimeout.set(true); |
| 296 |
} |
| 297 |
break; |
| 298 |
} catch (InterruptedException e) { |
| 299 |
// Since we just want to emulate a busy method, |
| 300 |
// we ignore the interrupt signal from Scheduler. |
| 301 |
} |
| 302 |
} |
| 303 |
observer.onNext(1); |
| 304 |
timeoutEmittedOne.countDown(); |
| 305 |
} |
| 306 |
}).subscribeOn(Schedulers.newThread()); |
| 307 |
} else { |
| 308 |
return PublishSubject.create(); |
| 309 |
} |
| 310 |
} |
| 311 |
}; |
| 312 |
|
| 313 |
final Observer<Integer> o = TestHelper.mockObserver(); |
| 314 |
doAnswer(new Answer<Void>() { |
| 315 |
|
| 316 |
@Override |
| 317 |
public Void answer(InvocationOnMock invocation) throws Throwable { |
| 318 |
observerReceivedTwo.countDown(); |
| 319 |
return null; |
| 320 |
} |
| 321 |
|
| 322 |
}).when(o).onNext(2); |
| 323 |
doAnswer(new Answer<Void>() { |
| 324 |
|
| 325 |
@Override |
| 326 |
public Void answer(InvocationOnMock invocation) throws Throwable { |
| 327 |
observerCompleted.countDown(); |
| 328 |
return null; |
| 329 |
} |
| 330 |
|
| 331 |
}).when(o).onComplete(); |
| 332 |
|
| 333 |
final TestObserver<Integer> to = new TestObserver<>(o); |
| 334 |
|
| 335 |
new Thread(new Runnable() { |
| 336 |
|
| 337 |
@Override |
| 338 |
public void run() { |
| 339 |
PublishSubject<Integer> source = PublishSubject.create(); |
| 340 |
source.timeout(timeoutFunc, Observable.just(3)).subscribe(to); |
| 341 |
source.onNext(1); // start timeout |
| 342 |
try { |
| 343 |
if (!enteredTimeoutOne.await(30, TimeUnit.SECONDS)) { |
| 344 |
latchTimeout.set(true); |
| 345 |
} |
| 346 |
} catch (InterruptedException e) { |
| 347 |
e.printStackTrace(); |
| 348 |
} |
| 349 |
source.onNext(2); // disable timeout |
| 350 |
try { |
| 351 |
if (!timeoutEmittedOne.await(30, TimeUnit.SECONDS)) { |
| 352 |
latchTimeout.set(true); |
| 353 |
} |
| 354 |
} catch (InterruptedException e) { |
| 355 |
e.printStackTrace(); |
| 356 |
} |
| 357 |
source.onComplete(); |
| 358 |
} |
| 359 |
|
| 360 |
}).start(); |
| 361 |
|
| 362 |
if (!observerCompleted.await(30, TimeUnit.SECONDS)) { |
| 363 |
latchTimeout.set(true); |
| 364 |
} |
| 365 |
|
| 366 |
assertFalse("CoundDownLatch timeout", latchTimeout.get()); |
| 367 |
|
| 368 |
InOrder inOrder = inOrder(o); |
| 369 |
inOrder.verify(o).onSubscribe((Disposable)notNull()); |
| 370 |
inOrder.verify(o).onNext(1); |
| 371 |
inOrder.verify(o).onNext(2); |
| 372 |
inOrder.verify(o, never()).onNext(3); |
| 373 |
inOrder.verify(o).onComplete(); |
| 374 |
inOrder.verifyNoMoreInteractions(); |
| 375 |
} |
| 376 |
|
| 377 |
@Test |
| 378 |
public void dispose() { |
| 379 |
TestHelper.checkDisposed(PublishSubject.create().timeout(Functions.justFunction(Observable.never()))); |
| 380 |
|
| 381 |
TestHelper.checkDisposed(PublishSubject.create().timeout(Functions.justFunction(Observable.never()), Observable.never())); |
| 382 |
} |
| 383 |
|
| 384 |
@Test |
| 385 |
public void doubleOnSubscribe() { |
| 386 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Object>>() { |
| 387 |
@Override |
| 388 |
public ObservableSource<Object> apply(Observable<Object> o) throws Exception { |
| 389 |
return o.timeout(Functions.justFunction(Observable.never())); |
| 390 |
} |
| 391 |
}); |
| 392 |
|
| 393 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Object>>() { |
| 394 |
@Override |
| 395 |
public ObservableSource<Object> apply(Observable<Object> o) throws Exception { |
| 396 |
return o.timeout(Functions.justFunction(Observable.never()), Observable.never()); |
| 397 |
} |
| 398 |
}); |
| 399 |
} |
| 400 |
|
| 401 |
@Test |
| 402 |
public void empty() { |
| 403 |
Observable.empty() |
| 404 |
.timeout(Functions.justFunction(Observable.never())) |
| 405 |
.test() |
| 406 |
.assertResult(); |
| 407 |
} |
| 408 |
|
| 409 |
@Test |
| 410 |
public void error() { |
| 411 |
Observable.error(new TestException()) |
| 412 |
.timeout(Functions.justFunction(Observable.never())) |
| 413 |
.test() |
| 414 |
.assertFailure(TestException.class); |
| 415 |
} |
| 416 |
|
| 417 |
@Test |
| 418 |
public void emptyInner() { |
| 419 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 420 |
|
| 421 |
TestObserver<Integer> to = ps |
| 422 |
.timeout(Functions.justFunction(Observable.empty())) |
| 423 |
.test(); |
| 424 |
|
| 425 |
ps.onNext(1); |
| 426 |
|
| 427 |
to.assertFailure(TimeoutException.class, 1); |
| 428 |
} |
| 429 |
|
| 430 |
@Test |
| 431 |
public void badInnerSource() { |
| 432 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 433 |
try { |
| 434 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 435 |
|
| 436 |
TestObserverEx<Integer> to = ps |
| 437 |
.timeout(Functions.justFunction(new Observable<Integer>() { |
| 438 |
@Override |
| 439 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 440 |
observer.onSubscribe(Disposable.empty()); |
| 441 |
observer.onError(new TestException("First")); |
| 442 |
observer.onNext(2); |
| 443 |
observer.onError(new TestException("Second")); |
| 444 |
observer.onComplete(); |
| 445 |
} |
| 446 |
})) |
| 447 |
.to(TestHelper.<Integer>testConsumer()); |
| 448 |
|
| 449 |
ps.onNext(1); |
| 450 |
|
| 451 |
to.assertFailureAndMessage(TestException.class, "First", 1); |
| 452 |
|
| 453 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second"); |
| 454 |
} finally { |
| 455 |
RxJavaPlugins.reset(); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
@Test |
| 460 |
public void badInnerSourceOther() { |
| 461 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 462 |
try { |
| 463 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 464 |
|
| 465 |
TestObserverEx<Integer> to = ps |
| 466 |
.timeout(Functions.justFunction(new Observable<Integer>() { |
| 467 |
@Override |
| 468 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 469 |
observer.onSubscribe(Disposable.empty()); |
| 470 |
observer.onError(new TestException("First")); |
| 471 |
observer.onNext(2); |
| 472 |
observer.onError(new TestException("Second")); |
| 473 |
observer.onComplete(); |
| 474 |
} |
| 475 |
}), Observable.just(2)) |
| 476 |
.to(TestHelper.<Integer>testConsumer()); |
| 477 |
|
| 478 |
ps.onNext(1); |
| 479 |
|
| 480 |
to.assertFailureAndMessage(TestException.class, "First", 1); |
| 481 |
|
| 482 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second"); |
| 483 |
} finally { |
| 484 |
RxJavaPlugins.reset(); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
@Test |
| 489 |
public void withOtherMainError() { |
| 490 |
Observable.error(new TestException()) |
| 491 |
.timeout(Functions.justFunction(Observable.never()), Observable.never()) |
| 492 |
.test() |
| 493 |
.assertFailure(TestException.class); |
| 494 |
} |
| 495 |
|
| 496 |
@Test |
| 497 |
@SuppressUndeliverable |
| 498 |
public void badSourceTimeout() { |
| 499 |
new Observable<Integer>() { |
| 500 |
@Override |
| 501 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 502 |
observer.onSubscribe(Disposable.empty()); |
| 503 |
observer.onNext(1); |
| 504 |
observer.onNext(2); |
| 505 |
observer.onError(new TestException("First")); |
| 506 |
observer.onNext(3); |
| 507 |
observer.onComplete(); |
| 508 |
observer.onError(new TestException("Second")); |
| 509 |
} |
| 510 |
} |
| 511 |
.timeout(Functions.justFunction(Observable.never()), Observable.<Integer>never()) |
| 512 |
.take(1) |
| 513 |
.test() |
| 514 |
.assertResult(1); |
| 515 |
} |
| 516 |
|
| 517 |
@Test |
| 518 |
public void selectorTake() { |
| 519 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 520 |
|
| 521 |
TestObserver<Integer> to = ps |
| 522 |
.timeout(Functions.justFunction(Observable.never())) |
| 523 |
.take(1) |
| 524 |
.test(); |
| 525 |
|
| 526 |
assertTrue(ps.hasObservers()); |
| 527 |
|
| 528 |
ps.onNext(1); |
| 529 |
|
| 530 |
assertFalse(ps.hasObservers()); |
| 531 |
|
| 532 |
to.assertResult(1); |
| 533 |
} |
| 534 |
|
| 535 |
@Test |
| 536 |
public void selectorFallbackTake() { |
| 537 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 538 |
|
| 539 |
TestObserver<Integer> to = ps |
| 540 |
.timeout(Functions.justFunction(Observable.never()), Observable.just(2)) |
| 541 |
.take(1) |
| 542 |
.test(); |
| 543 |
|
| 544 |
assertTrue(ps.hasObservers()); |
| 545 |
|
| 546 |
ps.onNext(1); |
| 547 |
|
| 548 |
assertFalse(ps.hasObservers()); |
| 549 |
|
| 550 |
to.assertResult(1); |
| 551 |
} |
| 552 |
|
| 553 |
@Test |
| 554 |
public void lateOnTimeoutError() { |
| 555 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 556 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 557 |
try { |
| 558 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 559 |
|
| 560 |
final Observer<?>[] sub = { null, null }; |
| 561 |
|
| 562 |
final Observable<Integer> pp2 = new Observable<Integer>() { |
| 563 |
|
| 564 |
int count; |
| 565 |
|
| 566 |
@Override |
| 567 |
protected void subscribeActual( |
| 568 |
Observer<? super Integer> observer) { |
| 569 |
observer.onSubscribe(Disposable.empty()); |
| 570 |
sub[count++] = observer; |
| 571 |
} |
| 572 |
}; |
| 573 |
|
| 574 |
TestObserver<Integer> to = ps.timeout(Functions.justFunction(pp2)).test(); |
| 575 |
|
| 576 |
ps.onNext(0); |
| 577 |
|
| 578 |
Runnable r1 = new Runnable() { |
| 579 |
@Override |
| 580 |
public void run() { |
| 581 |
ps.onNext(1); |
| 582 |
} |
| 583 |
}; |
| 584 |
|
| 585 |
final Throwable ex = new TestException(); |
| 586 |
|
| 587 |
Runnable r2 = new Runnable() { |
| 588 |
@Override |
| 589 |
public void run() { |
| 590 |
sub[0].onError(ex); |
| 591 |
} |
| 592 |
}; |
| 593 |
|
| 594 |
TestHelper.race(r1, r2); |
| 595 |
|
| 596 |
to.assertValueAt(0, 0); |
| 597 |
|
| 598 |
if (!errors.isEmpty()) { |
| 599 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 600 |
} |
| 601 |
} finally { |
| 602 |
RxJavaPlugins.reset(); |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
@Test |
| 608 |
public void lateOnTimeoutFallbackRace() { |
| 609 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 610 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 611 |
try { |
| 612 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 613 |
|
| 614 |
final Observer<?>[] sub = { null, null }; |
| 615 |
|
| 616 |
final Observable<Integer> pp2 = new Observable<Integer>() { |
| 617 |
|
| 618 |
int count; |
| 619 |
|
| 620 |
@Override |
| 621 |
protected void subscribeActual( |
| 622 |
Observer<? super Integer> observer) { |
| 623 |
assertFalse(((Disposable)observer).isDisposed()); |
| 624 |
observer.onSubscribe(Disposable.empty()); |
| 625 |
sub[count++] = observer; |
| 626 |
} |
| 627 |
}; |
| 628 |
|
| 629 |
TestObserver<Integer> to = ps.timeout(Functions.justFunction(pp2), Observable.<Integer>never()).test(); |
| 630 |
|
| 631 |
ps.onNext(0); |
| 632 |
|
| 633 |
Runnable r1 = new Runnable() { |
| 634 |
@Override |
| 635 |
public void run() { |
| 636 |
ps.onNext(1); |
| 637 |
} |
| 638 |
}; |
| 639 |
|
| 640 |
final Throwable ex = new TestException(); |
| 641 |
|
| 642 |
Runnable r2 = new Runnable() { |
| 643 |
@Override |
| 644 |
public void run() { |
| 645 |
sub[0].onError(ex); |
| 646 |
} |
| 647 |
}; |
| 648 |
|
| 649 |
TestHelper.race(r1, r2); |
| 650 |
|
| 651 |
to.assertValueAt(0, 0); |
| 652 |
|
| 653 |
if (!errors.isEmpty()) { |
| 654 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 655 |
} |
| 656 |
} finally { |
| 657 |
RxJavaPlugins.reset(); |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
@Test |
| 663 |
public void onErrorOnTimeoutRace() { |
| 664 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 665 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 666 |
try { |
| 667 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 668 |
|
| 669 |
final Observer<?>[] sub = { null, null }; |
| 670 |
|
| 671 |
final Observable<Integer> pp2 = new Observable<Integer>() { |
| 672 |
|
| 673 |
int count; |
| 674 |
|
| 675 |
@Override |
| 676 |
protected void subscribeActual( |
| 677 |
Observer<? super Integer> observer) { |
| 678 |
assertFalse(((Disposable)observer).isDisposed()); |
| 679 |
observer.onSubscribe(Disposable.empty()); |
| 680 |
sub[count++] = observer; |
| 681 |
} |
| 682 |
}; |
| 683 |
|
| 684 |
TestObserver<Integer> to = ps.timeout(Functions.justFunction(pp2)).test(); |
| 685 |
|
| 686 |
ps.onNext(0); |
| 687 |
|
| 688 |
final Throwable ex = new TestException(); |
| 689 |
|
| 690 |
Runnable r1 = new Runnable() { |
| 691 |
@Override |
| 692 |
public void run() { |
| 693 |
ps.onError(ex); |
| 694 |
} |
| 695 |
}; |
| 696 |
|
| 697 |
Runnable r2 = new Runnable() { |
| 698 |
@Override |
| 699 |
public void run() { |
| 700 |
sub[0].onComplete(); |
| 701 |
} |
| 702 |
}; |
| 703 |
|
| 704 |
TestHelper.race(r1, r2); |
| 705 |
|
| 706 |
to.assertValueAt(0, 0); |
| 707 |
|
| 708 |
if (!errors.isEmpty()) { |
| 709 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 710 |
} |
| 711 |
} finally { |
| 712 |
RxJavaPlugins.reset(); |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
@Test |
| 718 |
public void onCompleteOnTimeoutRace() { |
| 719 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 720 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 721 |
try { |
| 722 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 723 |
|
| 724 |
final Observer<?>[] sub = { null, null }; |
| 725 |
|
| 726 |
final Observable<Integer> pp2 = new Observable<Integer>() { |
| 727 |
|
| 728 |
int count; |
| 729 |
|
| 730 |
@Override |
| 731 |
protected void subscribeActual( |
| 732 |
Observer<? super Integer> observer) { |
| 733 |
assertFalse(((Disposable)observer).isDisposed()); |
| 734 |
observer.onSubscribe(Disposable.empty()); |
| 735 |
sub[count++] = observer; |
| 736 |
} |
| 737 |
}; |
| 738 |
|
| 739 |
TestObserver<Integer> to = ps.timeout(Functions.justFunction(pp2)).test(); |
| 740 |
|
| 741 |
ps.onNext(0); |
| 742 |
|
| 743 |
Runnable r1 = new Runnable() { |
| 744 |
@Override |
| 745 |
public void run() { |
| 746 |
ps.onComplete(); |
| 747 |
} |
| 748 |
}; |
| 749 |
|
| 750 |
Runnable r2 = new Runnable() { |
| 751 |
@Override |
| 752 |
public void run() { |
| 753 |
sub[0].onComplete(); |
| 754 |
} |
| 755 |
}; |
| 756 |
|
| 757 |
TestHelper.race(r1, r2); |
| 758 |
|
| 759 |
to.assertValueAt(0, 0); |
| 760 |
|
| 761 |
if (!errors.isEmpty()) { |
| 762 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 763 |
} |
| 764 |
} finally { |
| 765 |
RxJavaPlugins.reset(); |
| 766 |
} |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
@Test |
| 771 |
public void onCompleteOnTimeoutRaceFallback() { |
| 772 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 773 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 774 |
try { |
| 775 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 776 |
|
| 777 |
final Observer<?>[] sub = { null, null }; |
| 778 |
|
| 779 |
final Observable<Integer> pp2 = new Observable<Integer>() { |
| 780 |
|
| 781 |
int count; |
| 782 |
|
| 783 |
@Override |
| 784 |
protected void subscribeActual( |
| 785 |
Observer<? super Integer> observer) { |
| 786 |
assertFalse(((Disposable)observer).isDisposed()); |
| 787 |
observer.onSubscribe(Disposable.empty()); |
| 788 |
sub[count++] = observer; |
| 789 |
} |
| 790 |
}; |
| 791 |
|
| 792 |
TestObserver<Integer> to = ps.timeout(Functions.justFunction(pp2), Observable.<Integer>never()).test(); |
| 793 |
|
| 794 |
ps.onNext(0); |
| 795 |
|
| 796 |
Runnable r1 = new Runnable() { |
| 797 |
@Override |
| 798 |
public void run() { |
| 799 |
ps.onComplete(); |
| 800 |
} |
| 801 |
}; |
| 802 |
|
| 803 |
Runnable r2 = new Runnable() { |
| 804 |
@Override |
| 805 |
public void run() { |
| 806 |
sub[0].onComplete(); |
| 807 |
} |
| 808 |
}; |
| 809 |
|
| 810 |
TestHelper.race(r1, r2); |
| 811 |
|
| 812 |
to.assertValueAt(0, 0); |
| 813 |
|
| 814 |
if (!errors.isEmpty()) { |
| 815 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 816 |
} |
| 817 |
} finally { |
| 818 |
RxJavaPlugins.reset(); |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
@Test |
| 824 |
public void disposedUpfront() { |
| 825 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 826 |
final AtomicInteger counter = new AtomicInteger(); |
| 827 |
|
| 828 |
Observable<Object> timeoutAndFallback = Observable.never().doOnSubscribe(new Consumer<Disposable>() { |
| 829 |
@Override |
| 830 |
public void accept(Disposable d) throws Exception { |
| 831 |
counter.incrementAndGet(); |
| 832 |
} |
| 833 |
}); |
| 834 |
|
| 835 |
ps |
| 836 |
.timeout(timeoutAndFallback, Functions.justFunction(timeoutAndFallback)) |
| 837 |
.test(true) |
| 838 |
.assertEmpty(); |
| 839 |
|
| 840 |
assertEquals(0, counter.get()); |
| 841 |
} |
| 842 |
|
| 843 |
@Test |
| 844 |
public void disposedUpfrontFallback() { |
| 845 |
PublishSubject<Object> ps = PublishSubject.create(); |
| 846 |
final AtomicInteger counter = new AtomicInteger(); |
| 847 |
|
| 848 |
Observable<Object> timeoutAndFallback = Observable.never().doOnSubscribe(new Consumer<Disposable>() { |
| 849 |
@Override |
| 850 |
public void accept(Disposable d) throws Exception { |
| 851 |
counter.incrementAndGet(); |
| 852 |
} |
| 853 |
}); |
| 854 |
|
| 855 |
ps |
| 856 |
.timeout(timeoutAndFallback, Functions.justFunction(timeoutAndFallback), timeoutAndFallback) |
| 857 |
.test(true) |
| 858 |
.assertEmpty(); |
| 859 |
|
| 860 |
assertEquals(0, counter.get()); |
| 861 |
} |
| 862 |
} |