| 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.Mockito.*; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.*; |
| 25 |
import org.mockito.InOrder; |
| 26 |
import org.reactivestreams.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 29 |
import io.reactivex.rxjava3.core.*; |
| 30 |
import io.reactivex.rxjava3.disposables.CompositeDisposable; |
| 31 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 32 |
import io.reactivex.rxjava3.functions.*; |
| 33 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 34 |
import io.reactivex.rxjava3.internal.util.CrashingMappedIterable; |
| 35 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 36 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 37 |
import io.reactivex.rxjava3.schedulers.*; |
| 38 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 39 |
import io.reactivex.rxjava3.testsupport.*; |
| 40 |
|
| 41 |
public class FlowableAmbTest extends RxJavaTest { |
| 42 |
|
| 43 |
private TestScheduler scheduler; |
| 44 |
private Scheduler.Worker innerScheduler; |
| 45 |
|
| 46 |
@Before |
| 47 |
public void setUp() { |
| 48 |
scheduler = new TestScheduler(); |
| 49 |
innerScheduler = scheduler.createWorker(); |
| 50 |
} |
| 51 |
|
| 52 |
private Flowable<String> createFlowable(final String[] values, |
| 53 |
final long interval, final Throwable e) { |
| 54 |
return Flowable.unsafeCreate(new Publisher<String>() { |
| 55 |
|
| 56 |
@Override |
| 57 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 58 |
final CompositeDisposable parentSubscription = new CompositeDisposable(); |
| 59 |
|
| 60 |
subscriber.onSubscribe(new Subscription() { |
| 61 |
@Override |
| 62 |
public void request(long n) { |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void cancel() { |
| 68 |
parentSubscription.dispose(); |
| 69 |
} |
| 70 |
}); |
| 71 |
|
| 72 |
long delay = interval; |
| 73 |
for (final String value : values) { |
| 74 |
parentSubscription.add(innerScheduler.schedule(new Runnable() { |
| 75 |
@Override |
| 76 |
public void run() { |
| 77 |
subscriber.onNext(value); |
| 78 |
} |
| 79 |
} |
| 80 |
, delay, TimeUnit.MILLISECONDS)); |
| 81 |
delay += interval; |
| 82 |
} |
| 83 |
parentSubscription.add(innerScheduler.schedule(new Runnable() { |
| 84 |
@Override |
| 85 |
public void run() { |
| 86 |
if (e == null) { |
| 87 |
subscriber.onComplete(); |
| 88 |
} else { |
| 89 |
subscriber.onError(e); |
| 90 |
} |
| 91 |
} |
| 92 |
}, delay, TimeUnit.MILLISECONDS)); |
| 93 |
} |
| 94 |
}); |
| 95 |
} |
| 96 |
|
| 97 |
@Test |
| 98 |
public void amb() { |
| 99 |
Flowable<String> flowable1 = createFlowable(new String[] { |
| 100 |
"1", "11", "111", "1111" }, 2000, null); |
| 101 |
Flowable<String> flowable2 = createFlowable(new String[] { |
| 102 |
"2", "22", "222", "2222" }, 1000, null); |
| 103 |
Flowable<String> flowable3 = createFlowable(new String[] { |
| 104 |
"3", "33", "333", "3333" }, 3000, null); |
| 105 |
|
| 106 |
Flowable<String> f = Flowable.ambArray(flowable1, |
| 107 |
flowable2, flowable3); |
| 108 |
|
| 109 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 110 |
f.subscribe(subscriber); |
| 111 |
|
| 112 |
scheduler.advanceTimeBy(100000, TimeUnit.MILLISECONDS); |
| 113 |
|
| 114 |
InOrder inOrder = inOrder(subscriber); |
| 115 |
inOrder.verify(subscriber, times(1)).onNext("2"); |
| 116 |
inOrder.verify(subscriber, times(1)).onNext("22"); |
| 117 |
inOrder.verify(subscriber, times(1)).onNext("222"); |
| 118 |
inOrder.verify(subscriber, times(1)).onNext("2222"); |
| 119 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 120 |
inOrder.verifyNoMoreInteractions(); |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void amb2() { |
| 125 |
IOException expectedException = new IOException( |
| 126 |
"fake exception"); |
| 127 |
Flowable<String> flowable1 = createFlowable(new String[] {}, |
| 128 |
2000, new IOException("fake exception")); |
| 129 |
Flowable<String> flowable2 = createFlowable(new String[] { |
| 130 |
"2", "22", "222", "2222" }, 1000, expectedException); |
| 131 |
Flowable<String> flowable3 = createFlowable(new String[] {}, |
| 132 |
3000, new IOException("fake exception")); |
| 133 |
|
| 134 |
Flowable<String> f = Flowable.ambArray(flowable1, |
| 135 |
flowable2, flowable3); |
| 136 |
|
| 137 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 138 |
f.subscribe(subscriber); |
| 139 |
|
| 140 |
scheduler.advanceTimeBy(100000, TimeUnit.MILLISECONDS); |
| 141 |
|
| 142 |
InOrder inOrder = inOrder(subscriber); |
| 143 |
inOrder.verify(subscriber, times(1)).onNext("2"); |
| 144 |
inOrder.verify(subscriber, times(1)).onNext("22"); |
| 145 |
inOrder.verify(subscriber, times(1)).onNext("222"); |
| 146 |
inOrder.verify(subscriber, times(1)).onNext("2222"); |
| 147 |
inOrder.verify(subscriber, times(1)).onError(expectedException); |
| 148 |
inOrder.verifyNoMoreInteractions(); |
| 149 |
} |
| 150 |
|
| 151 |
@Test |
| 152 |
public void amb3() { |
| 153 |
Flowable<String> flowable1 = createFlowable(new String[] { |
| 154 |
"1" }, 2000, null); |
| 155 |
Flowable<String> flowable2 = createFlowable(new String[] {}, |
| 156 |
1000, null); |
| 157 |
Flowable<String> flowable3 = createFlowable(new String[] { |
| 158 |
"3" }, 3000, null); |
| 159 |
|
| 160 |
Flowable<String> f = Flowable.ambArray(flowable1, |
| 161 |
flowable2, flowable3); |
| 162 |
|
| 163 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 164 |
f.subscribe(subscriber); |
| 165 |
|
| 166 |
scheduler.advanceTimeBy(100000, TimeUnit.MILLISECONDS); |
| 167 |
InOrder inOrder = inOrder(subscriber); |
| 168 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 169 |
inOrder.verifyNoMoreInteractions(); |
| 170 |
} |
| 171 |
|
| 172 |
@Test |
| 173 |
public void producerRequestThroughAmb() { |
| 174 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 175 |
ts.request(3); |
| 176 |
final AtomicLong requested1 = new AtomicLong(); |
| 177 |
final AtomicLong requested2 = new AtomicLong(); |
| 178 |
Flowable<Integer> f1 = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 179 |
|
| 180 |
@Override |
| 181 |
public void subscribe(Subscriber<? super Integer> s) { |
| 182 |
s.onSubscribe(new Subscription() { |
| 183 |
|
| 184 |
@Override |
| 185 |
public void request(long n) { |
| 186 |
System.out.println("1-requested: " + n); |
| 187 |
requested1.set(n); |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public void cancel() { |
| 192 |
|
| 193 |
} |
| 194 |
}); |
| 195 |
} |
| 196 |
|
| 197 |
}); |
| 198 |
Flowable<Integer> f2 = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 199 |
|
| 200 |
@Override |
| 201 |
public void subscribe(Subscriber<? super Integer> s) { |
| 202 |
s.onSubscribe(new Subscription() { |
| 203 |
|
| 204 |
@Override |
| 205 |
public void request(long n) { |
| 206 |
System.out.println("2-requested: " + n); |
| 207 |
requested2.set(n); |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public void cancel() { |
| 212 |
|
| 213 |
} |
| 214 |
}); |
| 215 |
} |
| 216 |
|
| 217 |
}); |
| 218 |
Flowable.ambArray(f1, f2).subscribe(ts); |
| 219 |
assertEquals(3, requested1.get()); |
| 220 |
assertEquals(3, requested2.get()); |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void backpressure() { |
| 225 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 226 |
Flowable.range(0, Flowable.bufferSize() * 2) |
| 227 |
.ambWith(Flowable.range(0, Flowable.bufferSize() * 2)) |
| 228 |
.observeOn(Schedulers.computation()) // observeOn has a backpressured RxRingBuffer |
| 229 |
.delay(1, TimeUnit.MICROSECONDS) // make it a slightly slow consumer |
| 230 |
.subscribe(ts); |
| 231 |
|
| 232 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 233 |
ts.assertNoErrors(); |
| 234 |
assertEquals(Flowable.bufferSize() * 2, ts.values().size()); |
| 235 |
} |
| 236 |
|
| 237 |
@Test |
| 238 |
public void subscriptionOnlyHappensOnce() throws InterruptedException { |
| 239 |
final AtomicLong count = new AtomicLong(); |
| 240 |
Consumer<Subscription> incrementer = new Consumer<Subscription>() { |
| 241 |
@Override |
| 242 |
public void accept(Subscription s) { |
| 243 |
count.incrementAndGet(); |
| 244 |
} |
| 245 |
}; |
| 246 |
|
| 247 |
//this aync stream should emit first |
| 248 |
Flowable<Integer> f1 = Flowable.just(1).doOnSubscribe(incrementer) |
| 249 |
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); |
| 250 |
//this stream emits second |
| 251 |
Flowable<Integer> f2 = Flowable.just(1).doOnSubscribe(incrementer) |
| 252 |
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); |
| 253 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 254 |
Flowable.ambArray(f1, f2).subscribe(ts); |
| 255 |
ts.request(1); |
| 256 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 257 |
ts.assertNoErrors(); |
| 258 |
assertEquals(2, count.get()); |
| 259 |
} |
| 260 |
|
| 261 |
@Test |
| 262 |
public void secondaryRequestsPropagatedToChildren() throws InterruptedException { |
| 263 |
//this aync stream should emit first |
| 264 |
Flowable<Integer> f1 = Flowable.fromArray(1, 2, 3) |
| 265 |
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); |
| 266 |
//this stream emits second |
| 267 |
Flowable<Integer> f2 = Flowable.fromArray(4, 5, 6) |
| 268 |
.delay(200, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); |
| 269 |
TestSubscriber<Integer> ts = new TestSubscriber<>(1L); |
| 270 |
|
| 271 |
Flowable.ambArray(f1, f2).subscribe(ts); |
| 272 |
// before first emission request 20 more |
| 273 |
// this request should suffice to emit all |
| 274 |
ts.request(20); |
| 275 |
//ensure stream does not hang |
| 276 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 277 |
ts.assertNoErrors(); |
| 278 |
} |
| 279 |
|
| 280 |
@Test |
| 281 |
public void synchronousSources() { |
| 282 |
// under async subscription the second Flowable would complete before |
| 283 |
// the first but because this is a synchronous subscription to sources |
| 284 |
// then second Flowable does not get subscribed to before first |
| 285 |
// subscription completes hence first Flowable emits result through |
| 286 |
// amb |
| 287 |
int result = Flowable.just(1).doOnNext(new Consumer<Integer>() { |
| 288 |
@Override |
| 289 |
public void accept(Integer t) { |
| 290 |
try { |
| 291 |
Thread.sleep(100); |
| 292 |
} catch (InterruptedException e) { |
| 293 |
// |
| 294 |
} |
| 295 |
} |
| 296 |
}).ambWith(Flowable.just(2)).blockingSingle(); |
| 297 |
assertEquals(1, result); |
| 298 |
} |
| 299 |
|
| 300 |
@Test |
| 301 |
public void ambCancelsOthers() { |
| 302 |
PublishProcessor<Integer> source1 = PublishProcessor.create(); |
| 303 |
PublishProcessor<Integer> source2 = PublishProcessor.create(); |
| 304 |
PublishProcessor<Integer> source3 = PublishProcessor.create(); |
| 305 |
|
| 306 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 307 |
|
| 308 |
Flowable.ambArray(source1, source2, source3).subscribe(ts); |
| 309 |
|
| 310 |
assertTrue("Source 1 doesn't have subscribers!", source1.hasSubscribers()); |
| 311 |
assertTrue("Source 2 doesn't have subscribers!", source2.hasSubscribers()); |
| 312 |
assertTrue("Source 3 doesn't have subscribers!", source3.hasSubscribers()); |
| 313 |
|
| 314 |
source1.onNext(1); |
| 315 |
|
| 316 |
assertTrue("Source 1 doesn't have subscribers!", source1.hasSubscribers()); |
| 317 |
assertFalse("Source 2 still has subscribers!", source2.hasSubscribers()); |
| 318 |
assertFalse("Source 2 still has subscribers!", source3.hasSubscribers()); |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
@Test |
| 323 |
public void multipleUse() { |
| 324 |
TestSubscriber<Long> ts1 = new TestSubscriber<>(); |
| 325 |
TestSubscriber<Long> ts2 = new TestSubscriber<>(); |
| 326 |
|
| 327 |
Flowable<Long> amb = Flowable.timer(100, TimeUnit.MILLISECONDS).ambWith(Flowable.timer(200, TimeUnit.MILLISECONDS)); |
| 328 |
|
| 329 |
amb.subscribe(ts1); |
| 330 |
amb.subscribe(ts2); |
| 331 |
|
| 332 |
ts1.awaitDone(5, TimeUnit.SECONDS); |
| 333 |
ts2.awaitDone(5, TimeUnit.SECONDS); |
| 334 |
|
| 335 |
ts1.assertValue(0L); |
| 336 |
ts1.assertComplete(); |
| 337 |
ts1.assertNoErrors(); |
| 338 |
|
| 339 |
ts2.assertValue(0L); |
| 340 |
ts2.assertComplete(); |
| 341 |
ts2.assertNoErrors(); |
| 342 |
} |
| 343 |
|
| 344 |
@Test |
| 345 |
public void ambIterable() { |
| 346 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 347 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 348 |
|
| 349 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 350 |
|
| 351 |
Flowable.amb(Arrays.asList(pp1, pp2)).subscribe(ts); |
| 352 |
|
| 353 |
ts.assertNoValues(); |
| 354 |
|
| 355 |
pp1.onNext(1); |
| 356 |
pp1.onComplete(); |
| 357 |
|
| 358 |
assertFalse(pp1.hasSubscribers()); |
| 359 |
assertFalse(pp2.hasSubscribers()); |
| 360 |
|
| 361 |
ts.assertValue(1); |
| 362 |
ts.assertNoErrors(); |
| 363 |
ts.assertComplete(); |
| 364 |
} |
| 365 |
|
| 366 |
@Test |
| 367 |
public void ambIterable2() { |
| 368 |
PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 369 |
PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 370 |
|
| 371 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 372 |
|
| 373 |
Flowable.amb(Arrays.asList(pp1, pp2)).subscribe(ts); |
| 374 |
|
| 375 |
ts.assertNoValues(); |
| 376 |
|
| 377 |
pp2.onNext(2); |
| 378 |
pp2.onComplete(); |
| 379 |
|
| 380 |
assertFalse(pp1.hasSubscribers()); |
| 381 |
assertFalse(pp2.hasSubscribers()); |
| 382 |
|
| 383 |
ts.assertValue(2); |
| 384 |
ts.assertNoErrors(); |
| 385 |
ts.assertComplete(); |
| 386 |
} |
| 387 |
|
| 388 |
@Test |
| 389 |
public void ambArrayEmpty() { |
| 390 |
assertSame(Flowable.empty(), Flowable.ambArray()); |
| 391 |
} |
| 392 |
|
| 393 |
@Test |
| 394 |
public void ambArraySingleElement() { |
| 395 |
assertSame(Flowable.never(), Flowable.ambArray(Flowable.never())); |
| 396 |
} |
| 397 |
|
| 398 |
@Test |
| 399 |
public void disposed() { |
| 400 |
TestHelper.checkDisposed(Flowable.ambArray(Flowable.never(), Flowable.never())); |
| 401 |
} |
| 402 |
|
| 403 |
@Test |
| 404 |
public void manySources() { |
| 405 |
Flowable<?>[] a = new Flowable[32]; |
| 406 |
Arrays.fill(a, Flowable.never()); |
| 407 |
a[31] = Flowable.just(1); |
| 408 |
|
| 409 |
Flowable.amb(Arrays.asList(a)) |
| 410 |
.test() |
| 411 |
.assertResult(1); |
| 412 |
} |
| 413 |
|
| 414 |
@Test |
| 415 |
public void emptyIterable() { |
| 416 |
Flowable.amb(Collections.<Flowable<Integer>>emptyList()) |
| 417 |
.test() |
| 418 |
.assertResult(); |
| 419 |
} |
| 420 |
|
| 421 |
@Test |
| 422 |
public void singleIterable() { |
| 423 |
Flowable.amb(Collections.singletonList(Flowable.just(1))) |
| 424 |
.test() |
| 425 |
.assertResult(1); |
| 426 |
} |
| 427 |
|
| 428 |
@Test |
| 429 |
public void onNextRace() { |
| 430 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 431 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 432 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 433 |
|
| 434 |
TestSubscriberEx<Integer> ts = Flowable.ambArray(pp1, pp2).to(TestHelper.<Integer>testConsumer()); |
| 435 |
|
| 436 |
Runnable r1 = new Runnable() { |
| 437 |
@Override |
| 438 |
public void run() { |
| 439 |
pp1.onNext(1); |
| 440 |
} |
| 441 |
}; |
| 442 |
Runnable r2 = new Runnable() { |
| 443 |
@Override |
| 444 |
public void run() { |
| 445 |
pp2.onNext(1); |
| 446 |
} |
| 447 |
}; |
| 448 |
|
| 449 |
TestHelper.race(r1, r2); |
| 450 |
|
| 451 |
ts.assertSubscribed().assertNoErrors() |
| 452 |
.assertNotComplete().assertValueCount(1); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
@Test |
| 457 |
public void onCompleteRace() { |
| 458 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 459 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 460 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 461 |
|
| 462 |
TestSubscriber<Integer> ts = Flowable.ambArray(pp1, pp2).test(); |
| 463 |
|
| 464 |
Runnable r1 = new Runnable() { |
| 465 |
@Override |
| 466 |
public void run() { |
| 467 |
pp1.onComplete(); |
| 468 |
} |
| 469 |
}; |
| 470 |
Runnable r2 = new Runnable() { |
| 471 |
@Override |
| 472 |
public void run() { |
| 473 |
pp2.onComplete(); |
| 474 |
} |
| 475 |
}; |
| 476 |
|
| 477 |
TestHelper.race(r1, r2); |
| 478 |
|
| 479 |
ts.assertResult(); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
@Test |
| 484 |
public void onErrorRace() { |
| 485 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 486 |
final PublishProcessor<Integer> pp1 = PublishProcessor.create(); |
| 487 |
final PublishProcessor<Integer> pp2 = PublishProcessor.create(); |
| 488 |
|
| 489 |
TestSubscriber<Integer> ts = Flowable.ambArray(pp1, pp2).test(); |
| 490 |
|
| 491 |
final Throwable ex = new TestException(); |
| 492 |
|
| 493 |
Runnable r1 = new Runnable() { |
| 494 |
@Override |
| 495 |
public void run() { |
| 496 |
pp1.onError(ex); |
| 497 |
} |
| 498 |
}; |
| 499 |
Runnable r2 = new Runnable() { |
| 500 |
@Override |
| 501 |
public void run() { |
| 502 |
pp2.onError(ex); |
| 503 |
} |
| 504 |
}; |
| 505 |
|
| 506 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 507 |
try { |
| 508 |
TestHelper.race(r1, r2); |
| 509 |
} finally { |
| 510 |
RxJavaPlugins.reset(); |
| 511 |
} |
| 512 |
|
| 513 |
ts.assertFailure(TestException.class); |
| 514 |
if (!errors.isEmpty()) { |
| 515 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
@Test |
| 521 |
public void nullIterableElement() { |
| 522 |
Flowable.amb(Arrays.asList(Flowable.never(), null, Flowable.never())) |
| 523 |
.test() |
| 524 |
.assertFailure(NullPointerException.class); |
| 525 |
} |
| 526 |
|
| 527 |
@Test |
| 528 |
public void iteratorThrows() { |
| 529 |
Flowable.amb(new CrashingMappedIterable<>(1, 100, 100, new Function<Integer, Flowable<Integer>>() { |
| 530 |
@Override |
| 531 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 532 |
return Flowable.never(); |
| 533 |
} |
| 534 |
})) |
| 535 |
.to(TestHelper.<Integer>testConsumer()) |
| 536 |
.assertFailureAndMessage(TestException.class, "iterator()"); |
| 537 |
|
| 538 |
Flowable.amb(new CrashingMappedIterable<>(100, 1, 100, new Function<Integer, Flowable<Integer>>() { |
| 539 |
@Override |
| 540 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 541 |
return Flowable.never(); |
| 542 |
} |
| 543 |
})) |
| 544 |
.to(TestHelper.<Integer>testConsumer()) |
| 545 |
.assertFailureAndMessage(TestException.class, "hasNext()"); |
| 546 |
|
| 547 |
Flowable.amb(new CrashingMappedIterable<>(100, 100, 1, new Function<Integer, Flowable<Integer>>() { |
| 548 |
@Override |
| 549 |
public Flowable<Integer> apply(Integer v) throws Exception { |
| 550 |
return Flowable.never(); |
| 551 |
} |
| 552 |
})) |
| 553 |
.to(TestHelper.<Integer>testConsumer()) |
| 554 |
.assertFailureAndMessage(TestException.class, "next()"); |
| 555 |
} |
| 556 |
|
| 557 |
@Test |
| 558 |
public void ambWithOrder() { |
| 559 |
Flowable<Integer> error = Flowable.error(new RuntimeException()); |
| 560 |
Flowable.just(1).ambWith(error).test().assertValue(1).assertComplete(); |
| 561 |
} |
| 562 |
|
| 563 |
@Test |
| 564 |
public void ambIterableOrder() { |
| 565 |
Flowable<Integer> error = Flowable.error(new RuntimeException()); |
| 566 |
Flowable.amb(Arrays.asList(Flowable.just(1), error)).test().assertValue(1).assertComplete(); |
| 567 |
} |
| 568 |
|
| 569 |
@Test |
| 570 |
public void ambArrayOrder() { |
| 571 |
Flowable<Integer> error = Flowable.error(new RuntimeException()); |
| 572 |
Flowable.ambArray(Flowable.just(1), error).test().assertValue(1).assertComplete(); |
| 573 |
} |
| 574 |
|
| 575 |
@Test |
| 576 |
public void noWinnerSuccessDispose() throws Exception { |
| 577 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 578 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 579 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 580 |
|
| 581 |
Flowable.ambArray( |
| 582 |
Flowable.just(1) |
| 583 |
.subscribeOn(Schedulers.single()) |
| 584 |
.observeOn(Schedulers.computation()), |
| 585 |
Flowable.never() |
| 586 |
) |
| 587 |
.subscribe(new Consumer<Object>() { |
| 588 |
@Override |
| 589 |
public void accept(Object v) throws Exception { |
| 590 |
interrupted.set(Thread.currentThread().isInterrupted()); |
| 591 |
cdl.countDown(); |
| 592 |
} |
| 593 |
}); |
| 594 |
|
| 595 |
assertTrue(cdl.await(500, TimeUnit.SECONDS)); |
| 596 |
assertFalse("Interrupted!", interrupted.get()); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
@Test |
| 601 |
public void noWinnerErrorDispose() throws Exception { |
| 602 |
final TestException ex = new TestException(); |
| 603 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 604 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 605 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 606 |
|
| 607 |
Flowable.ambArray( |
| 608 |
Flowable.error(ex) |
| 609 |
.subscribeOn(Schedulers.single()) |
| 610 |
.observeOn(Schedulers.computation()), |
| 611 |
Flowable.never() |
| 612 |
) |
| 613 |
.subscribe(Functions.emptyConsumer(), new Consumer<Throwable>() { |
| 614 |
@Override |
| 615 |
public void accept(Throwable e) throws Exception { |
| 616 |
interrupted.set(Thread.currentThread().isInterrupted()); |
| 617 |
cdl.countDown(); |
| 618 |
} |
| 619 |
}); |
| 620 |
|
| 621 |
assertTrue(cdl.await(500, TimeUnit.SECONDS)); |
| 622 |
assertFalse("Interrupted!", interrupted.get()); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
@Test |
| 627 |
public void noWinnerCompleteDispose() throws Exception { |
| 628 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 629 |
final AtomicBoolean interrupted = new AtomicBoolean(); |
| 630 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 631 |
|
| 632 |
Flowable.ambArray( |
| 633 |
Flowable.empty() |
| 634 |
.subscribeOn(Schedulers.single()) |
| 635 |
.observeOn(Schedulers.computation()), |
| 636 |
Flowable.never() |
| 637 |
) |
| 638 |
.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer(), new Action() { |
| 639 |
@Override |
| 640 |
public void run() throws Exception { |
| 641 |
interrupted.set(Thread.currentThread().isInterrupted()); |
| 642 |
cdl.countDown(); |
| 643 |
} |
| 644 |
}); |
| 645 |
|
| 646 |
assertTrue(cdl.await(500, TimeUnit.SECONDS)); |
| 647 |
assertFalse("Interrupted!", interrupted.get()); |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
@Test |
| 652 |
public void publishersInIterable() { |
| 653 |
Publisher<Integer> source = new Publisher<Integer>() { |
| 654 |
@Override |
| 655 |
public void subscribe(Subscriber<? super Integer> subscriber) { |
| 656 |
Flowable.just(1).subscribe(subscriber); |
| 657 |
} |
| 658 |
}; |
| 659 |
|
| 660 |
Flowable.amb(Arrays.asList(source, source)) |
| 661 |
.test() |
| 662 |
.assertResult(1); |
| 663 |
} |
| 664 |
|
| 665 |
@Test |
| 666 |
public void badRequest() { |
| 667 |
TestHelper.assertBadRequestReported(Flowable.amb(Arrays.asList(Flowable.never(), Flowable.never()))); |
| 668 |
} |
| 669 |
|
| 670 |
@Test |
| 671 |
public void requestAfterCancel() { |
| 672 |
Flowable.amb(Arrays.asList(Flowable.never(), Flowable.never())) |
| 673 |
.subscribe(new FlowableSubscriber<Object>() { |
| 674 |
|
| 675 |
@Override |
| 676 |
public void onNext(@NonNull Object t) { |
| 677 |
} |
| 678 |
|
| 679 |
@Override |
| 680 |
public void onError(Throwable t) { |
| 681 |
} |
| 682 |
|
| 683 |
@Override |
| 684 |
public void onComplete() { |
| 685 |
} |
| 686 |
|
| 687 |
@Override |
| 688 |
public void onSubscribe(@NonNull Subscription s) { |
| 689 |
s.cancel(); |
| 690 |
s.request(1); |
| 691 |
} |
| 692 |
}); |
| 693 |
} |
| 694 |
} |