| 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 |
|
| 18 |
import java.util.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.*; |
| 24 |
import io.reactivex.rxjava3.functions.Function; |
| 25 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 26 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 27 |
import io.reactivex.rxjava3.testsupport.TestSubscriberEx; |
| 28 |
|
| 29 |
public class FlowableConcatDelayErrorTest extends RxJavaTest { |
| 30 |
|
| 31 |
@Test |
| 32 |
public void mainCompletes() { |
| 33 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 34 |
|
| 35 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 36 |
|
| 37 |
source.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 38 |
@Override |
| 39 |
public Flowable<Integer> apply(Integer v) { |
| 40 |
return Flowable.range(v, 2); |
| 41 |
} |
| 42 |
}).subscribe(ts); |
| 43 |
|
| 44 |
source.onNext(1); |
| 45 |
source.onNext(2); |
| 46 |
source.onComplete(); |
| 47 |
|
| 48 |
ts.assertValues(1, 2, 2, 3); |
| 49 |
ts.assertNoErrors(); |
| 50 |
ts.assertComplete(); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void mainErrors() { |
| 55 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 56 |
|
| 57 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 58 |
|
| 59 |
source.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 60 |
@Override |
| 61 |
public Flowable<Integer> apply(Integer v) { |
| 62 |
return Flowable.range(v, 2); |
| 63 |
} |
| 64 |
}).subscribe(ts); |
| 65 |
|
| 66 |
source.onNext(1); |
| 67 |
source.onNext(2); |
| 68 |
source.onError(new TestException()); |
| 69 |
|
| 70 |
ts.assertValues(1, 2, 2, 3); |
| 71 |
ts.assertError(TestException.class); |
| 72 |
ts.assertNotComplete(); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void innerErrors() { |
| 77 |
final Flowable<Integer> inner = Flowable.range(1, 2) |
| 78 |
.concatWith(Flowable.<Integer>error(new TestException())); |
| 79 |
|
| 80 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 81 |
|
| 82 |
Flowable.range(1, 3).concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 83 |
@Override |
| 84 |
public Flowable<Integer> apply(Integer v) { |
| 85 |
return inner; |
| 86 |
} |
| 87 |
}).subscribe(ts); |
| 88 |
|
| 89 |
ts.assertValues(1, 2, 1, 2, 1, 2); |
| 90 |
ts.assertError(CompositeException.class); |
| 91 |
ts.assertNotComplete(); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void singleInnerErrors() { |
| 96 |
final Flowable<Integer> inner = Flowable.range(1, 2).concatWith(Flowable.<Integer>error(new TestException())); |
| 97 |
|
| 98 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 99 |
|
| 100 |
Flowable.just(1) |
| 101 |
.hide() // prevent scalar optimization |
| 102 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 103 |
@Override |
| 104 |
public Flowable<Integer> apply(Integer v) { |
| 105 |
return inner; |
| 106 |
} |
| 107 |
}).subscribe(ts); |
| 108 |
|
| 109 |
ts.assertValues(1, 2); |
| 110 |
ts.assertError(TestException.class); |
| 111 |
ts.assertNotComplete(); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void innerNull() { |
| 116 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 117 |
|
| 118 |
Flowable.just(1) |
| 119 |
.hide() // prevent scalar optimization |
| 120 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 121 |
@Override |
| 122 |
public Flowable<Integer> apply(Integer v) { |
| 123 |
return null; |
| 124 |
} |
| 125 |
}).subscribe(ts); |
| 126 |
|
| 127 |
ts.assertNoValues(); |
| 128 |
ts.assertError(NullPointerException.class); |
| 129 |
ts.assertNotComplete(); |
| 130 |
} |
| 131 |
|
| 132 |
@Test |
| 133 |
public void innerThrows() { |
| 134 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 135 |
|
| 136 |
Flowable.just(1) |
| 137 |
.hide() // prevent scalar optimization |
| 138 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 139 |
@Override |
| 140 |
public Flowable<Integer> apply(Integer v) { |
| 141 |
throw new TestException(); |
| 142 |
} |
| 143 |
}).subscribe(ts); |
| 144 |
|
| 145 |
ts.assertNoValues(); |
| 146 |
ts.assertError(TestException.class); |
| 147 |
ts.assertNotComplete(); |
| 148 |
} |
| 149 |
|
| 150 |
@Test |
| 151 |
public void innerWithEmpty() { |
| 152 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 153 |
|
| 154 |
Flowable.range(1, 3) |
| 155 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 156 |
@Override |
| 157 |
public Flowable<Integer> apply(Integer v) { |
| 158 |
return v == 2 ? Flowable.<Integer>empty() : Flowable.range(1, 2); |
| 159 |
} |
| 160 |
}).subscribe(ts); |
| 161 |
|
| 162 |
ts.assertValues(1, 2, 1, 2); |
| 163 |
ts.assertNoErrors(); |
| 164 |
ts.assertComplete(); |
| 165 |
} |
| 166 |
|
| 167 |
@Test |
| 168 |
public void innerWithScalar() { |
| 169 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 170 |
|
| 171 |
Flowable.range(1, 3) |
| 172 |
.concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 173 |
@Override |
| 174 |
public Flowable<Integer> apply(Integer v) { |
| 175 |
return v == 2 ? Flowable.just(3) : Flowable.range(1, 2); |
| 176 |
} |
| 177 |
}).subscribe(ts); |
| 178 |
|
| 179 |
ts.assertValues(1, 2, 3, 1, 2); |
| 180 |
ts.assertNoErrors(); |
| 181 |
ts.assertComplete(); |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void backpressure() { |
| 186 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 187 |
|
| 188 |
Flowable.range(1, 3).concatMapDelayError(new Function<Integer, Flowable<Integer>>() { |
| 189 |
@Override |
| 190 |
public Flowable<Integer> apply(Integer v) { |
| 191 |
return Flowable.range(v, 2); |
| 192 |
} |
| 193 |
}).subscribe(ts); |
| 194 |
|
| 195 |
ts.assertNoValues(); |
| 196 |
ts.assertNoErrors(); |
| 197 |
ts.assertNotComplete(); |
| 198 |
|
| 199 |
ts.request(1); |
| 200 |
ts.assertValues(1); |
| 201 |
ts.assertNoErrors(); |
| 202 |
ts.assertNotComplete(); |
| 203 |
|
| 204 |
ts.request(3); |
| 205 |
ts.assertValues(1, 2, 2, 3); |
| 206 |
ts.assertNoErrors(); |
| 207 |
ts.assertNotComplete(); |
| 208 |
|
| 209 |
ts.request(2); |
| 210 |
|
| 211 |
ts.assertValues(1, 2, 2, 3, 3, 4); |
| 212 |
ts.assertNoErrors(); |
| 213 |
ts.assertComplete(); |
| 214 |
} |
| 215 |
|
| 216 |
static <T> Flowable<T> withError(Flowable<T> source) { |
| 217 |
return source.concatWith(Flowable.<T>error(new TestException())); |
| 218 |
} |
| 219 |
|
| 220 |
@Test |
| 221 |
public void concatDelayErrorFlowable() { |
| 222 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 223 |
|
| 224 |
Flowable.concatDelayError( |
| 225 |
Flowable.just(Flowable.just(1), Flowable.just(2))) |
| 226 |
.subscribe(ts); |
| 227 |
|
| 228 |
ts.assertValues(1, 2); |
| 229 |
ts.assertNoErrors(); |
| 230 |
ts.assertComplete(); |
| 231 |
} |
| 232 |
|
| 233 |
@Test |
| 234 |
public void concatDelayErrorFlowableError() { |
| 235 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 236 |
|
| 237 |
Flowable.concatDelayError( |
| 238 |
withError(Flowable.just(withError(Flowable.just(1)), withError(Flowable.just(2))))) |
| 239 |
.subscribe(ts); |
| 240 |
|
| 241 |
ts.assertValues(1, 2); |
| 242 |
ts.assertError(CompositeException.class); |
| 243 |
ts.assertNotComplete(); |
| 244 |
|
| 245 |
CompositeException ce = (CompositeException)ts.errors().get(0); |
| 246 |
List<Throwable> cex = ce.getExceptions(); |
| 247 |
|
| 248 |
assertEquals(3, cex.size()); |
| 249 |
|
| 250 |
assertTrue(cex.get(0).toString(), cex.get(0) instanceof TestException); |
| 251 |
assertTrue(cex.get(1).toString(), cex.get(1) instanceof TestException); |
| 252 |
assertTrue(cex.get(2).toString(), cex.get(2) instanceof TestException); |
| 253 |
} |
| 254 |
|
| 255 |
@Test |
| 256 |
public void concatDelayErrorIterable() { |
| 257 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 258 |
|
| 259 |
Flowable.concatDelayError( |
| 260 |
Arrays.asList(Flowable.just(1), Flowable.just(2))) |
| 261 |
.subscribe(ts); |
| 262 |
|
| 263 |
ts.assertValues(1, 2); |
| 264 |
ts.assertNoErrors(); |
| 265 |
ts.assertComplete(); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void concatDelayErrorIterableError() { |
| 270 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 271 |
|
| 272 |
Flowable.concatDelayError( |
| 273 |
Arrays.asList(withError(Flowable.just(1)), withError(Flowable.just(2)))) |
| 274 |
.subscribe(ts); |
| 275 |
|
| 276 |
ts.assertValues(1, 2); |
| 277 |
ts.assertError(CompositeException.class); |
| 278 |
ts.assertNotComplete(); |
| 279 |
|
| 280 |
assertEquals(2, ((CompositeException)ts.errors().get(0)).getExceptions().size()); |
| 281 |
} |
| 282 |
|
| 283 |
} |