| 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 org.junit.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.functions.Predicate; |
| 20 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 21 |
import io.reactivex.rxjava3.operators.ScalarSupplier; |
| 22 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 23 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 24 |
|
| 25 |
public class FlowableFromArrayTest extends RxJavaTest { |
| 26 |
|
| 27 |
Flowable<Integer> create(int n) { |
| 28 |
Integer[] array = new Integer[n]; |
| 29 |
for (int i = 0; i < n; i++) { |
| 30 |
array[i] = i; |
| 31 |
} |
| 32 |
return Flowable.fromArray(array); |
| 33 |
} |
| 34 |
|
| 35 |
@Test |
| 36 |
public void simple() { |
| 37 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 38 |
|
| 39 |
create(1000).subscribe(ts); |
| 40 |
|
| 41 |
ts.assertNoErrors(); |
| 42 |
ts.assertValueCount(1000); |
| 43 |
ts.assertComplete(); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void backpressure() { |
| 48 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 49 |
|
| 50 |
create(1000).subscribe(ts); |
| 51 |
|
| 52 |
ts.assertNoErrors(); |
| 53 |
ts.assertNoValues(); |
| 54 |
ts.assertNotComplete(); |
| 55 |
|
| 56 |
ts.request(10); |
| 57 |
|
| 58 |
ts.assertNoErrors(); |
| 59 |
ts.assertValueCount(10); |
| 60 |
ts.assertNotComplete(); |
| 61 |
|
| 62 |
ts.request(1000); |
| 63 |
|
| 64 |
ts.assertNoErrors(); |
| 65 |
ts.assertValueCount(1000); |
| 66 |
ts.assertComplete(); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void conditionalBackpressure() { |
| 71 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 72 |
|
| 73 |
create(1000) |
| 74 |
.filter(Functions.alwaysTrue()) |
| 75 |
.subscribe(ts); |
| 76 |
|
| 77 |
ts.assertNoErrors(); |
| 78 |
ts.assertNoValues(); |
| 79 |
ts.assertNotComplete(); |
| 80 |
|
| 81 |
ts.request(10); |
| 82 |
|
| 83 |
ts.assertNoErrors(); |
| 84 |
ts.assertValueCount(10); |
| 85 |
ts.assertNotComplete(); |
| 86 |
|
| 87 |
ts.request(1000); |
| 88 |
|
| 89 |
ts.assertNoErrors(); |
| 90 |
ts.assertValueCount(1000); |
| 91 |
ts.assertComplete(); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void empty() { |
| 96 |
Assert.assertSame(Flowable.empty(), Flowable.fromArray(new Object[0])); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void just() { |
| 101 |
Flowable<Integer> source = Flowable.fromArray(new Integer[] { 1 }); |
| 102 |
Assert.assertTrue(source.getClass().toString(), source instanceof ScalarSupplier); |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void just10Arguments() { |
| 107 |
Flowable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 108 |
.test() |
| 109 |
.assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void badRequest() { |
| 114 |
TestHelper.assertBadRequestReported(Flowable.just(1, 2, 3)); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void conditionalOneIsNull() { |
| 119 |
Flowable.fromArray(new Integer[] { null, 1 }) |
| 120 |
.filter(Functions.alwaysTrue()) |
| 121 |
.test() |
| 122 |
.assertFailure(NullPointerException.class); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void conditionalOneIsNullSlowPath() { |
| 127 |
Flowable.fromArray(new Integer[] { null, 1 }) |
| 128 |
.filter(Functions.alwaysTrue()) |
| 129 |
.test(2L) |
| 130 |
.assertFailure(NullPointerException.class); |
| 131 |
} |
| 132 |
|
| 133 |
@Test |
| 134 |
public void conditionalOneByOne() { |
| 135 |
Flowable.fromArray(new Integer[] { 1, 2, 3, 4, 5 }) |
| 136 |
.filter(Functions.alwaysTrue()) |
| 137 |
.rebatchRequests(1) |
| 138 |
.test() |
| 139 |
.assertResult(1, 2, 3, 4, 5); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void conditionalFiltered() { |
| 144 |
Flowable.fromArray(new Integer[] { 1, 2, 3, 4, 5 }) |
| 145 |
.filter(new Predicate<Integer>() { |
| 146 |
@Override |
| 147 |
public boolean test(Integer v) throws Exception { |
| 148 |
return v % 2 == 0; |
| 149 |
} |
| 150 |
}) |
| 151 |
.test() |
| 152 |
.assertResult(2, 4); |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void conditionalSlowPathCancel() { |
| 157 |
Flowable.fromArray(new Integer[] { 1, 2, 3, 4, 5 }) |
| 158 |
.filter(Functions.alwaysTrue()) |
| 159 |
.subscribeWith(new TestSubscriber<Integer>(5L) { |
| 160 |
@Override |
| 161 |
public void onNext(Integer t) { |
| 162 |
super.onNext(t); |
| 163 |
if (t == 1) { |
| 164 |
cancel(); |
| 165 |
onComplete(); |
| 166 |
} |
| 167 |
} |
| 168 |
}) |
| 169 |
.assertResult(1); |
| 170 |
} |
| 171 |
|
| 172 |
@Test |
| 173 |
public void conditionalSlowPathSkipCancel() { |
| 174 |
Flowable.fromArray(new Integer[] { 1, 2, 3, 4, 5 }) |
| 175 |
.filter(new Predicate<Integer>() { |
| 176 |
@Override |
| 177 |
public boolean test(Integer v) throws Exception { |
| 178 |
return v < 2; |
| 179 |
} |
| 180 |
}) |
| 181 |
.subscribeWith(new TestSubscriber<Integer>(5L) { |
| 182 |
@Override |
| 183 |
public void onNext(Integer t) { |
| 184 |
super.onNext(t); |
| 185 |
if (t == 1) { |
| 186 |
cancel(); |
| 187 |
onComplete(); |
| 188 |
} |
| 189 |
} |
| 190 |
}) |
| 191 |
.assertResult(1); |
| 192 |
} |
| 193 |
} |