| 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.parallel; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
import org.reactivestreams.Subscriber; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.*; |
| 26 |
import io.reactivex.rxjava3.functions.Predicate; |
| 27 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 28 |
import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler; |
| 29 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 34 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 35 |
|
| 36 |
public class ParallelRunOnTest extends RxJavaTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void subscriberCount() { |
| 40 |
ParallelFlowableTest.checkSubscriberCount(Flowable.range(1, 5).parallel() |
| 41 |
.runOn(Schedulers.computation())); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void doubleError() { |
| 46 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 47 |
try { |
| 48 |
new ParallelInvalid() |
| 49 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 50 |
.sequential() |
| 51 |
.test() |
| 52 |
.assertFailure(TestException.class); |
| 53 |
|
| 54 |
assertFalse(errors.isEmpty()); |
| 55 |
for (Throwable ex : errors) { |
| 56 |
assertTrue(ex.toString(), ex.getCause() instanceof TestException); |
| 57 |
} |
| 58 |
} finally { |
| 59 |
RxJavaPlugins.reset(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void conditionalPath() { |
| 65 |
Flowable.range(1, 1000) |
| 66 |
.parallel(2) |
| 67 |
.runOn(Schedulers.computation()) |
| 68 |
.filter(new Predicate<Integer>() { |
| 69 |
@Override |
| 70 |
public boolean test(Integer v) throws Exception { |
| 71 |
return v % 2 == 0; |
| 72 |
} |
| 73 |
}) |
| 74 |
.sequential() |
| 75 |
.test() |
| 76 |
.awaitDone(5, TimeUnit.SECONDS) |
| 77 |
.assertValueCount(500) |
| 78 |
.assertNoErrors() |
| 79 |
.assertComplete(); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void missingBackpressure() { |
| 84 |
new ParallelFlowable<Integer>() { |
| 85 |
@Override |
| 86 |
public int parallelism() { |
| 87 |
return 1; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void subscribe(Subscriber<? super Integer>[] subscribers) { |
| 92 |
subscribers[0].onSubscribe(new BooleanSubscription()); |
| 93 |
subscribers[0].onNext(1); |
| 94 |
subscribers[0].onNext(2); |
| 95 |
subscribers[0].onNext(3); |
| 96 |
} |
| 97 |
} |
| 98 |
.runOn(ImmediateThinScheduler.INSTANCE, 1) |
| 99 |
.sequential(1) |
| 100 |
.test(0) |
| 101 |
.assertFailure(MissingBackpressureException.class); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void error() { |
| 106 |
Flowable.error(new TestException()) |
| 107 |
.parallel(1) |
| 108 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 109 |
.sequential() |
| 110 |
.test() |
| 111 |
.assertFailure(TestException.class); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void errorBackpressured() { |
| 116 |
Flowable.error(new TestException()) |
| 117 |
.parallel(1) |
| 118 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 119 |
.sequential(1) |
| 120 |
.test(0) |
| 121 |
.assertFailure(TestException.class); |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void errorConditional() { |
| 126 |
Flowable.error(new TestException()) |
| 127 |
.parallel(1) |
| 128 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 129 |
.filter(Functions.alwaysTrue()) |
| 130 |
.sequential() |
| 131 |
.test() |
| 132 |
.assertFailure(TestException.class); |
| 133 |
} |
| 134 |
|
| 135 |
@SuppressWarnings("unchecked") |
| 136 |
@Test |
| 137 |
public void errorConditionalBackpressured() { |
| 138 |
TestSubscriber<Object> ts = new TestSubscriber<>(0L); |
| 139 |
|
| 140 |
Flowable.error(new TestException()) |
| 141 |
.parallel(1) |
| 142 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 143 |
.filter(Functions.alwaysTrue()) |
| 144 |
.subscribe(new Subscriber[] { ts }); |
| 145 |
|
| 146 |
ts |
| 147 |
.assertFailure(TestException.class); |
| 148 |
} |
| 149 |
|
| 150 |
@SuppressWarnings("unchecked") |
| 151 |
@Test |
| 152 |
public void emptyConditionalBackpressured() { |
| 153 |
TestSubscriber<Object> ts = new TestSubscriber<>(0L); |
| 154 |
|
| 155 |
Flowable.empty() |
| 156 |
.parallel(1) |
| 157 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 158 |
.filter(Functions.alwaysTrue()) |
| 159 |
.subscribe(new Subscriber[] { ts }); |
| 160 |
|
| 161 |
ts |
| 162 |
.assertResult(); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
public void nextCancelRace() { |
| 167 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 168 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 169 |
|
| 170 |
final TestSubscriber<Integer> ts = pp.parallel(1) |
| 171 |
.runOn(Schedulers.computation()) |
| 172 |
.sequential() |
| 173 |
.test(); |
| 174 |
|
| 175 |
Runnable r1 = new Runnable() { |
| 176 |
@Override |
| 177 |
public void run() { |
| 178 |
pp.onNext(1); |
| 179 |
} |
| 180 |
}; |
| 181 |
|
| 182 |
Runnable r2 = new Runnable() { |
| 183 |
@Override |
| 184 |
public void run() { |
| 185 |
ts.cancel(); |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
TestHelper.race(r1, r2); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
@SuppressWarnings("unchecked") |
| 194 |
@Test |
| 195 |
public void nextCancelRaceBackpressured() { |
| 196 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 197 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 198 |
|
| 199 |
final TestSubscriber<Integer> ts = TestSubscriber.create(0L); |
| 200 |
|
| 201 |
pp.parallel(1) |
| 202 |
.runOn(Schedulers.computation()) |
| 203 |
.subscribe(new Subscriber[] { ts }); |
| 204 |
|
| 205 |
Runnable r1 = new Runnable() { |
| 206 |
@Override |
| 207 |
public void run() { |
| 208 |
pp.onNext(1); |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
Runnable r2 = new Runnable() { |
| 213 |
@Override |
| 214 |
public void run() { |
| 215 |
ts.cancel(); |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
TestHelper.race(r1, r2); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void nextCancelRaceConditional() { |
| 225 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 226 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 227 |
|
| 228 |
final TestSubscriber<Integer> ts = pp.parallel(1) |
| 229 |
.runOn(Schedulers.computation()) |
| 230 |
.filter(Functions.alwaysTrue()) |
| 231 |
.sequential() |
| 232 |
.test(); |
| 233 |
|
| 234 |
Runnable r1 = new Runnable() { |
| 235 |
@Override |
| 236 |
public void run() { |
| 237 |
pp.onNext(1); |
| 238 |
} |
| 239 |
}; |
| 240 |
|
| 241 |
Runnable r2 = new Runnable() { |
| 242 |
@Override |
| 243 |
public void run() { |
| 244 |
ts.cancel(); |
| 245 |
} |
| 246 |
}; |
| 247 |
|
| 248 |
TestHelper.race(r1, r2); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
@SuppressWarnings("unchecked") |
| 253 |
@Test |
| 254 |
public void nextCancelRaceBackpressuredConditional() { |
| 255 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 256 |
final PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 257 |
|
| 258 |
final TestSubscriber<Integer> ts = TestSubscriber.create(0L); |
| 259 |
|
| 260 |
pp.parallel(1) |
| 261 |
.runOn(Schedulers.computation()) |
| 262 |
.filter(Functions.alwaysTrue()) |
| 263 |
.subscribe(new Subscriber[] { ts }); |
| 264 |
|
| 265 |
Runnable r1 = new Runnable() { |
| 266 |
@Override |
| 267 |
public void run() { |
| 268 |
pp.onNext(1); |
| 269 |
} |
| 270 |
}; |
| 271 |
|
| 272 |
Runnable r2 = new Runnable() { |
| 273 |
@Override |
| 274 |
public void run() { |
| 275 |
ts.cancel(); |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
TestHelper.race(r1, r2); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
@SuppressWarnings("unchecked") |
| 284 |
@Test |
| 285 |
public void normalCancelAfterRequest1() { |
| 286 |
|
| 287 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1) { |
| 288 |
@Override |
| 289 |
public void onNext(Integer t) { |
| 290 |
super.onNext(t); |
| 291 |
cancel(); |
| 292 |
onComplete(); |
| 293 |
} |
| 294 |
}; |
| 295 |
|
| 296 |
Flowable.range(1, 5) |
| 297 |
.parallel(1) |
| 298 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 299 |
.subscribe(new Subscriber[] { ts }); |
| 300 |
|
| 301 |
ts.assertResult(1); |
| 302 |
} |
| 303 |
|
| 304 |
@SuppressWarnings("unchecked") |
| 305 |
@Test |
| 306 |
public void conditionalCancelAfterRequest1() { |
| 307 |
|
| 308 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1) { |
| 309 |
@Override |
| 310 |
public void onNext(Integer t) { |
| 311 |
super.onNext(t); |
| 312 |
cancel(); |
| 313 |
onComplete(); |
| 314 |
} |
| 315 |
}; |
| 316 |
|
| 317 |
Flowable.range(1, 5) |
| 318 |
.parallel(1) |
| 319 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 320 |
.filter(Functions.alwaysTrue()) |
| 321 |
.subscribe(new Subscriber[] { ts }); |
| 322 |
|
| 323 |
ts.assertResult(1); |
| 324 |
} |
| 325 |
|
| 326 |
@Test |
| 327 |
public void doubleOnSubscribe() { |
| 328 |
TestHelper.checkDoubleOnSubscribeParallel(pf -> pf.runOn(ImmediateThinScheduler.INSTANCE)); |
| 329 |
} |
| 330 |
|
| 331 |
@Test |
| 332 |
public void doubleOnSubscribeConditional() { |
| 333 |
TestHelper.checkDoubleOnSubscribeParallel(pf -> |
| 334 |
pf.runOn(ImmediateThinScheduler.INSTANCE) |
| 335 |
.filter(v -> true) |
| 336 |
); |
| 337 |
} |
| 338 |
|
| 339 |
@Test |
| 340 |
public void badRequest() { |
| 341 |
TestHelper.assertBadRequestReported( |
| 342 |
ParallelFlowable.fromArray(PublishProcessor.create()) |
| 343 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 344 |
); |
| 345 |
} |
| 346 |
|
| 347 |
@SuppressWarnings("unchecked") |
| 348 |
@Test |
| 349 |
public void asManyItemsAsRequested() { |
| 350 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0); |
| 351 |
|
| 352 |
Flowable.range(1, 5) |
| 353 |
.parallel(1) |
| 354 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 355 |
.subscribe(new Subscriber[] { |
| 356 |
ts |
| 357 |
}); |
| 358 |
|
| 359 |
ts |
| 360 |
.requestMore(5) |
| 361 |
.assertResult(1, 2, 3, 4, 5); |
| 362 |
} |
| 363 |
|
| 364 |
@SuppressWarnings("unchecked") |
| 365 |
@Test |
| 366 |
public void asManyItemsAsRequestedConditional() { |
| 367 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0); |
| 368 |
|
| 369 |
Flowable.range(1, 5) |
| 370 |
.parallel(1) |
| 371 |
.runOn(ImmediateThinScheduler.INSTANCE) |
| 372 |
.filter(v -> true) |
| 373 |
.subscribe(new Subscriber[] { |
| 374 |
ts |
| 375 |
}); |
| 376 |
|
| 377 |
ts |
| 378 |
.requestMore(5) |
| 379 |
.assertResult(1, 2, 3, 4, 5); |
| 380 |
} |
| 381 |
} |