| 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 io.reactivex.rxjava3.core.Flowable; |
| 17 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 18 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 19 |
import io.reactivex.rxjava3.functions.BiFunction; |
| 20 |
import io.reactivex.rxjava3.functions.Supplier; |
| 21 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 22 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 23 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 24 |
import io.reactivex.rxjava3.testsupport.TestSubscriberEx; |
| 25 |
import org.junit.Assert; |
| 26 |
import org.junit.Test; |
| 27 |
|
| 28 |
import java.util.*; |
| 29 |
import java.util.concurrent.TimeUnit; |
| 30 |
|
| 31 |
public class FlowableOnBackpressureReduceWithTest extends RxJavaTest { |
| 32 |
|
| 33 |
private static <T> BiFunction<List<T>, T, List<T>> createTestReducer() { |
| 34 |
return (list, number) -> { |
| 35 |
list.add(number); |
| 36 |
return list; |
| 37 |
}; |
| 38 |
} |
| 39 |
|
| 40 |
private static <T> Supplier<List<T>> createTestSupplier() { |
| 41 |
return ArrayList::new; |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void simple() { |
| 46 |
TestSubscriberEx<List<Integer>> ts = new TestSubscriberEx<>(); |
| 47 |
|
| 48 |
Flowable.range(1, 5).onBackpressureReduce(createTestSupplier(), createTestReducer()).subscribe(ts); |
| 49 |
|
| 50 |
ts.assertNoErrors(); |
| 51 |
ts.assertTerminated(); |
| 52 |
ts.assertValues( |
| 53 |
Collections.singletonList(1), |
| 54 |
Collections.singletonList(2), |
| 55 |
Collections.singletonList(3), |
| 56 |
Collections.singletonList(4), |
| 57 |
Collections.singletonList(5) |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void simpleError() { |
| 63 |
TestSubscriberEx<List<Integer>> ts = new TestSubscriberEx<>(); |
| 64 |
|
| 65 |
Flowable.range(1, 5).concatWith(Flowable.error(new TestException())) |
| 66 |
.onBackpressureReduce(createTestSupplier(), createTestReducer()).subscribe(ts); |
| 67 |
|
| 68 |
ts.assertTerminated(); |
| 69 |
ts.assertError(TestException.class); |
| 70 |
ts.assertValues( |
| 71 |
Collections.singletonList(1), |
| 72 |
Collections.singletonList(2), |
| 73 |
Collections.singletonList(3), |
| 74 |
Collections.singletonList(4), |
| 75 |
Collections.singletonList(5) |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void simpleBackpressure() { |
| 81 |
TestSubscriberEx<List<Integer>> ts = new TestSubscriberEx<>(2L); |
| 82 |
|
| 83 |
Flowable.range(1, 5).onBackpressureReduce(createTestSupplier(), createTestReducer()).subscribe(ts); |
| 84 |
|
| 85 |
ts.assertNoErrors(); |
| 86 |
ts.assertValues( |
| 87 |
Collections.singletonList(1), |
| 88 |
Collections.singletonList(2) |
| 89 |
); |
| 90 |
ts.assertNotComplete(); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void reduceBackpressuredSync() { |
| 95 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 96 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L); |
| 97 |
|
| 98 |
source.onBackpressureReduce(() -> 0, Integer::sum).subscribe(ts); |
| 99 |
|
| 100 |
source.onNext(1); |
| 101 |
source.onNext(2); |
| 102 |
source.onNext(3); |
| 103 |
|
| 104 |
ts.request(1); |
| 105 |
|
| 106 |
ts.assertValuesOnly(6); |
| 107 |
|
| 108 |
source.onNext(4); |
| 109 |
source.onComplete(); |
| 110 |
|
| 111 |
ts.assertValuesOnly(6); |
| 112 |
|
| 113 |
ts.request(1); |
| 114 |
ts.assertResult(6, 4); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void synchronousDrop() { |
| 119 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 120 |
TestSubscriberEx<List<Integer>> ts = new TestSubscriberEx<>(0L); |
| 121 |
|
| 122 |
source.onBackpressureReduce(createTestSupplier(), createTestReducer()).subscribe(ts); |
| 123 |
|
| 124 |
ts.assertNoValues(); |
| 125 |
|
| 126 |
source.onNext(1); |
| 127 |
ts.request(2); |
| 128 |
|
| 129 |
ts.assertValues(Collections.singletonList(1)); |
| 130 |
|
| 131 |
source.onNext(2); |
| 132 |
|
| 133 |
ts.assertValues( |
| 134 |
Collections.singletonList(1), |
| 135 |
Collections.singletonList(2) |
| 136 |
); |
| 137 |
|
| 138 |
source.onNext(3); |
| 139 |
source.onNext(4); |
| 140 |
source.onNext(5); |
| 141 |
source.onNext(6); |
| 142 |
|
| 143 |
ts.request(2); |
| 144 |
|
| 145 |
ts.assertValues( |
| 146 |
Collections.singletonList(1), |
| 147 |
Collections.singletonList(2), |
| 148 |
Arrays.asList(3, 4, 5, 6) |
| 149 |
); |
| 150 |
|
| 151 |
source.onNext(7); |
| 152 |
|
| 153 |
ts.assertValues( |
| 154 |
Collections.singletonList(1), |
| 155 |
Collections.singletonList(2), |
| 156 |
Arrays.asList(3, 4, 5, 6), |
| 157 |
Collections.singletonList(7) |
| 158 |
); |
| 159 |
|
| 160 |
source.onNext(8); |
| 161 |
source.onNext(9); |
| 162 |
source.onComplete(); |
| 163 |
|
| 164 |
ts.request(1); |
| 165 |
|
| 166 |
ts.assertValues( |
| 167 |
Collections.singletonList(1), |
| 168 |
Collections.singletonList(2), |
| 169 |
Arrays.asList(3, 4, 5, 6), |
| 170 |
Collections.singletonList(7), |
| 171 |
Arrays.asList(8, 9) |
| 172 |
); |
| 173 |
ts.assertNoErrors(); |
| 174 |
ts.assertTerminated(); |
| 175 |
} |
| 176 |
|
| 177 |
private <T> TestSubscriberEx<T> createDelayedSubscriber() { |
| 178 |
return new TestSubscriberEx<T>(1L) { |
| 179 |
final Random rnd = new Random(); |
| 180 |
|
| 181 |
@Override |
| 182 |
public void onNext(T t) { |
| 183 |
super.onNext(t); |
| 184 |
if (rnd.nextDouble() < 0.001) { |
| 185 |
try { |
| 186 |
Thread.sleep(1); |
| 187 |
} catch (InterruptedException ex) { |
| 188 |
ex.printStackTrace(); |
| 189 |
} |
| 190 |
} |
| 191 |
request(1); |
| 192 |
} |
| 193 |
}; |
| 194 |
} |
| 195 |
|
| 196 |
private <T> void assertValuesDropped(TestSubscriberEx<T> ts, int totalValues) { |
| 197 |
int n = ts.values().size(); |
| 198 |
System.out.println("testAsynchronousDrop -> " + n); |
| 199 |
Assert.assertTrue("All events received?", n < totalValues); |
| 200 |
} |
| 201 |
|
| 202 |
private void assertIncreasingSequence(TestSubscriberEx<Integer> ts) { |
| 203 |
int previous = 0; |
| 204 |
for (Integer current : ts.values()) { |
| 205 |
Assert.assertTrue("The sequence must be increasing [current value=" + previous + |
| 206 |
", previous value=" + current + "]", previous <= current); |
| 207 |
previous = current; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
@Test |
| 212 |
public void asynchronousDrop() { |
| 213 |
TestSubscriberEx<Integer> ts = createDelayedSubscriber(); |
| 214 |
int m = 100000; |
| 215 |
Flowable.range(1, m) |
| 216 |
.subscribeOn(Schedulers.computation()) |
| 217 |
.onBackpressureReduce((Supplier<List<Integer>>) Collections::emptyList, (list, current) -> { |
| 218 |
//in that case it works like onBackpressureLatest |
| 219 |
//the output sequence of number must be increasing |
| 220 |
return Collections.singletonList(current); |
| 221 |
}) |
| 222 |
.observeOn(Schedulers.io()) |
| 223 |
.concatMap(Flowable::fromIterable) |
| 224 |
.subscribe(ts); |
| 225 |
|
| 226 |
ts.awaitDone(2, TimeUnit.SECONDS); |
| 227 |
ts.assertTerminated(); |
| 228 |
assertValuesDropped(ts, m); |
| 229 |
assertIncreasingSequence(ts); |
| 230 |
} |
| 231 |
|
| 232 |
@Test |
| 233 |
public void asynchronousDrop2() { |
| 234 |
TestSubscriberEx<Long> ts = createDelayedSubscriber(); |
| 235 |
int m = 100000; |
| 236 |
Flowable.rangeLong(1, m) |
| 237 |
.subscribeOn(Schedulers.computation()) |
| 238 |
.onBackpressureReduce(createTestSupplier(), createTestReducer()) |
| 239 |
.observeOn(Schedulers.io()) |
| 240 |
.concatMap(list -> Flowable.just(list.stream().reduce(Long::sum).orElseThrow(() -> { |
| 241 |
throw new IllegalArgumentException("No value in list"); |
| 242 |
}))) |
| 243 |
.subscribe(ts); |
| 244 |
|
| 245 |
ts.awaitDone(2, TimeUnit.SECONDS); |
| 246 |
ts.assertTerminated(); |
| 247 |
assertValuesDropped(ts, m); |
| 248 |
long sum = 0; |
| 249 |
for (Long i : ts.values()) { |
| 250 |
sum += i; |
| 251 |
} |
| 252 |
//sum = (A1 + An) * n / 2 = 100_001 * 50_000 = 50_000_00000 + 50_000 = 50_000_50_000 |
| 253 |
Assert.assertEquals("Wrong sum: " + sum, 5000050000L, sum); |
| 254 |
} |
| 255 |
|
| 256 |
@Test |
| 257 |
public void nullPointerFromReducer() { |
| 258 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 259 |
TestSubscriberEx<List<Integer>> ts = new TestSubscriberEx<>(0L); |
| 260 |
source.onBackpressureReduce(createTestSupplier(), (BiFunction<List<Integer>, ? super Integer, List<Integer>>) (list, number) -> null).subscribe(ts); |
| 261 |
|
| 262 |
source.onNext(1); |
| 263 |
source.onNext(2); |
| 264 |
|
| 265 |
TestHelper.assertError(ts.errors(), 0, NullPointerException.class, "The reducer returned a null value"); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void nullPointerFromSupplier() { |
| 270 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 271 |
TestSubscriberEx<List<Integer>> ts = new TestSubscriberEx<>(0L); |
| 272 |
source.onBackpressureReduce(() -> null, createTestReducer()).subscribe(ts); |
| 273 |
|
| 274 |
source.onNext(1); |
| 275 |
source.onNext(2); |
| 276 |
|
| 277 |
TestHelper.assertError(ts.errors(), 0, NullPointerException.class, "The supplier returned a null value"); |
| 278 |
} |
| 279 |
|
| 280 |
@Test |
| 281 |
public void exceptionFromReducer() { |
| 282 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 283 |
TestSubscriberEx<List<Integer>> ts = new TestSubscriberEx<>(0L); |
| 284 |
source.onBackpressureReduce(createTestSupplier(), (BiFunction<List<Integer>, ? super Integer, List<Integer>>) (l, r) -> { |
| 285 |
throw new TestException("Test exception"); |
| 286 |
}).subscribe(ts); |
| 287 |
|
| 288 |
source.onNext(1); |
| 289 |
source.onNext(2); |
| 290 |
|
| 291 |
TestHelper.assertError(ts.errors(), 0, TestException.class, "Test exception"); |
| 292 |
} |
| 293 |
|
| 294 |
@Test |
| 295 |
public void exceptionFromSupplier() { |
| 296 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 297 |
TestSubscriberEx<List<Integer>> ts = new TestSubscriberEx<>(0L); |
| 298 |
source.onBackpressureReduce(() -> { |
| 299 |
throw new TestException("Test exception"); |
| 300 |
}, createTestReducer()).subscribe(ts); |
| 301 |
|
| 302 |
source.onNext(1); |
| 303 |
source.onNext(2); |
| 304 |
|
| 305 |
TestHelper.assertError(ts.errors(), 0, TestException.class, "Test exception"); |
| 306 |
} |
| 307 |
|
| 308 |
@Test |
| 309 |
public void doubleOnSubscribe() { |
| 310 |
TestHelper.checkDoubleOnSubscribeFlowable(f -> f.onBackpressureReduce(createTestSupplier(), createTestReducer())); |
| 311 |
} |
| 312 |
|
| 313 |
@Test |
| 314 |
public void take() { |
| 315 |
Flowable.just(1, 2) |
| 316 |
.onBackpressureReduce(createTestSupplier(), createTestReducer()) |
| 317 |
.take(1) |
| 318 |
.test() |
| 319 |
.assertResult(Collections.singletonList(1)); |
| 320 |
} |
| 321 |
|
| 322 |
@Test |
| 323 |
public void dispose() { |
| 324 |
TestHelper.checkDisposed(Flowable.never().onBackpressureReduce(createTestSupplier(), createTestReducer())); |
| 325 |
} |
| 326 |
|
| 327 |
@Test |
| 328 |
public void badRequest() { |
| 329 |
TestHelper.assertBadRequestReported(Flowable.never().onBackpressureReduce(createTestSupplier(), createTestReducer())); |
| 330 |
} |
| 331 |
} |