|
| 1 | +package rx.operators; |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2013 Netflix, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +import static org.mockito.Matchers.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import java.util.concurrent.atomic.AtomicInteger; |
| 22 | + |
| 23 | +import org.junit.Test; |
| 24 | +import org.mockito.InOrder; |
| 25 | + |
| 26 | +import rx.Observable; |
| 27 | +import rx.Observable.OnSubscribeFunc; |
| 28 | +import rx.Observer; |
| 29 | +import rx.Subscription; |
| 30 | +import rx.concurrency.Schedulers; |
| 31 | +import rx.subscriptions.CompositeSubscription; |
| 32 | +import rx.subscriptions.Subscriptions; |
| 33 | +import rx.util.functions.Action0; |
| 34 | + |
| 35 | +public class OperationRetry { |
| 36 | + |
| 37 | + private static final int INFINITE_RETRY = -1; |
| 38 | + |
| 39 | + public static <T> OnSubscribeFunc<T> retry(final Observable<T> observable, final int retryCount) { |
| 40 | + return new Retry<T>(observable, retryCount); |
| 41 | + } |
| 42 | + |
| 43 | + public static <T> OnSubscribeFunc<T> retry(final Observable<T> observable) { |
| 44 | + return new Retry<T>(observable, INFINITE_RETRY); |
| 45 | + } |
| 46 | + |
| 47 | + private static class Retry<T> implements OnSubscribeFunc<T> { |
| 48 | + |
| 49 | + private final Observable<T> source; |
| 50 | + private final int retryCount; |
| 51 | + private final AtomicInteger attempts = new AtomicInteger(0); |
| 52 | + private final CompositeSubscription subscription = new CompositeSubscription(); |
| 53 | + |
| 54 | + public Retry(Observable<T> source, int retryCount) { |
| 55 | + this.source = source; |
| 56 | + this.retryCount = retryCount; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Subscription onSubscribe(Observer<? super T> observer) { |
| 61 | + subscription.add(Schedulers.currentThread().schedule(attemptSubscription(observer))); |
| 62 | + return subscription; |
| 63 | + } |
| 64 | + |
| 65 | + private Action0 attemptSubscription(final Observer<? super T> observer) { |
| 66 | + return new Action0() { |
| 67 | + |
| 68 | + @Override |
| 69 | + public void call() { |
| 70 | + attempts.incrementAndGet(); |
| 71 | + source.subscribe(new Observer<T>() { |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onCompleted() { |
| 75 | + observer.onCompleted(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onError(Throwable e) { |
| 80 | + if ((retryCount == INFINITE_RETRY || attempts.get() <= retryCount) && !subscription.isUnsubscribed()) { |
| 81 | + // retry again |
| 82 | + // remove the last subscription since we have completed (so as we retry we don't build up a huge list) |
| 83 | + subscription.removeLast(); |
| 84 | + // add the new subscription and schedule a retry |
| 85 | + subscription.add(Schedulers.currentThread().schedule(attemptSubscription(observer))); |
| 86 | + } else { |
| 87 | + // give up and pass the failure |
| 88 | + observer.onError(e); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void onNext(T v) { |
| 94 | + observer.onNext(v); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + } |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + public static class UnitTest { |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testOriginFails() { |
| 108 | + @SuppressWarnings("unchecked") |
| 109 | + Observer<String> observer = mock(Observer.class); |
| 110 | + Observable<String> origin = Observable.create(new FuncWithErrors(2)); |
| 111 | + origin.subscribe(observer); |
| 112 | + |
| 113 | + InOrder inOrder = inOrder(observer); |
| 114 | + inOrder.verify(observer, times(1)).onNext("beginningEveryTime"); |
| 115 | + inOrder.verify(observer, times(1)).onError(any(RuntimeException.class)); |
| 116 | + inOrder.verify(observer, never()).onNext("onSuccessOnly"); |
| 117 | + inOrder.verify(observer, never()).onCompleted(); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testRetryFail() { |
| 122 | + int NUM_RETRIES = 1; |
| 123 | + int NUM_FAILURES = 2; |
| 124 | + @SuppressWarnings("unchecked") |
| 125 | + Observer<String> observer = mock(Observer.class); |
| 126 | + Observable<String> origin = Observable.create(new FuncWithErrors(NUM_FAILURES)); |
| 127 | + Observable.create(retry(origin, NUM_RETRIES)).subscribe(observer); |
| 128 | + |
| 129 | + InOrder inOrder = inOrder(observer); |
| 130 | + // should show 2 attempts (first time fail, second time (1st retry) fail) |
| 131 | + inOrder.verify(observer, times(1 + NUM_RETRIES)).onNext("beginningEveryTime"); |
| 132 | + // should only retry once, fail again and emit onError |
| 133 | + inOrder.verify(observer, times(1)).onError(any(RuntimeException.class)); |
| 134 | + // no success |
| 135 | + inOrder.verify(observer, never()).onNext("onSuccessOnly"); |
| 136 | + inOrder.verify(observer, never()).onCompleted(); |
| 137 | + inOrder.verifyNoMoreInteractions(); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testRetrySuccess() { |
| 142 | + int NUM_RETRIES = 3; |
| 143 | + int NUM_FAILURES = 2; |
| 144 | + @SuppressWarnings("unchecked") |
| 145 | + Observer<String> observer = mock(Observer.class); |
| 146 | + Observable<String> origin = Observable.create(new FuncWithErrors(NUM_FAILURES)); |
| 147 | + Observable.create(retry(origin, NUM_RETRIES)).subscribe(observer); |
| 148 | + |
| 149 | + InOrder inOrder = inOrder(observer); |
| 150 | + // should show 3 attempts |
| 151 | + inOrder.verify(observer, times(1 + NUM_FAILURES)).onNext("beginningEveryTime"); |
| 152 | + // should have no errors |
| 153 | + inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 154 | + // should have a single success |
| 155 | + inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 156 | + // should have a single successful onCompleted |
| 157 | + inOrder.verify(observer, times(1)).onCompleted(); |
| 158 | + inOrder.verifyNoMoreInteractions(); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testInfiniteRetry() { |
| 163 | + int NUM_FAILURES = 20; |
| 164 | + @SuppressWarnings("unchecked") |
| 165 | + Observer<String> observer = mock(Observer.class); |
| 166 | + Observable<String> origin = Observable.create(new FuncWithErrors(NUM_FAILURES)); |
| 167 | + Observable.create(retry(origin)).subscribe(observer); |
| 168 | + |
| 169 | + InOrder inOrder = inOrder(observer); |
| 170 | + // should show 3 attempts |
| 171 | + inOrder.verify(observer, times(1 + NUM_FAILURES)).onNext("beginningEveryTime"); |
| 172 | + // should have no errors |
| 173 | + inOrder.verify(observer, never()).onError(any(Throwable.class)); |
| 174 | + // should have a single success |
| 175 | + inOrder.verify(observer, times(1)).onNext("onSuccessOnly"); |
| 176 | + // should have a single successful onCompleted |
| 177 | + inOrder.verify(observer, times(1)).onCompleted(); |
| 178 | + inOrder.verifyNoMoreInteractions(); |
| 179 | + } |
| 180 | + |
| 181 | + public static class FuncWithErrors implements OnSubscribeFunc<String> { |
| 182 | + |
| 183 | + private final int numFailures; |
| 184 | + private final AtomicInteger count = new AtomicInteger(0); |
| 185 | + |
| 186 | + FuncWithErrors(int count) { |
| 187 | + this.numFailures = count; |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public Subscription onSubscribe(Observer<? super String> o) { |
| 192 | + o.onNext("beginningEveryTime"); |
| 193 | + if (count.incrementAndGet() <= numFailures) { |
| 194 | + o.onError(new RuntimeException("forced failure: " + count.get())); |
| 195 | + } else { |
| 196 | + o.onNext("onSuccessOnly"); |
| 197 | + o.onCompleted(); |
| 198 | + } |
| 199 | + return Subscriptions.empty(); |
| 200 | + } |
| 201 | + }; |
| 202 | + } |
| 203 | +} |
0 commit comments