| 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.assertFalse; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.util.*; |
| 20 |
import java.util.function.*; |
| 21 |
import java.util.stream.*; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.reactivestreams.Subscriber; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 29 |
import io.reactivex.rxjava3.processors.*; |
| 30 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 31 |
|
| 32 |
public class FlowableCollectWithCollectorTest extends RxJavaTest { |
| 33 |
|
| 34 |
@Test |
| 35 |
public void basic() { |
| 36 |
Flowable.range(1, 5) |
| 37 |
.collect(Collectors.toList()) |
| 38 |
.test() |
| 39 |
.assertResult(Arrays.asList(1, 2, 3, 4, 5)); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void empty() { |
| 44 |
Flowable.empty() |
| 45 |
.collect(Collectors.toList()) |
| 46 |
.test() |
| 47 |
.assertResult(Collections.emptyList()); |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void error() { |
| 52 |
Flowable.error(new TestException()) |
| 53 |
.collect(Collectors.toList()) |
| 54 |
.test() |
| 55 |
.assertFailure(TestException.class); |
| 56 |
} |
| 57 |
|
| 58 |
@Test |
| 59 |
public void collectorSupplierCrash() { |
| 60 |
Flowable.range(1, 5) |
| 61 |
.collect(new Collector<Integer, Integer, Integer>() { |
| 62 |
|
| 63 |
@Override |
| 64 |
public Supplier<Integer> supplier() { |
| 65 |
throw new TestException(); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public BiConsumer<Integer, Integer> accumulator() { |
| 70 |
return (a, b) -> { }; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public BinaryOperator<Integer> combiner() { |
| 75 |
return (a, b) -> a + b; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public Function<Integer, Integer> finisher() { |
| 80 |
return a -> a; |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public Set<Characteristics> characteristics() { |
| 85 |
return Collections.emptySet(); |
| 86 |
} |
| 87 |
}) |
| 88 |
.test() |
| 89 |
.assertFailure(TestException.class); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void collectorAccumulatorCrash() { |
| 94 |
BehaviorProcessor<Integer> source = BehaviorProcessor.createDefault(1); |
| 95 |
|
| 96 |
source |
| 97 |
.collect(new Collector<Integer, Integer, Integer>() { |
| 98 |
|
| 99 |
@Override |
| 100 |
public Supplier<Integer> supplier() { |
| 101 |
return () -> 1; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public BiConsumer<Integer, Integer> accumulator() { |
| 106 |
return (a, b) -> { throw new TestException(); }; |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public BinaryOperator<Integer> combiner() { |
| 111 |
return (a, b) -> a + b; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public Function<Integer, Integer> finisher() { |
| 116 |
return a -> a; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public Set<Characteristics> characteristics() { |
| 121 |
return Collections.emptySet(); |
| 122 |
} |
| 123 |
}) |
| 124 |
.test() |
| 125 |
.assertFailure(TestException.class); |
| 126 |
|
| 127 |
assertFalse(source.hasSubscribers()); |
| 128 |
} |
| 129 |
|
| 130 |
@Test |
| 131 |
public void collectorFinisherCrash() { |
| 132 |
Flowable.range(1, 5) |
| 133 |
.collect(new Collector<Integer, Integer, Integer>() { |
| 134 |
|
| 135 |
@Override |
| 136 |
public Supplier<Integer> supplier() { |
| 137 |
return () -> 1; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public BiConsumer<Integer, Integer> accumulator() { |
| 142 |
return (a, b) -> { }; |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public BinaryOperator<Integer> combiner() { |
| 147 |
return (a, b) -> a + b; |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public Function<Integer, Integer> finisher() { |
| 152 |
return a -> { throw new TestException(); }; |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public Set<Characteristics> characteristics() { |
| 157 |
return Collections.emptySet(); |
| 158 |
} |
| 159 |
}) |
| 160 |
.test() |
| 161 |
.assertFailure(TestException.class); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void collectorAccumulatorDropSignals() throws Throwable { |
| 166 |
TestHelper.withErrorTracking(errors -> { |
| 167 |
Flowable<Integer> source = new Flowable<Integer>() { |
| 168 |
@Override |
| 169 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 170 |
s.onSubscribe(new BooleanSubscription()); |
| 171 |
s.onNext(1); |
| 172 |
s.onNext(2); |
| 173 |
s.onError(new IOException()); |
| 174 |
s.onComplete(); |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
source |
| 179 |
.collect(new Collector<Integer, Integer, Integer>() { |
| 180 |
|
| 181 |
@Override |
| 182 |
public Supplier<Integer> supplier() { |
| 183 |
return () -> 1; |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public BiConsumer<Integer, Integer> accumulator() { |
| 188 |
return (a, b) -> { throw new TestException(); }; |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public BinaryOperator<Integer> combiner() { |
| 193 |
return (a, b) -> a + b; |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public Function<Integer, Integer> finisher() { |
| 198 |
return a -> a; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public Set<Characteristics> characteristics() { |
| 203 |
return Collections.emptySet(); |
| 204 |
} |
| 205 |
}) |
| 206 |
.test() |
| 207 |
.assertFailure(TestException.class); |
| 208 |
|
| 209 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 210 |
}); |
| 211 |
} |
| 212 |
|
| 213 |
@Test |
| 214 |
public void dispose() { |
| 215 |
TestHelper.checkDisposed(PublishProcessor.create() |
| 216 |
.collect(Collectors.toList())); |
| 217 |
} |
| 218 |
|
| 219 |
@Test |
| 220 |
public void onSubscribe() { |
| 221 |
TestHelper.checkDoubleOnSubscribeFlowableToSingle(f -> f.collect(Collectors.toList())); |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void basicToFlowable() { |
| 226 |
Flowable.range(1, 5) |
| 227 |
.collect(Collectors.toList()) |
| 228 |
.toFlowable() |
| 229 |
.test() |
| 230 |
.assertResult(Arrays.asList(1, 2, 3, 4, 5)); |
| 231 |
} |
| 232 |
|
| 233 |
@Test |
| 234 |
public void emptyToFlowable() { |
| 235 |
Flowable.empty() |
| 236 |
.collect(Collectors.toList()) |
| 237 |
.toFlowable() |
| 238 |
.test() |
| 239 |
.assertResult(Collections.emptyList()); |
| 240 |
} |
| 241 |
|
| 242 |
@Test |
| 243 |
public void errorToFlowable() { |
| 244 |
Flowable.error(new TestException()) |
| 245 |
.collect(Collectors.toList()) |
| 246 |
.toFlowable() |
| 247 |
.test() |
| 248 |
.assertFailure(TestException.class); |
| 249 |
} |
| 250 |
|
| 251 |
@Test |
| 252 |
public void collectorSupplierCrashToFlowable() { |
| 253 |
Flowable.range(1, 5) |
| 254 |
.collect(new Collector<Integer, Integer, Integer>() { |
| 255 |
|
| 256 |
@Override |
| 257 |
public Supplier<Integer> supplier() { |
| 258 |
throw new TestException(); |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public BiConsumer<Integer, Integer> accumulator() { |
| 263 |
return (a, b) -> { }; |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
public BinaryOperator<Integer> combiner() { |
| 268 |
return (a, b) -> a + b; |
| 269 |
} |
| 270 |
|
| 271 |
@Override |
| 272 |
public Function<Integer, Integer> finisher() { |
| 273 |
return a -> a; |
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public Set<Characteristics> characteristics() { |
| 278 |
return Collections.emptySet(); |
| 279 |
} |
| 280 |
}) |
| 281 |
.toFlowable() |
| 282 |
.test() |
| 283 |
.assertFailure(TestException.class); |
| 284 |
} |
| 285 |
|
| 286 |
@Test |
| 287 |
public void collectorAccumulatorCrashToFlowable() { |
| 288 |
BehaviorProcessor<Integer> source = BehaviorProcessor.createDefault(1); |
| 289 |
|
| 290 |
source |
| 291 |
.collect(new Collector<Integer, Integer, Integer>() { |
| 292 |
|
| 293 |
@Override |
| 294 |
public Supplier<Integer> supplier() { |
| 295 |
return () -> 1; |
| 296 |
} |
| 297 |
|
| 298 |
@Override |
| 299 |
public BiConsumer<Integer, Integer> accumulator() { |
| 300 |
return (a, b) -> { throw new TestException(); }; |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public BinaryOperator<Integer> combiner() { |
| 305 |
return (a, b) -> a + b; |
| 306 |
} |
| 307 |
|
| 308 |
@Override |
| 309 |
public Function<Integer, Integer> finisher() { |
| 310 |
return a -> a; |
| 311 |
} |
| 312 |
|
| 313 |
@Override |
| 314 |
public Set<Characteristics> characteristics() { |
| 315 |
return Collections.emptySet(); |
| 316 |
} |
| 317 |
}) |
| 318 |
.toFlowable() |
| 319 |
.test() |
| 320 |
.assertFailure(TestException.class); |
| 321 |
|
| 322 |
assertFalse(source.hasSubscribers()); |
| 323 |
} |
| 324 |
|
| 325 |
@Test |
| 326 |
public void collectorFinisherCrashToFlowable() { |
| 327 |
Flowable.range(1, 5) |
| 328 |
.collect(new Collector<Integer, Integer, Integer>() { |
| 329 |
|
| 330 |
@Override |
| 331 |
public Supplier<Integer> supplier() { |
| 332 |
return () -> 1; |
| 333 |
} |
| 334 |
|
| 335 |
@Override |
| 336 |
public BiConsumer<Integer, Integer> accumulator() { |
| 337 |
return (a, b) -> { }; |
| 338 |
} |
| 339 |
|
| 340 |
@Override |
| 341 |
public BinaryOperator<Integer> combiner() { |
| 342 |
return (a, b) -> a + b; |
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
public Function<Integer, Integer> finisher() { |
| 347 |
return a -> { throw new TestException(); }; |
| 348 |
} |
| 349 |
|
| 350 |
@Override |
| 351 |
public Set<Characteristics> characteristics() { |
| 352 |
return Collections.emptySet(); |
| 353 |
} |
| 354 |
}) |
| 355 |
.toFlowable() |
| 356 |
.test() |
| 357 |
.assertFailure(TestException.class); |
| 358 |
} |
| 359 |
|
| 360 |
@Test |
| 361 |
public void collectorAccumulatorDropSignalsToFlowable() throws Throwable { |
| 362 |
TestHelper.withErrorTracking(errors -> { |
| 363 |
Flowable<Integer> source = new Flowable<Integer>() { |
| 364 |
@Override |
| 365 |
protected void subscribeActual(Subscriber<? super Integer> s) { |
| 366 |
s.onSubscribe(new BooleanSubscription()); |
| 367 |
s.onNext(1); |
| 368 |
s.onNext(2); |
| 369 |
s.onError(new IOException()); |
| 370 |
s.onComplete(); |
| 371 |
} |
| 372 |
}; |
| 373 |
|
| 374 |
source |
| 375 |
.collect(new Collector<Integer, Integer, Integer>() { |
| 376 |
|
| 377 |
@Override |
| 378 |
public Supplier<Integer> supplier() { |
| 379 |
return () -> 1; |
| 380 |
} |
| 381 |
|
| 382 |
@Override |
| 383 |
public BiConsumer<Integer, Integer> accumulator() { |
| 384 |
return (a, b) -> { throw new TestException(); }; |
| 385 |
} |
| 386 |
|
| 387 |
@Override |
| 388 |
public BinaryOperator<Integer> combiner() { |
| 389 |
return (a, b) -> a + b; |
| 390 |
} |
| 391 |
|
| 392 |
@Override |
| 393 |
public Function<Integer, Integer> finisher() { |
| 394 |
return a -> a; |
| 395 |
} |
| 396 |
|
| 397 |
@Override |
| 398 |
public Set<Characteristics> characteristics() { |
| 399 |
return Collections.emptySet(); |
| 400 |
} |
| 401 |
}) |
| 402 |
.toFlowable() |
| 403 |
.test() |
| 404 |
.assertFailure(TestException.class); |
| 405 |
|
| 406 |
TestHelper.assertUndeliverable(errors, 0, IOException.class); |
| 407 |
}); |
| 408 |
} |
| 409 |
|
| 410 |
@Test |
| 411 |
public void disposeToFlowable() { |
| 412 |
TestHelper.checkDisposed(PublishProcessor.create() |
| 413 |
.collect(Collectors.toList()).toFlowable()); |
| 414 |
} |
| 415 |
|
| 416 |
@Test |
| 417 |
public void onSubscribeToFlowable() { |
| 418 |
TestHelper.checkDoubleOnSubscribeFlowable(f -> f.collect(Collectors.toList()).toFlowable()); |
| 419 |
} |
| 420 |
} |