| 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.flowable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.util.concurrent.TimeUnit; |
| 22 |
import java.util.concurrent.atomic.AtomicReference; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.mockito.Mockito; |
| 26 |
import org.reactivestreams.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.core.*; |
| 29 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 30 |
import io.reactivex.rxjava3.functions.Function; |
| 31 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 32 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 33 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 34 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 35 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 36 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 37 |
|
| 38 |
public class FlowableOnErrorResumeNextViaFunctionTest extends RxJavaTest { |
| 39 |
|
| 40 |
@Test |
| 41 |
public void resumeNextWithSynchronousExecution() { |
| 42 |
final AtomicReference<Throwable> receivedException = new AtomicReference<>(); |
| 43 |
Flowable<String> w = Flowable.unsafeCreate(new Publisher<String>() { |
| 44 |
|
| 45 |
@Override |
| 46 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 47 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 48 |
subscriber.onNext("one"); |
| 49 |
subscriber.onError(new Throwable("injected failure")); |
| 50 |
subscriber.onNext("two"); |
| 51 |
subscriber.onNext("three"); |
| 52 |
} |
| 53 |
}); |
| 54 |
|
| 55 |
Function<Throwable, Flowable<String>> resume = new Function<Throwable, Flowable<String>>() { |
| 56 |
|
| 57 |
@Override |
| 58 |
public Flowable<String> apply(Throwable t1) { |
| 59 |
receivedException.set(t1); |
| 60 |
return Flowable.just("twoResume", "threeResume"); |
| 61 |
} |
| 62 |
|
| 63 |
}; |
| 64 |
Flowable<String> flowable = w.onErrorResumeNext(resume); |
| 65 |
|
| 66 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 67 |
|
| 68 |
flowable.subscribe(subscriber); |
| 69 |
|
| 70 |
verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 71 |
verify(subscriber, times(1)).onComplete(); |
| 72 |
verify(subscriber, times(1)).onNext("one"); |
| 73 |
verify(subscriber, Mockito.never()).onNext("two"); |
| 74 |
verify(subscriber, Mockito.never()).onNext("three"); |
| 75 |
verify(subscriber, times(1)).onNext("twoResume"); |
| 76 |
verify(subscriber, times(1)).onNext("threeResume"); |
| 77 |
assertNotNull(receivedException.get()); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void resumeNextWithAsyncExecution() { |
| 82 |
final AtomicReference<Throwable> receivedException = new AtomicReference<>(); |
| 83 |
Subscription s = mock(Subscription.class); |
| 84 |
TestFlowable w = new TestFlowable(s, "one"); |
| 85 |
Function<Throwable, Flowable<String>> resume = new Function<Throwable, Flowable<String>>() { |
| 86 |
|
| 87 |
@Override |
| 88 |
public Flowable<String> apply(Throwable t1) { |
| 89 |
receivedException.set(t1); |
| 90 |
return Flowable.just("twoResume", "threeResume"); |
| 91 |
} |
| 92 |
|
| 93 |
}; |
| 94 |
Flowable<String> flowable = Flowable.unsafeCreate(w).onErrorResumeNext(resume); |
| 95 |
|
| 96 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 97 |
|
| 98 |
flowable.subscribe(subscriber); |
| 99 |
|
| 100 |
try { |
| 101 |
w.t.join(); |
| 102 |
} catch (InterruptedException e) { |
| 103 |
fail(e.getMessage()); |
| 104 |
} |
| 105 |
|
| 106 |
verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 107 |
verify(subscriber, times(1)).onComplete(); |
| 108 |
verify(subscriber, times(1)).onNext("one"); |
| 109 |
verify(subscriber, Mockito.never()).onNext("two"); |
| 110 |
verify(subscriber, Mockito.never()).onNext("three"); |
| 111 |
verify(subscriber, times(1)).onNext("twoResume"); |
| 112 |
verify(subscriber, times(1)).onNext("threeResume"); |
| 113 |
assertNotNull(receivedException.get()); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Test that when a function throws an exception this is propagated through onError. |
| 118 |
*/ |
| 119 |
@Test |
| 120 |
public void functionThrowsError() { |
| 121 |
Subscription s = mock(Subscription.class); |
| 122 |
TestFlowable w = new TestFlowable(s, "one"); |
| 123 |
Function<Throwable, Flowable<String>> resume = new Function<Throwable, Flowable<String>>() { |
| 124 |
|
| 125 |
@Override |
| 126 |
public Flowable<String> apply(Throwable t1) { |
| 127 |
throw new RuntimeException("exception from function"); |
| 128 |
} |
| 129 |
|
| 130 |
}; |
| 131 |
Flowable<String> flowable = Flowable.unsafeCreate(w).onErrorResumeNext(resume); |
| 132 |
|
| 133 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 134 |
flowable.subscribe(subscriber); |
| 135 |
|
| 136 |
try { |
| 137 |
w.t.join(); |
| 138 |
} catch (InterruptedException e) { |
| 139 |
fail(e.getMessage()); |
| 140 |
} |
| 141 |
|
| 142 |
// we should get the "one" value before the error |
| 143 |
verify(subscriber, times(1)).onNext("one"); |
| 144 |
|
| 145 |
// we should have received an onError call on the Observer since the resume function threw an exception |
| 146 |
verify(subscriber, times(1)).onError(any(Throwable.class)); |
| 147 |
verify(subscriber, times(0)).onComplete(); |
| 148 |
} |
| 149 |
|
| 150 |
@Test |
| 151 |
public void mapResumeAsyncNext() { |
| 152 |
// Trigger multiple failures |
| 153 |
Flowable<String> w = Flowable.just("one", "fail", "two", "three", "fail"); |
| 154 |
|
| 155 |
// Introduce map function that fails intermittently (Map does not prevent this when the observer is a |
| 156 |
// rx.operator incl onErrorResumeNextViaFlowable) |
| 157 |
w = w.map(new Function<String, String>() { |
| 158 |
@Override |
| 159 |
public String apply(String s) { |
| 160 |
if ("fail".equals(s)) { |
| 161 |
throw new RuntimeException("Forced Failure"); |
| 162 |
} |
| 163 |
System.out.println("BadMapper:" + s); |
| 164 |
return s; |
| 165 |
} |
| 166 |
}); |
| 167 |
|
| 168 |
Flowable<String> flowable = w.onErrorResumeNext(new Function<Throwable, Flowable<String>>() { |
| 169 |
|
| 170 |
@Override |
| 171 |
public Flowable<String> apply(Throwable t1) { |
| 172 |
return Flowable.just("twoResume", "threeResume").subscribeOn(Schedulers.computation()); |
| 173 |
} |
| 174 |
|
| 175 |
}); |
| 176 |
|
| 177 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 178 |
|
| 179 |
TestSubscriber<String> ts = new TestSubscriber<>(subscriber, Long.MAX_VALUE); |
| 180 |
flowable.subscribe(ts); |
| 181 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 182 |
|
| 183 |
verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 184 |
verify(subscriber, times(1)).onComplete(); |
| 185 |
verify(subscriber, times(1)).onNext("one"); |
| 186 |
verify(subscriber, Mockito.never()).onNext("two"); |
| 187 |
verify(subscriber, Mockito.never()).onNext("three"); |
| 188 |
verify(subscriber, times(1)).onNext("twoResume"); |
| 189 |
verify(subscriber, times(1)).onNext("threeResume"); |
| 190 |
} |
| 191 |
|
| 192 |
private static class TestFlowable implements Publisher<String> { |
| 193 |
|
| 194 |
final String[] values; |
| 195 |
Thread t; |
| 196 |
|
| 197 |
TestFlowable(Subscription s, String... values) { |
| 198 |
this.values = values; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 203 |
System.out.println("TestFlowable subscribed to ..."); |
| 204 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 205 |
t = new Thread(new Runnable() { |
| 206 |
|
| 207 |
@Override |
| 208 |
public void run() { |
| 209 |
try { |
| 210 |
System.out.println("running TestFlowable thread"); |
| 211 |
for (String s : values) { |
| 212 |
System.out.println("TestFlowable onNext: " + s); |
| 213 |
subscriber.onNext(s); |
| 214 |
} |
| 215 |
throw new RuntimeException("Forced Failure"); |
| 216 |
} catch (Throwable e) { |
| 217 |
subscriber.onError(e); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
}); |
| 222 |
System.out.println("starting TestFlowable thread"); |
| 223 |
t.start(); |
| 224 |
System.out.println("done starting TestFlowable thread"); |
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
@Test |
| 230 |
public void backpressure() { |
| 231 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 232 |
Flowable.range(0, 100000) |
| 233 |
.onErrorResumeNext(new Function<Throwable, Flowable<Integer>>() { |
| 234 |
|
| 235 |
@Override |
| 236 |
public Flowable<Integer> apply(Throwable t1) { |
| 237 |
return Flowable.just(1); |
| 238 |
} |
| 239 |
|
| 240 |
}) |
| 241 |
.observeOn(Schedulers.computation()) |
| 242 |
.map(new Function<Integer, Integer>() { |
| 243 |
int c; |
| 244 |
|
| 245 |
@Override |
| 246 |
public Integer apply(Integer t1) { |
| 247 |
if (c++ <= 1) { |
| 248 |
// slow |
| 249 |
try { |
| 250 |
Thread.sleep(500); |
| 251 |
} catch (InterruptedException e) { |
| 252 |
e.printStackTrace(); |
| 253 |
} |
| 254 |
} |
| 255 |
return t1; |
| 256 |
} |
| 257 |
|
| 258 |
}) |
| 259 |
.subscribe(ts); |
| 260 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 261 |
ts.assertNoErrors(); |
| 262 |
} |
| 263 |
|
| 264 |
@Test |
| 265 |
public void normalBackpressure() { |
| 266 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 267 |
|
| 268 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 269 |
|
| 270 |
pp.onErrorResumeNext(new Function<Throwable, Flowable<Integer>>() { |
| 271 |
@Override |
| 272 |
public Flowable<Integer> apply(Throwable v) { |
| 273 |
return Flowable.range(3, 2); |
| 274 |
} |
| 275 |
}).subscribe(ts); |
| 276 |
|
| 277 |
ts.request(2); |
| 278 |
|
| 279 |
pp.onNext(1); |
| 280 |
pp.onNext(2); |
| 281 |
pp.onError(new TestException("Forced failure")); |
| 282 |
|
| 283 |
ts.assertValues(1, 2); |
| 284 |
ts.assertNoErrors(); |
| 285 |
ts.assertNotComplete(); |
| 286 |
|
| 287 |
ts.request(2); |
| 288 |
|
| 289 |
ts.assertValues(1, 2, 3, 4); |
| 290 |
ts.assertNoErrors(); |
| 291 |
ts.assertComplete(); |
| 292 |
} |
| 293 |
|
| 294 |
@Test |
| 295 |
public void badOtherSource() { |
| 296 |
TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() { |
| 297 |
@Override |
| 298 |
public Object apply(Flowable<Integer> f) throws Exception { |
| 299 |
return Flowable.error(new IOException()) |
| 300 |
.onErrorResumeNext(Functions.justFunction(f)); |
| 301 |
} |
| 302 |
}, false, 1, 1, 1); |
| 303 |
} |
| 304 |
|
| 305 |
} |