| 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.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.mockito.InOrder; |
| 26 |
import org.reactivestreams.*; |
| 27 |
|
| 28 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 29 |
import io.reactivex.rxjava3.core.*; |
| 30 |
import io.reactivex.rxjava3.disposables.*; |
| 31 |
import io.reactivex.rxjava3.exceptions.*; |
| 32 |
import io.reactivex.rxjava3.functions.*; |
| 33 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 34 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber; |
| 35 |
import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler; |
| 36 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 37 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 38 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 39 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 40 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 41 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 42 |
import io.reactivex.rxjava3.processors.*; |
| 43 |
import io.reactivex.rxjava3.schedulers.*; |
| 44 |
import io.reactivex.rxjava3.subscribers.*; |
| 45 |
import io.reactivex.rxjava3.testsupport.*; |
| 46 |
|
| 47 |
public class FlowableObserveOnTest extends RxJavaTest { |
| 48 |
|
| 49 |
/** |
| 50 |
* This is testing a no-op path since it uses Schedulers.immediate() which will not do scheduling. |
| 51 |
*/ |
| 52 |
@Test |
| 53 |
public void observeOn() { |
| 54 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 55 |
Flowable.just(1, 2, 3).observeOn(ImmediateThinScheduler.INSTANCE).subscribe(subscriber); |
| 56 |
|
| 57 |
verify(subscriber, times(1)).onNext(1); |
| 58 |
verify(subscriber, times(1)).onNext(2); |
| 59 |
verify(subscriber, times(1)).onNext(3); |
| 60 |
verify(subscriber, times(1)).onComplete(); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void ordering() throws InterruptedException { |
| 65 |
Flowable<String> obs = Flowable.just("one", "null", "two", "three", "four"); |
| 66 |
|
| 67 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 68 |
|
| 69 |
InOrder inOrder = inOrder(subscriber); |
| 70 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(subscriber); |
| 71 |
|
| 72 |
obs.observeOn(Schedulers.computation()).subscribe(ts); |
| 73 |
|
| 74 |
ts.awaitDone(1000, TimeUnit.MILLISECONDS); |
| 75 |
if (ts.errors().size() > 0) { |
| 76 |
for (Throwable t : ts.errors()) { |
| 77 |
t.printStackTrace(); |
| 78 |
} |
| 79 |
fail("failed with exception"); |
| 80 |
} |
| 81 |
|
| 82 |
inOrder.verify(subscriber, times(1)).onNext("one"); |
| 83 |
inOrder.verify(subscriber, times(1)).onNext("null"); |
| 84 |
inOrder.verify(subscriber, times(1)).onNext("two"); |
| 85 |
inOrder.verify(subscriber, times(1)).onNext("three"); |
| 86 |
inOrder.verify(subscriber, times(1)).onNext("four"); |
| 87 |
inOrder.verify(subscriber, times(1)).onComplete(); |
| 88 |
inOrder.verifyNoMoreInteractions(); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void threadName() throws InterruptedException { |
| 93 |
System.out.println("Main Thread: " + Thread.currentThread().getName()); |
| 94 |
Flowable<String> obs = Flowable.just("one", "null", "two", "three", "four"); |
| 95 |
|
| 96 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 97 |
final String parentThreadName = Thread.currentThread().getName(); |
| 98 |
|
| 99 |
final CountDownLatch completedLatch = new CountDownLatch(1); |
| 100 |
|
| 101 |
// assert subscribe is on main thread |
| 102 |
obs = obs.doOnNext(new Consumer<String>() { |
| 103 |
|
| 104 |
@Override |
| 105 |
public void accept(String s) { |
| 106 |
String threadName = Thread.currentThread().getName(); |
| 107 |
System.out.println("Source ThreadName: " + threadName + " Expected => " + parentThreadName); |
| 108 |
assertEquals(parentThreadName, threadName); |
| 109 |
} |
| 110 |
|
| 111 |
}); |
| 112 |
|
| 113 |
// assert observe is on new thread |
| 114 |
obs.observeOn(Schedulers.newThread()).doOnNext(new Consumer<String>() { |
| 115 |
|
| 116 |
@Override |
| 117 |
public void accept(String t1) { |
| 118 |
String threadName = Thread.currentThread().getName(); |
| 119 |
boolean correctThreadName = threadName.startsWith("RxNewThreadScheduler"); |
| 120 |
System.out.println("ObserveOn ThreadName: " + threadName + " Correct => " + correctThreadName); |
| 121 |
assertTrue(correctThreadName); |
| 122 |
} |
| 123 |
|
| 124 |
}).doAfterTerminate(new Action() { |
| 125 |
|
| 126 |
@Override |
| 127 |
public void run() { |
| 128 |
completedLatch.countDown(); |
| 129 |
|
| 130 |
} |
| 131 |
}).subscribe(subscriber); |
| 132 |
|
| 133 |
if (!completedLatch.await(1000, TimeUnit.MILLISECONDS)) { |
| 134 |
fail("timed out waiting"); |
| 135 |
} |
| 136 |
|
| 137 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 138 |
verify(subscriber, times(5)).onNext(any(String.class)); |
| 139 |
verify(subscriber, times(1)).onComplete(); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void observeOnTheSameSchedulerTwice() { |
| 144 |
Scheduler scheduler = ImmediateThinScheduler.INSTANCE; |
| 145 |
|
| 146 |
Flowable<Integer> f = Flowable.just(1, 2, 3); |
| 147 |
Flowable<Integer> f2 = f.observeOn(scheduler); |
| 148 |
|
| 149 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 150 |
Subscriber<Object> subscriber2 = TestHelper.mockSubscriber(); |
| 151 |
|
| 152 |
InOrder inOrder1 = inOrder(subscriber1); |
| 153 |
InOrder inOrder2 = inOrder(subscriber2); |
| 154 |
|
| 155 |
f2.subscribe(subscriber1); |
| 156 |
f2.subscribe(subscriber2); |
| 157 |
|
| 158 |
inOrder1.verify(subscriber1, times(1)).onNext(1); |
| 159 |
inOrder1.verify(subscriber1, times(1)).onNext(2); |
| 160 |
inOrder1.verify(subscriber1, times(1)).onNext(3); |
| 161 |
inOrder1.verify(subscriber1, times(1)).onComplete(); |
| 162 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 163 |
inOrder1.verifyNoMoreInteractions(); |
| 164 |
|
| 165 |
inOrder2.verify(subscriber2, times(1)).onNext(1); |
| 166 |
inOrder2.verify(subscriber2, times(1)).onNext(2); |
| 167 |
inOrder2.verify(subscriber2, times(1)).onNext(3); |
| 168 |
inOrder2.verify(subscriber2, times(1)).onComplete(); |
| 169 |
verify(subscriber2, never()).onError(any(Throwable.class)); |
| 170 |
inOrder2.verifyNoMoreInteractions(); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void observeSameOnMultipleSchedulers() { |
| 175 |
TestScheduler scheduler1 = new TestScheduler(); |
| 176 |
TestScheduler scheduler2 = new TestScheduler(); |
| 177 |
|
| 178 |
Flowable<Integer> f = Flowable.just(1, 2, 3); |
| 179 |
Flowable<Integer> f1 = f.observeOn(scheduler1); |
| 180 |
Flowable<Integer> f2 = f.observeOn(scheduler2); |
| 181 |
|
| 182 |
Subscriber<Object> subscriber1 = TestHelper.mockSubscriber(); |
| 183 |
Subscriber<Object> subscriber2 = TestHelper.mockSubscriber(); |
| 184 |
|
| 185 |
InOrder inOrder1 = inOrder(subscriber1); |
| 186 |
InOrder inOrder2 = inOrder(subscriber2); |
| 187 |
|
| 188 |
f1.subscribe(subscriber1); |
| 189 |
f2.subscribe(subscriber2); |
| 190 |
|
| 191 |
scheduler1.advanceTimeBy(1, TimeUnit.SECONDS); |
| 192 |
scheduler2.advanceTimeBy(1, TimeUnit.SECONDS); |
| 193 |
|
| 194 |
inOrder1.verify(subscriber1, times(1)).onNext(1); |
| 195 |
inOrder1.verify(subscriber1, times(1)).onNext(2); |
| 196 |
inOrder1.verify(subscriber1, times(1)).onNext(3); |
| 197 |
inOrder1.verify(subscriber1, times(1)).onComplete(); |
| 198 |
verify(subscriber1, never()).onError(any(Throwable.class)); |
| 199 |
inOrder1.verifyNoMoreInteractions(); |
| 200 |
|
| 201 |
inOrder2.verify(subscriber2, times(1)).onNext(1); |
| 202 |
inOrder2.verify(subscriber2, times(1)).onNext(2); |
| 203 |
inOrder2.verify(subscriber2, times(1)).onNext(3); |
| 204 |
inOrder2.verify(subscriber2, times(1)).onComplete(); |
| 205 |
verify(subscriber2, never()).onError(any(Throwable.class)); |
| 206 |
inOrder2.verifyNoMoreInteractions(); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Confirm that running on a NewThreadScheduler uses the same thread for the entire stream. |
| 211 |
*/ |
| 212 |
@Test |
| 213 |
public void observeOnWithNewThreadScheduler() { |
| 214 |
final AtomicInteger count = new AtomicInteger(); |
| 215 |
final int _multiple = 99; |
| 216 |
|
| 217 |
Flowable.range(1, 100000).map(new Function<Integer, Integer>() { |
| 218 |
|
| 219 |
@Override |
| 220 |
public Integer apply(Integer t1) { |
| 221 |
return t1 * _multiple; |
| 222 |
} |
| 223 |
|
| 224 |
}).observeOn(Schedulers.newThread()) |
| 225 |
.blockingForEach(new Consumer<Integer>() { |
| 226 |
|
| 227 |
@Override |
| 228 |
public void accept(Integer t1) { |
| 229 |
assertEquals(count.incrementAndGet() * _multiple, t1.intValue()); |
| 230 |
// FIXME toBlocking methods run on the current thread |
| 231 |
String name = Thread.currentThread().getName(); |
| 232 |
assertFalse("Wrong thread name: " + name, name.startsWith("Rx")); |
| 233 |
} |
| 234 |
|
| 235 |
}); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Confirm that running on a ThreadPoolScheduler allows multiple threads but is still ordered. |
| 241 |
*/ |
| 242 |
@Test |
| 243 |
public void observeOnWithThreadPoolScheduler() { |
| 244 |
final AtomicInteger count = new AtomicInteger(); |
| 245 |
final int _multiple = 99; |
| 246 |
|
| 247 |
Flowable.range(1, 100000).map(new Function<Integer, Integer>() { |
| 248 |
|
| 249 |
@Override |
| 250 |
public Integer apply(Integer t1) { |
| 251 |
return t1 * _multiple; |
| 252 |
} |
| 253 |
|
| 254 |
}).observeOn(Schedulers.computation()) |
| 255 |
.blockingForEach(new Consumer<Integer>() { |
| 256 |
|
| 257 |
@Override |
| 258 |
public void accept(Integer t1) { |
| 259 |
assertEquals(count.incrementAndGet() * _multiple, t1.intValue()); |
| 260 |
// FIXME toBlocking methods run on the caller's thread |
| 261 |
String name = Thread.currentThread().getName(); |
| 262 |
assertFalse("Wrong thread name: " + name, name.startsWith("Rx")); |
| 263 |
} |
| 264 |
|
| 265 |
}); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Attempts to confirm that when pauses exist between events, the ScheduledObserver |
| 270 |
* does not lose or reorder any events since the scheduler will not block, but will |
| 271 |
* be re-scheduled when it receives new events after each pause. |
| 272 |
* |
| 273 |
* |
| 274 |
* This is non-deterministic in proving success, but if it ever fails (non-deterministically) |
| 275 |
* it is a sign of potential issues as thread-races and scheduling should not affect output. |
| 276 |
*/ |
| 277 |
@Test |
| 278 |
public void observeOnOrderingConcurrency() { |
| 279 |
final AtomicInteger count = new AtomicInteger(); |
| 280 |
final int _multiple = 99; |
| 281 |
|
| 282 |
Flowable.range(1, 10000).map(new Function<Integer, Integer>() { |
| 283 |
|
| 284 |
@Override |
| 285 |
public Integer apply(Integer t1) { |
| 286 |
if (randomIntFrom0to100() > 98) { |
| 287 |
try { |
| 288 |
Thread.sleep(2); |
| 289 |
} catch (InterruptedException e) { |
| 290 |
e.printStackTrace(); |
| 291 |
} |
| 292 |
} |
| 293 |
return t1 * _multiple; |
| 294 |
} |
| 295 |
|
| 296 |
}).observeOn(Schedulers.computation()) |
| 297 |
.blockingForEach(new Consumer<Integer>() { |
| 298 |
|
| 299 |
@Override |
| 300 |
public void accept(Integer t1) { |
| 301 |
assertEquals(count.incrementAndGet() * _multiple, t1.intValue()); |
| 302 |
// assertTrue(name.startsWith("RxComputationThreadPool")); |
| 303 |
// FIXME toBlocking now runs its methods on the caller thread |
| 304 |
String name = Thread.currentThread().getName(); |
| 305 |
assertFalse("Wrong thread name: " + name, name.startsWith("Rx")); |
| 306 |
} |
| 307 |
|
| 308 |
}); |
| 309 |
} |
| 310 |
|
| 311 |
@Test |
| 312 |
public void nonBlockingOuterWhileBlockingOnNext() throws InterruptedException { |
| 313 |
|
| 314 |
final CountDownLatch completedLatch = new CountDownLatch(1); |
| 315 |
final CountDownLatch nextLatch = new CountDownLatch(1); |
| 316 |
final AtomicLong completeTime = new AtomicLong(); |
| 317 |
// use subscribeOn to make async, observeOn to move |
| 318 |
Flowable.range(1, 2).subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread()).subscribe(new DefaultSubscriber<Integer>() { |
| 319 |
|
| 320 |
@Override |
| 321 |
public void onComplete() { |
| 322 |
System.out.println("onComplete"); |
| 323 |
completeTime.set(System.nanoTime()); |
| 324 |
completedLatch.countDown(); |
| 325 |
} |
| 326 |
|
| 327 |
@Override |
| 328 |
public void onError(Throwable e) { |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public void onNext(Integer t) { |
| 334 |
// don't let this thing finish yet |
| 335 |
try { |
| 336 |
if (!nextLatch.await(1000, TimeUnit.MILLISECONDS)) { |
| 337 |
throw new RuntimeException("it shouldn't have timed out"); |
| 338 |
} |
| 339 |
} catch (InterruptedException e) { |
| 340 |
throw new RuntimeException("it shouldn't have failed"); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
}); |
| 345 |
|
| 346 |
long afterSubscribeTime = System.nanoTime(); |
| 347 |
System.out.println("After subscribe: " + completedLatch.getCount()); |
| 348 |
assertEquals(1, completedLatch.getCount()); |
| 349 |
nextLatch.countDown(); |
| 350 |
completedLatch.await(1000, TimeUnit.MILLISECONDS); |
| 351 |
assertTrue(completeTime.get() > afterSubscribeTime); |
| 352 |
System.out.println("onComplete nanos after subscribe: " + (completeTime.get() - afterSubscribeTime)); |
| 353 |
} |
| 354 |
|
| 355 |
private static int randomIntFrom0to100() { |
| 356 |
// XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml |
| 357 |
long x = System.nanoTime(); |
| 358 |
x ^= (x << 21); |
| 359 |
x ^= (x >>> 35); |
| 360 |
x ^= (x << 4); |
| 361 |
return Math.abs((int) x % 100); |
| 362 |
} |
| 363 |
|
| 364 |
@Test |
| 365 |
public void delayedErrorDeliveryWhenSafeSubscriberUnsubscribes() { |
| 366 |
TestScheduler testScheduler = new TestScheduler(); |
| 367 |
|
| 368 |
Flowable<Integer> source = Flowable.concat(Flowable.<Integer> error(new TestException()), Flowable.just(1)); |
| 369 |
|
| 370 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 371 |
InOrder inOrder = inOrder(subscriber); |
| 372 |
|
| 373 |
source.observeOn(testScheduler).subscribe(subscriber); |
| 374 |
|
| 375 |
inOrder.verify(subscriber, never()).onError(any(TestException.class)); |
| 376 |
|
| 377 |
testScheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 378 |
|
| 379 |
inOrder.verify(subscriber).onError(any(TestException.class)); |
| 380 |
inOrder.verify(subscriber, never()).onNext(anyInt()); |
| 381 |
inOrder.verify(subscriber, never()).onComplete(); |
| 382 |
} |
| 383 |
|
| 384 |
@Test |
| 385 |
public void afterUnsubscribeCalledThenObserverOnNextNeverCalled() { |
| 386 |
final TestScheduler testScheduler = new TestScheduler(); |
| 387 |
|
| 388 |
final Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 389 |
TestSubscriber<Integer> ts = new TestSubscriber<>(subscriber); |
| 390 |
|
| 391 |
Flowable.just(1, 2, 3) |
| 392 |
.observeOn(testScheduler) |
| 393 |
.subscribe(ts); |
| 394 |
|
| 395 |
ts.cancel(); |
| 396 |
testScheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 397 |
|
| 398 |
final InOrder inOrder = inOrder(subscriber); |
| 399 |
|
| 400 |
inOrder.verify(subscriber, never()).onNext(anyInt()); |
| 401 |
inOrder.verify(subscriber, never()).onError(any(Exception.class)); |
| 402 |
inOrder.verify(subscriber, never()).onComplete(); |
| 403 |
} |
| 404 |
|
| 405 |
@Test |
| 406 |
public void backpressureWithTakeAfter() { |
| 407 |
final AtomicInteger generated = new AtomicInteger(); |
| 408 |
Flowable<Integer> flowable = Flowable.fromIterable(new Iterable<Integer>() { |
| 409 |
@Override |
| 410 |
public Iterator<Integer> iterator() { |
| 411 |
return new Iterator<Integer>() { |
| 412 |
|
| 413 |
@Override |
| 414 |
public void remove() { |
| 415 |
} |
| 416 |
|
| 417 |
@Override |
| 418 |
public Integer next() { |
| 419 |
return generated.getAndIncrement(); |
| 420 |
} |
| 421 |
|
| 422 |
@Override |
| 423 |
public boolean hasNext() { |
| 424 |
return true; |
| 425 |
} |
| 426 |
}; |
| 427 |
} |
| 428 |
}); |
| 429 |
|
| 430 |
TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>() { |
| 431 |
@Override |
| 432 |
public void onNext(Integer t) { |
| 433 |
System.err.println("c t = " + t + " thread " + Thread.currentThread()); |
| 434 |
super.onNext(t); |
| 435 |
try { |
| 436 |
Thread.sleep(10); |
| 437 |
} catch (InterruptedException e) { |
| 438 |
} |
| 439 |
} |
| 440 |
}; |
| 441 |
|
| 442 |
flowable |
| 443 |
.observeOn(Schedulers.newThread()) |
| 444 |
.take(3) |
| 445 |
.subscribe(testSubscriber); |
| 446 |
testSubscriber.awaitDone(5, TimeUnit.SECONDS); |
| 447 |
System.err.println(testSubscriber.values()); |
| 448 |
testSubscriber.assertValues(0, 1, 2); |
| 449 |
// it should be between the take num and requested batch size across the async boundary |
| 450 |
System.out.println("Generated: " + generated.get()); |
| 451 |
assertTrue(generated.get() >= 3 && generated.get() <= Flowable.bufferSize()); |
| 452 |
} |
| 453 |
|
| 454 |
@Test |
| 455 |
public void backpressureWithTakeAfterAndMultipleBatches() { |
| 456 |
int numForBatches = Flowable.bufferSize() * 3 + 1; // should be 4 batches == ((3*n)+1) items |
| 457 |
final AtomicInteger generated = new AtomicInteger(); |
| 458 |
Flowable<Integer> flowable = Flowable.fromIterable(new Iterable<Integer>() { |
| 459 |
@Override |
| 460 |
public Iterator<Integer> iterator() { |
| 461 |
return new Iterator<Integer>() { |
| 462 |
|
| 463 |
@Override |
| 464 |
public void remove() { |
| 465 |
} |
| 466 |
|
| 467 |
@Override |
| 468 |
public Integer next() { |
| 469 |
return generated.getAndIncrement(); |
| 470 |
} |
| 471 |
|
| 472 |
@Override |
| 473 |
public boolean hasNext() { |
| 474 |
return true; |
| 475 |
} |
| 476 |
}; |
| 477 |
} |
| 478 |
}); |
| 479 |
|
| 480 |
TestSubscriber<Integer> testSubscriber = new TestSubscriber<Integer>() { |
| 481 |
@Override |
| 482 |
public void onNext(Integer t) { |
| 483 |
// System.err.println("c t = " + t + " thread " + Thread.currentThread()); |
| 484 |
super.onNext(t); |
| 485 |
} |
| 486 |
}; |
| 487 |
|
| 488 |
flowable |
| 489 |
.observeOn(Schedulers.newThread()) |
| 490 |
.take(numForBatches) |
| 491 |
.subscribe(testSubscriber); |
| 492 |
testSubscriber.awaitDone(5, TimeUnit.SECONDS); |
| 493 |
System.err.println(testSubscriber.values()); |
| 494 |
// it should be between the take num and requested batch size across the async boundary |
| 495 |
System.out.println("Generated: " + generated.get()); |
| 496 |
assertTrue(generated.get() >= numForBatches && generated.get() <= numForBatches + Flowable.bufferSize()); |
| 497 |
} |
| 498 |
|
| 499 |
@Test |
| 500 |
public void backpressureWithTakeBefore() { |
| 501 |
final AtomicInteger generated = new AtomicInteger(); |
| 502 |
Flowable<Integer> flowable = Flowable.fromIterable(new Iterable<Integer>() { |
| 503 |
@Override |
| 504 |
public Iterator<Integer> iterator() { |
| 505 |
return new Iterator<Integer>() { |
| 506 |
|
| 507 |
@Override |
| 508 |
public void remove() { |
| 509 |
} |
| 510 |
|
| 511 |
@Override |
| 512 |
public Integer next() { |
| 513 |
return generated.getAndIncrement(); |
| 514 |
} |
| 515 |
|
| 516 |
@Override |
| 517 |
public boolean hasNext() { |
| 518 |
return true; |
| 519 |
} |
| 520 |
}; |
| 521 |
} |
| 522 |
}); |
| 523 |
|
| 524 |
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>(); |
| 525 |
flowable |
| 526 |
.take(7) |
| 527 |
.observeOn(Schedulers.newThread()) |
| 528 |
.subscribe(testSubscriber); |
| 529 |
|
| 530 |
testSubscriber.awaitDone(5, TimeUnit.SECONDS); |
| 531 |
testSubscriber.assertValues(0, 1, 2, 3, 4, 5, 6); |
| 532 |
assertEquals(7, generated.get()); |
| 533 |
} |
| 534 |
|
| 535 |
@Test |
| 536 |
public void queueFullEmitsError() { |
| 537 |
final CountDownLatch latch = new CountDownLatch(1); |
| 538 |
Flowable<Integer> flowable = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 539 |
|
| 540 |
@Override |
| 541 |
public void subscribe(Subscriber<? super Integer> subscriber) { |
| 542 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 543 |
for (int i = 0; i < Flowable.bufferSize() + 10; i++) { |
| 544 |
subscriber.onNext(i); |
| 545 |
} |
| 546 |
latch.countDown(); |
| 547 |
subscriber.onComplete(); |
| 548 |
} |
| 549 |
|
| 550 |
}); |
| 551 |
|
| 552 |
TestSubscriberEx<Integer> testSubscriber = new TestSubscriberEx<>(new DefaultSubscriber<Integer>() { |
| 553 |
|
| 554 |
@Override |
| 555 |
public void onComplete() { |
| 556 |
|
| 557 |
} |
| 558 |
|
| 559 |
@Override |
| 560 |
public void onError(Throwable e) { |
| 561 |
|
| 562 |
} |
| 563 |
|
| 564 |
@Override |
| 565 |
public void onNext(Integer t) { |
| 566 |
// force it to be slow and wait until we have queued everything |
| 567 |
try { |
| 568 |
latch.await(500, TimeUnit.MILLISECONDS); |
| 569 |
} catch (InterruptedException e) { |
| 570 |
e.printStackTrace(); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
}); |
| 575 |
flowable.observeOn(Schedulers.newThread()).subscribe(testSubscriber); |
| 576 |
|
| 577 |
testSubscriber.awaitDone(5, TimeUnit.SECONDS); |
| 578 |
List<Throwable> errors = testSubscriber.errors(); |
| 579 |
assertEquals(1, errors.size()); |
| 580 |
System.out.println("Errors: " + errors); |
| 581 |
Throwable t = errors.get(0); |
| 582 |
if (t instanceof MissingBackpressureException) { |
| 583 |
// success, we expect this |
| 584 |
} else { |
| 585 |
if (t.getCause() instanceof MissingBackpressureException) { |
| 586 |
// this is also okay |
| 587 |
} else { |
| 588 |
fail("Expecting MissingBackpressureException"); |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
@Test |
| 594 |
public void asyncChild() { |
| 595 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 596 |
Flowable.range(0, 100000).observeOn(Schedulers.newThread()).observeOn(Schedulers.newThread()).subscribe(ts); |
| 597 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 598 |
ts.assertNoErrors(); |
| 599 |
} |
| 600 |
|
| 601 |
@Test |
| 602 |
public void onErrorCutsAheadOfOnNext() { |
| 603 |
for (int i = 0; i < 50; i++) { |
| 604 |
final PublishProcessor<Long> processor = PublishProcessor.create(); |
| 605 |
|
| 606 |
final AtomicLong counter = new AtomicLong(); |
| 607 |
TestSubscriberEx<Long> ts = new TestSubscriberEx<>(new DefaultSubscriber<Long>() { |
| 608 |
|
| 609 |
@Override |
| 610 |
public void onComplete() { |
| 611 |
|
| 612 |
} |
| 613 |
|
| 614 |
@Override |
| 615 |
public void onError(Throwable e) { |
| 616 |
|
| 617 |
} |
| 618 |
|
| 619 |
@Override |
| 620 |
public void onNext(Long t) { |
| 621 |
// simulate slow consumer to force backpressure failure |
| 622 |
try { |
| 623 |
Thread.sleep(1); |
| 624 |
} catch (InterruptedException e) { |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
}); |
| 629 |
processor.observeOn(Schedulers.computation()).subscribe(ts); |
| 630 |
|
| 631 |
// this will blow up with backpressure |
| 632 |
while (counter.get() < 102400) { |
| 633 |
processor.onNext(counter.get()); |
| 634 |
counter.incrementAndGet(); |
| 635 |
} |
| 636 |
|
| 637 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 638 |
assertEquals(1, ts.errors().size()); |
| 639 |
ts.assertError(MissingBackpressureException.class); |
| 640 |
// assert that the values are sequential, that cutting in didn't allow skipping some but emitting others. |
| 641 |
// example [0, 1, 2] not [0, 1, 4] |
| 642 |
List<Long> onNextEvents = ts.values(); |
| 643 |
assertTrue(onNextEvents.isEmpty() || onNextEvents.size() == onNextEvents.get(onNextEvents.size() - 1) + 1); |
| 644 |
// we should emit the error without emitting the full buffer size |
| 645 |
assertTrue(onNextEvents.size() < Flowable.bufferSize()); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* Make sure we get a MissingBackpressureException propagated through when we have a fast temporal (hot) producer. |
| 651 |
*/ |
| 652 |
@Test |
| 653 |
public void hotOperatorBackpressure() { |
| 654 |
TestSubscriberEx<String> ts = new TestSubscriberEx<>(); |
| 655 |
Flowable.interval(0, 1, TimeUnit.MICROSECONDS) |
| 656 |
.observeOn(Schedulers.computation()) |
| 657 |
.map(new Function<Long, String>() { |
| 658 |
|
| 659 |
@Override |
| 660 |
public String apply(Long t1) { |
| 661 |
System.out.println(t1); |
| 662 |
try { |
| 663 |
Thread.sleep(100); |
| 664 |
} catch (InterruptedException e) { |
| 665 |
} |
| 666 |
return t1 + " slow value"; |
| 667 |
} |
| 668 |
|
| 669 |
}).subscribe(ts); |
| 670 |
|
| 671 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 672 |
System.out.println("Errors: " + ts.errors()); |
| 673 |
assertEquals(1, ts.errors().size()); |
| 674 |
assertEquals(MissingBackpressureException.class, ts.errors().get(0).getClass()); |
| 675 |
} |
| 676 |
|
| 677 |
@Test |
| 678 |
public void errorPropagatesWhenNoOutstandingRequests() { |
| 679 |
Flowable<Long> timer = Flowable.interval(0, 1, TimeUnit.MICROSECONDS) |
| 680 |
.doOnEach(new Consumer<Notification<Long>>() { |
| 681 |
|
| 682 |
@Override |
| 683 |
public void accept(Notification<Long> n) { |
| 684 |
// System.out.println("BEFORE " + n); |
| 685 |
} |
| 686 |
|
| 687 |
}) |
| 688 |
.observeOn(Schedulers.newThread()) |
| 689 |
.doOnEach(new Consumer<Notification<Long>>() { |
| 690 |
|
| 691 |
@Override |
| 692 |
public void accept(Notification<Long> n) { |
| 693 |
try { |
| 694 |
Thread.sleep(100); |
| 695 |
} catch (InterruptedException e) { |
| 696 |
} |
| 697 |
// System.out.println("AFTER " + n); |
| 698 |
} |
| 699 |
|
| 700 |
}); |
| 701 |
|
| 702 |
TestSubscriberEx<Long> ts = new TestSubscriberEx<>(); |
| 703 |
|
| 704 |
Flowable.combineLatest(timer, Flowable.<Integer> never(), new BiFunction<Long, Integer, Long>() { |
| 705 |
|
| 706 |
@Override |
| 707 |
public Long apply(Long t1, Integer t2) { |
| 708 |
return t1; |
| 709 |
} |
| 710 |
|
| 711 |
}).take(Flowable.bufferSize() * 2).subscribe(ts); |
| 712 |
|
| 713 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 714 |
assertEquals(1, ts.errors().size()); |
| 715 |
assertEquals(MissingBackpressureException.class, ts.errors().get(0).getClass()); |
| 716 |
} |
| 717 |
|
| 718 |
@Test |
| 719 |
public void requestOverflow() throws InterruptedException { |
| 720 |
|
| 721 |
final CountDownLatch latch = new CountDownLatch(1); |
| 722 |
final AtomicInteger count = new AtomicInteger(); |
| 723 |
Flowable.range(1, 100).observeOn(Schedulers.computation()) |
| 724 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 725 |
|
| 726 |
boolean first = true; |
| 727 |
|
| 728 |
@Override |
| 729 |
public void onStart() { |
| 730 |
request(2); |
| 731 |
} |
| 732 |
|
| 733 |
@Override |
| 734 |
public void onComplete() { |
| 735 |
latch.countDown(); |
| 736 |
} |
| 737 |
|
| 738 |
@Override |
| 739 |
public void onError(Throwable e) { |
| 740 |
|
| 741 |
} |
| 742 |
|
| 743 |
@Override |
| 744 |
public void onNext(Integer t) { |
| 745 |
count.incrementAndGet(); |
| 746 |
if (first) { |
| 747 |
request(Long.MAX_VALUE - 1); |
| 748 |
request(Long.MAX_VALUE - 1); |
| 749 |
request(10); |
| 750 |
first = false; |
| 751 |
} |
| 752 |
} |
| 753 |
}); |
| 754 |
assertTrue(latch.await(10, TimeUnit.SECONDS)); |
| 755 |
assertEquals(100, count.get()); |
| 756 |
|
| 757 |
} |
| 758 |
|
| 759 |
@Test |
| 760 |
public void noMoreRequestsAfterUnsubscribe() throws InterruptedException { |
| 761 |
final CountDownLatch latch = new CountDownLatch(1); |
| 762 |
final List<Long> requests = Collections.synchronizedList(new ArrayList<>()); |
| 763 |
Flowable.range(1, 1000000) |
| 764 |
.doOnRequest(new LongConsumer() { |
| 765 |
|
| 766 |
@Override |
| 767 |
public void accept(long n) { |
| 768 |
requests.add(n); |
| 769 |
} |
| 770 |
}) |
| 771 |
.observeOn(Schedulers.io()) |
| 772 |
.subscribe(new DefaultSubscriber<Integer>() { |
| 773 |
|
| 774 |
@Override |
| 775 |
public void onStart() { |
| 776 |
request(1); |
| 777 |
} |
| 778 |
|
| 779 |
@Override |
| 780 |
public void onComplete() { |
| 781 |
} |
| 782 |
|
| 783 |
@Override |
| 784 |
public void onError(Throwable e) { |
| 785 |
} |
| 786 |
|
| 787 |
@Override |
| 788 |
public void onNext(Integer t) { |
| 789 |
cancel(); |
| 790 |
latch.countDown(); |
| 791 |
} |
| 792 |
}); |
| 793 |
assertTrue(latch.await(10, TimeUnit.SECONDS)); |
| 794 |
// FIXME observeOn requests bufferSize at first always |
| 795 |
assertEquals(Arrays.asList(128L), requests); |
| 796 |
} |
| 797 |
|
| 798 |
@Test |
| 799 |
public void errorDelayed() { |
| 800 |
TestScheduler s = new TestScheduler(); |
| 801 |
|
| 802 |
Flowable<Integer> source = Flowable.just(1, 2, 3) |
| 803 |
.concatWith(Flowable.<Integer>error(new TestException())); |
| 804 |
|
| 805 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 806 |
|
| 807 |
source.observeOn(s, true).subscribe(ts); |
| 808 |
|
| 809 |
ts.assertNoValues(); |
| 810 |
ts.assertNoErrors(); |
| 811 |
ts.assertNotComplete(); |
| 812 |
|
| 813 |
s.advanceTimeBy(1, TimeUnit.SECONDS); |
| 814 |
|
| 815 |
ts.assertNoValues(); |
| 816 |
ts.assertNoErrors(); |
| 817 |
ts.assertNotComplete(); |
| 818 |
|
| 819 |
ts.request(1); |
| 820 |
s.advanceTimeBy(1, TimeUnit.SECONDS); |
| 821 |
|
| 822 |
ts.assertValues(1); |
| 823 |
ts.assertNoErrors(); |
| 824 |
ts.assertNotComplete(); |
| 825 |
|
| 826 |
ts.request(3); // requesting 2 doesn't switch to the error() source for some reason in concat. |
| 827 |
s.advanceTimeBy(1, TimeUnit.SECONDS); |
| 828 |
|
| 829 |
ts.assertValues(1, 2, 3); |
| 830 |
ts.assertError(TestException.class); |
| 831 |
ts.assertNotComplete(); |
| 832 |
} |
| 833 |
|
| 834 |
@Test |
| 835 |
public void errorDelayedAsync() { |
| 836 |
Flowable<Integer> source = Flowable.just(1, 2, 3) |
| 837 |
.concatWith(Flowable.<Integer>error(new TestException())); |
| 838 |
|
| 839 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 840 |
|
| 841 |
source.observeOn(Schedulers.computation(), true).subscribe(ts); |
| 842 |
|
| 843 |
ts.awaitDone(2, TimeUnit.SECONDS); |
| 844 |
ts.assertValues(1, 2, 3); |
| 845 |
ts.assertError(TestException.class); |
| 846 |
ts.assertNotComplete(); |
| 847 |
} |
| 848 |
|
| 849 |
@Test |
| 850 |
public void requestExactCompletesImmediately() { |
| 851 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 852 |
|
| 853 |
TestScheduler test = new TestScheduler(); |
| 854 |
|
| 855 |
Flowable.range(1, 10).observeOn(test).subscribe(ts); |
| 856 |
|
| 857 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 858 |
|
| 859 |
ts.assertNoValues(); |
| 860 |
ts.assertNoErrors(); |
| 861 |
ts.assertNotComplete(); |
| 862 |
|
| 863 |
ts.request(10); |
| 864 |
|
| 865 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 866 |
|
| 867 |
ts.assertValueCount(10); |
| 868 |
ts.assertNoErrors(); |
| 869 |
ts.assertComplete(); |
| 870 |
} |
| 871 |
|
| 872 |
@Test |
| 873 |
public void fixedReplenishPattern() { |
| 874 |
TestSubscriber<Integer> ts = TestSubscriber.create(0); |
| 875 |
|
| 876 |
TestScheduler test = new TestScheduler(); |
| 877 |
|
| 878 |
final List<Long> requests = new ArrayList<>(); |
| 879 |
|
| 880 |
Flowable.range(1, 100) |
| 881 |
.doOnRequest(new LongConsumer() { |
| 882 |
@Override |
| 883 |
public void accept(long v) { |
| 884 |
requests.add(v); |
| 885 |
} |
| 886 |
}) |
| 887 |
.observeOn(test, false, 16).subscribe(ts); |
| 888 |
|
| 889 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 890 |
ts.request(20); |
| 891 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 892 |
ts.request(10); |
| 893 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 894 |
ts.request(50); |
| 895 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 896 |
ts.request(35); |
| 897 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 898 |
|
| 899 |
ts.assertValueCount(100); |
| 900 |
ts.assertComplete(); |
| 901 |
ts.assertNoErrors(); |
| 902 |
|
| 903 |
assertEquals(Arrays.asList(16L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), requests); |
| 904 |
} |
| 905 |
|
| 906 |
@Test |
| 907 |
public void bufferSizesWork() { |
| 908 |
for (int i = 1; i <= 1024; i = i * 2) { |
| 909 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 910 |
|
| 911 |
Flowable.range(1, 1000 * 1000).observeOn(Schedulers.computation(), false, i) |
| 912 |
.subscribe(ts); |
| 913 |
|
| 914 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 915 |
ts.assertValueCount(1000 * 1000); |
| 916 |
ts.assertComplete(); |
| 917 |
ts.assertNoErrors(); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
@Test |
| 922 |
public void synchronousRebatching() { |
| 923 |
final List<Long> requests = new ArrayList<>(); |
| 924 |
|
| 925 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 926 |
|
| 927 |
Flowable.range(1, 50) |
| 928 |
.doOnRequest(new LongConsumer() { |
| 929 |
@Override |
| 930 |
public void accept(long r) { |
| 931 |
requests.add(r); |
| 932 |
} |
| 933 |
}) |
| 934 |
.rebatchRequests(20) |
| 935 |
.subscribe(ts); |
| 936 |
|
| 937 |
ts.assertValueCount(50); |
| 938 |
ts.assertNoErrors(); |
| 939 |
ts.assertComplete(); |
| 940 |
|
| 941 |
assertEquals(Arrays.asList(20L, 15L, 15L, 15L), requests); |
| 942 |
} |
| 943 |
|
| 944 |
@Test |
| 945 |
public void rebatchRequestsArgumentCheck() { |
| 946 |
try { |
| 947 |
Flowable.never().rebatchRequests(-99); |
| 948 |
fail("Didn't throw IAE"); |
| 949 |
} catch (IllegalArgumentException ex) { |
| 950 |
assertEquals("bufferSize > 0 required but it was -99", ex.getMessage()); |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
@Test |
| 955 |
public void delayError() { |
| 956 |
Flowable.range(1, 5).concatWith(Flowable.<Integer>error(new TestException())) |
| 957 |
.observeOn(Schedulers.computation(), true) |
| 958 |
.doOnNext(new Consumer<Integer>() { |
| 959 |
@Override |
| 960 |
public void accept(Integer v) throws Exception { |
| 961 |
if (v == 1) { |
| 962 |
Thread.sleep(100); |
| 963 |
} |
| 964 |
} |
| 965 |
}) |
| 966 |
.test() |
| 967 |
.awaitDone(5, TimeUnit.SECONDS) |
| 968 |
.assertFailure(TestException.class, 1, 2, 3, 4, 5); |
| 969 |
} |
| 970 |
|
| 971 |
@Test |
| 972 |
public void conditionalConsumer() { |
| 973 |
Flowable.range(1, 5) |
| 974 |
.observeOn(Schedulers.single()) |
| 975 |
.filter(new Predicate<Integer>() { |
| 976 |
@Override |
| 977 |
public boolean test(Integer v) throws Exception { |
| 978 |
return v % 2 == 0; |
| 979 |
} |
| 980 |
}) |
| 981 |
.test() |
| 982 |
.awaitDone(5, TimeUnit.SECONDS) |
| 983 |
.assertResult(2, 4); |
| 984 |
} |
| 985 |
|
| 986 |
@Test |
| 987 |
public void take() { |
| 988 |
Flowable.range(1, 5) |
| 989 |
.observeOn(Schedulers.single()) |
| 990 |
.take(3) |
| 991 |
.take(3) |
| 992 |
.test() |
| 993 |
.awaitDone(5, TimeUnit.SECONDS) |
| 994 |
.assertResult(1, 2, 3); |
| 995 |
} |
| 996 |
|
| 997 |
@Test |
| 998 |
public void cancelCleanup() { |
| 999 |
TestSubscriber<Integer> ts = Flowable.range(1, 5) |
| 1000 |
.observeOn(Schedulers.single()) |
| 1001 |
.test(0L); |
| 1002 |
|
| 1003 |
ts.cancel(); |
| 1004 |
} |
| 1005 |
|
| 1006 |
@Test |
| 1007 |
public void conditionalConsumerFused() { |
| 1008 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1009 |
|
| 1010 |
Flowable.range(1, 5) |
| 1011 |
.observeOn(Schedulers.single()) |
| 1012 |
.filter(new Predicate<Integer>() { |
| 1013 |
@Override |
| 1014 |
public boolean test(Integer v) throws Exception { |
| 1015 |
return v % 2 == 0; |
| 1016 |
} |
| 1017 |
}) |
| 1018 |
.subscribe(ts); |
| 1019 |
|
| 1020 |
ts |
| 1021 |
.assertFuseable() |
| 1022 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 1023 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1024 |
.assertResult(2, 4); |
| 1025 |
} |
| 1026 |
|
| 1027 |
@Test |
| 1028 |
public void conditionalConsumerFusedReject() { |
| 1029 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC); |
| 1030 |
|
| 1031 |
Flowable.range(1, 5) |
| 1032 |
.observeOn(Schedulers.single()) |
| 1033 |
.filter(new Predicate<Integer>() { |
| 1034 |
@Override |
| 1035 |
public boolean test(Integer v) throws Exception { |
| 1036 |
return v % 2 == 0; |
| 1037 |
} |
| 1038 |
}) |
| 1039 |
.subscribe(ts); |
| 1040 |
|
| 1041 |
ts |
| 1042 |
.assertFuseable() |
| 1043 |
.assertFusionMode(QueueFuseable.NONE) |
| 1044 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1045 |
.assertResult(2, 4); |
| 1046 |
} |
| 1047 |
|
| 1048 |
@Test |
| 1049 |
public void requestOne() throws Exception { |
| 1050 |
TestSubscriberEx<Integer> ts = Flowable.range(1, 5) |
| 1051 |
.observeOn(Schedulers.single()) |
| 1052 |
.to(TestHelper.<Integer>testSubscriber(1L)); |
| 1053 |
|
| 1054 |
Thread.sleep(100); |
| 1055 |
|
| 1056 |
ts.assertSubscribed().assertValue(1).assertNoErrors().assertNotComplete(); |
| 1057 |
} |
| 1058 |
|
| 1059 |
@Test |
| 1060 |
public void requestOneConditional() throws Exception { |
| 1061 |
TestSubscriberEx<Integer> ts = Flowable.range(1, 5) |
| 1062 |
.observeOn(Schedulers.single()) |
| 1063 |
.filter(Functions.alwaysTrue()) |
| 1064 |
.to(TestHelper.<Integer>testSubscriber(1L)); |
| 1065 |
|
| 1066 |
Thread.sleep(100); |
| 1067 |
|
| 1068 |
ts.assertSubscribed().assertValue(1).assertNoErrors().assertNotComplete(); |
| 1069 |
} |
| 1070 |
|
| 1071 |
@Test |
| 1072 |
public void conditionalConsumerFusedAsync() { |
| 1073 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1074 |
|
| 1075 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1076 |
|
| 1077 |
up |
| 1078 |
.observeOn(Schedulers.single()) |
| 1079 |
.filter(new Predicate<Integer>() { |
| 1080 |
@Override |
| 1081 |
public boolean test(Integer v) throws Exception { |
| 1082 |
return v % 2 == 0; |
| 1083 |
} |
| 1084 |
}) |
| 1085 |
.subscribe(ts); |
| 1086 |
|
| 1087 |
up.onNext(1); |
| 1088 |
up.onNext(2); |
| 1089 |
up.onNext(3); |
| 1090 |
up.onNext(4); |
| 1091 |
up.onNext(5); |
| 1092 |
up.onComplete(); |
| 1093 |
|
| 1094 |
ts |
| 1095 |
.assertFuseable() |
| 1096 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 1097 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1098 |
.assertResult(2, 4); |
| 1099 |
} |
| 1100 |
|
| 1101 |
@Test |
| 1102 |
public void conditionalConsumerHidden() { |
| 1103 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1104 |
|
| 1105 |
Flowable.range(1, 5).hide() |
| 1106 |
.observeOn(Schedulers.single()) |
| 1107 |
.filter(new Predicate<Integer>() { |
| 1108 |
@Override |
| 1109 |
public boolean test(Integer v) throws Exception { |
| 1110 |
return v % 2 == 0; |
| 1111 |
} |
| 1112 |
}) |
| 1113 |
.subscribe(ts); |
| 1114 |
|
| 1115 |
ts |
| 1116 |
.assertFuseable() |
| 1117 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 1118 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1119 |
.assertResult(2, 4); |
| 1120 |
} |
| 1121 |
|
| 1122 |
@Test |
| 1123 |
public void conditionalConsumerBarrier() { |
| 1124 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1125 |
|
| 1126 |
Flowable.range(1, 5) |
| 1127 |
.map(Functions.<Integer>identity()) |
| 1128 |
.observeOn(Schedulers.single()) |
| 1129 |
.filter(new Predicate<Integer>() { |
| 1130 |
@Override |
| 1131 |
public boolean test(Integer v) throws Exception { |
| 1132 |
return v % 2 == 0; |
| 1133 |
} |
| 1134 |
}) |
| 1135 |
.subscribe(ts); |
| 1136 |
|
| 1137 |
ts |
| 1138 |
.assertFuseable() |
| 1139 |
.assertFusionMode(QueueFuseable.ASYNC) |
| 1140 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1141 |
.assertResult(2, 4); |
| 1142 |
} |
| 1143 |
|
| 1144 |
@Test |
| 1145 |
public void dispose() { |
| 1146 |
TestHelper.checkDisposed(PublishProcessor.create().observeOn(new TestScheduler())); |
| 1147 |
} |
| 1148 |
|
| 1149 |
@Test |
| 1150 |
public void doubleOnSubscribe() { |
| 1151 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 1152 |
@Override |
| 1153 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 1154 |
return f.observeOn(new TestScheduler()); |
| 1155 |
} |
| 1156 |
}); |
| 1157 |
} |
| 1158 |
|
| 1159 |
@Test |
| 1160 |
public void doubleOnSubscribeConditional() { |
| 1161 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 1162 |
@Override |
| 1163 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 1164 |
return f.observeOn(new TestScheduler()).compose(TestHelper.conditional()); |
| 1165 |
} |
| 1166 |
}); |
| 1167 |
} |
| 1168 |
|
| 1169 |
@Test |
| 1170 |
public void badSource() { |
| 1171 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1172 |
try { |
| 1173 |
TestScheduler scheduler = new TestScheduler(); |
| 1174 |
TestSubscriber<Integer> ts = new Flowable<Integer>() { |
| 1175 |
@Override |
| 1176 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 1177 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1178 |
subscriber.onComplete(); |
| 1179 |
subscriber.onNext(1); |
| 1180 |
subscriber.onError(new TestException()); |
| 1181 |
subscriber.onComplete(); |
| 1182 |
} |
| 1183 |
} |
| 1184 |
.observeOn(scheduler) |
| 1185 |
.test(); |
| 1186 |
|
| 1187 |
scheduler.triggerActions(); |
| 1188 |
|
| 1189 |
ts.assertResult(); |
| 1190 |
|
| 1191 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 1192 |
} finally { |
| 1193 |
RxJavaPlugins.reset(); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|
| 1197 |
@Test |
| 1198 |
public void inputSyncFused() { |
| 1199 |
Flowable.range(1, 5) |
| 1200 |
.observeOn(Schedulers.single()) |
| 1201 |
.test() |
| 1202 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1203 |
.assertResult(1, 2, 3, 4, 5); |
| 1204 |
} |
| 1205 |
|
| 1206 |
@Test |
| 1207 |
public void inputAsyncFused() { |
| 1208 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1209 |
|
| 1210 |
TestSubscriber<Integer> ts = up.observeOn(Schedulers.single()).test(); |
| 1211 |
|
| 1212 |
TestHelper.emit(up, 1, 2, 3, 4, 5); |
| 1213 |
|
| 1214 |
ts |
| 1215 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1216 |
.assertResult(1, 2, 3, 4, 5); |
| 1217 |
} |
| 1218 |
|
| 1219 |
@Test |
| 1220 |
public void inputAsyncFusedError() { |
| 1221 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1222 |
|
| 1223 |
TestSubscriber<Integer> ts = up.observeOn(Schedulers.single()).test(); |
| 1224 |
|
| 1225 |
up.onError(new TestException()); |
| 1226 |
|
| 1227 |
ts |
| 1228 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1229 |
.assertFailure(TestException.class); |
| 1230 |
} |
| 1231 |
|
| 1232 |
@Test |
| 1233 |
public void inputAsyncFusedErrorDelayed() { |
| 1234 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1235 |
|
| 1236 |
TestSubscriber<Integer> ts = up.observeOn(Schedulers.single(), true).test(); |
| 1237 |
|
| 1238 |
up.onError(new TestException()); |
| 1239 |
|
| 1240 |
ts |
| 1241 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1242 |
.assertFailure(TestException.class); |
| 1243 |
} |
| 1244 |
|
| 1245 |
@Test |
| 1246 |
public void outputFused() { |
| 1247 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1248 |
|
| 1249 |
Flowable.range(1, 5).hide() |
| 1250 |
.observeOn(Schedulers.single()) |
| 1251 |
.subscribe(ts); |
| 1252 |
|
| 1253 |
ts.assertFusionMode(QueueFuseable.ASYNC) |
| 1254 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1255 |
.assertResult(1, 2, 3, 4, 5); |
| 1256 |
} |
| 1257 |
|
| 1258 |
@Test |
| 1259 |
public void outputFusedReject() { |
| 1260 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC); |
| 1261 |
|
| 1262 |
Flowable.range(1, 5).hide() |
| 1263 |
.observeOn(Schedulers.single()) |
| 1264 |
.subscribe(ts); |
| 1265 |
|
| 1266 |
ts.assertFusionMode(QueueFuseable.NONE) |
| 1267 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1268 |
.assertResult(1, 2, 3, 4, 5); |
| 1269 |
} |
| 1270 |
|
| 1271 |
@Test |
| 1272 |
public void inputOutputAsyncFusedError() { |
| 1273 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1274 |
|
| 1275 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1276 |
|
| 1277 |
up.observeOn(Schedulers.single()) |
| 1278 |
.subscribe(ts); |
| 1279 |
|
| 1280 |
up.onError(new TestException()); |
| 1281 |
|
| 1282 |
ts |
| 1283 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1284 |
.assertFailure(TestException.class); |
| 1285 |
|
| 1286 |
ts.assertFusionMode(QueueFuseable.ASYNC) |
| 1287 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1288 |
.assertFailure(TestException.class); |
| 1289 |
} |
| 1290 |
|
| 1291 |
@Test |
| 1292 |
public void inputOutputAsyncFusedErrorDelayed() { |
| 1293 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1294 |
|
| 1295 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1296 |
|
| 1297 |
up.observeOn(Schedulers.single(), true) |
| 1298 |
.subscribe(ts); |
| 1299 |
|
| 1300 |
up.onError(new TestException()); |
| 1301 |
|
| 1302 |
ts |
| 1303 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1304 |
.assertFailure(TestException.class); |
| 1305 |
|
| 1306 |
ts.assertFusionMode(QueueFuseable.ASYNC) |
| 1307 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1308 |
.assertFailure(TestException.class); |
| 1309 |
} |
| 1310 |
|
| 1311 |
@Test |
| 1312 |
public void outputFusedCancelReentrant() throws Exception { |
| 1313 |
final UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1314 |
|
| 1315 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 1316 |
|
| 1317 |
up.observeOn(Schedulers.single()) |
| 1318 |
.subscribe(new FlowableSubscriber<Integer>() { |
| 1319 |
Subscription upstream; |
| 1320 |
int count; |
| 1321 |
@Override |
| 1322 |
public void onSubscribe(Subscription s) { |
| 1323 |
this.upstream = s; |
| 1324 |
((QueueSubscription<?>)s).requestFusion(QueueFuseable.ANY); |
| 1325 |
} |
| 1326 |
|
| 1327 |
@Override |
| 1328 |
public void onNext(Integer value) { |
| 1329 |
if (++count == 1) { |
| 1330 |
up.onNext(2); |
| 1331 |
upstream.cancel(); |
| 1332 |
cdl.countDown(); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
@Override |
| 1337 |
public void onError(Throwable e) { |
| 1338 |
|
| 1339 |
} |
| 1340 |
|
| 1341 |
@Override |
| 1342 |
public void onComplete() { |
| 1343 |
|
| 1344 |
} |
| 1345 |
}); |
| 1346 |
|
| 1347 |
up.onNext(1); |
| 1348 |
|
| 1349 |
cdl.await(); |
| 1350 |
} |
| 1351 |
|
| 1352 |
@Test |
| 1353 |
public void nonFusedPollThrows() { |
| 1354 |
new Flowable<Integer>() { |
| 1355 |
@Override |
| 1356 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 1357 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1358 |
|
| 1359 |
@SuppressWarnings("unchecked") |
| 1360 |
BaseObserveOnSubscriber<Integer> oo = (BaseObserveOnSubscriber<Integer>)subscriber; |
| 1361 |
|
| 1362 |
oo.sourceMode = QueueFuseable.SYNC; |
| 1363 |
oo.requested.lazySet(1); |
| 1364 |
oo.queue = new SimpleQueue<Integer>() { |
| 1365 |
|
| 1366 |
@Override |
| 1367 |
public boolean offer(Integer value) { |
| 1368 |
return false; |
| 1369 |
} |
| 1370 |
|
| 1371 |
@Override |
| 1372 |
public boolean offer(Integer v1, Integer v2) { |
| 1373 |
return false; |
| 1374 |
} |
| 1375 |
|
| 1376 |
@Nullable |
| 1377 |
@Override |
| 1378 |
public Integer poll() throws Exception { |
| 1379 |
throw new TestException(); |
| 1380 |
} |
| 1381 |
|
| 1382 |
@Override |
| 1383 |
public boolean isEmpty() { |
| 1384 |
return false; |
| 1385 |
} |
| 1386 |
|
| 1387 |
@Override |
| 1388 |
public void clear() { |
| 1389 |
} |
| 1390 |
}; |
| 1391 |
|
| 1392 |
oo.clear(); |
| 1393 |
|
| 1394 |
oo.trySchedule(); |
| 1395 |
} |
| 1396 |
} |
| 1397 |
.observeOn(Schedulers.single()) |
| 1398 |
.test(0L) |
| 1399 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1400 |
.assertFailure(TestException.class); |
| 1401 |
} |
| 1402 |
|
| 1403 |
@Test |
| 1404 |
public void conditionalNonFusedPollThrows() { |
| 1405 |
new Flowable<Integer>() { |
| 1406 |
@Override |
| 1407 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 1408 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1409 |
|
| 1410 |
@SuppressWarnings("unchecked") |
| 1411 |
BaseObserveOnSubscriber<Integer> oo = (BaseObserveOnSubscriber<Integer>)subscriber; |
| 1412 |
|
| 1413 |
oo.sourceMode = QueueFuseable.SYNC; |
| 1414 |
oo.requested.lazySet(1); |
| 1415 |
oo.queue = new SimpleQueue<Integer>() { |
| 1416 |
|
| 1417 |
@Override |
| 1418 |
public boolean offer(Integer value) { |
| 1419 |
return false; |
| 1420 |
} |
| 1421 |
|
| 1422 |
@Override |
| 1423 |
public boolean offer(Integer v1, Integer v2) { |
| 1424 |
return false; |
| 1425 |
} |
| 1426 |
|
| 1427 |
@Nullable |
| 1428 |
@Override |
| 1429 |
public Integer poll() throws Exception { |
| 1430 |
throw new TestException(); |
| 1431 |
} |
| 1432 |
|
| 1433 |
@Override |
| 1434 |
public boolean isEmpty() { |
| 1435 |
return false; |
| 1436 |
} |
| 1437 |
|
| 1438 |
@Override |
| 1439 |
public void clear() { |
| 1440 |
} |
| 1441 |
}; |
| 1442 |
|
| 1443 |
oo.clear(); |
| 1444 |
|
| 1445 |
oo.trySchedule(); |
| 1446 |
} |
| 1447 |
} |
| 1448 |
.observeOn(Schedulers.single()) |
| 1449 |
.filter(Functions.alwaysTrue()) |
| 1450 |
.test(0L) |
| 1451 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1452 |
.assertFailure(TestException.class); |
| 1453 |
} |
| 1454 |
|
| 1455 |
@Test |
| 1456 |
public void asycFusedPollThrows() { |
| 1457 |
new Flowable<Integer>() { |
| 1458 |
@Override |
| 1459 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 1460 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1461 |
|
| 1462 |
@SuppressWarnings("unchecked") |
| 1463 |
BaseObserveOnSubscriber<Integer> oo = (BaseObserveOnSubscriber<Integer>)subscriber; |
| 1464 |
|
| 1465 |
oo.sourceMode = QueueFuseable.ASYNC; |
| 1466 |
oo.requested.lazySet(1); |
| 1467 |
oo.queue = new SimpleQueue<Integer>() { |
| 1468 |
|
| 1469 |
@Override |
| 1470 |
public boolean offer(Integer value) { |
| 1471 |
return false; |
| 1472 |
} |
| 1473 |
|
| 1474 |
@Override |
| 1475 |
public boolean offer(Integer v1, Integer v2) { |
| 1476 |
return false; |
| 1477 |
} |
| 1478 |
|
| 1479 |
@Nullable |
| 1480 |
@Override |
| 1481 |
public Integer poll() throws Exception { |
| 1482 |
throw new TestException(); |
| 1483 |
} |
| 1484 |
|
| 1485 |
@Override |
| 1486 |
public boolean isEmpty() { |
| 1487 |
return false; |
| 1488 |
} |
| 1489 |
|
| 1490 |
@Override |
| 1491 |
public void clear() { |
| 1492 |
} |
| 1493 |
}; |
| 1494 |
|
| 1495 |
oo.clear(); |
| 1496 |
|
| 1497 |
oo.trySchedule(); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
.observeOn(Schedulers.single()) |
| 1501 |
.test(0L) |
| 1502 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1503 |
.assertFailure(TestException.class); |
| 1504 |
} |
| 1505 |
|
| 1506 |
@Test |
| 1507 |
public void conditionalAsyncFusedPollThrows() { |
| 1508 |
new Flowable<Integer>() { |
| 1509 |
@Override |
| 1510 |
protected void subscribeActual(Subscriber<? super Integer> subscriber) { |
| 1511 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 1512 |
|
| 1513 |
@SuppressWarnings("unchecked") |
| 1514 |
BaseObserveOnSubscriber<Integer> oo = (BaseObserveOnSubscriber<Integer>)subscriber; |
| 1515 |
|
| 1516 |
oo.sourceMode = QueueFuseable.ASYNC; |
| 1517 |
oo.requested.lazySet(1); |
| 1518 |
oo.queue = new SimpleQueue<Integer>() { |
| 1519 |
|
| 1520 |
@Override |
| 1521 |
public boolean offer(Integer value) { |
| 1522 |
return false; |
| 1523 |
} |
| 1524 |
|
| 1525 |
@Override |
| 1526 |
public boolean offer(Integer v1, Integer v2) { |
| 1527 |
return false; |
| 1528 |
} |
| 1529 |
|
| 1530 |
@Nullable |
| 1531 |
@Override |
| 1532 |
public Integer poll() throws Exception { |
| 1533 |
throw new TestException(); |
| 1534 |
} |
| 1535 |
|
| 1536 |
@Override |
| 1537 |
public boolean isEmpty() { |
| 1538 |
return false; |
| 1539 |
} |
| 1540 |
|
| 1541 |
@Override |
| 1542 |
public void clear() { |
| 1543 |
} |
| 1544 |
}; |
| 1545 |
|
| 1546 |
oo.clear(); |
| 1547 |
|
| 1548 |
oo.trySchedule(); |
| 1549 |
} |
| 1550 |
} |
| 1551 |
.observeOn(Schedulers.single()) |
| 1552 |
.filter(Functions.alwaysTrue()) |
| 1553 |
.test(0L) |
| 1554 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1555 |
.assertFailure(TestException.class); |
| 1556 |
} |
| 1557 |
|
| 1558 |
@Test |
| 1559 |
public void trampolineScheduler() { |
| 1560 |
Flowable.just(1) |
| 1561 |
.observeOn(Schedulers.trampoline()) |
| 1562 |
.test() |
| 1563 |
.assertResult(1); |
| 1564 |
} |
| 1565 |
|
| 1566 |
@Test |
| 1567 |
public void conditionalNormal() { |
| 1568 |
Flowable.range(1, 1000).hide() |
| 1569 |
.observeOn(Schedulers.single()) |
| 1570 |
.filter(new Predicate<Integer>() { |
| 1571 |
@Override |
| 1572 |
public boolean test(Integer v) throws Exception { |
| 1573 |
return v % 2 == 0; |
| 1574 |
} |
| 1575 |
}) |
| 1576 |
.take(250) |
| 1577 |
.to(TestHelper.<Integer>testConsumer()) |
| 1578 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1579 |
.assertSubscribed() |
| 1580 |
.assertValueCount(250) |
| 1581 |
.assertNoErrors() |
| 1582 |
.assertComplete(); |
| 1583 |
} |
| 1584 |
|
| 1585 |
@Test |
| 1586 |
public void syncFusedCancelAfterRequest() { |
| 1587 |
final TestSubscriber<Integer> ts = new TestSubscriber<Integer>(2L) { |
| 1588 |
@Override |
| 1589 |
public void onNext(Integer t) { |
| 1590 |
super.onNext(t); |
| 1591 |
if (t == 2) { |
| 1592 |
cancel(); |
| 1593 |
onComplete(); |
| 1594 |
} |
| 1595 |
} |
| 1596 |
}; |
| 1597 |
|
| 1598 |
Flowable.range(1, 3) |
| 1599 |
.observeOn(Schedulers.single()) |
| 1600 |
.subscribe(ts); |
| 1601 |
|
| 1602 |
ts |
| 1603 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1604 |
.assertResult(1, 2); |
| 1605 |
} |
| 1606 |
|
| 1607 |
@Test |
| 1608 |
public void syncFusedCancelAfterRequest2() { |
| 1609 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(2L); |
| 1610 |
|
| 1611 |
Flowable.range(1, 2) |
| 1612 |
.observeOn(Schedulers.single()) |
| 1613 |
.subscribe(ts); |
| 1614 |
|
| 1615 |
ts |
| 1616 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1617 |
.assertResult(1, 2); |
| 1618 |
} |
| 1619 |
|
| 1620 |
@Test |
| 1621 |
public void syncFusedCancelAfterRequestConditional() { |
| 1622 |
final TestSubscriber<Integer> ts = new TestSubscriber<Integer>(2L) { |
| 1623 |
@Override |
| 1624 |
public void onNext(Integer t) { |
| 1625 |
super.onNext(t); |
| 1626 |
if (t == 2) { |
| 1627 |
cancel(); |
| 1628 |
onComplete(); |
| 1629 |
} |
| 1630 |
} |
| 1631 |
}; |
| 1632 |
|
| 1633 |
Flowable.range(1, 3) |
| 1634 |
.observeOn(Schedulers.single()) |
| 1635 |
.filter(Functions.alwaysTrue()) |
| 1636 |
.subscribe(ts); |
| 1637 |
|
| 1638 |
ts |
| 1639 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1640 |
.assertResult(1, 2); |
| 1641 |
} |
| 1642 |
|
| 1643 |
@Test |
| 1644 |
public void syncFusedCancelAfterRequestConditional2() { |
| 1645 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(2L); |
| 1646 |
|
| 1647 |
Flowable.range(1, 2) |
| 1648 |
.observeOn(Schedulers.single()) |
| 1649 |
.filter(Functions.alwaysTrue()) |
| 1650 |
.subscribe(ts); |
| 1651 |
|
| 1652 |
ts |
| 1653 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1654 |
.assertResult(1, 2); |
| 1655 |
} |
| 1656 |
|
| 1657 |
@Test |
| 1658 |
public void nonFusedCancelAfterRequestConditional2() { |
| 1659 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(2L); |
| 1660 |
|
| 1661 |
Flowable.range(1, 2).hide() |
| 1662 |
.observeOn(Schedulers.single()) |
| 1663 |
.filter(Functions.alwaysTrue()) |
| 1664 |
.subscribe(ts); |
| 1665 |
|
| 1666 |
ts |
| 1667 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1668 |
.assertResult(1, 2); |
| 1669 |
} |
| 1670 |
|
| 1671 |
@Test |
| 1672 |
public void doubleObserveOn() { |
| 1673 |
Flowable.just(1).hide() |
| 1674 |
.observeOn(Schedulers.computation()) |
| 1675 |
.observeOn(Schedulers.single()) |
| 1676 |
.test() |
| 1677 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1678 |
.assertResult(1); |
| 1679 |
} |
| 1680 |
|
| 1681 |
@Test |
| 1682 |
public void doubleObserveOnError() { |
| 1683 |
Flowable.error(new TestException()) |
| 1684 |
.observeOn(Schedulers.computation()) |
| 1685 |
.observeOn(Schedulers.single()) |
| 1686 |
.test() |
| 1687 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1688 |
.assertFailure(TestException.class); |
| 1689 |
} |
| 1690 |
|
| 1691 |
@Test |
| 1692 |
public void doubleObserveOnConditional() { |
| 1693 |
Flowable.just(1).hide() |
| 1694 |
.observeOn(Schedulers.computation()) |
| 1695 |
.distinct() |
| 1696 |
.observeOn(Schedulers.single()) |
| 1697 |
.test() |
| 1698 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1699 |
.assertResult(1); |
| 1700 |
} |
| 1701 |
|
| 1702 |
@Test |
| 1703 |
public void doubleObserveOnErrorConditional() { |
| 1704 |
Flowable.error(new TestException()) |
| 1705 |
.observeOn(Schedulers.computation()) |
| 1706 |
.distinct() |
| 1707 |
.observeOn(Schedulers.single()) |
| 1708 |
.test() |
| 1709 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1710 |
.assertFailure(TestException.class); |
| 1711 |
} |
| 1712 |
|
| 1713 |
@Test |
| 1714 |
public void request1Conditional() { |
| 1715 |
Flowable.range(1, 10).hide() |
| 1716 |
.observeOn(ImmediateThinScheduler.INSTANCE) |
| 1717 |
.filter(Functions.alwaysTrue()) |
| 1718 |
.test(1L) |
| 1719 |
.assertValue(1); |
| 1720 |
} |
| 1721 |
|
| 1722 |
@Test |
| 1723 |
public void backFusedConditional() { |
| 1724 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1725 |
|
| 1726 |
Flowable.range(1, 100).hide() |
| 1727 |
.observeOn(ImmediateThinScheduler.INSTANCE) |
| 1728 |
.filter(Functions.alwaysTrue()) |
| 1729 |
.subscribe(ts); |
| 1730 |
|
| 1731 |
ts.assertFusionMode(QueueFuseable.ASYNC) |
| 1732 |
.assertValueCount(100) |
| 1733 |
.assertComplete() |
| 1734 |
.assertNoErrors(); |
| 1735 |
} |
| 1736 |
|
| 1737 |
@Test |
| 1738 |
public void backFusedErrorConditional() { |
| 1739 |
TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1740 |
|
| 1741 |
Flowable.<Integer>error(new TestException()) |
| 1742 |
.observeOn(ImmediateThinScheduler.INSTANCE) |
| 1743 |
.filter(Functions.alwaysTrue()) |
| 1744 |
.subscribe(ts); |
| 1745 |
|
| 1746 |
ts.assertFusionMode(QueueFuseable.ASYNC) |
| 1747 |
.assertFailure(TestException.class); |
| 1748 |
} |
| 1749 |
|
| 1750 |
@Test |
| 1751 |
public void backFusedCancelConditional() { |
| 1752 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 1753 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 1754 |
|
| 1755 |
final TestScheduler scheduler = new TestScheduler(); |
| 1756 |
|
| 1757 |
Flowable.just(1).hide() |
| 1758 |
.observeOn(scheduler) |
| 1759 |
.filter(Functions.alwaysTrue()) |
| 1760 |
.subscribe(ts); |
| 1761 |
|
| 1762 |
Runnable r1 = new Runnable() { |
| 1763 |
@Override |
| 1764 |
public void run() { |
| 1765 |
ts.cancel(); |
| 1766 |
} |
| 1767 |
}; |
| 1768 |
|
| 1769 |
Runnable r2 = new Runnable() { |
| 1770 |
@Override |
| 1771 |
public void run() { |
| 1772 |
scheduler.triggerActions(); |
| 1773 |
} |
| 1774 |
}; |
| 1775 |
|
| 1776 |
TestHelper.race(r1, r2); |
| 1777 |
|
| 1778 |
ts.assertFusionMode(QueueFuseable.ASYNC); |
| 1779 |
|
| 1780 |
if (ts.values().size() != 0) { |
| 1781 |
ts.assertResult(1); |
| 1782 |
} |
| 1783 |
} |
| 1784 |
} |
| 1785 |
|
| 1786 |
@Test |
| 1787 |
public void syncFusedRequestOneByOneConditional() { |
| 1788 |
Flowable.range(1, 5) |
| 1789 |
.observeOn(ImmediateThinScheduler.INSTANCE) |
| 1790 |
.filter(Functions.alwaysTrue()) |
| 1791 |
.rebatchRequests(1) |
| 1792 |
.test() |
| 1793 |
.assertResult(1, 2, 3, 4, 5); |
| 1794 |
} |
| 1795 |
|
| 1796 |
public static final class DisposeTrackingScheduler extends Scheduler { |
| 1797 |
|
| 1798 |
public final AtomicInteger disposedCount = new AtomicInteger(); |
| 1799 |
|
| 1800 |
@Override |
| 1801 |
public Worker createWorker() { |
| 1802 |
return new TrackingWorker(); |
| 1803 |
} |
| 1804 |
|
| 1805 |
final class TrackingWorker extends Scheduler.Worker { |
| 1806 |
|
| 1807 |
@Override |
| 1808 |
public void dispose() { |
| 1809 |
disposedCount.getAndIncrement(); |
| 1810 |
} |
| 1811 |
|
| 1812 |
@Override |
| 1813 |
public boolean isDisposed() { |
| 1814 |
return false; |
| 1815 |
} |
| 1816 |
|
| 1817 |
@Override |
| 1818 |
public Disposable schedule(Runnable run, long delay, |
| 1819 |
TimeUnit unit) { |
| 1820 |
run.run(); |
| 1821 |
return Disposable.empty(); |
| 1822 |
} |
| 1823 |
} |
| 1824 |
} |
| 1825 |
|
| 1826 |
@Test |
| 1827 |
public void workerNotDisposedPrematurelyNormalInNormalOut() { |
| 1828 |
DisposeTrackingScheduler s = new DisposeTrackingScheduler(); |
| 1829 |
|
| 1830 |
Flowable.concat( |
| 1831 |
Flowable.just(1).hide().observeOn(s), |
| 1832 |
Flowable.just(2) |
| 1833 |
) |
| 1834 |
.test() |
| 1835 |
.assertResult(1, 2); |
| 1836 |
|
| 1837 |
assertEquals(1, s.disposedCount.get()); |
| 1838 |
} |
| 1839 |
|
| 1840 |
@Test |
| 1841 |
public void workerNotDisposedPrematurelySyncInNormalOut() { |
| 1842 |
DisposeTrackingScheduler s = new DisposeTrackingScheduler(); |
| 1843 |
|
| 1844 |
Flowable.concat( |
| 1845 |
Flowable.just(1).observeOn(s), |
| 1846 |
Flowable.just(2) |
| 1847 |
) |
| 1848 |
.test() |
| 1849 |
.assertResult(1, 2); |
| 1850 |
|
| 1851 |
assertEquals(1, s.disposedCount.get()); |
| 1852 |
} |
| 1853 |
|
| 1854 |
@Test |
| 1855 |
public void workerNotDisposedPrematurelyAsyncInNormalOut() { |
| 1856 |
DisposeTrackingScheduler s = new DisposeTrackingScheduler(); |
| 1857 |
|
| 1858 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1859 |
up.onNext(1); |
| 1860 |
up.onComplete(); |
| 1861 |
|
| 1862 |
Flowable.concat( |
| 1863 |
up.observeOn(s), |
| 1864 |
Flowable.just(2) |
| 1865 |
) |
| 1866 |
.test() |
| 1867 |
.assertResult(1, 2); |
| 1868 |
|
| 1869 |
assertEquals(1, s.disposedCount.get()); |
| 1870 |
} |
| 1871 |
|
| 1872 |
static final class TestSubscriberFusedCanceling |
| 1873 |
extends TestSubscriberEx<Integer> { |
| 1874 |
|
| 1875 |
TestSubscriberFusedCanceling() { |
| 1876 |
super(); |
| 1877 |
initialFusionMode = QueueFuseable.ANY; |
| 1878 |
} |
| 1879 |
|
| 1880 |
@Override |
| 1881 |
public void onComplete() { |
| 1882 |
cancel(); |
| 1883 |
super.onComplete(); |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
@Test |
| 1888 |
public void workerNotDisposedPrematurelyNormalInAsyncOut() { |
| 1889 |
DisposeTrackingScheduler s = new DisposeTrackingScheduler(); |
| 1890 |
|
| 1891 |
TestSubscriberEx<Integer> ts = new TestSubscriberFusedCanceling(); |
| 1892 |
|
| 1893 |
Flowable.just(1).hide().observeOn(s).subscribe(ts); |
| 1894 |
|
| 1895 |
assertEquals(1, s.disposedCount.get()); |
| 1896 |
} |
| 1897 |
|
| 1898 |
@Test |
| 1899 |
public void workerNotDisposedPrematurelyNormalInNormalOutConditional() { |
| 1900 |
DisposeTrackingScheduler s = new DisposeTrackingScheduler(); |
| 1901 |
|
| 1902 |
Flowable.concat( |
| 1903 |
Flowable.just(1).hide().observeOn(s).filter(Functions.alwaysTrue()), |
| 1904 |
Flowable.just(2) |
| 1905 |
) |
| 1906 |
.test() |
| 1907 |
.assertResult(1, 2); |
| 1908 |
|
| 1909 |
assertEquals(1, s.disposedCount.get()); |
| 1910 |
} |
| 1911 |
|
| 1912 |
@Test |
| 1913 |
public void workerNotDisposedPrematurelySyncInNormalOutConditional() { |
| 1914 |
DisposeTrackingScheduler s = new DisposeTrackingScheduler(); |
| 1915 |
|
| 1916 |
Flowable.concat( |
| 1917 |
Flowable.just(1).observeOn(s).filter(Functions.alwaysTrue()), |
| 1918 |
Flowable.just(2) |
| 1919 |
) |
| 1920 |
.test() |
| 1921 |
.assertResult(1, 2); |
| 1922 |
|
| 1923 |
assertEquals(1, s.disposedCount.get()); |
| 1924 |
} |
| 1925 |
|
| 1926 |
@Test |
| 1927 |
public void workerNotDisposedPrematurelyAsyncInNormalOutConditional() { |
| 1928 |
DisposeTrackingScheduler s = new DisposeTrackingScheduler(); |
| 1929 |
|
| 1930 |
UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1931 |
up.onNext(1); |
| 1932 |
up.onComplete(); |
| 1933 |
|
| 1934 |
Flowable.concat( |
| 1935 |
up.observeOn(s).filter(Functions.alwaysTrue()), |
| 1936 |
Flowable.just(2) |
| 1937 |
) |
| 1938 |
.test() |
| 1939 |
.assertResult(1, 2); |
| 1940 |
|
| 1941 |
assertEquals(1, s.disposedCount.get()); |
| 1942 |
} |
| 1943 |
|
| 1944 |
@Test |
| 1945 |
public void workerNotDisposedPrematurelyNormalInAsyncOutConditional() { |
| 1946 |
DisposeTrackingScheduler s = new DisposeTrackingScheduler(); |
| 1947 |
|
| 1948 |
TestSubscriberEx<Integer> ts = new TestSubscriberFusedCanceling(); |
| 1949 |
|
| 1950 |
Flowable.just(1).hide().observeOn(s).filter(Functions.alwaysTrue()).subscribe(ts); |
| 1951 |
|
| 1952 |
assertEquals(1, s.disposedCount.get()); |
| 1953 |
} |
| 1954 |
|
| 1955 |
@Test |
| 1956 |
public void fusedNoConcurrentCleanDueToCancel() { |
| 1957 |
for (int j = 0; j < TestHelper.RACE_LONG_LOOPS; j++) { |
| 1958 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 1959 |
try { |
| 1960 |
final UnicastProcessor<Integer> up = UnicastProcessor.create(); |
| 1961 |
|
| 1962 |
TestObserver<Integer> to = up.hide() |
| 1963 |
.observeOn(Schedulers.io()) |
| 1964 |
.observeOn(Schedulers.single()) |
| 1965 |
.unsubscribeOn(Schedulers.computation()) |
| 1966 |
.firstOrError() |
| 1967 |
.test(); |
| 1968 |
|
| 1969 |
for (int i = 0; up.hasSubscribers() && i < 10000; i++) { |
| 1970 |
up.onNext(i); |
| 1971 |
} |
| 1972 |
|
| 1973 |
to |
| 1974 |
.awaitDone(5, TimeUnit.SECONDS) |
| 1975 |
; |
| 1976 |
|
| 1977 |
if (!errors.isEmpty()) { |
| 1978 |
throw new CompositeException(errors); |
| 1979 |
} |
| 1980 |
|
| 1981 |
to.assertResult(0); |
| 1982 |
} finally { |
| 1983 |
RxJavaPlugins.reset(); |
| 1984 |
} |
| 1985 |
} |
| 1986 |
} |
| 1987 |
|
| 1988 |
@Test |
| 1989 |
public void fusedParallelProcessing() { |
| 1990 |
Flowable.range(0, 500000) |
| 1991 |
.subscribeOn(Schedulers.single()) |
| 1992 |
.observeOn(Schedulers.computation()) |
| 1993 |
.parallel() |
| 1994 |
.runOn(Schedulers.computation()) |
| 1995 |
.map(Functions.<Integer>identity()) |
| 1996 |
.sequential() |
| 1997 |
.test() |
| 1998 |
.awaitDone(20, TimeUnit.SECONDS) |
| 1999 |
.assertValueCount(500000) |
| 2000 |
.assertComplete() |
| 2001 |
.assertNoErrors(); |
| 2002 |
} |
| 2003 |
|
| 2004 |
@Test |
| 2005 |
public void badRequest() { |
| 2006 |
TestHelper.assertBadRequestReported(Flowable.never().observeOn(ImmediateThinScheduler.INSTANCE)); |
| 2007 |
} |
| 2008 |
|
| 2009 |
@Test |
| 2010 |
public void syncFusedCancelAfterPoll() { |
| 2011 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 2012 |
|
| 2013 |
Flowable.just(1) |
| 2014 |
.map(v -> { |
| 2015 |
ts.cancel(); |
| 2016 |
return v + 1; |
| 2017 |
}) |
| 2018 |
.compose(TestHelper.flowableStripBoundary()) |
| 2019 |
.observeOn(ImmediateThinScheduler.INSTANCE) |
| 2020 |
.subscribe(ts); |
| 2021 |
|
| 2022 |
ts.assertEmpty(); |
| 2023 |
} |
| 2024 |
|
| 2025 |
@Test |
| 2026 |
public void syncFusedCancelAfterPollConditional() { |
| 2027 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 2028 |
|
| 2029 |
Flowable.just(1) |
| 2030 |
.map(v -> { |
| 2031 |
ts.cancel(); |
| 2032 |
return v + 1; |
| 2033 |
}) |
| 2034 |
.compose(TestHelper.flowableStripBoundary()) |
| 2035 |
.observeOn(ImmediateThinScheduler.INSTANCE) |
| 2036 |
.compose(TestHelper.conditional()) |
| 2037 |
.subscribe(ts); |
| 2038 |
|
| 2039 |
ts.assertEmpty(); |
| 2040 |
} |
| 2041 |
|
| 2042 |
@Test |
| 2043 |
public void backFusedMoreWork() { |
| 2044 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 2045 |
|
| 2046 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2047 |
|
| 2048 |
pp.observeOn(ImmediateThinScheduler.INSTANCE) |
| 2049 |
.doOnNext(v -> { |
| 2050 |
if (v == 1) { |
| 2051 |
pp.onNext(2); |
| 2052 |
} |
| 2053 |
}) |
| 2054 |
.subscribe(ts); |
| 2055 |
|
| 2056 |
pp.onNext(1); |
| 2057 |
|
| 2058 |
ts.assertValuesOnly(1, 2); |
| 2059 |
} |
| 2060 |
|
| 2061 |
@Test |
| 2062 |
public void moreWorkInRunAsync() { |
| 2063 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 2064 |
|
| 2065 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2066 |
|
| 2067 |
pp.observeOn(ImmediateThinScheduler.INSTANCE) |
| 2068 |
.doOnNext(v -> { |
| 2069 |
if (v == 1) { |
| 2070 |
pp.onNext(2); |
| 2071 |
} |
| 2072 |
}) |
| 2073 |
.subscribe(ts); |
| 2074 |
|
| 2075 |
pp.onNext(1); |
| 2076 |
|
| 2077 |
ts.assertValuesOnly(1, 2); |
| 2078 |
} |
| 2079 |
|
| 2080 |
@Test |
| 2081 |
public void backFusedConditionalMoreWork() { |
| 2082 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY); |
| 2083 |
|
| 2084 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2085 |
|
| 2086 |
pp.observeOn(ImmediateThinScheduler.INSTANCE) |
| 2087 |
.doOnNext(v -> { |
| 2088 |
if (v == 1) { |
| 2089 |
pp.onNext(2); |
| 2090 |
} |
| 2091 |
}) |
| 2092 |
.compose(TestHelper.conditional()) |
| 2093 |
.subscribe(ts); |
| 2094 |
|
| 2095 |
pp.onNext(1); |
| 2096 |
|
| 2097 |
ts.assertValuesOnly(1, 2); |
| 2098 |
} |
| 2099 |
|
| 2100 |
@Test |
| 2101 |
public void conditionalMoreWorkInRunAsync() { |
| 2102 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 2103 |
|
| 2104 |
PublishProcessor<Integer> pp = PublishProcessor.create(); |
| 2105 |
|
| 2106 |
pp.observeOn(ImmediateThinScheduler.INSTANCE) |
| 2107 |
.doOnNext(v -> { |
| 2108 |
if (v == 1) { |
| 2109 |
pp.onNext(2); |
| 2110 |
} |
| 2111 |
}) |
| 2112 |
.compose(TestHelper.conditional()) |
| 2113 |
.subscribe(ts); |
| 2114 |
|
| 2115 |
pp.onNext(1); |
| 2116 |
|
| 2117 |
ts.assertValuesOnly(1, 2); |
| 2118 |
} |
| 2119 |
} |