| 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.jdk8; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.util.Iterator; |
| 21 |
import java.util.concurrent.atomic.AtomicInteger; |
| 22 |
import java.util.stream.*; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 29 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 30 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 31 |
import io.reactivex.rxjava3.subjects.*; |
| 32 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 33 |
|
| 34 |
public class ObservableFlatMapStreamTest extends RxJavaTest { |
| 35 |
|
| 36 |
@Test |
| 37 |
public void empty() { |
| 38 |
Observable.empty() |
| 39 |
.flatMapStream(v -> Stream.of(1, 2, 3, 4, 5)) |
| 40 |
.test() |
| 41 |
.assertResult(); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void emptyHidden() { |
| 46 |
Observable.empty() |
| 47 |
.hide() |
| 48 |
.flatMapStream(v -> Stream.of(1, 2, 3, 4, 5)) |
| 49 |
.test() |
| 50 |
.assertResult(); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void just() { |
| 55 |
Observable.just(1) |
| 56 |
.flatMapStream(v -> Stream.of(v + 1, v + 2, v + 3, v + 4, v + 5)) |
| 57 |
.test() |
| 58 |
.assertResult(2, 3, 4, 5, 6); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void justHidden() { |
| 63 |
Observable.just(1).hide() |
| 64 |
.flatMapStream(v -> Stream.of(v + 1, v + 2, v + 3, v + 4, v + 5)) |
| 65 |
.test() |
| 66 |
.assertResult(2, 3, 4, 5, 6); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void error() { |
| 71 |
Observable.error(new TestException()) |
| 72 |
.flatMapStream(v -> Stream.of(1, 2, 3, 4, 5)) |
| 73 |
.test() |
| 74 |
.assertFailure(TestException.class); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void supplierFusedError() { |
| 79 |
Observable.fromCallable(() -> { throw new TestException(); }) |
| 80 |
.flatMapStream(v -> Stream.of(1, 2, 3, 4, 5)) |
| 81 |
.test() |
| 82 |
.assertFailure(TestException.class); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void errorHidden() { |
| 87 |
Observable.error(new TestException()) |
| 88 |
.hide() |
| 89 |
.flatMapStream(v -> Stream.of(1, 2, 3, 4, 5)) |
| 90 |
.test() |
| 91 |
.assertFailure(TestException.class); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void range() { |
| 96 |
Observable.range(1, 5) |
| 97 |
.flatMapStream(v -> IntStream.range(v * 10, v * 10 + 5).boxed()) |
| 98 |
.test() |
| 99 |
.assertResult( |
| 100 |
10, 11, 12, 13, 14, |
| 101 |
20, 21, 22, 23, 24, |
| 102 |
30, 31, 32, 33, 34, |
| 103 |
40, 41, 42, 43, 44, |
| 104 |
50, 51, 52, 53, 54 |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void rangeHidden() { |
| 110 |
Observable.range(1, 5) |
| 111 |
.hide() |
| 112 |
.flatMapStream(v -> IntStream.range(v * 10, v * 10 + 5).boxed()) |
| 113 |
.test() |
| 114 |
.assertResult( |
| 115 |
10, 11, 12, 13, 14, |
| 116 |
20, 21, 22, 23, 24, |
| 117 |
30, 31, 32, 33, 34, |
| 118 |
40, 41, 42, 43, 44, |
| 119 |
50, 51, 52, 53, 54 |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void rangeToEmpty() { |
| 125 |
Observable.range(1, 5) |
| 126 |
.flatMapStream(v -> Stream.of()) |
| 127 |
.test() |
| 128 |
.assertResult(); |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void rangeTake() { |
| 133 |
Observable.range(1, 5) |
| 134 |
.flatMapStream(v -> IntStream.range(v * 10, v * 10 + 5).boxed()) |
| 135 |
.take(12) |
| 136 |
.test() |
| 137 |
.assertResult( |
| 138 |
10, 11, 12, 13, 14, |
| 139 |
20, 21, 22, 23, 24, |
| 140 |
30, 31 |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void rangeTakeHidden() { |
| 146 |
Observable.range(1, 5) |
| 147 |
.hide() |
| 148 |
.flatMapStream(v -> IntStream.range(v * 10, v * 10 + 5).boxed()) |
| 149 |
.take(12) |
| 150 |
.test() |
| 151 |
.assertResult( |
| 152 |
10, 11, 12, 13, 14, |
| 153 |
20, 21, 22, 23, 24, |
| 154 |
30, 31 |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void upstreamCancelled() { |
| 160 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 161 |
|
| 162 |
AtomicInteger calls = new AtomicInteger(); |
| 163 |
|
| 164 |
TestObserver<Integer> to = ps |
| 165 |
.flatMapStream(v -> Stream.of(v + 1, v + 2).onClose(() -> calls.getAndIncrement())) |
| 166 |
.take(1) |
| 167 |
.test(); |
| 168 |
|
| 169 |
assertTrue(ps.hasObservers()); |
| 170 |
|
| 171 |
ps.onNext(1); |
| 172 |
|
| 173 |
to.assertResult(2); |
| 174 |
|
| 175 |
assertFalse(ps.hasObservers()); |
| 176 |
|
| 177 |
assertEquals(1, calls.get()); |
| 178 |
} |
| 179 |
|
| 180 |
@Test |
| 181 |
public void upstreamCancelledCloseCrash() throws Throwable { |
| 182 |
TestHelper.withErrorTracking(errors -> { |
| 183 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 184 |
|
| 185 |
TestObserver<Integer> to = ps |
| 186 |
.flatMapStream(v -> Stream.of(v + 1, v + 2).onClose(() -> { throw new TestException(); })) |
| 187 |
.take(1) |
| 188 |
.test(); |
| 189 |
|
| 190 |
assertTrue(ps.hasObservers()); |
| 191 |
|
| 192 |
ps.onNext(1); |
| 193 |
|
| 194 |
to.assertResult(2); |
| 195 |
|
| 196 |
assertFalse(ps.hasObservers()); |
| 197 |
|
| 198 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 199 |
}); |
| 200 |
} |
| 201 |
|
| 202 |
@Test |
| 203 |
public void crossMap() { |
| 204 |
Observable.range(1, 1000) |
| 205 |
.flatMapStream(v -> IntStream.range(v * 1000, v * 1000 + 1000).boxed()) |
| 206 |
.test() |
| 207 |
.assertValueCount(1_000_000) |
| 208 |
.assertNoErrors() |
| 209 |
.assertComplete(); |
| 210 |
} |
| 211 |
|
| 212 |
@Test |
| 213 |
public void crossMapHidden() { |
| 214 |
Observable.range(1, 1000) |
| 215 |
.hide() |
| 216 |
.flatMapStream(v -> IntStream.range(v * 1000, v * 1000 + 1000).boxed()) |
| 217 |
.test() |
| 218 |
.assertValueCount(1_000_000) |
| 219 |
.assertNoErrors() |
| 220 |
.assertComplete(); |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void onSubscribe() { |
| 225 |
TestHelper.checkDoubleOnSubscribeObservable(f -> f.flatMapStream(v -> Stream.of(1, 2))); |
| 226 |
} |
| 227 |
|
| 228 |
@Test |
| 229 |
public void mapperThrows() { |
| 230 |
Observable.just(1).hide() |
| 231 |
.concatMapStream(v -> { throw new TestException(); }) |
| 232 |
.test() |
| 233 |
.assertFailure(TestException.class); |
| 234 |
} |
| 235 |
|
| 236 |
@Test |
| 237 |
public void mapperNull() { |
| 238 |
Observable.just(1).hide() |
| 239 |
.concatMapStream(v -> null) |
| 240 |
.test() |
| 241 |
.assertFailure(NullPointerException.class); |
| 242 |
} |
| 243 |
|
| 244 |
@Test |
| 245 |
public void streamNull() { |
| 246 |
Observable.just(1).hide() |
| 247 |
.concatMapStream(v -> Stream.of(1, null)) |
| 248 |
.test() |
| 249 |
.assertFailure(NullPointerException.class, 1); |
| 250 |
} |
| 251 |
|
| 252 |
@Test |
| 253 |
public void hasNextThrows() { |
| 254 |
Observable.just(1).hide() |
| 255 |
.concatMapStream(v -> Stream.generate(() -> { throw new TestException(); })) |
| 256 |
.test() |
| 257 |
.assertFailure(TestException.class); |
| 258 |
} |
| 259 |
|
| 260 |
@Test |
| 261 |
public void hasNextThrowsLater() { |
| 262 |
AtomicInteger counter = new AtomicInteger(); |
| 263 |
Observable.just(1).hide() |
| 264 |
.concatMapStream(v -> Stream.generate(() -> { |
| 265 |
if (counter.getAndIncrement() == 0) { |
| 266 |
return 1; |
| 267 |
} |
| 268 |
throw new TestException(); |
| 269 |
})) |
| 270 |
.test() |
| 271 |
.assertFailure(TestException.class, 1); |
| 272 |
} |
| 273 |
|
| 274 |
@Test |
| 275 |
public void mapperThrowsWhenUpstreamErrors() throws Throwable { |
| 276 |
TestHelper.withErrorTracking(errors -> { |
| 277 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 278 |
|
| 279 |
AtomicInteger counter = new AtomicInteger(); |
| 280 |
|
| 281 |
TestObserver<Integer> to = ps.hide() |
| 282 |
.concatMapStream(v -> { |
| 283 |
if (counter.getAndIncrement() == 0) { |
| 284 |
return Stream.of(1, 2); |
| 285 |
} |
| 286 |
ps.onError(new IOException()); |
| 287 |
throw new TestException(); |
| 288 |
}) |
| 289 |
.test(); |
| 290 |
|
| 291 |
ps.onNext(1); |
| 292 |
ps.onNext(2); |
| 293 |
|
| 294 |
to |
| 295 |
.assertFailure(IOException.class, 1, 2); |
| 296 |
|
| 297 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 298 |
}); |
| 299 |
} |
| 300 |
|
| 301 |
@Test |
| 302 |
public void cancelAfterIteratorNext() throws Exception { |
| 303 |
TestObserver<Integer> to = new TestObserver<>(); |
| 304 |
|
| 305 |
@SuppressWarnings("unchecked") |
| 306 |
Stream<Integer> stream = mock(Stream.class); |
| 307 |
when(stream.iterator()).thenReturn(new Iterator<Integer>() { |
| 308 |
|
| 309 |
@Override |
| 310 |
public boolean hasNext() { |
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
@Override |
| 315 |
public Integer next() { |
| 316 |
to.dispose(); |
| 317 |
return 1; |
| 318 |
} |
| 319 |
}); |
| 320 |
|
| 321 |
Observable.just(1) |
| 322 |
.hide() |
| 323 |
.concatMapStream(v -> stream) |
| 324 |
.subscribe(to); |
| 325 |
|
| 326 |
to.assertEmpty(); |
| 327 |
} |
| 328 |
|
| 329 |
@Test |
| 330 |
public void cancelAfterIteratorHasNext() throws Exception { |
| 331 |
TestObserver<Integer> to = new TestObserver<>(); |
| 332 |
|
| 333 |
@SuppressWarnings("unchecked") |
| 334 |
Stream<Integer> stream = mock(Stream.class); |
| 335 |
when(stream.iterator()).thenReturn(new Iterator<Integer>() { |
| 336 |
|
| 337 |
@Override |
| 338 |
public boolean hasNext() { |
| 339 |
to.dispose(); |
| 340 |
return true; |
| 341 |
} |
| 342 |
|
| 343 |
@Override |
| 344 |
public Integer next() { |
| 345 |
return 1; |
| 346 |
} |
| 347 |
}); |
| 348 |
|
| 349 |
Observable.just(1) |
| 350 |
.hide() |
| 351 |
.concatMapStream(v -> stream) |
| 352 |
.subscribe(to); |
| 353 |
|
| 354 |
to.assertEmpty(); |
| 355 |
} |
| 356 |
|
| 357 |
@Test |
| 358 |
public void asyncUpstreamFused() { |
| 359 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 360 |
|
| 361 |
TestObserver<Integer> to = us.flatMapStream(v -> Stream.of(1, 2)) |
| 362 |
.test(); |
| 363 |
|
| 364 |
assertTrue(us.hasObservers()); |
| 365 |
|
| 366 |
us.onNext(1); |
| 367 |
|
| 368 |
to.assertValuesOnly(1, 2); |
| 369 |
|
| 370 |
us.onComplete(); |
| 371 |
|
| 372 |
to.assertResult(1, 2); |
| 373 |
} |
| 374 |
|
| 375 |
@Test |
| 376 |
public void asyncUpstreamFusionBoundary() { |
| 377 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 378 |
|
| 379 |
TestObserver<Integer> to = us |
| 380 |
.map(v -> v + 1) |
| 381 |
.flatMapStream(v -> Stream.of(1, 2)) |
| 382 |
.test(); |
| 383 |
|
| 384 |
assertTrue(us.hasObservers()); |
| 385 |
|
| 386 |
us.onNext(1); |
| 387 |
|
| 388 |
to.assertValuesOnly(1, 2); |
| 389 |
|
| 390 |
us.onComplete(); |
| 391 |
|
| 392 |
to.assertResult(1, 2); |
| 393 |
} |
| 394 |
|
| 395 |
@Test |
| 396 |
public void fusedPollCrash() { |
| 397 |
UnicastSubject<Integer> us = UnicastSubject.create(); |
| 398 |
|
| 399 |
TestObserver<Integer> to = us |
| 400 |
.map(v -> { throw new TestException(); }) |
| 401 |
.compose(TestHelper.observableStripBoundary()) |
| 402 |
.flatMapStream(v -> Stream.of(1, 2)) |
| 403 |
.test(); |
| 404 |
|
| 405 |
assertTrue(us.hasObservers()); |
| 406 |
|
| 407 |
us.onNext(1); |
| 408 |
|
| 409 |
assertFalse(us.hasObservers()); |
| 410 |
|
| 411 |
to.assertFailure(TestException.class); |
| 412 |
} |
| 413 |
|
| 414 |
@Test |
| 415 |
public void dispose() { |
| 416 |
TestHelper.checkDisposed(PublishSubject.create().flatMapStream(v -> Stream.of(1))); |
| 417 |
} |
| 418 |
|
| 419 |
@Test |
| 420 |
public void eventsIgnoredAfterCrash() { |
| 421 |
AtomicInteger calls = new AtomicInteger(); |
| 422 |
|
| 423 |
new Observable<Integer>() { |
| 424 |
@Override |
| 425 |
protected void subscribeActual(@NonNull Observer<? super Integer> observer) { |
| 426 |
observer.onSubscribe(Disposable.empty()); |
| 427 |
observer.onNext(1); |
| 428 |
observer.onNext(2); |
| 429 |
observer.onComplete(); |
| 430 |
} |
| 431 |
} |
| 432 |
.flatMapStream(v -> { |
| 433 |
calls.getAndIncrement(); |
| 434 |
throw new TestException(); |
| 435 |
}) |
| 436 |
.take(1) |
| 437 |
.test() |
| 438 |
.assertFailure(TestException.class); |
| 439 |
|
| 440 |
assertEquals(1, calls.get()); |
| 441 |
} |
| 442 |
|
| 443 |
@Test |
| 444 |
public void eventsIgnoredAfterDispose() { |
| 445 |
AtomicInteger calls = new AtomicInteger(); |
| 446 |
|
| 447 |
new Observable<Integer>() { |
| 448 |
@Override |
| 449 |
protected void subscribeActual(@NonNull Observer<? super Integer> observer) { |
| 450 |
observer.onSubscribe(Disposable.empty()); |
| 451 |
observer.onNext(1); |
| 452 |
observer.onNext(2); |
| 453 |
observer.onComplete(); |
| 454 |
} |
| 455 |
} |
| 456 |
.flatMapStream(v -> { |
| 457 |
calls.getAndIncrement(); |
| 458 |
return Stream.of(1); |
| 459 |
}) |
| 460 |
.take(1) |
| 461 |
.test() |
| 462 |
.assertResult(1); |
| 463 |
|
| 464 |
assertEquals(1, calls.get()); |
| 465 |
} |
| 466 |
} |