| 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.mixed; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
import java.util.concurrent.atomic.AtomicReference; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import org.junit.Test; |
| 23 |
import org.reactivestreams.Subscriber; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.exceptions.*; |
| 27 |
import io.reactivex.rxjava3.functions.Function; |
| 28 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 29 |
import io.reactivex.rxjava3.internal.operators.mixed.FlowableConcatMapSingle.ConcatMapSingleSubscriber; |
| 30 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 31 |
import io.reactivex.rxjava3.internal.util.ErrorMode; |
| 32 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 33 |
import io.reactivex.rxjava3.processors.*; |
| 34 |
import io.reactivex.rxjava3.subjects.SingleSubject; |
| 35 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 36 |
import io.reactivex.rxjava3.testsupport.*; |
| 37 |
|
| 38 |
public class FlowableConcatMapSingleTest extends RxJavaTest { |
| 39 |
|
| 40 |
@Test |
| 41 |
public void simple() { |
| 42 |
Flowable.range(1, 5) |
| 43 |
.concatMapSingle(new Function<Integer, SingleSource<Integer>>() { |
| 44 |
@Override |
| 45 |
public SingleSource<Integer> apply(Integer v) |
| 46 |
throws Exception { |
| 47 |
return Single.just(v); |
| 48 |
} |
| 49 |
}) |
| 50 |
.test() |
| 51 |
.assertResult(1, 2, 3, 4, 5); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void simpleLongPrefetch() { |
| 56 |
Flowable.range(1, 1024) |
| 57 |
.concatMapSingle(Single::just, 32) |
| 58 |
.test() |
| 59 |
.assertValueCount(1024) |
| 60 |
.assertNoErrors() |
| 61 |
.assertComplete(); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void simpleLongPrefetchHidden() { |
| 66 |
Flowable.range(1, 1024).hide() |
| 67 |
.concatMapSingle(Single::just, 32) |
| 68 |
.test() |
| 69 |
.assertValueCount(1024) |
| 70 |
.assertNoErrors() |
| 71 |
.assertComplete(); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void backpressure() { |
| 76 |
TestSubscriber<Integer> ts = Flowable.range(1, 1024) |
| 77 |
.concatMapSingle(new Function<Integer, SingleSource<Integer>>() { |
| 78 |
@Override |
| 79 |
public SingleSource<Integer> apply(Integer v) |
| 80 |
throws Exception { |
| 81 |
return Single.just(v); |
| 82 |
} |
| 83 |
}, 32) |
| 84 |
.test(0); |
| 85 |
|
| 86 |
for (int i = 1; i <= 1024; i++) { |
| 87 |
ts.assertValueCount(i - 1) |
| 88 |
.assertNoErrors() |
| 89 |
.assertNotComplete() |
| 90 |
.requestMore(1) |
| 91 |
.assertValueCount(i) |
| 92 |
.assertNoErrors(); |
| 93 |
} |
| 94 |
|
| 95 |
ts.assertComplete(); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void mainError() { |
| 100 |
Flowable.error(new TestException()) |
| 101 |
.concatMapSingle(Functions.justFunction(Single.just(1))) |
| 102 |
.test() |
| 103 |
.assertFailure(TestException.class); |
| 104 |
} |
| 105 |
|
| 106 |
@Test |
| 107 |
public void innerError() { |
| 108 |
Flowable.just(1) |
| 109 |
.concatMapSingle(Functions.justFunction(Single.error(new TestException()))) |
| 110 |
.test() |
| 111 |
.assertFailure(TestException.class); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void mainBoundaryErrorInnerSuccess() { |
| 116 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 117 |
SingleSubject<Integer> ms = SingleSubject.create(); |
| 118 |
|
| 119 |
TestSubscriber<Integer> ts = pp.concatMapSingleDelayError(Functions.justFunction(ms), false).test(); |
| 120 |
|
| 121 |
ts.assertEmpty(); |
| 122 |
|
| 123 |
pp.onNext(1); |
| 124 |
|
| 125 |
assertTrue(ms.hasObservers()); |
| 126 |
|
| 127 |
pp.onError(new TestException()); |
| 128 |
|
| 129 |
assertTrue(ms.hasObservers()); |
| 130 |
|
| 131 |
ts.assertEmpty(); |
| 132 |
|
| 133 |
ms.onSuccess(1); |
| 134 |
|
| 135 |
ts.assertFailure(TestException.class, 1); |
| 136 |
} |
| 137 |
|
| 138 |
@Test |
| 139 |
public void doubleOnSubscribe() { |
| 140 |
TestHelper.checkDoubleOnSubscribeFlowable( |
| 141 |
new Function<Flowable<Object>, Flowable<Object>>() { |
| 142 |
@Override |
| 143 |
public Flowable<Object> apply(Flowable<Object> f) |
| 144 |
throws Exception { |
| 145 |
return f.concatMapSingleDelayError( |
| 146 |
Functions.justFunction(Single.just((Object)1))); |
| 147 |
} |
| 148 |
} |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
@Test |
| 153 |
public void queueOverflow() { |
| 154 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 155 |
try { |
| 156 |
new Flowable<Integer>() { |
| 157 |
@Override |
| 158 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 159 |
s.onSubscribe(new BooleanSubscription()); |
| 160 |
s.onNext(1); |
| 161 |
s.onNext(2); |
| 162 |
s.onNext(3); |
| 163 |
s.onError(new TestException()); |
| 164 |
} |
| 165 |
} |
| 166 |
.concatMapSingle( |
| 167 |
Functions.justFunction(Single.never()), 1 |
| 168 |
) |
| 169 |
.test() |
| 170 |
.assertFailure(MissingBackpressureException.class); |
| 171 |
|
| 172 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 173 |
} finally { |
| 174 |
RxJavaPlugins.reset(); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void limit() { |
| 180 |
Flowable.range(1, 5) |
| 181 |
.concatMapSingle(new Function<Integer, SingleSource<Integer>>() { |
| 182 |
@Override |
| 183 |
public SingleSource<Integer> apply(Integer v) |
| 184 |
throws Exception { |
| 185 |
return Single.just(v); |
| 186 |
} |
| 187 |
}) |
| 188 |
.take(3) |
| 189 |
.test() |
| 190 |
.assertResult(1, 2, 3); |
| 191 |
} |
| 192 |
|
| 193 |
@Test |
| 194 |
public void cancel() { |
| 195 |
Flowable.range(1, 5) |
| 196 |
.concatMapSingle(new Function<Integer, SingleSource<Integer>>() { |
| 197 |
@Override |
| 198 |
public SingleSource<Integer> apply(Integer v) |
| 199 |
throws Exception { |
| 200 |
return Single.just(v); |
| 201 |
} |
| 202 |
}) |
| 203 |
.test(3) |
| 204 |
.assertValues(1, 2, 3) |
| 205 |
.assertNoErrors() |
| 206 |
.assertNotComplete() |
| 207 |
.cancel(); |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void innerErrorAfterMainError() { |
| 212 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 213 |
try { |
| 214 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 215 |
|
| 216 |
final AtomicReference<SingleObserver<? super Integer>> obs = new AtomicReference<>(); |
| 217 |
|
| 218 |
TestSubscriberEx<Integer> ts = pp.concatMapSingle( |
| 219 |
new Function<Integer, SingleSource<Integer>>() { |
| 220 |
@Override |
| 221 |
public SingleSource<Integer> apply(Integer v) |
| 222 |
throws Exception { |
| 223 |
return new Single<Integer>() { |
| 224 |
@Override |
| 225 |
protected void subscribeActual( |
| 226 |
SingleObserver<? super Integer> observer) { |
| 227 |
observer.onSubscribe(Disposable.empty()); |
| 228 |
obs.set(observer); |
| 229 |
} |
| 230 |
}; |
| 231 |
} |
| 232 |
} |
| 233 |
).to(TestHelper.<Integer>testConsumer()); |
| 234 |
|
| 235 |
pp.onNext(1); |
| 236 |
|
| 237 |
pp.onError(new TestException("outer")); |
| 238 |
obs.get().onError(new TestException("inner")); |
| 239 |
|
| 240 |
ts.assertFailureAndMessage(TestException.class, "outer"); |
| 241 |
|
| 242 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "inner"); |
| 243 |
} finally { |
| 244 |
RxJavaPlugins.reset(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
@Test |
| 249 |
public void delayAllErrors() { |
| 250 |
TestSubscriberEx<Object> ts = Flowable.range(1, 5) |
| 251 |
.concatMapSingleDelayError(new Function<Integer, SingleSource<? extends Object>>() { |
| 252 |
@Override |
| 253 |
public SingleSource<? extends Object> apply(Integer v) |
| 254 |
throws Exception { |
| 255 |
return Single.error(new TestException()); |
| 256 |
} |
| 257 |
}) |
| 258 |
.to(TestHelper.<Object>testConsumer()) |
| 259 |
.assertFailure(CompositeException.class) |
| 260 |
; |
| 261 |
|
| 262 |
CompositeException ce = (CompositeException)ts.errors().get(0); |
| 263 |
assertEquals(5, ce.getExceptions().size()); |
| 264 |
} |
| 265 |
|
| 266 |
@Test |
| 267 |
public void mapperCrash() { |
| 268 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 269 |
|
| 270 |
TestSubscriber<Object> ts = pp |
| 271 |
.concatMapSingle(new Function<Integer, SingleSource<? extends Object>>() { |
| 272 |
@Override |
| 273 |
public SingleSource<? extends Object> apply(Integer v) |
| 274 |
throws Exception { |
| 275 |
throw new TestException(); |
| 276 |
} |
| 277 |
}) |
| 278 |
.test(); |
| 279 |
|
| 280 |
ts.assertEmpty(); |
| 281 |
|
| 282 |
assertTrue(pp.hasSubscribers()); |
| 283 |
|
| 284 |
pp.onNext(1); |
| 285 |
|
| 286 |
ts.assertFailure(TestException.class); |
| 287 |
|
| 288 |
assertFalse(pp.hasSubscribers()); |
| 289 |
} |
| 290 |
|
| 291 |
@Test |
| 292 |
public void cancelNoConcurrentClean() { |
| 293 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 294 |
ConcatMapSingleSubscriber<Integer, Integer> operator = |
| 295 |
new ConcatMapSingleSubscriber<>( |
| 296 |
ts, Functions.justFunction(Single.<Integer>never()), 16, ErrorMode.IMMEDIATE); |
| 297 |
|
| 298 |
operator.onSubscribe(new BooleanSubscription()); |
| 299 |
|
| 300 |
operator.queue.offer(1); |
| 301 |
|
| 302 |
operator.getAndIncrement(); |
| 303 |
|
| 304 |
ts.cancel(); |
| 305 |
|
| 306 |
assertFalse(operator.queue.isEmpty()); |
| 307 |
|
| 308 |
operator.addAndGet(-2); |
| 309 |
|
| 310 |
operator.cancel(); |
| 311 |
|
| 312 |
assertTrue(operator.queue.isEmpty()); |
| 313 |
} |
| 314 |
|
| 315 |
@Test |
| 316 |
public void innerSuccessDisposeRace() { |
| 317 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 318 |
|
| 319 |
final SingleSubject<Integer> ss = SingleSubject.create(); |
| 320 |
|
| 321 |
final TestSubscriber<Integer> ts = Flowable.just(1) |
| 322 |
.hide() |
| 323 |
.concatMapSingle(Functions.justFunction(ss)) |
| 324 |
.test(); |
| 325 |
|
| 326 |
Runnable r1 = new Runnable() { |
| 327 |
@Override |
| 328 |
public void run() { |
| 329 |
ss.onSuccess(1); |
| 330 |
} |
| 331 |
}; |
| 332 |
Runnable r2 = new Runnable() { |
| 333 |
@Override |
| 334 |
public void run() { |
| 335 |
ts.cancel(); |
| 336 |
} |
| 337 |
}; |
| 338 |
|
| 339 |
TestHelper.race(r1, r2); |
| 340 |
|
| 341 |
ts.assertNoErrors(); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
@Test |
| 346 |
public void undeliverableUponCancel() { |
| 347 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 348 |
@Override |
| 349 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 350 |
return upstream.concatMapSingle(new Function<Integer, Single<Integer>>() { |
| 351 |
@Override |
| 352 |
public Single<Integer> apply(Integer v) throws Throwable { |
| 353 |
return Single.just(v).hide(); |
| 354 |
} |
| 355 |
}); |
| 356 |
} |
| 357 |
}); |
| 358 |
} |
| 359 |
|
| 360 |
@Test |
| 361 |
public void undeliverableUponCancelDelayError() { |
| 362 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 363 |
@Override |
| 364 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 365 |
return upstream.concatMapSingleDelayError(new Function<Integer, Single<Integer>>() { |
| 366 |
@Override |
| 367 |
public Single<Integer> apply(Integer v) throws Throwable { |
| 368 |
return Single.just(v).hide(); |
| 369 |
} |
| 370 |
}, false, 2); |
| 371 |
} |
| 372 |
}); |
| 373 |
} |
| 374 |
|
| 375 |
@Test |
| 376 |
public void undeliverableUponCancelDelayErrorTillEnd() { |
| 377 |
TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() { |
| 378 |
@Override |
| 379 |
public Flowable<Integer> apply(Flowable<Integer> upstream) { |
| 380 |
return upstream.concatMapSingleDelayError(new Function<Integer, Single<Integer>>() { |
| 381 |
@Override |
| 382 |
public Single<Integer> apply(Integer v) throws Throwable { |
| 383 |
return Single.just(v).hide(); |
| 384 |
} |
| 385 |
}, true, 2); |
| 386 |
} |
| 387 |
}); |
| 388 |
} |
| 389 |
|
| 390 |
@Test |
| 391 |
public void basicNonFused() { |
| 392 |
Flowable.range(1, 5).hide() |
| 393 |
.concatMapSingle(v -> Single.just(v).hide()) |
| 394 |
.test() |
| 395 |
.assertResult(1, 2, 3, 4, 5); |
| 396 |
} |
| 397 |
|
| 398 |
@Test |
| 399 |
public void basicSyncFused() { |
| 400 |
Flowable.range(1, 5) |
| 401 |
.concatMapSingle(v -> Single.just(v).hide()) |
| 402 |
.test() |
| 403 |
.assertResult(1, 2, 3, 4, 5); |
| 404 |
} |
| 405 |
|
| 406 |
@Test |
| 407 |
public void basicAsyncFused() { |
| 408 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 409 |
TestHelper.emit(up, 1, 2, 3, 4, 5); |
| 410 |
|
| 411 |
up |
| 412 |
.concatMapSingle(v -> Single.just(v).hide()) |
| 413 |
.test() |
| 414 |
.assertResult(1, 2, 3, 4, 5); |
| 415 |
} |
| 416 |
|
| 417 |
@Test |
| 418 |
public void basicFusionRejected() { |
| 419 |
TestHelper.<Integer>rejectFlowableFusion() |
| 420 |
.concatMapSingle(v -> Single.just(v).hide()) |
| 421 |
.test() |
| 422 |
.assertEmpty(); |
| 423 |
} |
| 424 |
|
| 425 |
@Test |
| 426 |
public void fusedPollCrash() { |
| 427 |
Flowable.range(1, 5) |
| 428 |
.map(v -> { |
| 429 |
if (v == 3) { |
| 430 |
throw new TestException(); |
| 431 |
} |
| 432 |
return v; |
| 433 |
}) |
| 434 |
.compose(TestHelper.flowableStripBoundary()) |
| 435 |
.concatMapSingle(v -> Single.just(v).hide()) |
| 436 |
.test() |
| 437 |
.assertFailure(TestException.class, 1, 2); |
| 438 |
} |
| 439 |
} |