| 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 |
|
| 18 |
import java.util.List; |
| 19 |
import java.util.stream.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 25 |
import io.reactivex.rxjava3.processors.UnicastProcessor; |
| 26 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 27 |
|
| 28 |
public class ObservableBlockingStreamTest extends RxJavaTest { |
| 29 |
|
| 30 |
@Test |
| 31 |
public void empty() { |
| 32 |
try (Stream<Integer> stream = Observable.<Integer>empty().blockingStream()) { |
| 33 |
assertEquals(0, stream.toArray().length); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
@Test |
| 38 |
public void just() { |
| 39 |
try (Stream<Integer> stream = Observable.just(1).blockingStream()) { |
| 40 |
assertArrayEquals(new Integer[] { 1 }, stream.toArray(Integer[]::new)); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void range() { |
| 46 |
try (Stream<Integer> stream = Observable.range(1, 5).blockingStream()) { |
| 47 |
assertArrayEquals(new Integer[] { 1, 2, 3, 4, 5 }, stream.toArray(Integer[]::new)); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void rangeBackpressured() { |
| 53 |
try (Stream<Integer> stream = Observable.range(1, 5).blockingStream(1)) { |
| 54 |
assertArrayEquals(new Integer[] { 1, 2, 3, 4, 5 }, stream.toArray(Integer[]::new)); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
@Test |
| 59 |
public void rangeAsyncBackpressured() { |
| 60 |
try (Stream<Integer> stream = Observable.range(1, 1000).subscribeOn(Schedulers.computation()).blockingStream()) { |
| 61 |
List<Integer> list = stream.collect(Collectors.toList()); |
| 62 |
|
| 63 |
assertEquals(1000, list.size()); |
| 64 |
for (int i = 1; i <= 1000; i++) { |
| 65 |
assertEquals(i, list.get(i - 1).intValue()); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void rangeAsyncBackpressured1() { |
| 72 |
try (Stream<Integer> stream = Observable.range(1, 1000).subscribeOn(Schedulers.computation()).blockingStream(1)) { |
| 73 |
List<Integer> list = stream.collect(Collectors.toList()); |
| 74 |
|
| 75 |
assertEquals(1000, list.size()); |
| 76 |
for (int i = 1; i <= 1000; i++) { |
| 77 |
assertEquals(i, list.get(i - 1).intValue()); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void error() { |
| 84 |
try (Stream<Integer> stream = Observable.<Integer>error(new TestException()).blockingStream()) { |
| 85 |
stream.toArray(Integer[]::new); |
| 86 |
fail("Should have thrown!"); |
| 87 |
} catch (TestException expected) { |
| 88 |
// expected |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void close() { |
| 94 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 95 |
up.onNext(1); |
| 96 |
up.onNext(2); |
| 97 |
up.onNext(3); |
| 98 |
up.onNext(4); |
| 99 |
up.onNext(5); |
| 100 |
|
| 101 |
try (Stream<Integer> stream = up.blockingStream()) { |
| 102 |
assertArrayEquals(new Integer[] { 1, 2, 3 }, stream.limit(3).toArray(Integer[]::new)); |
| 103 |
} |
| 104 |
|
| 105 |
assertFalse(up.hasSubscribers()); |
| 106 |
} |
| 107 |
} |