| 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 static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
|
| 22 |
import org.junit.*; |
| 23 |
import org.mockito.MockitoAnnotations; |
| 24 |
import org.reactivestreams.*; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.*; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 30 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableGroupJoin.*; |
| 31 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 32 |
import io.reactivex.rxjava3.processors.*; |
| 33 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 34 |
import io.reactivex.rxjava3.testsupport.*; |
| 35 |
|
| 36 |
public class FlowableGroupJoinTest extends RxJavaTest { |
| 37 |
|
| 38 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 39 |
|
| 40 |
BiFunction<Integer, Integer, Integer> add = new BiFunction<Integer, Integer, Integer>() { |
| 41 |
@Override |
| 42 |
public Integer apply(Integer t1, Integer t2) { |
| 43 |
return t1 + t2; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
<T> Function<Integer, Flowable<T>> just(final Flowable<T> flowable) { |
| 48 |
return new Function<Integer, Flowable<T>>() { |
| 49 |
@Override |
| 50 |
public Flowable<T> apply(Integer t1) { |
| 51 |
return flowable; |
| 52 |
} |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
<T, R> Function<T, Flowable<R>> just2(final Flowable<R> flowable) { |
| 57 |
return new Function<T, Flowable<R>>() { |
| 58 |
@Override |
| 59 |
public Flowable<R> apply(T t1) { |
| 60 |
return flowable; |
| 61 |
} |
| 62 |
}; |
| 63 |
} |
| 64 |
|
| 65 |
BiFunction<Integer, Flowable<Integer>, Flowable<Integer>> add2 = new BiFunction<Integer, Flowable<Integer>, Flowable<Integer>>() { |
| 66 |
@Override |
| 67 |
public Flowable<Integer> apply(final Integer leftValue, Flowable<Integer> rightValues) { |
| 68 |
return rightValues.map(new Function<Integer, Integer>() { |
| 69 |
@Override |
| 70 |
public Integer apply(Integer rightValue) throws Throwable { |
| 71 |
return add.apply(leftValue, rightValue); |
| 72 |
} |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
}; |
| 77 |
|
| 78 |
@Before |
| 79 |
public void before() { |
| 80 |
MockitoAnnotations.openMocks(this); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void behaveAsJoin() { |
| 85 |
PublishProcessor<Integer> source1 = PublishProcessor.create(); |
| 86 |
PublishProcessor<Integer> source2 = PublishProcessor.create(); |
| 87 |
|
| 88 |
Flowable<Integer> m = Flowable.merge(source1.groupJoin(source2, |
| 89 |
just(Flowable.never()), |
| 90 |
just(Flowable.never()), add2)); |
| 91 |
|
| 92 |
m.subscribe(subscriber); |
| 93 |
|
| 94 |
source1.onNext(1); |
| 95 |
source1.onNext(2); |
| 96 |
source1.onNext(4); |
| 97 |
|
| 98 |
source2.onNext(16); |
| 99 |
source2.onNext(32); |
| 100 |
source2.onNext(64); |
| 101 |
|
| 102 |
source1.onComplete(); |
| 103 |
source2.onComplete(); |
| 104 |
|
| 105 |
verify(subscriber, times(1)).onNext(17); |
| 106 |
verify(subscriber, times(1)).onNext(18); |
| 107 |
verify(subscriber, times(1)).onNext(20); |
| 108 |
verify(subscriber, times(1)).onNext(33); |
| 109 |
verify(subscriber, times(1)).onNext(34); |
| 110 |
verify(subscriber, times(1)).onNext(36); |
| 111 |
verify(subscriber, times(1)).onNext(65); |
| 112 |
verify(subscriber, times(1)).onNext(66); |
| 113 |
verify(subscriber, times(1)).onNext(68); |
| 114 |
|
| 115 |
verify(subscriber, times(1)).onComplete(); //Never emitted? |
| 116 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 117 |
} |
| 118 |
|
| 119 |
class Person { |
| 120 |
final int id; |
| 121 |
final String name; |
| 122 |
|
| 123 |
Person(int id, String name) { |
| 124 |
this.id = id; |
| 125 |
this.name = name; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
class PersonFruit { |
| 130 |
final int personId; |
| 131 |
final String fruit; |
| 132 |
|
| 133 |
PersonFruit(int personId, String fruit) { |
| 134 |
this.personId = personId; |
| 135 |
this.fruit = fruit; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
class PPF { |
| 140 |
final Person person; |
| 141 |
final Flowable<PersonFruit> fruits; |
| 142 |
|
| 143 |
PPF(Person person, Flowable<PersonFruit> fruits) { |
| 144 |
this.person = person; |
| 145 |
this.fruits = fruits; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void normal1() { |
| 151 |
Flowable<Person> source1 = Flowable.fromIterable(Arrays.asList( |
| 152 |
new Person(1, "Joe"), |
| 153 |
new Person(2, "Mike"), |
| 154 |
new Person(3, "Charlie") |
| 155 |
)); |
| 156 |
|
| 157 |
Flowable<PersonFruit> source2 = Flowable.fromIterable(Arrays.asList( |
| 158 |
new PersonFruit(1, "Strawberry"), |
| 159 |
new PersonFruit(1, "Apple"), |
| 160 |
new PersonFruit(3, "Peach") |
| 161 |
)); |
| 162 |
|
| 163 |
Flowable<PPF> q = source1.groupJoin( |
| 164 |
source2, |
| 165 |
just2(Flowable.<Object> never()), |
| 166 |
just2(Flowable.<Object> never()), |
| 167 |
new BiFunction<Person, Flowable<PersonFruit>, PPF>() { |
| 168 |
@Override |
| 169 |
public PPF apply(Person t1, Flowable<PersonFruit> t2) { |
| 170 |
return new PPF(t1, t2); |
| 171 |
} |
| 172 |
}); |
| 173 |
|
| 174 |
q.subscribe( |
| 175 |
new FlowableSubscriber<PPF>() { |
| 176 |
@Override |
| 177 |
public void onNext(final PPF ppf) { |
| 178 |
ppf.fruits.filter(new Predicate<PersonFruit>() { |
| 179 |
@Override |
| 180 |
public boolean test(PersonFruit t1) { |
| 181 |
return ppf.person.id == t1.personId; |
| 182 |
} |
| 183 |
}).subscribe(new Consumer<PersonFruit>() { |
| 184 |
@Override |
| 185 |
public void accept(PersonFruit t1) { |
| 186 |
subscriber.onNext(Arrays.asList(ppf.person.name, t1.fruit)); |
| 187 |
} |
| 188 |
}); |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public void onError(Throwable e) { |
| 193 |
subscriber.onError(e); |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public void onComplete() { |
| 198 |
subscriber.onComplete(); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public void onSubscribe(Subscription s) { |
| 203 |
s.request(Long.MAX_VALUE); |
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
); |
| 208 |
|
| 209 |
verify(subscriber, times(1)).onNext(Arrays.asList("Joe", "Strawberry")); |
| 210 |
verify(subscriber, times(1)).onNext(Arrays.asList("Joe", "Apple")); |
| 211 |
verify(subscriber, times(1)).onNext(Arrays.asList("Charlie", "Peach")); |
| 212 |
|
| 213 |
verify(subscriber, times(1)).onComplete(); |
| 214 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 215 |
} |
| 216 |
|
| 217 |
@Test |
| 218 |
public void leftThrows() { |
| 219 |
PublishProcessor<Integer> source1 = PublishProcessor.create(); |
| 220 |
PublishProcessor<Integer> source2 = PublishProcessor.create(); |
| 221 |
|
| 222 |
Flowable<Flowable<Integer>> m = source1.groupJoin(source2, |
| 223 |
just(Flowable.never()), |
| 224 |
just(Flowable.never()), add2); |
| 225 |
|
| 226 |
m.subscribe(subscriber); |
| 227 |
|
| 228 |
source2.onNext(1); |
| 229 |
source1.onError(new RuntimeException("Forced failure")); |
| 230 |
|
| 231 |
verify(subscriber, times(1)).onError(any(Throwable.class)); |
| 232 |
verify(subscriber, never()).onComplete(); |
| 233 |
verify(subscriber, never()).onNext(any()); |
| 234 |
} |
| 235 |
|
| 236 |
@Test |
| 237 |
public void rightThrows() { |
| 238 |
PublishProcessor<Integer> source1 = PublishProcessor.create(); |
| 239 |
PublishProcessor<Integer> source2 = PublishProcessor.create(); |
| 240 |
|
| 241 |
Flowable<Flowable<Integer>> m = source1.groupJoin(source2, |
| 242 |
just(Flowable.never()), |
| 243 |
just(Flowable.never()), add2); |
| 244 |
|
| 245 |
m.subscribe(subscriber); |
| 246 |
|
| 247 |
source1.onNext(1); |
| 248 |
source2.onError(new RuntimeException("Forced failure")); |
| 249 |
|
| 250 |
verify(subscriber, times(1)).onNext(any(Flowable.class)); |
| 251 |
verify(subscriber, times(1)).onError(any(Throwable.class)); |
| 252 |
verify(subscriber, never()).onComplete(); |
| 253 |
} |
| 254 |
|
| 255 |
@Test |
| 256 |
public void leftDurationThrows() { |
| 257 |
PublishProcessor<Integer> source1 = PublishProcessor.create(); |
| 258 |
PublishProcessor<Integer> source2 = PublishProcessor.create(); |
| 259 |
|
| 260 |
Flowable<Integer> duration1 = Flowable.<Integer> error(new RuntimeException("Forced failure")); |
| 261 |
|
| 262 |
Flowable<Flowable<Integer>> m = source1.groupJoin(source2, |
| 263 |
just(duration1), |
| 264 |
just(Flowable.never()), add2); |
| 265 |
m.subscribe(subscriber); |
| 266 |
|
| 267 |
source1.onNext(1); |
| 268 |
|
| 269 |
verify(subscriber, times(1)).onError(any(Throwable.class)); |
| 270 |
verify(subscriber, never()).onComplete(); |
| 271 |
verify(subscriber, never()).onNext(any()); |
| 272 |
} |
| 273 |
|
| 274 |
@Test |
| 275 |
public void rightDurationThrows() { |
| 276 |
PublishProcessor<Integer> source1 = PublishProcessor.create(); |
| 277 |
PublishProcessor<Integer> source2 = PublishProcessor.create(); |
| 278 |
|
| 279 |
Flowable<Integer> duration1 = Flowable.<Integer> error(new RuntimeException("Forced failure")); |
| 280 |
|
| 281 |
Flowable<Flowable<Integer>> m = source1.groupJoin(source2, |
| 282 |
just(Flowable.never()), |
| 283 |
just(duration1), add2); |
| 284 |
m.subscribe(subscriber); |
| 285 |
|
| 286 |
source2.onNext(1); |
| 287 |
|
| 288 |
verify(subscriber, times(1)).onError(any(Throwable.class)); |
| 289 |
verify(subscriber, never()).onComplete(); |
| 290 |
verify(subscriber, never()).onNext(any()); |
| 291 |
} |
| 292 |
|
| 293 |
@Test |
| 294 |
public void leftDurationSelectorThrows() { |
| 295 |
PublishProcessor<Integer> source1 = PublishProcessor.create(); |
| 296 |
PublishProcessor<Integer> source2 = PublishProcessor.create(); |
| 297 |
|
| 298 |
Function<Integer, Flowable<Integer>> fail = new Function<Integer, Flowable<Integer>>() { |
| 299 |
@Override |
| 300 |
public Flowable<Integer> apply(Integer t1) { |
| 301 |
throw new RuntimeException("Forced failure"); |
| 302 |
} |
| 303 |
}; |
| 304 |
|
| 305 |
Flowable<Flowable<Integer>> m = source1.groupJoin(source2, |
| 306 |
fail, |
| 307 |
just(Flowable.never()), add2); |
| 308 |
m.subscribe(subscriber); |
| 309 |
|
| 310 |
source1.onNext(1); |
| 311 |
|
| 312 |
verify(subscriber, times(1)).onError(any(Throwable.class)); |
| 313 |
verify(subscriber, never()).onComplete(); |
| 314 |
verify(subscriber, never()).onNext(any()); |
| 315 |
} |
| 316 |
|
| 317 |
@Test |
| 318 |
public void rightDurationSelectorThrows() { |
| 319 |
PublishProcessor<Integer> source1 = PublishProcessor.create(); |
| 320 |
PublishProcessor<Integer> source2 = PublishProcessor.create(); |
| 321 |
|
| 322 |
Function<Integer, Flowable<Integer>> fail = new Function<Integer, Flowable<Integer>>() { |
| 323 |
@Override |
| 324 |
public Flowable<Integer> apply(Integer t1) { |
| 325 |
throw new RuntimeException("Forced failure"); |
| 326 |
} |
| 327 |
}; |
| 328 |
|
| 329 |
Flowable<Flowable<Integer>> m = source1.groupJoin(source2, |
| 330 |
just(Flowable.never()), |
| 331 |
fail, add2); |
| 332 |
m.subscribe(subscriber); |
| 333 |
|
| 334 |
source2.onNext(1); |
| 335 |
|
| 336 |
verify(subscriber, times(1)).onError(any(Throwable.class)); |
| 337 |
verify(subscriber, never()).onComplete(); |
| 338 |
verify(subscriber, never()).onNext(any()); |
| 339 |
} |
| 340 |
|
| 341 |
@Test |
| 342 |
public void resultSelectorThrows() { |
| 343 |
PublishProcessor<Integer> source1 = PublishProcessor.create(); |
| 344 |
PublishProcessor<Integer> source2 = PublishProcessor.create(); |
| 345 |
|
| 346 |
BiFunction<Integer, Flowable<Integer>, Integer> fail = new BiFunction<Integer, Flowable<Integer>, Integer>() { |
| 347 |
@Override |
| 348 |
public Integer apply(Integer t1, Flowable<Integer> t2) { |
| 349 |
throw new RuntimeException("Forced failure"); |
| 350 |
} |
| 351 |
}; |
| 352 |
|
| 353 |
Flowable<Integer> m = source1.groupJoin(source2, |
| 354 |
just(Flowable.never()), |
| 355 |
just(Flowable.never()), fail); |
| 356 |
m.subscribe(subscriber); |
| 357 |
|
| 358 |
source1.onNext(1); |
| 359 |
source2.onNext(2); |
| 360 |
|
| 361 |
verify(subscriber, times(1)).onError(any(Throwable.class)); |
| 362 |
verify(subscriber, never()).onComplete(); |
| 363 |
verify(subscriber, never()).onNext(any()); |
| 364 |
} |
| 365 |
|
| 366 |
@Test |
| 367 |
public void dispose() { |
| 368 |
TestHelper.checkDisposed(Flowable.just(1).groupJoin( |
| 369 |
Flowable.just(2), |
| 370 |
new Function<Integer, Flowable<Object>>() { |
| 371 |
@Override |
| 372 |
public Flowable<Object> apply(Integer left) throws Exception { |
| 373 |
return Flowable.never(); |
| 374 |
} |
| 375 |
}, |
| 376 |
new Function<Integer, Flowable<Object>>() { |
| 377 |
@Override |
| 378 |
public Flowable<Object> apply(Integer right) throws Exception { |
| 379 |
return Flowable.never(); |
| 380 |
} |
| 381 |
}, |
| 382 |
new BiFunction<Integer, Flowable<Integer>, Object>() { |
| 383 |
@Override |
| 384 |
public Object apply(Integer r, Flowable<Integer> l) throws Exception { |
| 385 |
return l; |
| 386 |
} |
| 387 |
} |
| 388 |
)); |
| 389 |
} |
| 390 |
|
| 391 |
@Test |
| 392 |
public void innerCompleteLeft() { |
| 393 |
Flowable.just(1) |
| 394 |
.groupJoin( |
| 395 |
Flowable.just(2), |
| 396 |
new Function<Integer, Flowable<Object>>() { |
| 397 |
@Override |
| 398 |
public Flowable<Object> apply(Integer left) throws Exception { |
| 399 |
return Flowable.empty(); |
| 400 |
} |
| 401 |
}, |
| 402 |
new Function<Integer, Flowable<Object>>() { |
| 403 |
@Override |
| 404 |
public Flowable<Object> apply(Integer right) throws Exception { |
| 405 |
return Flowable.never(); |
| 406 |
} |
| 407 |
}, |
| 408 |
new BiFunction<Integer, Flowable<Integer>, Flowable<Integer>>() { |
| 409 |
@Override |
| 410 |
public Flowable<Integer> apply(Integer r, Flowable<Integer> l) throws Exception { |
| 411 |
return l; |
| 412 |
} |
| 413 |
} |
| 414 |
) |
| 415 |
.flatMap(Functions.<Flowable<Integer>>identity()) |
| 416 |
.test() |
| 417 |
.assertResult(); |
| 418 |
} |
| 419 |
|
| 420 |
@Test |
| 421 |
public void innerErrorLeft() { |
| 422 |
Flowable.just(1) |
| 423 |
.groupJoin( |
| 424 |
Flowable.just(2), |
| 425 |
new Function<Integer, Flowable<Object>>() { |
| 426 |
@Override |
| 427 |
public Flowable<Object> apply(Integer left) throws Exception { |
| 428 |
return Flowable.error(new TestException()); |
| 429 |
} |
| 430 |
}, |
| 431 |
new Function<Integer, Flowable<Object>>() { |
| 432 |
@Override |
| 433 |
public Flowable<Object> apply(Integer right) throws Exception { |
| 434 |
return Flowable.never(); |
| 435 |
} |
| 436 |
}, |
| 437 |
new BiFunction<Integer, Flowable<Integer>, Flowable<Integer>>() { |
| 438 |
@Override |
| 439 |
public Flowable<Integer> apply(Integer r, Flowable<Integer> l) throws Exception { |
| 440 |
return l; |
| 441 |
} |
| 442 |
} |
| 443 |
) |
| 444 |
.flatMap(Functions.<Flowable<Integer>>identity()) |
| 445 |
.test() |
| 446 |
.assertFailure(TestException.class); |
| 447 |
} |
| 448 |
|
| 449 |
@Test |
| 450 |
public void innerCompleteRight() { |
| 451 |
Flowable.just(1) |
| 452 |
.groupJoin( |
| 453 |
Flowable.just(2), |
| 454 |
new Function<Integer, Flowable<Object>>() { |
| 455 |
@Override |
| 456 |
public Flowable<Object> apply(Integer left) throws Exception { |
| 457 |
return Flowable.never(); |
| 458 |
} |
| 459 |
}, |
| 460 |
new Function<Integer, Flowable<Object>>() { |
| 461 |
@Override |
| 462 |
public Flowable<Object> apply(Integer right) throws Exception { |
| 463 |
return Flowable.empty(); |
| 464 |
} |
| 465 |
}, |
| 466 |
new BiFunction<Integer, Flowable<Integer>, Flowable<Integer>>() { |
| 467 |
@Override |
| 468 |
public Flowable<Integer> apply(Integer r, Flowable<Integer> l) throws Exception { |
| 469 |
return l; |
| 470 |
} |
| 471 |
} |
| 472 |
) |
| 473 |
.flatMap(Functions.<Flowable<Integer>>identity()) |
| 474 |
.test() |
| 475 |
.assertResult(2); |
| 476 |
} |
| 477 |
|
| 478 |
@Test |
| 479 |
public void innerErrorRight() { |
| 480 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 481 |
try { |
| 482 |
Flowable.just(1) |
| 483 |
.groupJoin( |
| 484 |
Flowable.just(2), |
| 485 |
new Function<Integer, Flowable<Object>>() { |
| 486 |
@Override |
| 487 |
public Flowable<Object> apply(Integer left) throws Exception { |
| 488 |
return Flowable.never(); |
| 489 |
} |
| 490 |
}, |
| 491 |
new Function<Integer, Flowable<Object>>() { |
| 492 |
@Override |
| 493 |
public Flowable<Object> apply(Integer right) throws Exception { |
| 494 |
return Flowable.error(new TestException()); |
| 495 |
} |
| 496 |
}, |
| 497 |
new BiFunction<Integer, Flowable<Integer>, Flowable<Integer>>() { |
| 498 |
@Override |
| 499 |
public Flowable<Integer> apply(Integer r, Flowable<Integer> l) throws Exception { |
| 500 |
return l; |
| 501 |
} |
| 502 |
} |
| 503 |
) |
| 504 |
.flatMap(Functions.<Flowable<Integer>>identity()) |
| 505 |
.test() |
| 506 |
.assertFailure(TestException.class); |
| 507 |
|
| 508 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 509 |
} finally { |
| 510 |
RxJavaPlugins.reset(); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
@Test |
| 515 |
public void innerErrorRace() { |
| 516 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 517 |
final PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 518 |
final PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 519 |
|
| 520 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 521 |
|
| 522 |
try { |
| 523 |
TestSubscriberEx<Flowable<Integer>> ts = Flowable.just(1) |
| 524 |
.groupJoin( |
| 525 |
Flowable.just(2).concatWith(Flowable.<Integer>never()), |
| 526 |
new Function<Integer, Flowable<Object>>() { |
| 527 |
@Override |
| 528 |
public Flowable<Object> apply(Integer left) throws Exception { |
| 529 |
return pp1; |
| 530 |
} |
| 531 |
}, |
| 532 |
new Function<Integer, Flowable<Object>>() { |
| 533 |
@Override |
| 534 |
public Flowable<Object> apply(Integer right) throws Exception { |
| 535 |
return pp2; |
| 536 |
} |
| 537 |
}, |
| 538 |
new BiFunction<Integer, Flowable<Integer>, Flowable<Integer>>() { |
| 539 |
@Override |
| 540 |
public Flowable<Integer> apply(Integer r, Flowable<Integer> l) throws Exception { |
| 541 |
return l; |
| 542 |
} |
| 543 |
} |
| 544 |
) |
| 545 |
.to(TestHelper.<Flowable<Integer>>testConsumer()); |
| 546 |
|
| 547 |
final TestException ex1 = new TestException(); |
| 548 |
final TestException ex2 = new TestException(); |
| 549 |
|
| 550 |
Runnable r1 = new Runnable() { |
| 551 |
@Override |
| 552 |
public void run() { |
| 553 |
pp1.onError(ex1); |
| 554 |
} |
| 555 |
}; |
| 556 |
Runnable r2 = new Runnable() { |
| 557 |
@Override |
| 558 |
public void run() { |
| 559 |
pp2.onError(ex2); |
| 560 |
} |
| 561 |
}; |
| 562 |
|
| 563 |
TestHelper.race(r1, r2); |
| 564 |
|
| 565 |
ts.assertError(Throwable.class).assertSubscribed().assertNotComplete().assertValueCount(1); |
| 566 |
|
| 567 |
Throwable exc = ts.errors().get(0); |
| 568 |
|
| 569 |
if (exc instanceof CompositeException) { |
| 570 |
List<Throwable> es = TestHelper.compositeList(exc); |
| 571 |
TestHelper.assertError(es, 0, TestException.class); |
| 572 |
TestHelper.assertError(es, 1, TestException.class); |
| 573 |
} else { |
| 574 |
ts.assertError(TestException.class); |
| 575 |
} |
| 576 |
|
| 577 |
if (!errors.isEmpty()) { |
| 578 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 579 |
} |
| 580 |
} finally { |
| 581 |
RxJavaPlugins.reset(); |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
@Test |
| 587 |
public void outerErrorRace() { |
| 588 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 589 |
final PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 590 |
final PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 591 |
|
| 592 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 593 |
|
| 594 |
try { |
| 595 |
TestSubscriberEx<Object> ts = pp1 |
| 596 |
.groupJoin( |
| 597 |
pp2, |
| 598 |
new Function<Object, Flowable<Object>>() { |
| 599 |
@Override |
| 600 |
public Flowable<Object> apply(Object left) throws Exception { |
| 601 |
return Flowable.never(); |
| 602 |
} |
| 603 |
}, |
| 604 |
new Function<Object, Flowable<Object>>() { |
| 605 |
@Override |
| 606 |
public Flowable<Object> apply(Object right) throws Exception { |
| 607 |
return Flowable.never(); |
| 608 |
} |
| 609 |
}, |
| 610 |
new BiFunction<Object, Flowable<Object>, Flowable<Object>>() { |
| 611 |
@Override |
| 612 |
public Flowable<Object> apply(Object r, Flowable<Object> l) throws Exception { |
| 613 |
return l; |
| 614 |
} |
| 615 |
} |
| 616 |
) |
| 617 |
.flatMap(Functions.<Flowable<Object>>identity()) |
| 618 |
.to(TestHelper.<Object>testConsumer()); |
| 619 |
|
| 620 |
final TestException ex1 = new TestException(); |
| 621 |
final TestException ex2 = new TestException(); |
| 622 |
|
| 623 |
Runnable r1 = new Runnable() { |
| 624 |
@Override |
| 625 |
public void run() { |
| 626 |
pp1.onError(ex1); |
| 627 |
} |
| 628 |
}; |
| 629 |
Runnable r2 = new Runnable() { |
| 630 |
@Override |
| 631 |
public void run() { |
| 632 |
pp2.onError(ex2); |
| 633 |
} |
| 634 |
}; |
| 635 |
|
| 636 |
TestHelper.race(r1, r2); |
| 637 |
|
| 638 |
ts.assertError(Throwable.class).assertSubscribed().assertNotComplete().assertNoValues(); |
| 639 |
|
| 640 |
Throwable exc = ts.errors().get(0); |
| 641 |
|
| 642 |
if (exc instanceof CompositeException) { |
| 643 |
List<Throwable> es = TestHelper.compositeList(exc); |
| 644 |
TestHelper.assertError(es, 0, TestException.class); |
| 645 |
TestHelper.assertError(es, 1, TestException.class); |
| 646 |
} else { |
| 647 |
ts.assertError(TestException.class); |
| 648 |
} |
| 649 |
|
| 650 |
if (!errors.isEmpty()) { |
| 651 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 652 |
} |
| 653 |
} finally { |
| 654 |
RxJavaPlugins.reset(); |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
@Test |
| 660 |
public void rightEmission() { |
| 661 |
final PublishProcessor<Object> pp1 = PublishProcessor.create(); |
| 662 |
final PublishProcessor<Object> pp2 = PublishProcessor.create(); |
| 663 |
|
| 664 |
TestSubscriber<Object> ts = pp1 |
| 665 |
.groupJoin( |
| 666 |
pp2, |
| 667 |
new Function<Object, Flowable<Object>>() { |
| 668 |
@Override |
| 669 |
public Flowable<Object> apply(Object left) throws Exception { |
| 670 |
return Flowable.never(); |
| 671 |
} |
| 672 |
}, |
| 673 |
new Function<Object, Flowable<Object>>() { |
| 674 |
@Override |
| 675 |
public Flowable<Object> apply(Object right) throws Exception { |
| 676 |
return Flowable.never(); |
| 677 |
} |
| 678 |
}, |
| 679 |
new BiFunction<Object, Flowable<Object>, Flowable<Object>>() { |
| 680 |
@Override |
| 681 |
public Flowable<Object> apply(Object r, Flowable<Object> l) throws Exception { |
| 682 |
return l; |
| 683 |
} |
| 684 |
} |
| 685 |
) |
| 686 |
.flatMap(Functions.<Flowable<Object>>identity()) |
| 687 |
.test(); |
| 688 |
|
| 689 |
pp2.onNext(2); |
| 690 |
|
| 691 |
pp1.onNext(1); |
| 692 |
pp1.onComplete(); |
| 693 |
|
| 694 |
pp2.onComplete(); |
| 695 |
|
| 696 |
ts.assertResult(2); |
| 697 |
} |
| 698 |
|
| 699 |
@Test |
| 700 |
public void leftRightState() { |
| 701 |
JoinSupport js = mock(JoinSupport.class); |
| 702 |
|
| 703 |
LeftRightSubscriber o = new LeftRightSubscriber(js, false); |
| 704 |
|
| 705 |
assertFalse(o.isDisposed()); |
| 706 |
|
| 707 |
o.onNext(1); |
| 708 |
o.onNext(2); |
| 709 |
|
| 710 |
o.dispose(); |
| 711 |
|
| 712 |
assertTrue(o.isDisposed()); |
| 713 |
|
| 714 |
verify(js).innerValue(false, 1); |
| 715 |
verify(js).innerValue(false, 2); |
| 716 |
} |
| 717 |
|
| 718 |
@Test |
| 719 |
public void leftRightEndState() { |
| 720 |
JoinSupport js = mock(JoinSupport.class); |
| 721 |
|
| 722 |
LeftRightEndSubscriber o = new LeftRightEndSubscriber(js, false, 0); |
| 723 |
|
| 724 |
assertFalse(o.isDisposed()); |
| 725 |
|
| 726 |
o.onNext(1); |
| 727 |
o.onNext(2); |
| 728 |
|
| 729 |
assertTrue(o.isDisposed()); |
| 730 |
|
| 731 |
verify(js).innerClose(false, o); |
| 732 |
} |
| 733 |
|
| 734 |
@Test |
| 735 |
public void disposeAfterOnNext() { |
| 736 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 737 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 738 |
|
| 739 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 740 |
|
| 741 |
pp1.groupJoin(pp2, v -> Flowable.never(), v -> Flowable.never(), (a, b) -> a) |
| 742 |
.doOnNext(v -> { |
| 743 |
ts.cancel(); |
| 744 |
}) |
| 745 |
.subscribe(ts); |
| 746 |
|
| 747 |
pp2.onNext(1); |
| 748 |
pp1.onNext(1); |
| 749 |
} |
| 750 |
|
| 751 |
@Test |
| 752 |
public void completeWithMoreWork() { |
| 753 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 754 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 755 |
|
| 756 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 757 |
|
| 758 |
pp1.groupJoin(pp2, v -> Flowable.never(), v -> Flowable.never(), (a, b) -> a) |
| 759 |
.doOnNext(v -> { |
| 760 |
if (v == 1) { |
| 761 |
pp2.onNext(2); |
| 762 |
pp1.onComplete(); |
| 763 |
pp2.onComplete(); |
| 764 |
} |
| 765 |
}) |
| 766 |
.subscribe(ts); |
| 767 |
|
| 768 |
pp2.onNext(1); |
| 769 |
pp1.onNext(1); |
| 770 |
} |
| 771 |
|
| 772 |
@Test |
| 773 |
public void badRequest() { |
| 774 |
TestHelper.assertBadRequestReported(Flowable.never().groupJoin(Flowable.never(), v -> Flowable.never(), v -> Flowable.never(), (a, b) -> a)); |
| 775 |
} |
| 776 |
|
| 777 |
@Test |
| 778 |
public void missingBackpressure() { |
| 779 |
Flowable.just(1) |
| 780 |
.groupJoin(Flowable.never(), v -> BehaviorProcessor.createDefault(1), v -> Flowable.never(), (a, b) -> a) |
| 781 |
.test(0) |
| 782 |
.assertFailure(MissingBackpressureException.class); |
| 783 |
} |
| 784 |
} |