| 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.processors.PublishProcessor; |
| 21 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 22 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 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.Random; |
| 29 |
import java.util.concurrent.TimeUnit; |
| 30 |
|
| 31 |
public class FlowableOnBackpressureReduceTest extends RxJavaTest { |
| 32 |
|
| 33 |
static final BiFunction<Integer, Integer, Integer> TEST_INT_REDUCER = (previous, current) -> previous + current + 50; |
| 34 |
|
| 35 |
static final BiFunction<Object, Object, Object> TEST_OBJECT_REDUCER = (previous, current) -> current; |
| 36 |
|
| 37 |
@Test |
| 38 |
public void simple() { |
| 39 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 40 |
|
| 41 |
Flowable.range(1, 5).onBackpressureReduce(TEST_INT_REDUCER).subscribe(ts); |
| 42 |
|
| 43 |
ts.assertNoErrors(); |
| 44 |
ts.assertTerminated(); |
| 45 |
ts.assertValues(1, 2, 3, 4, 5); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void simpleError() { |
| 50 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 51 |
|
| 52 |
Flowable.range(1, 5).concatWith(Flowable.error(new TestException())) |
| 53 |
.onBackpressureReduce(TEST_INT_REDUCER).subscribe(ts); |
| 54 |
|
| 55 |
ts.assertTerminated(); |
| 56 |
ts.assertError(TestException.class); |
| 57 |
ts.assertValues(1, 2, 3, 4, 5); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void simpleBackpressure() { |
| 62 |
TestSubscriber<Integer> ts = new TestSubscriber<>(2L); |
| 63 |
|
| 64 |
Flowable.range(1, 5).onBackpressureReduce(TEST_INT_REDUCER).subscribe(ts); |
| 65 |
|
| 66 |
ts.assertNoErrors(); |
| 67 |
ts.assertValues(1, 2); |
| 68 |
ts.assertNotComplete(); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void synchronousDrop() { |
| 73 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 74 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L); |
| 75 |
|
| 76 |
source.onBackpressureReduce(TEST_INT_REDUCER).subscribe(ts); |
| 77 |
|
| 78 |
ts.assertNoValues(); |
| 79 |
|
| 80 |
source.onNext(1); |
| 81 |
ts.request(2); |
| 82 |
|
| 83 |
ts.assertValue(1); |
| 84 |
|
| 85 |
source.onNext(2); |
| 86 |
|
| 87 |
ts.assertValues(1, 2); |
| 88 |
|
| 89 |
source.onNext(3); |
| 90 |
source.onNext(4); //3 + 4 + 50 == 57 |
| 91 |
source.onNext(5); //57 + 5 + 50 == 112 |
| 92 |
source.onNext(6); //112 + 6 + 50 == 168 |
| 93 |
|
| 94 |
ts.request(2); |
| 95 |
|
| 96 |
ts.assertValues(1, 2, 168); |
| 97 |
|
| 98 |
source.onNext(7); |
| 99 |
|
| 100 |
ts.assertValues(1, 2, 168, 7); |
| 101 |
|
| 102 |
source.onNext(8); |
| 103 |
source.onNext(9); //8 + 9 + 50 == 67 |
| 104 |
source.onComplete(); |
| 105 |
|
| 106 |
ts.request(1); |
| 107 |
|
| 108 |
ts.assertValues(1, 2, 168, 7, 67); |
| 109 |
ts.assertNoErrors(); |
| 110 |
ts.assertTerminated(); |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void reduceBackpressuredSync() { |
| 115 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 116 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L); |
| 117 |
|
| 118 |
source.onBackpressureReduce(Integer::sum).subscribe(ts); |
| 119 |
|
| 120 |
source.onNext(1); |
| 121 |
source.onNext(2); |
| 122 |
source.onNext(3); |
| 123 |
|
| 124 |
ts.request(1); |
| 125 |
|
| 126 |
ts.assertValuesOnly(6); |
| 127 |
|
| 128 |
source.onNext(4); |
| 129 |
source.onComplete(); |
| 130 |
|
| 131 |
ts.assertValuesOnly(6); |
| 132 |
|
| 133 |
ts.request(1); |
| 134 |
ts.assertResult(6, 4); |
| 135 |
} |
| 136 |
|
| 137 |
private <T> TestSubscriberEx<T> createDelayedSubscriber() { |
| 138 |
return new TestSubscriberEx<T>(1L) { |
| 139 |
final Random rnd = new Random(); |
| 140 |
|
| 141 |
@Override |
| 142 |
public void onNext(T t) { |
| 143 |
super.onNext(t); |
| 144 |
if (rnd.nextDouble() < 0.001) { |
| 145 |
try { |
| 146 |
Thread.sleep(1); |
| 147 |
} catch (InterruptedException ex) { |
| 148 |
ex.printStackTrace(); |
| 149 |
} |
| 150 |
} |
| 151 |
request(1); |
| 152 |
} |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 156 |
private <T> void assertValuesDropped(TestSubscriberEx<T> ts, int totalValues) { |
| 157 |
int n = ts.values().size(); |
| 158 |
System.out.println("testAsynchronousDrop -> " + n); |
| 159 |
Assert.assertTrue("All events received?", n < totalValues); |
| 160 |
} |
| 161 |
|
| 162 |
private void assertIncreasingSequence(TestSubscriberEx<Integer> ts) { |
| 163 |
int previous = 0; |
| 164 |
for (Integer current : ts.values()) { |
| 165 |
Assert.assertTrue("The sequence must be increasing [current value=" + previous + |
| 166 |
", previous value=" + current + "]", previous <= current); |
| 167 |
previous = current; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
@Test |
| 172 |
public void asynchronousDrop() { |
| 173 |
TestSubscriberEx<Integer> ts = createDelayedSubscriber(); |
| 174 |
int m = 100000; |
| 175 |
Flowable.range(1, m) |
| 176 |
.subscribeOn(Schedulers.computation()) |
| 177 |
.onBackpressureReduce((previous, current) -> { |
| 178 |
//in that case it works like onBackpressureLatest |
| 179 |
//the output sequence of number must be increasing |
| 180 |
return current; |
| 181 |
}) |
| 182 |
.observeOn(Schedulers.io()) |
| 183 |
.subscribe(ts); |
| 184 |
|
| 185 |
ts.awaitDone(2, TimeUnit.SECONDS); |
| 186 |
ts.assertTerminated(); |
| 187 |
assertValuesDropped(ts, m); |
| 188 |
assertIncreasingSequence(ts); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
public void asynchronousDrop2() { |
| 193 |
TestSubscriberEx<Long> ts = createDelayedSubscriber(); |
| 194 |
int m = 100000; |
| 195 |
Flowable.rangeLong(1, m) |
| 196 |
.subscribeOn(Schedulers.computation()) |
| 197 |
.onBackpressureReduce(Long::sum) |
| 198 |
.observeOn(Schedulers.io()) |
| 199 |
.subscribe(ts); |
| 200 |
|
| 201 |
ts.awaitDone(2, TimeUnit.SECONDS); |
| 202 |
ts.assertTerminated(); |
| 203 |
assertValuesDropped(ts, m); |
| 204 |
long sum = 0; |
| 205 |
for (Long i : ts.values()) { |
| 206 |
sum += i; |
| 207 |
} |
| 208 |
//sum = (A1 + An) * n / 2 = 100_001 * 50_000 = 50_000_00000 + 50_000 = 50_000_50_000 |
| 209 |
Assert.assertEquals("Wrong sum: " + sum, 5000050000L, sum); |
| 210 |
} |
| 211 |
|
| 212 |
@Test |
| 213 |
public void nullPointerFromReducer() { |
| 214 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 215 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0); |
| 216 |
source.onBackpressureReduce((l, r) -> null).subscribe(ts); |
| 217 |
|
| 218 |
source.onNext(1); |
| 219 |
source.onNext(2); |
| 220 |
|
| 221 |
TestHelper.assertError(ts.errors(), 0, NullPointerException.class, "The reducer returned a null value"); |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void exceptionFromReducer() { |
| 226 |
PublishProcessor<Integer> source = PublishProcessor.create(); |
| 227 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0); |
| 228 |
source.onBackpressureReduce((l, r) -> { |
| 229 |
throw new TestException("Test exception"); |
| 230 |
}).subscribe(ts); |
| 231 |
|
| 232 |
source.onNext(1); |
| 233 |
source.onNext(2); |
| 234 |
|
| 235 |
TestHelper.assertError(ts.errors(), 0, TestException.class, "Test exception"); |
| 236 |
} |
| 237 |
|
| 238 |
@Test |
| 239 |
public void doubleOnSubscribe() { |
| 240 |
TestHelper.checkDoubleOnSubscribeFlowable(f -> f.onBackpressureReduce(TEST_OBJECT_REDUCER)); |
| 241 |
} |
| 242 |
|
| 243 |
@Test |
| 244 |
public void take() { |
| 245 |
Flowable.just(1, 2) |
| 246 |
.onBackpressureReduce(TEST_INT_REDUCER) |
| 247 |
.take(1) |
| 248 |
.test() |
| 249 |
.assertResult(1); |
| 250 |
} |
| 251 |
|
| 252 |
@Test |
| 253 |
public void dispose() { |
| 254 |
TestHelper.checkDisposed(Flowable.never().onBackpressureReduce(TEST_OBJECT_REDUCER)); |
| 255 |
} |
| 256 |
|
| 257 |
@Test |
| 258 |
public void badRequest() { |
| 259 |
TestHelper.assertBadRequestReported(Flowable.never().onBackpressureReduce(TEST_OBJECT_REDUCER)); |
| 260 |
} |
| 261 |
} |