| 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.*; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.core.Observable; |
| 29 |
import io.reactivex.rxjava3.core.Observer; |
| 30 |
import io.reactivex.rxjava3.disposables.*; |
| 31 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 32 |
import io.reactivex.rxjava3.functions.*; |
| 33 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 34 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 35 |
import io.reactivex.rxjava3.observables.GroupedObservable; |
| 36 |
import io.reactivex.rxjava3.observers.*; |
| 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 ObservableRetryTest extends RxJavaTest { |
| 43 |
|
| 44 |
@Test |
| 45 |
public void iterativeBackoff() { |
| 46 |
Observer<String> consumer = TestHelper.mockObserver(); |
| 47 |
|
| 48 |
Observable<String> producer = Observable.unsafeCreate(new ObservableSource<String>() { |
| 49 |
|
| 50 |
private AtomicInteger count = new AtomicInteger(4); |
| 51 |
long last = System.currentTimeMillis(); |
| 52 |
|
| 53 |
@Override |
| 54 |
public void subscribe(Observer<? super String> t1) { |
| 55 |
t1.onSubscribe(Disposable.empty()); |
| 56 |
System.out.println(count.get() + " @ " + String.valueOf(last - System.currentTimeMillis())); |
| 57 |
last = System.currentTimeMillis(); |
| 58 |
if (count.getAndDecrement() == 0) { |
| 59 |
t1.onNext("hello"); |
| 60 |
t1.onComplete(); |
| 61 |
} else { |
| 62 |
t1.onError(new RuntimeException()); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
}); |
| 67 |
TestObserver<String> to = new TestObserver<>(consumer); |
| 68 |
producer.retryWhen(new Function<Observable<? extends Throwable>, Observable<Object>>() { |
| 69 |
|
| 70 |
@Override |
| 71 |
public Observable<Object> apply(Observable<? extends Throwable> attempts) { |
| 72 |
// Worker w = Schedulers.computation().createWorker(); |
| 73 |
return attempts |
| 74 |
.map(new Function<Throwable, Tuple>() { |
| 75 |
@Override |
| 76 |
public Tuple apply(Throwable n) { |
| 77 |
return new Tuple(1L, n); |
| 78 |
}}) |
| 79 |
.scan(new BiFunction<Tuple, Tuple, Tuple>() { |
| 80 |
@Override |
| 81 |
public Tuple apply(Tuple t, Tuple n) { |
| 82 |
return new Tuple(t.count + n.count, n.n); |
| 83 |
}}) |
| 84 |
.flatMap(new Function<Tuple, Observable<Long>>() { |
| 85 |
@Override |
| 86 |
public Observable<Long> apply(Tuple t) { |
| 87 |
System.out.println("Retry # " + t.count); |
| 88 |
return t.count > 20 ? |
| 89 |
Observable.<Long>error(t.n) : |
| 90 |
Observable.timer(t.count * 1L, TimeUnit.MILLISECONDS); |
| 91 |
}}).cast(Object.class); |
| 92 |
} |
| 93 |
}).subscribe(to); |
| 94 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 95 |
to.assertNoErrors(); |
| 96 |
|
| 97 |
InOrder inOrder = inOrder(consumer); |
| 98 |
inOrder.verify(consumer, never()).onError(any(Throwable.class)); |
| 99 |
inOrder.verify(consumer, times(1)).onNext("hello"); |
| 100 |
inOrder.verify(consumer, times(1)).onComplete(); |
| 101 |
inOrder.verifyNoMoreInteractions(); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
public static class Tuple { |
| 106 |
Long count; |
| 107 |
Throwable n; |
| 108 |
|
| 109 |
Tuple(Long c, Throwable n) { |
| 110 |
count = c; |
| 111 |
this.n = n; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void retryIndefinitely() { |
| 117 |
Observer<String> observer = TestHelper.mockObserver(); |
| 118 |
int numRetries = 20; |
| 119 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(numRetries)); |
| 120 |
origin.retry().subscribe(new TestObserver<>(observer)); |
| 121 |
|
| 122 |
InOrder inOrder = inOrder(observer); |
| 123 |
// should show 3 attempts |
| 124 |
inOrder.verify(observer, times(numRetries + 1)).onNext("beginningEveryTime"); |
| 125 |
// should have no errors |
| 126 |
inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 127 |
// should have a single success |
| 128 |
inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 129 |
// should have a single successful onComplete |
| 130 |
inOrder.verify(observer, times(1)).onComplete(); |
| 131 |
inOrder.verifyNoMoreInteractions(); |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void schedulingNotificationHandler() { |
| 136 |
Observer<String> observer = TestHelper.mockObserver(); |
| 137 |
int numRetries = 2; |
| 138 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(numRetries)); |
| 139 |
TestObserver<String> to = new TestObserver<>(observer); |
| 140 |
origin.retryWhen(new Function<Observable<? extends Throwable>, Observable<Object>>() { |
| 141 |
@Override |
| 142 |
public Observable<Object> apply(Observable<? extends Throwable> t1) { |
| 143 |
return t1 |
| 144 |
.observeOn(Schedulers.computation()) |
| 145 |
.map(new Function<Throwable, Object>() { |
| 146 |
@Override |
| 147 |
public Object apply(Throwable t1) { |
| 148 |
return 1; |
| 149 |
} |
| 150 |
}).startWithItem(1); |
| 151 |
} |
| 152 |
}) |
| 153 |
.doOnError(new Consumer<Throwable>() { |
| 154 |
@Override |
| 155 |
public void accept(Throwable e) { |
| 156 |
e.printStackTrace(); |
| 157 |
} |
| 158 |
}) |
| 159 |
.subscribe(to); |
| 160 |
|
| 161 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 162 |
InOrder inOrder = inOrder(observer); |
| 163 |
// should show 3 attempts |
| 164 |
inOrder.verify(observer, times(1 + numRetries)).onNext("beginningEveryTime"); |
| 165 |
// should have no errors |
| 166 |
inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 167 |
// should have a single success |
| 168 |
inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 169 |
// should have a single successful onComplete |
| 170 |
inOrder.verify(observer, times(1)).onComplete(); |
| 171 |
inOrder.verifyNoMoreInteractions(); |
| 172 |
} |
| 173 |
|
| 174 |
@Test |
| 175 |
public void onNextFromNotificationHandler() { |
| 176 |
Observer<String> observer = TestHelper.mockObserver(); |
| 177 |
int numRetries = 2; |
| 178 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(numRetries)); |
| 179 |
origin.retryWhen(new Function<Observable<? extends Throwable>, Observable<Object>>() { |
| 180 |
@Override |
| 181 |
public Observable<Object> apply(Observable<? extends Throwable> t1) { |
| 182 |
return t1.map(new Function<Throwable, Integer>() { |
| 183 |
|
| 184 |
@Override |
| 185 |
public Integer apply(Throwable t1) { |
| 186 |
return 0; |
| 187 |
} |
| 188 |
}).startWithItem(0).cast(Object.class); |
| 189 |
} |
| 190 |
}).subscribe(observer); |
| 191 |
|
| 192 |
InOrder inOrder = inOrder(observer); |
| 193 |
// should show 3 attempts |
| 194 |
inOrder.verify(observer, times(numRetries + 1)).onNext("beginningEveryTime"); |
| 195 |
// should have no errors |
| 196 |
inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 197 |
// should have a single success |
| 198 |
inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 199 |
// should have a single successful onComplete |
| 200 |
inOrder.verify(observer, times(1)).onComplete(); |
| 201 |
inOrder.verifyNoMoreInteractions(); |
| 202 |
} |
| 203 |
|
| 204 |
@Test |
| 205 |
public void onCompletedFromNotificationHandler() { |
| 206 |
Observer<String> observer = TestHelper.mockObserver(); |
| 207 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(1)); |
| 208 |
TestObserver<String> to = new TestObserver<>(observer); |
| 209 |
origin.retryWhen(new Function<Observable<? extends Throwable>, Observable<?>>() { |
| 210 |
@Override |
| 211 |
public Observable<?> apply(Observable<? extends Throwable> t1) { |
| 212 |
return Observable.empty(); |
| 213 |
} |
| 214 |
}).subscribe(to); |
| 215 |
|
| 216 |
InOrder inOrder = inOrder(observer); |
| 217 |
inOrder.verify(observer).onSubscribe((Disposable)notNull()); |
| 218 |
inOrder.verify(observer, never()).onNext("beginningEveryTime"); |
| 219 |
inOrder.verify(observer, never()).onNext("onSuccessOnly"); |
| 220 |
inOrder.verify(observer, times(1)).onComplete(); |
| 221 |
inOrder.verify(observer, never()).onError(any(Exception.class)); |
| 222 |
inOrder.verifyNoMoreInteractions(); |
| 223 |
} |
| 224 |
|
| 225 |
@Test |
| 226 |
public void onErrorFromNotificationHandler() { |
| 227 |
Observer<String> observer = TestHelper.mockObserver(); |
| 228 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(2)); |
| 229 |
origin.retryWhen(new Function<Observable<? extends Throwable>, Observable<?>>() { |
| 230 |
@Override |
| 231 |
public Observable<?> apply(Observable<? extends Throwable> t1) { |
| 232 |
return Observable.error(new RuntimeException()); |
| 233 |
} |
| 234 |
}).subscribe(observer); |
| 235 |
|
| 236 |
InOrder inOrder = inOrder(observer); |
| 237 |
inOrder.verify(observer).onSubscribe((Disposable)notNull()); |
| 238 |
inOrder.verify(observer, never()).onNext("beginningEveryTime"); |
| 239 |
inOrder.verify(observer, never()).onNext("onSuccessOnly"); |
| 240 |
inOrder.verify(observer, never()).onComplete(); |
| 241 |
inOrder.verify(observer, times(1)).onError(any(RuntimeException.class)); |
| 242 |
inOrder.verifyNoMoreInteractions(); |
| 243 |
} |
| 244 |
|
| 245 |
@Test |
| 246 |
public void singleSubscriptionOnFirst() throws Exception { |
| 247 |
final AtomicInteger inc = new AtomicInteger(0); |
| 248 |
ObservableSource<Integer> onSubscribe = new ObservableSource<Integer>() { |
| 249 |
@Override |
| 250 |
public void subscribe(Observer<? super Integer> observer) { |
| 251 |
observer.onSubscribe(Disposable.empty()); |
| 252 |
final int emit = inc.incrementAndGet(); |
| 253 |
observer.onNext(emit); |
| 254 |
observer.onComplete(); |
| 255 |
} |
| 256 |
}; |
| 257 |
|
| 258 |
int first = Observable.unsafeCreate(onSubscribe) |
| 259 |
.retryWhen(new Function<Observable<? extends Throwable>, Observable<?>>() { |
| 260 |
@Override |
| 261 |
public Observable<?> apply(Observable<? extends Throwable> attempt) { |
| 262 |
return attempt.zipWith(Observable.just(1), new BiFunction<Throwable, Integer, Void>() { |
| 263 |
@Override |
| 264 |
public Void apply(Throwable o, Integer integer) { |
| 265 |
return null; |
| 266 |
} |
| 267 |
}); |
| 268 |
} |
| 269 |
}) |
| 270 |
.blockingFirst(); |
| 271 |
|
| 272 |
assertEquals("Observer did not receive the expected output", 1, first); |
| 273 |
assertEquals("Subscribe was not called once", 1, inc.get()); |
| 274 |
} |
| 275 |
|
| 276 |
@Test |
| 277 |
public void originFails() { |
| 278 |
Observer<String> observer = TestHelper.mockObserver(); |
| 279 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(1)); |
| 280 |
origin.subscribe(observer); |
| 281 |
|
| 282 |
InOrder inOrder = inOrder(observer); |
| 283 |
inOrder.verify(observer, times(1)).onNext("beginningEveryTime"); |
| 284 |
inOrder.verify(observer, times(1)).onError(any(RuntimeException.class)); |
| 285 |
inOrder.verify(observer, never()).onNext("onSuccessOnly"); |
| 286 |
inOrder.verify(observer, never()).onComplete(); |
| 287 |
} |
| 288 |
|
| 289 |
@Test |
| 290 |
public void retryFail() { |
| 291 |
int numRetries = 1; |
| 292 |
int numFailures = 2; |
| 293 |
Observer<String> observer = TestHelper.mockObserver(); |
| 294 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(numFailures)); |
| 295 |
origin.retry(numRetries).subscribe(observer); |
| 296 |
|
| 297 |
InOrder inOrder = inOrder(observer); |
| 298 |
// should show 2 attempts (first time fail, second time (1st retry) fail) |
| 299 |
inOrder.verify(observer, times(1 + numRetries)).onNext("beginningEveryTime"); |
| 300 |
// should only retry once, fail again and emit onError |
| 301 |
inOrder.verify(observer, times(1)).onError(any(RuntimeException.class)); |
| 302 |
// no success |
| 303 |
inOrder.verify(observer, never()).onNext("onSuccessOnly"); |
| 304 |
inOrder.verify(observer, never()).onComplete(); |
| 305 |
inOrder.verifyNoMoreInteractions(); |
| 306 |
} |
| 307 |
|
| 308 |
@Test |
| 309 |
public void retrySuccess() { |
| 310 |
int numFailures = 1; |
| 311 |
Observer<String> observer = TestHelper.mockObserver(); |
| 312 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(numFailures)); |
| 313 |
origin.retry(3).subscribe(observer); |
| 314 |
|
| 315 |
InOrder inOrder = inOrder(observer); |
| 316 |
// should show 3 attempts |
| 317 |
inOrder.verify(observer, times(1 + numFailures)).onNext("beginningEveryTime"); |
| 318 |
// should have no errors |
| 319 |
inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 320 |
// should have a single success |
| 321 |
inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 322 |
// should have a single successful onComplete |
| 323 |
inOrder.verify(observer, times(1)).onComplete(); |
| 324 |
inOrder.verifyNoMoreInteractions(); |
| 325 |
} |
| 326 |
|
| 327 |
@Test |
| 328 |
public void infiniteRetry() { |
| 329 |
int numFailures = 20; |
| 330 |
Observer<String> observer = TestHelper.mockObserver(); |
| 331 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(numFailures)); |
| 332 |
origin.retry().subscribe(observer); |
| 333 |
|
| 334 |
InOrder inOrder = inOrder(observer); |
| 335 |
// should show 3 attempts |
| 336 |
inOrder.verify(observer, times(1 + numFailures)).onNext("beginningEveryTime"); |
| 337 |
// should have no errors |
| 338 |
inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 339 |
// should have a single success |
| 340 |
inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 341 |
// should have a single successful onComplete |
| 342 |
inOrder.verify(observer, times(1)).onComplete(); |
| 343 |
inOrder.verifyNoMoreInteractions(); |
| 344 |
} |
| 345 |
|
| 346 |
/* |
| 347 |
* Checks in a simple and synchronous way that retry resubscribes |
| 348 |
* after error. This test fails against 0.16.1-0.17.4, hangs on 0.17.5 and |
| 349 |
* passes in 0.17.6 thanks to fix for issue #1027. |
| 350 |
*/ |
| 351 |
@SuppressWarnings("unchecked") |
| 352 |
@Test |
| 353 |
public void retrySubscribesAgainAfterError() throws Throwable { |
| 354 |
|
| 355 |
// record emitted values with this action |
| 356 |
Consumer<Integer> record = mock(Consumer.class); |
| 357 |
InOrder inOrder = inOrder(record); |
| 358 |
|
| 359 |
// always throw an exception with this action |
| 360 |
Consumer<Integer> throwException = mock(Consumer.class); |
| 361 |
doThrow(new RuntimeException()).when(throwException).accept(Mockito.anyInt()); |
| 362 |
|
| 363 |
// create a retrying Observable based on a PublishSubject |
| 364 |
PublishSubject<Integer> subject = PublishSubject.create(); |
| 365 |
subject |
| 366 |
// record item |
| 367 |
.doOnNext(record) |
| 368 |
// throw a RuntimeException |
| 369 |
.doOnNext(throwException) |
| 370 |
// retry on error |
| 371 |
.retry() |
| 372 |
// subscribe and ignore |
| 373 |
.subscribe(); |
| 374 |
|
| 375 |
inOrder.verifyNoMoreInteractions(); |
| 376 |
|
| 377 |
subject.onNext(1); |
| 378 |
inOrder.verify(record).accept(1); |
| 379 |
|
| 380 |
subject.onNext(2); |
| 381 |
inOrder.verify(record).accept(2); |
| 382 |
|
| 383 |
subject.onNext(3); |
| 384 |
inOrder.verify(record).accept(3); |
| 385 |
|
| 386 |
inOrder.verifyNoMoreInteractions(); |
| 387 |
} |
| 388 |
|
| 389 |
public static class FuncWithErrors implements ObservableSource<String> { |
| 390 |
|
| 391 |
private final int numFailures; |
| 392 |
private final AtomicInteger count = new AtomicInteger(0); |
| 393 |
|
| 394 |
FuncWithErrors(int count) { |
| 395 |
this.numFailures = count; |
| 396 |
} |
| 397 |
|
| 398 |
@Override |
| 399 |
public void subscribe(final Observer<? super String> o) { |
| 400 |
o.onSubscribe(Disposable.empty()); |
| 401 |
o.onNext("beginningEveryTime"); |
| 402 |
int i = count.getAndIncrement(); |
| 403 |
if (i < numFailures) { |
| 404 |
o.onError(new RuntimeException("forced failure: " + (i + 1))); |
| 405 |
} else { |
| 406 |
o.onNext("onSuccessOnly"); |
| 407 |
o.onComplete(); |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
@Test |
| 413 |
public void unsubscribeFromRetry() { |
| 414 |
PublishSubject<Integer> subject = PublishSubject.create(); |
| 415 |
final AtomicInteger count = new AtomicInteger(0); |
| 416 |
Disposable sub = subject.retry().subscribe(new Consumer<Integer>() { |
| 417 |
@Override |
| 418 |
public void accept(Integer n) { |
| 419 |
count.incrementAndGet(); |
| 420 |
} |
| 421 |
}); |
| 422 |
subject.onNext(1); |
| 423 |
sub.dispose(); |
| 424 |
subject.onNext(2); |
| 425 |
assertEquals(1, count.get()); |
| 426 |
} |
| 427 |
|
| 428 |
@Test |
| 429 |
public void retryAllowsSubscriptionAfterAllSubscriptionsUnsubscribed() throws InterruptedException { |
| 430 |
final AtomicInteger subsCount = new AtomicInteger(0); |
| 431 |
ObservableSource<String> onSubscribe = new ObservableSource<String>() { |
| 432 |
@Override |
| 433 |
public void subscribe(Observer<? super String> observer) { |
| 434 |
subsCount.incrementAndGet(); |
| 435 |
observer.onSubscribe(Disposable.fromRunnable(new Runnable() { |
| 436 |
@Override |
| 437 |
public void run() { |
| 438 |
subsCount.decrementAndGet(); |
| 439 |
} |
| 440 |
})); |
| 441 |
|
| 442 |
} |
| 443 |
}; |
| 444 |
Observable<String> stream = Observable.unsafeCreate(onSubscribe); |
| 445 |
Observable<String> streamWithRetry = stream.retry(); |
| 446 |
Disposable sub = streamWithRetry.subscribe(); |
| 447 |
assertEquals(1, subsCount.get()); |
| 448 |
sub.dispose(); |
| 449 |
assertEquals(0, subsCount.get()); |
| 450 |
streamWithRetry.subscribe(); |
| 451 |
assertEquals(1, subsCount.get()); |
| 452 |
} |
| 453 |
|
| 454 |
@Test |
| 455 |
public void sourceObservableCallsUnsubscribe() throws InterruptedException { |
| 456 |
final AtomicInteger subsCount = new AtomicInteger(0); |
| 457 |
|
| 458 |
final TestObserver<String> to = new TestObserver<>(); |
| 459 |
|
| 460 |
ObservableSource<String> onSubscribe = new ObservableSource<String>() { |
| 461 |
@Override |
| 462 |
public void subscribe(Observer<? super String> observer) { |
| 463 |
BooleanSubscription bs = new BooleanSubscription(); |
| 464 |
// if isUnsubscribed is true that means we have a bug such as |
| 465 |
// https://github.com/ReactiveX/RxJava/issues/1024 |
| 466 |
if (!bs.isCancelled()) { |
| 467 |
subsCount.incrementAndGet(); |
| 468 |
observer.onError(new RuntimeException("failed")); |
| 469 |
// it unsubscribes the child directly |
| 470 |
// this simulates various error/completion scenarios that could occur |
| 471 |
// or just a source that proactively triggers cleanup |
| 472 |
// FIXME can't unsubscribe child |
| 473 |
// s.unsubscribe(); |
| 474 |
bs.cancel(); |
| 475 |
} else { |
| 476 |
observer.onError(new RuntimeException()); |
| 477 |
} |
| 478 |
} |
| 479 |
}; |
| 480 |
|
| 481 |
Observable.unsafeCreate(onSubscribe).retry(3).subscribe(to); |
| 482 |
assertEquals(4, subsCount.get()); // 1 + 3 retries |
| 483 |
} |
| 484 |
|
| 485 |
@Test |
| 486 |
public void sourceObservableRetry1() throws InterruptedException { |
| 487 |
final AtomicInteger subsCount = new AtomicInteger(0); |
| 488 |
|
| 489 |
final TestObserver<String> to = new TestObserver<>(); |
| 490 |
|
| 491 |
ObservableSource<String> onSubscribe = new ObservableSource<String>() { |
| 492 |
@Override |
| 493 |
public void subscribe(Observer<? super String> observer) { |
| 494 |
observer.onSubscribe(Disposable.empty()); |
| 495 |
subsCount.incrementAndGet(); |
| 496 |
observer.onError(new RuntimeException("failed")); |
| 497 |
} |
| 498 |
}; |
| 499 |
|
| 500 |
Observable.unsafeCreate(onSubscribe).retry(1).subscribe(to); |
| 501 |
assertEquals(2, subsCount.get()); |
| 502 |
} |
| 503 |
|
| 504 |
@Test |
| 505 |
public void sourceObservableRetry0() throws InterruptedException { |
| 506 |
final AtomicInteger subsCount = new AtomicInteger(0); |
| 507 |
|
| 508 |
final TestObserver<String> to = new TestObserver<>(); |
| 509 |
|
| 510 |
ObservableSource<String> onSubscribe = new ObservableSource<String>() { |
| 511 |
@Override |
| 512 |
public void subscribe(Observer<? super String> observer) { |
| 513 |
observer.onSubscribe(Disposable.empty()); |
| 514 |
subsCount.incrementAndGet(); |
| 515 |
observer.onError(new RuntimeException("failed")); |
| 516 |
} |
| 517 |
}; |
| 518 |
|
| 519 |
Observable.unsafeCreate(onSubscribe).retry(0).subscribe(to); |
| 520 |
assertEquals(1, subsCount.get()); |
| 521 |
} |
| 522 |
|
| 523 |
static final class SlowObservable implements ObservableSource<Long> { |
| 524 |
|
| 525 |
final AtomicInteger efforts = new AtomicInteger(0); |
| 526 |
final AtomicInteger active = new AtomicInteger(0), maxActive = new AtomicInteger(0); |
| 527 |
final AtomicInteger nextBeforeFailure; |
| 528 |
|
| 529 |
final String context; |
| 530 |
|
| 531 |
private final int emitDelay; |
| 532 |
|
| 533 |
SlowObservable(int emitDelay, int countNext, String context) { |
| 534 |
this.emitDelay = emitDelay; |
| 535 |
this.nextBeforeFailure = new AtomicInteger(countNext); |
| 536 |
this.context = context; |
| 537 |
} |
| 538 |
|
| 539 |
@Override |
| 540 |
public void subscribe(final Observer<? super Long> observer) { |
| 541 |
final AtomicBoolean terminate = new AtomicBoolean(false); |
| 542 |
observer.onSubscribe(Disposable.fromRunnable(new Runnable() { |
| 543 |
@Override |
| 544 |
public void run() { |
| 545 |
terminate.set(true); |
| 546 |
active.decrementAndGet(); |
| 547 |
} |
| 548 |
})); |
| 549 |
efforts.getAndIncrement(); |
| 550 |
active.getAndIncrement(); |
| 551 |
maxActive.set(Math.max(active.get(), maxActive.get())); |
| 552 |
final Thread thread = new Thread(context) { |
| 553 |
@Override |
| 554 |
public void run() { |
| 555 |
long nr = 0; |
| 556 |
try { |
| 557 |
while (!terminate.get()) { |
| 558 |
Thread.sleep(emitDelay); |
| 559 |
if (nextBeforeFailure.getAndDecrement() > 0) { |
| 560 |
observer.onNext(nr++); |
| 561 |
} else { |
| 562 |
active.decrementAndGet(); |
| 563 |
observer.onError(new RuntimeException("expected-failed")); |
| 564 |
break; |
| 565 |
} |
| 566 |
} |
| 567 |
} catch (InterruptedException t) { |
| 568 |
} |
| 569 |
} |
| 570 |
}; |
| 571 |
thread.start(); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
/** Observer for listener on separate thread. */ |
| 576 |
static final class AsyncObserver<T> extends DefaultObserver<T> { |
| 577 |
|
| 578 |
protected CountDownLatch latch = new CountDownLatch(1); |
| 579 |
|
| 580 |
protected Observer<T> target; |
| 581 |
|
| 582 |
/** |
| 583 |
* Wrap existing Observer. |
| 584 |
* @param target the target nbp subscriber |
| 585 |
*/ |
| 586 |
AsyncObserver(Observer<T> target) { |
| 587 |
this.target = target; |
| 588 |
} |
| 589 |
|
| 590 |
/** Wait. */ |
| 591 |
public void await() { |
| 592 |
try { |
| 593 |
latch.await(); |
| 594 |
} catch (InterruptedException e) { |
| 595 |
fail("Test interrupted"); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
// Observer implementation |
| 600 |
|
| 601 |
@Override |
| 602 |
public void onComplete() { |
| 603 |
target.onComplete(); |
| 604 |
latch.countDown(); |
| 605 |
} |
| 606 |
|
| 607 |
@Override |
| 608 |
public void onError(Throwable t) { |
| 609 |
target.onError(t); |
| 610 |
latch.countDown(); |
| 611 |
} |
| 612 |
|
| 613 |
@Override |
| 614 |
public void onNext(T v) { |
| 615 |
target.onNext(v); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
@Test |
| 620 |
public void unsubscribeAfterError() { |
| 621 |
|
| 622 |
Observer<Long> observer = TestHelper.mockObserver(); |
| 623 |
|
| 624 |
// Observable that always fails after 100ms |
| 625 |
SlowObservable so = new SlowObservable(100, 0, "testUnsubscribeAfterError"); |
| 626 |
Observable<Long> o = Observable.unsafeCreate(so).retry(5); |
| 627 |
|
| 628 |
AsyncObserver<Long> async = new AsyncObserver<>(observer); |
| 629 |
|
| 630 |
o.subscribe(async); |
| 631 |
|
| 632 |
async.await(); |
| 633 |
|
| 634 |
InOrder inOrder = inOrder(observer); |
| 635 |
// Should fail once |
| 636 |
inOrder.verify(observer, times(1)).onError(any(Throwable.class)); |
| 637 |
inOrder.verify(observer, never()).onComplete(); |
| 638 |
|
| 639 |
assertEquals("Start 6 threads, retry 5 then fail on 6", 6, so.efforts.get()); |
| 640 |
assertEquals("Only 1 active subscription", 1, so.maxActive.get()); |
| 641 |
} |
| 642 |
|
| 643 |
@Test |
| 644 |
public void timeoutWithRetry() { |
| 645 |
|
| 646 |
Observer<Long> observer = TestHelper.mockObserver(); |
| 647 |
|
| 648 |
// Observable that sends every 100ms (timeout fails instead) |
| 649 |
SlowObservable so = new SlowObservable(100, 10, "testTimeoutWithRetry"); |
| 650 |
Observable<Long> o = Observable.unsafeCreate(so).timeout(80, TimeUnit.MILLISECONDS).retry(5); |
| 651 |
|
| 652 |
AsyncObserver<Long> async = new AsyncObserver<>(observer); |
| 653 |
|
| 654 |
o.subscribe(async); |
| 655 |
|
| 656 |
async.await(); |
| 657 |
|
| 658 |
InOrder inOrder = inOrder(observer); |
| 659 |
// Should fail once |
| 660 |
inOrder.verify(observer, times(1)).onError(any(Throwable.class)); |
| 661 |
inOrder.verify(observer, never()).onComplete(); |
| 662 |
|
| 663 |
assertEquals("Start 6 threads, retry 5 then fail on 6", 6, so.efforts.get()); |
| 664 |
} |
| 665 |
|
| 666 |
@Test |
| 667 |
public void retryWithBackpressure() throws InterruptedException { |
| 668 |
final int NUM_LOOPS = 1; |
| 669 |
for (int j = 0; j < NUM_LOOPS; j++) { |
| 670 |
final int NUM_RETRIES = Flowable.bufferSize() * 2; |
| 671 |
for (int i = 0; i < 400; i++) { |
| 672 |
Observer<String> observer = TestHelper.mockObserver(); |
| 673 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(NUM_RETRIES)); |
| 674 |
TestObserver<String> to = new TestObserver<>(observer); |
| 675 |
origin.retry().observeOn(Schedulers.computation()).subscribe(to); |
| 676 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 677 |
|
| 678 |
InOrder inOrder = inOrder(observer); |
| 679 |
// should have no errors |
| 680 |
verify(observer, never()).onError(any(Throwable.class)); |
| 681 |
// should show NUM_RETRIES attempts |
| 682 |
inOrder.verify(observer, times(NUM_RETRIES + 1)).onNext("beginningEveryTime"); |
| 683 |
// should have a single success |
| 684 |
inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 685 |
// should have a single successful onComplete |
| 686 |
inOrder.verify(observer, times(1)).onComplete(); |
| 687 |
inOrder.verifyNoMoreInteractions(); |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
@Test |
| 693 |
public void retryWithBackpressureParallel() throws InterruptedException { |
| 694 |
final int NUM_LOOPS = 1; |
| 695 |
final int NUM_RETRIES = Flowable.bufferSize() * 2; |
| 696 |
int ncpu = Runtime.getRuntime().availableProcessors(); |
| 697 |
ExecutorService exec = Executors.newFixedThreadPool(Math.max(ncpu / 2, 2)); |
| 698 |
try { |
| 699 |
for (int r = 0; r < NUM_LOOPS; r++) { |
| 700 |
if (r % 10 == 0) { |
| 701 |
System.out.println("testRetryWithBackpressureParallelLoop -> " + r); |
| 702 |
} |
| 703 |
|
| 704 |
final AtomicInteger timeouts = new AtomicInteger(); |
| 705 |
final Map<Integer, List<String>> data = new ConcurrentHashMap<>(); |
| 706 |
|
| 707 |
int m = 5000; |
| 708 |
final CountDownLatch cdl = new CountDownLatch(m); |
| 709 |
for (int i = 0; i < m; i++) { |
| 710 |
final int j = i; |
| 711 |
exec.execute(new Runnable() { |
| 712 |
@Override |
| 713 |
public void run() { |
| 714 |
final AtomicInteger nexts = new AtomicInteger(); |
| 715 |
try { |
| 716 |
Observable<String> origin = Observable.unsafeCreate(new FuncWithErrors(NUM_RETRIES)); |
| 717 |
TestObserverEx<String> to = new TestObserverEx<>(); |
| 718 |
origin.retry() |
| 719 |
.observeOn(Schedulers.computation()).subscribe(to); |
| 720 |
to.awaitDone(2500, TimeUnit.MILLISECONDS); |
| 721 |
List<String> onNextEvents = new ArrayList<>(to.values()); |
| 722 |
if (onNextEvents.size() != NUM_RETRIES + 2) { |
| 723 |
for (Throwable t : to.errors()) { |
| 724 |
onNextEvents.add(t.toString()); |
| 725 |
} |
| 726 |
for (long err = to.completions(); err != 0; err--) { |
| 727 |
onNextEvents.add("onComplete"); |
| 728 |
} |
| 729 |
data.put(j, onNextEvents); |
| 730 |
} |
| 731 |
} catch (Throwable t) { |
| 732 |
timeouts.incrementAndGet(); |
| 733 |
System.out.println(j + " | " + cdl.getCount() + " !!! " + nexts.get()); |
| 734 |
} |
| 735 |
cdl.countDown(); |
| 736 |
} |
| 737 |
}); |
| 738 |
} |
| 739 |
cdl.await(); |
| 740 |
assertEquals(0, timeouts.get()); |
| 741 |
if (data.size() > 0) { |
| 742 |
fail("Data content mismatch: " + allSequenceFrequency(data)); |
| 743 |
} |
| 744 |
} |
| 745 |
} finally { |
| 746 |
exec.shutdown(); |
| 747 |
} |
| 748 |
} |
| 749 |
static <T> StringBuilder allSequenceFrequency(Map<Integer, List<T>> its) { |
| 750 |
StringBuilder b = new StringBuilder(); |
| 751 |
for (Map.Entry<Integer, List<T>> e : its.entrySet()) { |
| 752 |
if (b.length() > 0) { |
| 753 |
b.append(", "); |
| 754 |
} |
| 755 |
b.append(e.getKey()).append("={"); |
| 756 |
b.append(sequenceFrequency(e.getValue())); |
| 757 |
b.append("}"); |
| 758 |
} |
| 759 |
return b; |
| 760 |
} |
| 761 |
static <T> StringBuilder sequenceFrequency(Iterable<T> it) { |
| 762 |
StringBuilder sb = new StringBuilder(); |
| 763 |
|
| 764 |
Object prev = null; |
| 765 |
int cnt = 0; |
| 766 |
|
| 767 |
for (Object curr : it) { |
| 768 |
if (sb.length() > 0) { |
| 769 |
if (!curr.equals(prev)) { |
| 770 |
if (cnt > 1) { |
| 771 |
sb.append(" x ").append(cnt); |
| 772 |
cnt = 1; |
| 773 |
} |
| 774 |
sb.append(", "); |
| 775 |
sb.append(curr); |
| 776 |
} else { |
| 777 |
cnt++; |
| 778 |
} |
| 779 |
} else { |
| 780 |
sb.append(curr); |
| 781 |
cnt++; |
| 782 |
} |
| 783 |
prev = curr; |
| 784 |
} |
| 785 |
if (cnt > 1) { |
| 786 |
sb.append(" x ").append(cnt); |
| 787 |
} |
| 788 |
|
| 789 |
return sb; |
| 790 |
} |
| 791 |
|
| 792 |
@Test |
| 793 |
public void issue1900() throws InterruptedException { |
| 794 |
Observer<String> observer = TestHelper.mockObserver(); |
| 795 |
final int NUM_MSG = 1034; |
| 796 |
final AtomicInteger count = new AtomicInteger(); |
| 797 |
|
| 798 |
Observable<String> origin = Observable.range(0, NUM_MSG) |
| 799 |
.map(new Function<Integer, String>() { |
| 800 |
@Override |
| 801 |
public String apply(Integer t1) { |
| 802 |
return "msg: " + count.incrementAndGet(); |
| 803 |
} |
| 804 |
}); |
| 805 |
|
| 806 |
origin.retry() |
| 807 |
.groupBy(new Function<String, String>() { |
| 808 |
@Override |
| 809 |
public String apply(String t1) { |
| 810 |
return t1; |
| 811 |
} |
| 812 |
}) |
| 813 |
.flatMap(new Function<GroupedObservable<String, String>, Observable<String>>() { |
| 814 |
@Override |
| 815 |
public Observable<String> apply(GroupedObservable<String, String> t1) { |
| 816 |
return t1.take(1); |
| 817 |
} |
| 818 |
}) |
| 819 |
.subscribe(new TestObserver<>(observer)); |
| 820 |
|
| 821 |
InOrder inOrder = inOrder(observer); |
| 822 |
// should show 3 attempts |
| 823 |
inOrder.verify(observer, times(NUM_MSG)).onNext(any(java.lang.String.class)); |
| 824 |
// // should have no errors |
| 825 |
inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 826 |
// should have a single success |
| 827 |
//inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 828 |
// should have a single successful onComplete |
| 829 |
inOrder.verify(observer, times(1)).onComplete(); |
| 830 |
inOrder.verifyNoMoreInteractions(); |
| 831 |
} |
| 832 |
|
| 833 |
@Test |
| 834 |
public void issue1900SourceNotSupportingBackpressure() { |
| 835 |
Observer<String> observer = TestHelper.mockObserver(); |
| 836 |
final int NUM_MSG = 1034; |
| 837 |
final AtomicInteger count = new AtomicInteger(); |
| 838 |
|
| 839 |
Observable<String> origin = Observable.unsafeCreate(new ObservableSource<String>() { |
| 840 |
|
| 841 |
@Override |
| 842 |
public void subscribe(Observer<? super String> o) { |
| 843 |
o.onSubscribe(Disposable.empty()); |
| 844 |
for (int i = 0; i < NUM_MSG; i++) { |
| 845 |
o.onNext("msg:" + count.incrementAndGet()); |
| 846 |
} |
| 847 |
o.onComplete(); |
| 848 |
} |
| 849 |
}); |
| 850 |
|
| 851 |
origin.retry() |
| 852 |
.groupBy(new Function<String, String>() { |
| 853 |
@Override |
| 854 |
public String apply(String t1) { |
| 855 |
return t1; |
| 856 |
} |
| 857 |
}) |
| 858 |
.flatMap(new Function<GroupedObservable<String, String>, Observable<String>>() { |
| 859 |
@Override |
| 860 |
public Observable<String> apply(GroupedObservable<String, String> t1) { |
| 861 |
return t1.take(1); |
| 862 |
} |
| 863 |
}) |
| 864 |
.subscribe(new TestObserver<>(observer)); |
| 865 |
|
| 866 |
InOrder inOrder = inOrder(observer); |
| 867 |
// should show 3 attempts |
| 868 |
inOrder.verify(observer, times(NUM_MSG)).onNext(any(java.lang.String.class)); |
| 869 |
// // should have no errors |
| 870 |
inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 871 |
// should have a single success |
| 872 |
//inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 873 |
// should have a single successful onComplete |
| 874 |
inOrder.verify(observer, times(1)).onComplete(); |
| 875 |
inOrder.verifyNoMoreInteractions(); |
| 876 |
} |
| 877 |
|
| 878 |
@Test |
| 879 |
public void retryPredicate() { |
| 880 |
Observable.just(1).concatWith(Observable.<Integer>error(new TestException())) |
| 881 |
.retry(new Predicate<Throwable>() { |
| 882 |
@Override |
| 883 |
public boolean test(Throwable v) throws Exception { |
| 884 |
return true; |
| 885 |
} |
| 886 |
}) |
| 887 |
.take(5) |
| 888 |
.test() |
| 889 |
.assertResult(1, 1, 1, 1, 1); |
| 890 |
} |
| 891 |
|
| 892 |
@Test |
| 893 |
public void retryUntil() { |
| 894 |
Observable.just(1).concatWith(Observable.<Integer>error(new TestException())) |
| 895 |
.retryUntil(new BooleanSupplier() { |
| 896 |
@Override |
| 897 |
public boolean getAsBoolean() throws Exception { |
| 898 |
return false; |
| 899 |
} |
| 900 |
}) |
| 901 |
.take(5) |
| 902 |
.test() |
| 903 |
.assertResult(1, 1, 1, 1, 1); |
| 904 |
} |
| 905 |
|
| 906 |
@Test |
| 907 |
public void retryLongPredicateInvalid() { |
| 908 |
try { |
| 909 |
Observable.just(1).retry(-99, new Predicate<Throwable>() { |
| 910 |
@Override |
| 911 |
public boolean test(Throwable e) throws Exception { |
| 912 |
return true; |
| 913 |
} |
| 914 |
}); |
| 915 |
fail("Should have thrown"); |
| 916 |
} catch (IllegalArgumentException ex) { |
| 917 |
assertEquals("times >= 0 required but it was -99", ex.getMessage()); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
@Test |
| 922 |
public void shouldDisposeInnerObservable() { |
| 923 |
final PublishSubject<Object> subject = PublishSubject.create(); |
| 924 |
final Disposable disposable = Observable.error(new RuntimeException("Leak")) |
| 925 |
.retryWhen(new Function<Observable<Throwable>, ObservableSource<Object>>() { |
| 926 |
@Override |
| 927 |
public ObservableSource<Object> apply(Observable<Throwable> errors) throws Exception { |
| 928 |
return errors.switchMap(new Function<Throwable, ObservableSource<Object>>() { |
| 929 |
@Override |
| 930 |
public ObservableSource<Object> apply(Throwable ignore) throws Exception { |
| 931 |
return subject; |
| 932 |
} |
| 933 |
}); |
| 934 |
} |
| 935 |
}) |
| 936 |
.subscribe(); |
| 937 |
|
| 938 |
assertTrue(subject.hasObservers()); |
| 939 |
disposable.dispose(); |
| 940 |
assertFalse(subject.hasObservers()); |
| 941 |
} |
| 942 |
|
| 943 |
@Test |
| 944 |
public void noCancelPreviousRetry() { |
| 945 |
final AtomicInteger counter = new AtomicInteger(); |
| 946 |
|
| 947 |
final AtomicInteger times = new AtomicInteger(); |
| 948 |
|
| 949 |
Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() { |
| 950 |
@Override |
| 951 |
public ObservableSource<Integer> get() throws Exception { |
| 952 |
if (times.getAndIncrement() < 4) { |
| 953 |
return Observable.error(new TestException()); |
| 954 |
} |
| 955 |
return Observable.just(1); |
| 956 |
} |
| 957 |
}) |
| 958 |
.doOnDispose(new Action() { |
| 959 |
@Override |
| 960 |
public void run() throws Exception { |
| 961 |
counter.getAndIncrement(); |
| 962 |
} |
| 963 |
}); |
| 964 |
|
| 965 |
source.retry(5) |
| 966 |
.test() |
| 967 |
.assertResult(1); |
| 968 |
|
| 969 |
assertEquals(0, counter.get()); |
| 970 |
} |
| 971 |
|
| 972 |
@Test |
| 973 |
public void noCancelPreviousRetryWhile() { |
| 974 |
final AtomicInteger counter = new AtomicInteger(); |
| 975 |
|
| 976 |
final AtomicInteger times = new AtomicInteger(); |
| 977 |
|
| 978 |
Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() { |
| 979 |
@Override |
| 980 |
public ObservableSource<Integer> get() throws Exception { |
| 981 |
if (times.getAndIncrement() < 4) { |
| 982 |
return Observable.error(new TestException()); |
| 983 |
} |
| 984 |
return Observable.just(1); |
| 985 |
} |
| 986 |
}) |
| 987 |
.doOnDispose(new Action() { |
| 988 |
@Override |
| 989 |
public void run() throws Exception { |
| 990 |
counter.getAndIncrement(); |
| 991 |
} |
| 992 |
}); |
| 993 |
|
| 994 |
source.retry(5, Functions.alwaysTrue()) |
| 995 |
.test() |
| 996 |
.assertResult(1); |
| 997 |
|
| 998 |
assertEquals(0, counter.get()); |
| 999 |
} |
| 1000 |
|
| 1001 |
@Test |
| 1002 |
public void noCancelPreviousRetryWhile2() { |
| 1003 |
final AtomicInteger counter = new AtomicInteger(); |
| 1004 |
|
| 1005 |
final AtomicInteger times = new AtomicInteger(); |
| 1006 |
|
| 1007 |
Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() { |
| 1008 |
@Override |
| 1009 |
public ObservableSource<Integer> get() throws Exception { |
| 1010 |
if (times.getAndIncrement() < 4) { |
| 1011 |
return Observable.error(new TestException()); |
| 1012 |
} |
| 1013 |
return Observable.just(1); |
| 1014 |
} |
| 1015 |
}) |
| 1016 |
.doOnDispose(new Action() { |
| 1017 |
@Override |
| 1018 |
public void run() throws Exception { |
| 1019 |
counter.getAndIncrement(); |
| 1020 |
} |
| 1021 |
}); |
| 1022 |
|
| 1023 |
source.retry(new BiPredicate<Integer, Throwable>() { |
| 1024 |
@Override |
| 1025 |
public boolean test(Integer a, Throwable b) throws Exception { |
| 1026 |
return a < 5; |
| 1027 |
} |
| 1028 |
}) |
| 1029 |
.test() |
| 1030 |
.assertResult(1); |
| 1031 |
|
| 1032 |
assertEquals(0, counter.get()); |
| 1033 |
} |
| 1034 |
|
| 1035 |
@Test |
| 1036 |
public void noCancelPreviousRetryUntil() { |
| 1037 |
final AtomicInteger counter = new AtomicInteger(); |
| 1038 |
|
| 1039 |
final AtomicInteger times = new AtomicInteger(); |
| 1040 |
|
| 1041 |
Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() { |
| 1042 |
@Override |
| 1043 |
public ObservableSource<Integer> get() throws Exception { |
| 1044 |
if (times.getAndIncrement() < 4) { |
| 1045 |
return Observable.error(new TestException()); |
| 1046 |
} |
| 1047 |
return Observable.just(1); |
| 1048 |
} |
| 1049 |
}) |
| 1050 |
.doOnDispose(new Action() { |
| 1051 |
@Override |
| 1052 |
public void run() throws Exception { |
| 1053 |
counter.getAndIncrement(); |
| 1054 |
} |
| 1055 |
}); |
| 1056 |
|
| 1057 |
source.retryUntil(new BooleanSupplier() { |
| 1058 |
@Override |
| 1059 |
public boolean getAsBoolean() throws Exception { |
| 1060 |
return false; |
| 1061 |
} |
| 1062 |
}) |
| 1063 |
.test() |
| 1064 |
.assertResult(1); |
| 1065 |
|
| 1066 |
assertEquals(0, counter.get()); |
| 1067 |
} |
| 1068 |
|
| 1069 |
@Test |
| 1070 |
public void noCancelPreviousRepeatWhen() { |
| 1071 |
final AtomicInteger counter = new AtomicInteger(); |
| 1072 |
|
| 1073 |
final AtomicInteger times = new AtomicInteger(); |
| 1074 |
|
| 1075 |
Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() { |
| 1076 |
@Override |
| 1077 |
public ObservableSource<Integer> get() throws Exception { |
| 1078 |
if (times.get() < 4) { |
| 1079 |
return Observable.error(new TestException()); |
| 1080 |
} |
| 1081 |
return Observable.just(1); |
| 1082 |
} |
| 1083 |
}).doOnDispose(new Action() { |
| 1084 |
@Override |
| 1085 |
public void run() throws Exception { |
| 1086 |
counter.getAndIncrement(); |
| 1087 |
} |
| 1088 |
}); |
| 1089 |
|
| 1090 |
source.retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() { |
| 1091 |
@Override |
| 1092 |
public ObservableSource<?> apply(Observable<Throwable> e) throws Exception { |
| 1093 |
return e.takeWhile(new Predicate<Object>() { |
| 1094 |
@Override |
| 1095 |
public boolean test(Object v) throws Exception { |
| 1096 |
return times.getAndIncrement() < 4; |
| 1097 |
} |
| 1098 |
}); |
| 1099 |
} |
| 1100 |
}) |
| 1101 |
.test() |
| 1102 |
.assertResult(1); |
| 1103 |
|
| 1104 |
assertEquals(0, counter.get()); |
| 1105 |
} |
| 1106 |
|
| 1107 |
@Test |
| 1108 |
public void noCancelPreviousRepeatWhen2() { |
| 1109 |
final AtomicInteger counter = new AtomicInteger(); |
| 1110 |
|
| 1111 |
final AtomicInteger times = new AtomicInteger(); |
| 1112 |
|
| 1113 |
Observable<Integer> source = Observable.<Integer>error(new TestException()).doOnDispose(new Action() { |
| 1114 |
@Override |
| 1115 |
public void run() throws Exception { |
| 1116 |
counter.getAndIncrement(); |
| 1117 |
} |
| 1118 |
}); |
| 1119 |
|
| 1120 |
source.retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() { |
| 1121 |
@Override |
| 1122 |
public ObservableSource<?> apply(Observable<Throwable> e) throws Exception { |
| 1123 |
return e.takeWhile(new Predicate<Object>() { |
| 1124 |
@Override |
| 1125 |
public boolean test(Object v) throws Exception { |
| 1126 |
return times.getAndIncrement() < 4; |
| 1127 |
} |
| 1128 |
}); |
| 1129 |
} |
| 1130 |
}) |
| 1131 |
.test() |
| 1132 |
.assertResult(); |
| 1133 |
|
| 1134 |
assertEquals(0, counter.get()); |
| 1135 |
} |
| 1136 |
|
| 1137 |
@Test |
| 1138 |
public void repeatFloodNoSubscriptionError() { |
| 1139 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1140 |
|
| 1141 |
final TestException error = new TestException(); |
| 1142 |
|
| 1143 |
try { |
| 1144 |
final PublishSubject<Integer> source = PublishSubject.create(); |
| 1145 |
final PublishSubject<Integer> signaller = PublishSubject.create(); |
| 1146 |
|
| 1147 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1148 |
|
| 1149 |
TestObserver<Integer> to = source.take(1) |
| 1150 |
.map(new Function<Integer, Integer>() { |
| 1151 |
@Override |
| 1152 |
public Integer apply(Integer v) throws Exception { |
| 1153 |
throw error; |
| 1154 |
} |
| 1155 |
}) |
| 1156 |
.retryWhen(new Function<Observable<Throwable>, ObservableSource<Integer>>() { |
| 1157 |
@Override |
| 1158 |
public ObservableSource<Integer> apply(Observable<Throwable> v) |
| 1159 |
throws Exception { |
| 1160 |
return signaller; |
| 1161 |
} |
| 1162 |
}).test(); |
| 1163 |
|
| 1164 |
Runnable r1 = new Runnable() { |
| 1165 |
@Override |
| 1166 |
public void run() { |
| 1167 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1168 |
source.onNext(1); |
| 1169 |
} |
| 1170 |
} |
| 1171 |
}; |
| 1172 |
Runnable r2 = new Runnable() { |
| 1173 |
@Override |
| 1174 |
public void run() { |
| 1175 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1176 |
signaller.onNext(1); |
| 1177 |
} |
| 1178 |
} |
| 1179 |
}; |
| 1180 |
|
| 1181 |
TestHelper.race(r1, r2); |
| 1182 |
|
| 1183 |
to.dispose(); |
| 1184 |
} |
| 1185 |
|
| 1186 |
if (!errors.isEmpty()) { |
| 1187 |
for (Throwable e : errors) { |
| 1188 |
e.printStackTrace(); |
| 1189 |
} |
| 1190 |
fail(errors + ""); |
| 1191 |
} |
| 1192 |
} finally { |
| 1193 |
RxJavaPlugins.reset(); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
} |