| 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.fail; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.concurrent.TimeUnit; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
import org.mockito.Mockito; |
| 24 |
import org.reactivestreams.*; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.functions.Function; |
| 29 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 30 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 31 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 32 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 33 |
|
| 34 |
public class FlowableOnErrorResumeNextViaFlowableTest extends RxJavaTest { |
| 35 |
|
| 36 |
@Test |
| 37 |
public void resumeNext() { |
| 38 |
Subscription s = mock(Subscription.class); |
| 39 |
// Trigger failure on second element |
| 40 |
TestObservable f = new TestObservable(s, "one", "fail", "two", "three"); |
| 41 |
Flowable<String> w = Flowable.unsafeCreate(f); |
| 42 |
Flowable<String> resume = Flowable.just("twoResume", "threeResume"); |
| 43 |
Flowable<String> flowable = w.onErrorResumeWith(resume); |
| 44 |
|
| 45 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 46 |
flowable.subscribe(subscriber); |
| 47 |
|
| 48 |
try { |
| 49 |
f.t.join(); |
| 50 |
} catch (InterruptedException e) { |
| 51 |
fail(e.getMessage()); |
| 52 |
} |
| 53 |
|
| 54 |
verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 55 |
verify(subscriber, times(1)).onComplete(); |
| 56 |
verify(subscriber, times(1)).onNext("one"); |
| 57 |
verify(subscriber, Mockito.never()).onNext("two"); |
| 58 |
verify(subscriber, Mockito.never()).onNext("three"); |
| 59 |
verify(subscriber, times(1)).onNext("twoResume"); |
| 60 |
verify(subscriber, times(1)).onNext("threeResume"); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void mapResumeAsyncNext() { |
| 65 |
Subscription sr = mock(Subscription.class); |
| 66 |
// Trigger multiple failures |
| 67 |
Flowable<String> w = Flowable.just("one", "fail", "two", "three", "fail"); |
| 68 |
// Resume Observable is async |
| 69 |
TestObservable f = new TestObservable(sr, "twoResume", "threeResume"); |
| 70 |
Flowable<String> resume = Flowable.unsafeCreate(f); |
| 71 |
|
| 72 |
// Introduce map function that fails intermittently (Map does not prevent this when the observer is a |
| 73 |
// rx.operator incl onErrorResumeNextViaObservable) |
| 74 |
w = w.map(new Function<String, String>() { |
| 75 |
@Override |
| 76 |
public String apply(String s) { |
| 77 |
if ("fail".equals(s)) { |
| 78 |
throw new RuntimeException("Forced Failure"); |
| 79 |
} |
| 80 |
System.out.println("BadMapper:" + s); |
| 81 |
return s; |
| 82 |
} |
| 83 |
}); |
| 84 |
|
| 85 |
Flowable<String> flowable = w.onErrorResumeWith(resume); |
| 86 |
|
| 87 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 88 |
|
| 89 |
flowable.subscribe(subscriber); |
| 90 |
|
| 91 |
try { |
| 92 |
f.t.join(); |
| 93 |
} catch (InterruptedException e) { |
| 94 |
fail(e.getMessage()); |
| 95 |
} |
| 96 |
|
| 97 |
verify(subscriber, Mockito.never()).onError(any(Throwable.class)); |
| 98 |
verify(subscriber, times(1)).onComplete(); |
| 99 |
verify(subscriber, times(1)).onNext("one"); |
| 100 |
verify(subscriber, Mockito.never()).onNext("two"); |
| 101 |
verify(subscriber, Mockito.never()).onNext("three"); |
| 102 |
verify(subscriber, times(1)).onNext("twoResume"); |
| 103 |
verify(subscriber, times(1)).onNext("threeResume"); |
| 104 |
} |
| 105 |
|
| 106 |
static final class TestObservable implements Publisher<String> { |
| 107 |
|
| 108 |
final Subscription upstream; |
| 109 |
final String[] values; |
| 110 |
Thread t; |
| 111 |
|
| 112 |
TestObservable(Subscription s, String... values) { |
| 113 |
this.upstream = s; |
| 114 |
this.values = values; |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 119 |
System.out.println("TestObservable subscribed to ..."); |
| 120 |
subscriber.onSubscribe(upstream); |
| 121 |
t = new Thread(new Runnable() { |
| 122 |
|
| 123 |
@Override |
| 124 |
public void run() { |
| 125 |
try { |
| 126 |
System.out.println("running TestObservable thread"); |
| 127 |
for (String s : values) { |
| 128 |
if ("fail".equals(s)) { |
| 129 |
throw new RuntimeException("Forced Failure"); |
| 130 |
} |
| 131 |
System.out.println("TestObservable onNext: " + s); |
| 132 |
subscriber.onNext(s); |
| 133 |
} |
| 134 |
System.out.println("TestObservable onComplete"); |
| 135 |
subscriber.onComplete(); |
| 136 |
} catch (Throwable e) { |
| 137 |
System.out.println("TestObservable onError: " + e); |
| 138 |
subscriber.onError(e); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
}); |
| 143 |
System.out.println("starting TestObservable thread"); |
| 144 |
t.start(); |
| 145 |
System.out.println("done starting TestObservable thread"); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void backpressure() { |
| 151 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 152 |
Flowable.range(0, 100000) |
| 153 |
.onErrorResumeWith(Flowable.just(1)) |
| 154 |
.observeOn(Schedulers.computation()) |
| 155 |
.map(new Function<Integer, Integer>() { |
| 156 |
int c; |
| 157 |
|
| 158 |
@Override |
| 159 |
public Integer apply(Integer t1) { |
| 160 |
if (c++ <= 1) { |
| 161 |
// slow |
| 162 |
try { |
| 163 |
Thread.sleep(500); |
| 164 |
} catch (InterruptedException e) { |
| 165 |
e.printStackTrace(); |
| 166 |
} |
| 167 |
} |
| 168 |
return t1; |
| 169 |
} |
| 170 |
|
| 171 |
}) |
| 172 |
.subscribe(ts); |
| 173 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 174 |
ts.assertNoErrors(); |
| 175 |
} |
| 176 |
|
| 177 |
@Test |
| 178 |
public void normalBackpressure() { |
| 179 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 180 |
|
| 181 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 182 |
|
| 183 |
pp.onErrorResumeWith(Flowable.range(3, 2)).subscribe(ts); |
| 184 |
|
| 185 |
ts.request(2); |
| 186 |
|
| 187 |
pp.onNext(1); |
| 188 |
pp.onNext(2); |
| 189 |
pp.onError(new TestException("Forced failure")); |
| 190 |
|
| 191 |
ts.assertValues(1, 2); |
| 192 |
ts.assertNoErrors(); |
| 193 |
ts.assertNotComplete(); |
| 194 |
|
| 195 |
ts.request(2); |
| 196 |
|
| 197 |
ts.assertValues(1, 2, 3, 4); |
| 198 |
ts.assertNoErrors(); |
| 199 |
ts.assertComplete(); |
| 200 |
} |
| 201 |
|
| 202 |
} |