| 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.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertEquals; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.List; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import org.junit.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.functions.*; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 30 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 31 |
|
| 32 |
public class ObservableReduceTest extends RxJavaTest { |
| 33 |
Observer<Object> observer; |
| 34 |
SingleObserver<Object> singleObserver; |
| 35 |
|
| 36 |
@Before |
| 37 |
public void before() { |
| 38 |
observer = TestHelper.mockObserver(); |
| 39 |
singleObserver = TestHelper.mockSingleObserver(); |
| 40 |
} |
| 41 |
|
| 42 |
BiFunction<Integer, Integer, Integer> sum = new BiFunction<Integer, Integer, Integer>() { |
| 43 |
@Override |
| 44 |
public Integer apply(Integer t1, Integer t2) { |
| 45 |
return t1 + t2; |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
@Test |
| 50 |
public void aggregateAsIntSumObservable() { |
| 51 |
|
| 52 |
Observable<Integer> result = Observable.just(1, 2, 3, 4, 5).reduce(0, sum) |
| 53 |
.map(new Function<Integer, Integer>() { |
| 54 |
@Override |
| 55 |
public Integer apply(Integer v) { |
| 56 |
return v; |
| 57 |
} |
| 58 |
}).toObservable(); |
| 59 |
|
| 60 |
result.subscribe(observer); |
| 61 |
|
| 62 |
verify(observer).onNext(1 + 2 + 3 + 4 + 5); |
| 63 |
verify(observer).onComplete(); |
| 64 |
verify(observer, never()).onError(any(Throwable.class)); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void aggregateAsIntSumSourceThrowsObservable() { |
| 69 |
Observable<Integer> result = Observable.concat(Observable.just(1, 2, 3, 4, 5), |
| 70 |
Observable.<Integer> error(new TestException())) |
| 71 |
.reduce(0, sum).map(new Function<Integer, Integer>() { |
| 72 |
@Override |
| 73 |
public Integer apply(Integer v) { |
| 74 |
return v; |
| 75 |
} |
| 76 |
}).toObservable(); |
| 77 |
|
| 78 |
result.subscribe(observer); |
| 79 |
|
| 80 |
verify(observer, never()).onNext(any()); |
| 81 |
verify(observer, never()).onComplete(); |
| 82 |
verify(observer, times(1)).onError(any(TestException.class)); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void aggregateAsIntSumAccumulatorThrowsObservable() { |
| 87 |
BiFunction<Integer, Integer, Integer> sumErr = new BiFunction<Integer, Integer, Integer>() { |
| 88 |
@Override |
| 89 |
public Integer apply(Integer t1, Integer t2) { |
| 90 |
throw new TestException(); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
Observable<Integer> result = Observable.just(1, 2, 3, 4, 5) |
| 95 |
.reduce(0, sumErr).map(new Function<Integer, Integer>() { |
| 96 |
@Override |
| 97 |
public Integer apply(Integer v) { |
| 98 |
return v; |
| 99 |
} |
| 100 |
}).toObservable(); |
| 101 |
|
| 102 |
result.subscribe(observer); |
| 103 |
|
| 104 |
verify(observer, never()).onNext(any()); |
| 105 |
verify(observer, never()).onComplete(); |
| 106 |
verify(observer, times(1)).onError(any(TestException.class)); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void aggregateAsIntSumResultSelectorThrowsObservable() { |
| 111 |
|
| 112 |
Function<Integer, Integer> error = new Function<Integer, Integer>() { |
| 113 |
|
| 114 |
@Override |
| 115 |
public Integer apply(Integer t1) { |
| 116 |
throw new TestException(); |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
Observable<Integer> result = Observable.just(1, 2, 3, 4, 5) |
| 121 |
.reduce(0, sum).toObservable().map(error); |
| 122 |
|
| 123 |
result.subscribe(observer); |
| 124 |
|
| 125 |
verify(observer, never()).onNext(any()); |
| 126 |
verify(observer, never()).onComplete(); |
| 127 |
verify(observer, times(1)).onError(any(TestException.class)); |
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void backpressureWithNoInitialValueObservable() throws InterruptedException { |
| 132 |
Observable<Integer> source = Observable.just(1, 2, 3, 4, 5, 6); |
| 133 |
Observable<Integer> reduced = source.reduce(sum).toObservable(); |
| 134 |
|
| 135 |
Integer r = reduced.blockingFirst(); |
| 136 |
assertEquals(21, r.intValue()); |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void backpressureWithInitialValueObservable() throws InterruptedException { |
| 141 |
Observable<Integer> source = Observable.just(1, 2, 3, 4, 5, 6); |
| 142 |
Observable<Integer> reduced = source.reduce(0, sum).toObservable(); |
| 143 |
|
| 144 |
Integer r = reduced.blockingFirst(); |
| 145 |
assertEquals(21, r.intValue()); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void aggregateAsIntSum() { |
| 150 |
|
| 151 |
Single<Integer> result = Observable.just(1, 2, 3, 4, 5).reduce(0, sum) |
| 152 |
.map(new Function<Integer, Integer>() { |
| 153 |
@Override |
| 154 |
public Integer apply(Integer v) { |
| 155 |
return v; |
| 156 |
} |
| 157 |
}); |
| 158 |
|
| 159 |
result.subscribe(singleObserver); |
| 160 |
|
| 161 |
verify(singleObserver).onSuccess(1 + 2 + 3 + 4 + 5); |
| 162 |
verify(singleObserver, never()).onError(any(Throwable.class)); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
public void aggregateAsIntSumSourceThrows() { |
| 167 |
Single<Integer> result = Observable.concat(Observable.just(1, 2, 3, 4, 5), |
| 168 |
Observable.<Integer> error(new TestException())) |
| 169 |
.reduce(0, sum).map(new Function<Integer, Integer>() { |
| 170 |
@Override |
| 171 |
public Integer apply(Integer v) { |
| 172 |
return v; |
| 173 |
} |
| 174 |
}); |
| 175 |
|
| 176 |
result.subscribe(singleObserver); |
| 177 |
|
| 178 |
verify(singleObserver, never()).onSuccess(any()); |
| 179 |
verify(singleObserver, times(1)).onError(any(TestException.class)); |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void aggregateAsIntSumAccumulatorThrows() { |
| 184 |
BiFunction<Integer, Integer, Integer> sumErr = new BiFunction<Integer, Integer, Integer>() { |
| 185 |
@Override |
| 186 |
public Integer apply(Integer t1, Integer t2) { |
| 187 |
throw new TestException(); |
| 188 |
} |
| 189 |
}; |
| 190 |
|
| 191 |
Single<Integer> result = Observable.just(1, 2, 3, 4, 5) |
| 192 |
.reduce(0, sumErr).map(new Function<Integer, Integer>() { |
| 193 |
@Override |
| 194 |
public Integer apply(Integer v) { |
| 195 |
return v; |
| 196 |
} |
| 197 |
}); |
| 198 |
|
| 199 |
result.subscribe(singleObserver); |
| 200 |
|
| 201 |
verify(singleObserver, never()).onSuccess(any()); |
| 202 |
verify(singleObserver, times(1)).onError(any(TestException.class)); |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void aggregateAsIntSumResultSelectorThrows() { |
| 207 |
|
| 208 |
Function<Integer, Integer> error = new Function<Integer, Integer>() { |
| 209 |
|
| 210 |
@Override |
| 211 |
public Integer apply(Integer t1) { |
| 212 |
throw new TestException(); |
| 213 |
} |
| 214 |
}; |
| 215 |
|
| 216 |
Single<Integer> result = Observable.just(1, 2, 3, 4, 5) |
| 217 |
.reduce(0, sum).map(error); |
| 218 |
|
| 219 |
result.subscribe(singleObserver); |
| 220 |
|
| 221 |
verify(singleObserver, never()).onSuccess(any()); |
| 222 |
verify(singleObserver, times(1)).onError(any(TestException.class)); |
| 223 |
} |
| 224 |
|
| 225 |
@Test |
| 226 |
public void backpressureWithNoInitialValue() throws InterruptedException { |
| 227 |
Observable<Integer> source = Observable.just(1, 2, 3, 4, 5, 6); |
| 228 |
Maybe<Integer> reduced = source.reduce(sum); |
| 229 |
|
| 230 |
Integer r = reduced.blockingGet(); |
| 231 |
assertEquals(21, r.intValue()); |
| 232 |
} |
| 233 |
|
| 234 |
@Test |
| 235 |
public void backpressureWithInitialValue() throws InterruptedException { |
| 236 |
Observable<Integer> source = Observable.just(1, 2, 3, 4, 5, 6); |
| 237 |
Single<Integer> reduced = source.reduce(0, sum); |
| 238 |
|
| 239 |
Integer r = reduced.blockingGet(); |
| 240 |
assertEquals(21, r.intValue()); |
| 241 |
} |
| 242 |
|
| 243 |
@Test |
| 244 |
public void reduceWithSingle() { |
| 245 |
Observable.range(1, 5) |
| 246 |
.reduceWith(new Supplier<Integer>() { |
| 247 |
@Override |
| 248 |
public Integer get() throws Exception { |
| 249 |
return 0; |
| 250 |
} |
| 251 |
}, new BiFunction<Integer, Integer, Integer>() { |
| 252 |
@Override |
| 253 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 254 |
return a + b; |
| 255 |
} |
| 256 |
}) |
| 257 |
.test() |
| 258 |
.assertResult(15); |
| 259 |
} |
| 260 |
|
| 261 |
@Test |
| 262 |
public void reduceMaybeDoubleOnSubscribe() { |
| 263 |
TestHelper.checkDoubleOnSubscribeObservableToMaybe(new Function<Observable<Object>, MaybeSource<Object>>() { |
| 264 |
@Override |
| 265 |
public MaybeSource<Object> apply(Observable<Object> o) |
| 266 |
throws Exception { |
| 267 |
return o.reduce(new BiFunction<Object, Object, Object>() { |
| 268 |
@Override |
| 269 |
public Object apply(Object a, Object b) throws Exception { |
| 270 |
return a; |
| 271 |
} |
| 272 |
}); |
| 273 |
} |
| 274 |
}); |
| 275 |
} |
| 276 |
|
| 277 |
@Test |
| 278 |
public void reduceMaybeCheckDisposed() { |
| 279 |
TestHelper.checkDisposed(Observable.just(new Object()).reduce(new BiFunction<Object, Object, Object>() { |
| 280 |
@Override |
| 281 |
public Object apply(Object a, Object b) throws Exception { |
| 282 |
return a; |
| 283 |
} |
| 284 |
})); |
| 285 |
} |
| 286 |
|
| 287 |
@Test |
| 288 |
public void reduceMaybeBadSource() { |
| 289 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 290 |
try { |
| 291 |
new Observable<Object>() { |
| 292 |
@Override |
| 293 |
protected void subscribeActual(Observer<? super Object> observer) { |
| 294 |
observer.onSubscribe(Disposable.empty()); |
| 295 |
observer.onComplete(); |
| 296 |
observer.onNext(1); |
| 297 |
observer.onError(new TestException()); |
| 298 |
observer.onComplete(); |
| 299 |
} |
| 300 |
}.reduce(new BiFunction<Object, Object, Object>() { |
| 301 |
@Override |
| 302 |
public Object apply(Object a, Object b) throws Exception { |
| 303 |
return a; |
| 304 |
} |
| 305 |
}) |
| 306 |
.test() |
| 307 |
.assertResult(); |
| 308 |
|
| 309 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 310 |
} finally { |
| 311 |
RxJavaPlugins.reset(); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
@Test |
| 316 |
public void seedDoubleOnSubscribe() { |
| 317 |
TestHelper.checkDoubleOnSubscribeObservableToSingle(new Function<Observable<Integer>, SingleSource<Integer>>() { |
| 318 |
@Override |
| 319 |
public SingleSource<Integer> apply(Observable<Integer> o) |
| 320 |
throws Exception { |
| 321 |
return o.reduce(0, new BiFunction<Integer, Integer, Integer>() { |
| 322 |
@Override |
| 323 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 324 |
return a; |
| 325 |
} |
| 326 |
}); |
| 327 |
} |
| 328 |
}); |
| 329 |
} |
| 330 |
|
| 331 |
@Test |
| 332 |
public void seedDisposed() { |
| 333 |
TestHelper.checkDisposed(PublishSubject.<Integer>create().reduce(0, new BiFunction<Integer, Integer, Integer>() { |
| 334 |
@Override |
| 335 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 336 |
return a; |
| 337 |
} |
| 338 |
})); |
| 339 |
} |
| 340 |
|
| 341 |
@Test |
| 342 |
public void seedBadSource() { |
| 343 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 344 |
try { |
| 345 |
new Observable<Integer>() { |
| 346 |
@Override |
| 347 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 348 |
observer.onSubscribe(Disposable.empty()); |
| 349 |
observer.onComplete(); |
| 350 |
observer.onNext(1); |
| 351 |
observer.onError(new TestException()); |
| 352 |
observer.onComplete(); |
| 353 |
} |
| 354 |
} |
| 355 |
.reduce(0, new BiFunction<Integer, Integer, Integer>() { |
| 356 |
@Override |
| 357 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 358 |
return a; |
| 359 |
} |
| 360 |
}) |
| 361 |
.test() |
| 362 |
.assertResult(0); |
| 363 |
|
| 364 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 365 |
} finally { |
| 366 |
RxJavaPlugins.reset(); |
| 367 |
} |
| 368 |
} |
| 369 |
} |