| 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.core.*; |
| 29 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 30 |
import io.reactivex.rxjava3.functions.*; |
| 31 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 32 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 33 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 34 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 35 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 36 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 37 |
|
| 38 |
public class FlowableTakeTest extends RxJavaTest { |
| 39 |
|
| 40 |
@Test |
| 41 |
public void take1() { |
| 42 |
Flowable<String> w = Flowable.fromIterable(Arrays.asList("one", "two", "three")); |
| 43 |
Flowable<String> take = w.take(2); |
| 44 |
|
| 45 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 46 |
take.subscribe(subscriber); |
| 47 |
|
| 48 |
verify(subscriber, times(1)).onNext("one"); |
| 49 |
verify(subscriber, times(1)).onNext("two"); |
| 50 |
verify(subscriber, never()).onNext("three"); |
| 51 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 52 |
verify(subscriber, times(1)).onComplete(); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void take2() { |
| 57 |
Flowable<String> w = Flowable.fromIterable(Arrays.asList("one", "two", "three")); |
| 58 |
Flowable<String> take = w.take(1); |
| 59 |
|
| 60 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 61 |
take.subscribe(subscriber); |
| 62 |
|
| 63 |
verify(subscriber, times(1)).onNext("one"); |
| 64 |
verify(subscriber, never()).onNext("two"); |
| 65 |
verify(subscriber, never()).onNext("three"); |
| 66 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 67 |
verify(subscriber, times(1)).onComplete(); |
| 68 |
} |
| 69 |
|
| 70 |
@Test(expected = IllegalArgumentException.class) |
| 71 |
public void takeWithError() { |
| 72 |
Flowable.fromIterable(Arrays.asList(1, 2, 3)).take(1) |
| 73 |
.map(new Function<Integer, Integer>() { |
| 74 |
@Override |
| 75 |
public Integer apply(Integer t1) { |
| 76 |
throw new IllegalArgumentException("some error"); |
| 77 |
} |
| 78 |
}).blockingSingle(); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void takeWithErrorHappeningInOnNext() { |
| 83 |
Flowable<Integer> w = Flowable.fromIterable(Arrays.asList(1, 2, 3)) |
| 84 |
.take(2).map(new Function<Integer, Integer>() { |
| 85 |
@Override |
| 86 |
public Integer apply(Integer t1) { |
| 87 |
throw new IllegalArgumentException("some error"); |
| 88 |
} |
| 89 |
}); |
| 90 |
|
| 91 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 92 |
w.subscribe(subscriber); |
| 93 |
|
| 94 |
InOrder inOrder = inOrder(subscriber); |
| 95 |
inOrder.verify(subscriber, times(1)).onError(any(IllegalArgumentException.class)); |
| 96 |
inOrder.verifyNoMoreInteractions(); |
| 97 |
} |
| 98 |
|
| 99 |
@Test |
| 100 |
public void takeWithErrorHappeningInTheLastOnNext() { |
| 101 |
Flowable<Integer> w = Flowable.fromIterable(Arrays.asList(1, 2, 3)).take(1).map(new Function<Integer, Integer>() { |
| 102 |
@Override |
| 103 |
public Integer apply(Integer t1) { |
| 104 |
throw new IllegalArgumentException("some error"); |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
Subscriber<Integer> subscriber = TestHelper.mockSubscriber(); |
| 109 |
w.subscribe(subscriber); |
| 110 |
|
| 111 |
InOrder inOrder = inOrder(subscriber); |
| 112 |
inOrder.verify(subscriber, times(1)).onError(any(IllegalArgumentException.class)); |
| 113 |
inOrder.verifyNoMoreInteractions(); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void takeDoesntLeakErrors() { |
| 118 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 119 |
try { |
| 120 |
Flowable<String> source = Flowable.unsafeCreate(new Publisher<String>() { |
| 121 |
@Override |
| 122 |
public void subscribe(Subscriber<? super String> subscriber) { |
| 123 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 124 |
subscriber.onNext("one"); |
| 125 |
subscriber.onError(new Throwable("test failed")); |
| 126 |
} |
| 127 |
}); |
| 128 |
|
| 129 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 130 |
|
| 131 |
source.take(1).subscribe(subscriber); |
| 132 |
|
| 133 |
verify(subscriber).onSubscribe((Subscription)notNull()); |
| 134 |
verify(subscriber, times(1)).onNext("one"); |
| 135 |
// even though onError is called we take(1) so shouldn't see it |
| 136 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 137 |
verify(subscriber, times(1)).onComplete(); |
| 138 |
verifyNoMoreInteractions(subscriber); |
| 139 |
|
| 140 |
TestHelper.assertUndeliverable(errors, 0, Throwable.class, "test failed"); |
| 141 |
} finally { |
| 142 |
RxJavaPlugins.reset(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void takeEmitsErrors() { |
| 148 |
Flowable.error(new TestException()) |
| 149 |
.take(1) |
| 150 |
.test() |
| 151 |
.assertNoValues() |
| 152 |
.assertError(TestException.class); |
| 153 |
} |
| 154 |
|
| 155 |
@Test |
| 156 |
public void takeRequestOverflow() { |
| 157 |
TestSubscriber<Integer> ts = Flowable.just(1, 2, 3) |
| 158 |
.take(3) |
| 159 |
.test(0); |
| 160 |
ts.requestMore(1) |
| 161 |
.assertValues(1) |
| 162 |
.assertNotComplete() |
| 163 |
.requestMore(Long.MAX_VALUE) |
| 164 |
.assertValues(1, 2, 3); |
| 165 |
} |
| 166 |
|
| 167 |
@Test |
| 168 |
public void unsubscribeAfterTake() { |
| 169 |
TestFlowableFunc f = new TestFlowableFunc("one", "two", "three"); |
| 170 |
Flowable<String> w = Flowable.unsafeCreate(f); |
| 171 |
|
| 172 |
Subscriber<String> subscriber = TestHelper.mockSubscriber(); |
| 173 |
|
| 174 |
Flowable<String> take = w.take(1); |
| 175 |
take.subscribe(subscriber); |
| 176 |
|
| 177 |
// wait for the Flowable to complete |
| 178 |
try { |
| 179 |
f.t.join(); |
| 180 |
} catch (Throwable e) { |
| 181 |
e.printStackTrace(); |
| 182 |
fail(e.getMessage()); |
| 183 |
} |
| 184 |
|
| 185 |
System.out.println("TestFlowable thread finished"); |
| 186 |
verify(subscriber).onSubscribe((Subscription)notNull()); |
| 187 |
verify(subscriber, times(1)).onNext("one"); |
| 188 |
verify(subscriber, never()).onNext("two"); |
| 189 |
verify(subscriber, never()).onNext("three"); |
| 190 |
verify(subscriber, times(1)).onComplete(); |
| 191 |
// FIXME no longer assertable |
| 192 |
// verify(s, times(1)).unsubscribe(); |
| 193 |
verifyNoMoreInteractions(subscriber); |
| 194 |
} |
| 195 |
|
| 196 |
@Test |
| 197 |
public void unsubscribeFromSynchronousInfiniteFlowable() { |
| 198 |
final AtomicLong count = new AtomicLong(); |
| 199 |
INFINITE_OBSERVABLE.take(10).subscribe(new Consumer<Long>() { |
| 200 |
|
| 201 |
@Override |
| 202 |
public void accept(Long l) { |
| 203 |
count.set(l); |
| 204 |
} |
| 205 |
|
| 206 |
}); |
| 207 |
assertEquals(10, count.get()); |
| 208 |
} |
| 209 |
|
| 210 |
@Test |
| 211 |
public void multiTake() { |
| 212 |
final AtomicInteger count = new AtomicInteger(); |
| 213 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 214 |
|
| 215 |
@Override |
| 216 |
public void subscribe(Subscriber<? super Integer> s) { |
| 217 |
BooleanSubscription bs = new BooleanSubscription(); |
| 218 |
s.onSubscribe(bs); |
| 219 |
for (int i = 0; !bs.isCancelled(); i++) { |
| 220 |
System.out.println("Emit: " + i); |
| 221 |
count.incrementAndGet(); |
| 222 |
s.onNext(i); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
}).take(100).take(1).blockingForEach(new Consumer<Integer>() { |
| 227 |
|
| 228 |
@Override |
| 229 |
public void accept(Integer t1) { |
| 230 |
System.out.println("Receive: " + t1); |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
}); |
| 235 |
|
| 236 |
assertEquals(1, count.get()); |
| 237 |
} |
| 238 |
|
| 239 |
static class TestFlowableFunc implements Publisher<String> { |
| 240 |
|
| 241 |
final String[] values; |
| 242 |
Thread t; |
| 243 |
|
| 244 |
TestFlowableFunc(String... values) { |
| 245 |
this.values = values; |
| 246 |
} |
| 247 |
|
| 248 |
@Override |
| 249 |
public void subscribe(final Subscriber<? super String> subscriber) { |
| 250 |
subscriber.onSubscribe(new BooleanSubscription()); |
| 251 |
System.out.println("TestFlowable subscribed to ..."); |
| 252 |
t = new Thread(new Runnable() { |
| 253 |
|
| 254 |
@Override |
| 255 |
public void run() { |
| 256 |
try { |
| 257 |
System.out.println("running TestFlowable thread"); |
| 258 |
for (String s : values) { |
| 259 |
System.out.println("TestFlowable onNext: " + s); |
| 260 |
subscriber.onNext(s); |
| 261 |
} |
| 262 |
subscriber.onComplete(); |
| 263 |
} catch (Throwable e) { |
| 264 |
throw new RuntimeException(e); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
}); |
| 269 |
System.out.println("starting TestFlowable thread"); |
| 270 |
t.start(); |
| 271 |
System.out.println("done starting TestFlowable thread"); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
private static Flowable<Long> INFINITE_OBSERVABLE = Flowable.unsafeCreate(new Publisher<Long>() { |
| 276 |
|
| 277 |
@Override |
| 278 |
public void subscribe(Subscriber<? super Long> op) { |
| 279 |
BooleanSubscription bs = new BooleanSubscription(); |
| 280 |
op.onSubscribe(bs); |
| 281 |
long l = 1; |
| 282 |
while (!bs.isCancelled()) { |
| 283 |
op.onNext(l++); |
| 284 |
} |
| 285 |
op.onComplete(); |
| 286 |
} |
| 287 |
|
| 288 |
}); |
| 289 |
|
| 290 |
@Test |
| 291 |
public void takeObserveOn() { |
| 292 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 293 |
TestSubscriber<Object> ts = new TestSubscriber<>(subscriber); |
| 294 |
|
| 295 |
INFINITE_OBSERVABLE.onBackpressureDrop() |
| 296 |
.observeOn(Schedulers.newThread()).take(1).subscribe(ts); |
| 297 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 298 |
ts.assertNoErrors(); |
| 299 |
|
| 300 |
verify(subscriber).onNext(1L); |
| 301 |
verify(subscriber, never()).onNext(2L); |
| 302 |
verify(subscriber).onComplete(); |
| 303 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 304 |
} |
| 305 |
|
| 306 |
@Test |
| 307 |
public void producerRequestThroughTake() { |
| 308 |
TestSubscriber<Integer> ts = new TestSubscriber<>(3); |
| 309 |
final AtomicLong requested = new AtomicLong(); |
| 310 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 311 |
|
| 312 |
@Override |
| 313 |
public void subscribe(Subscriber<? super Integer> s) { |
| 314 |
s.onSubscribe(new Subscription() { |
| 315 |
|
| 316 |
@Override |
| 317 |
public void request(long n) { |
| 318 |
requested.set(n); |
| 319 |
} |
| 320 |
|
| 321 |
@Override |
| 322 |
public void cancel() { |
| 323 |
|
| 324 |
} |
| 325 |
}); |
| 326 |
} |
| 327 |
|
| 328 |
}).take(3).subscribe(ts); |
| 329 |
assertEquals(3, requested.get()); |
| 330 |
} |
| 331 |
|
| 332 |
@Test |
| 333 |
public void producerRequestThroughTakeIsModified() { |
| 334 |
TestSubscriber<Integer> ts = new TestSubscriber<>(3); |
| 335 |
final AtomicLong requested = new AtomicLong(); |
| 336 |
Flowable.unsafeCreate(new Publisher<Integer>() { |
| 337 |
|
| 338 |
@Override |
| 339 |
public void subscribe(Subscriber<? super Integer> s) { |
| 340 |
s.onSubscribe(new Subscription() { |
| 341 |
|
| 342 |
@Override |
| 343 |
public void request(long n) { |
| 344 |
requested.set(n); |
| 345 |
} |
| 346 |
|
| 347 |
@Override |
| 348 |
public void cancel() { |
| 349 |
|
| 350 |
} |
| 351 |
}); |
| 352 |
} |
| 353 |
|
| 354 |
}).take(1).subscribe(ts); |
| 355 |
//FIXME take triggers fast path if downstream requests more than the limit |
| 356 |
assertEquals(1, requested.get()); |
| 357 |
} |
| 358 |
|
| 359 |
@Test |
| 360 |
public void interrupt() throws InterruptedException { |
| 361 |
final AtomicReference<Object> exception = new AtomicReference<>(); |
| 362 |
final CountDownLatch latch = new CountDownLatch(1); |
| 363 |
Flowable.just(1).subscribeOn(Schedulers.computation()).take(1) |
| 364 |
.subscribe(new Consumer<Integer>() { |
| 365 |
|
| 366 |
@Override |
| 367 |
public void accept(Integer t1) { |
| 368 |
try { |
| 369 |
Thread.sleep(100); |
| 370 |
} catch (Exception e) { |
| 371 |
exception.set(e); |
| 372 |
e.printStackTrace(); |
| 373 |
} finally { |
| 374 |
latch.countDown(); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
}); |
| 379 |
|
| 380 |
latch.await(); |
| 381 |
assertNull(exception.get()); |
| 382 |
} |
| 383 |
|
| 384 |
@Test |
| 385 |
public void doesntRequestMoreThanNeededFromUpstream() throws InterruptedException { |
| 386 |
final AtomicLong requests = new AtomicLong(); |
| 387 |
TestSubscriber<Long> ts = new TestSubscriber<>(0L); |
| 388 |
Flowable.interval(100, TimeUnit.MILLISECONDS) |
| 389 |
// |
| 390 |
.doOnRequest(new LongConsumer() { |
| 391 |
@Override |
| 392 |
public void accept(long n) { |
| 393 |
System.out.println(n); |
| 394 |
requests.addAndGet(n); |
| 395 |
}}) |
| 396 |
// |
| 397 |
.take(2) |
| 398 |
// |
| 399 |
.subscribe(ts); |
| 400 |
Thread.sleep(50); |
| 401 |
ts.request(1); |
| 402 |
ts.request(1); |
| 403 |
ts.request(1); |
| 404 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 405 |
ts.assertComplete(); |
| 406 |
ts.assertNoErrors(); |
| 407 |
assertEquals(2, requests.get()); |
| 408 |
} |
| 409 |
|
| 410 |
@Test |
| 411 |
public void takeFinalValueThrows() { |
| 412 |
Flowable<Integer> source = Flowable.just(1).take(1); |
| 413 |
|
| 414 |
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { |
| 415 |
@Override |
| 416 |
public void onNext(Integer t) { |
| 417 |
throw new TestException(); |
| 418 |
} |
| 419 |
}; |
| 420 |
|
| 421 |
source.safeSubscribe(ts); |
| 422 |
|
| 423 |
ts.assertNoValues(); |
| 424 |
ts.assertError(TestException.class); |
| 425 |
ts.assertNotComplete(); |
| 426 |
} |
| 427 |
|
| 428 |
@Test |
| 429 |
public void reentrantTake() { |
| 430 |
final PublishProcessor<Integer> source = PublishProcessor.create(); |
| 431 |
|
| 432 |
TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 433 |
|
| 434 |
source.take(1).doOnNext(new Consumer<Integer>() { |
| 435 |
@Override |
| 436 |
public void accept(Integer v) { |
| 437 |
source.onNext(2); |
| 438 |
} |
| 439 |
}).subscribe(ts); |
| 440 |
|
| 441 |
source.onNext(1); |
| 442 |
|
| 443 |
ts.assertValue(1); |
| 444 |
ts.assertNoErrors(); |
| 445 |
ts.assertComplete(); |
| 446 |
} |
| 447 |
|
| 448 |
@Test |
| 449 |
public void takeNegative() { |
| 450 |
try { |
| 451 |
Flowable.just(1).take(-99); |
| 452 |
fail("Should have thrown"); |
| 453 |
} catch (IllegalArgumentException ex) { |
| 454 |
assertEquals("count >= 0 required but it was -99", ex.getMessage()); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
@Test |
| 459 |
public void takeZero() { |
| 460 |
Flowable.just(1) |
| 461 |
.take(0) |
| 462 |
.test() |
| 463 |
.assertResult(); |
| 464 |
} |
| 465 |
|
| 466 |
@Test |
| 467 |
public void dispose() { |
| 468 |
TestHelper.checkDisposed(PublishProcessor.create().take(2)); |
| 469 |
} |
| 470 |
|
| 471 |
@Test |
| 472 |
public void doubleOnSubscribe() { |
| 473 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 474 |
@Override |
| 475 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 476 |
return f.take(2); |
| 477 |
} |
| 478 |
}); |
| 479 |
} |
| 480 |
|
| 481 |
@Test |
| 482 |
public void badRequest() { |
| 483 |
TestHelper.assertBadRequestReported(Flowable.never().take(1)); |
| 484 |
} |
| 485 |
|
| 486 |
@Test |
| 487 |
public void requestRace() { |
| 488 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 489 |
|
| 490 |
final TestSubscriber<Integer> ts = Flowable.range(1, 2).take(2).test(0L); |
| 491 |
|
| 492 |
Runnable r1 = new Runnable() { |
| 493 |
@Override |
| 494 |
public void run() { |
| 495 |
ts.request(1); |
| 496 |
} |
| 497 |
}; |
| 498 |
|
| 499 |
TestHelper.race(r1, r1); |
| 500 |
|
| 501 |
ts.assertResult(1, 2); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
@Test |
| 506 |
public void errorAfterLimitReached() { |
| 507 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 508 |
try { |
| 509 |
Flowable.error(new TestException()) |
| 510 |
.take(0) |
| 511 |
.test() |
| 512 |
.assertResult(); |
| 513 |
|
| 514 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 515 |
} finally { |
| 516 |
RxJavaPlugins.reset(); |
| 517 |
} |
| 518 |
} |
| 519 |
} |