@Test public void cancelOn() throws Exception { CountDownLatch countDownLatch = new CountDownLatch(1); AtomicReference<Thread> thread = new AtomicReference<>(); Disposable res = Flux.never() .doOnCancel(() -> { thread.set(Thread.currentThread()); countDownLatch.countDown(); }) .cancelOn(asyncGroup) .subscribe(); res.dispose(); assertTrue(countDownLatch.await(3, TimeUnit.SECONDS)); assertTrue(thread.get() != Thread.currentThread()); }
@Test(timeout = 3000L) public void cancelOnDedicatedScheduler() throws Exception { CountDownLatch latch = new CountDownLatch(1); AtomicReference<Thread> threadHash = new AtomicReference<>(Thread.currentThread()); Schedulers.single().schedule(() -> threadHash.set(Thread.currentThread())); Flux.create(sink -> { sink.onDispose(() -> { if (threadHash.compareAndSet(Thread.currentThread(), null)) { latch.countDown(); } }); }) .cancelOn(Schedulers.single()) .subscribeWith(AssertSubscriber.create()) .cancel(); latch.await(); Assert.assertNull(threadHash.get()); }
@Test public void scanOperator() { final Flux<Integer> flux = Flux.just(1).cancelOn(Schedulers.elastic()); assertThat(flux).isInstanceOf(Scannable.class); assertThat(((Scannable) flux).scan(Scannable.Attr.RUN_ON)).isSameAs(Schedulers.elastic()); }
@Test public void timeoutDropWhenNoCancelWithoutFallback() { for (int i = 0; i < 50; i++) { StepVerifier.withVirtualTime( () -> Flux.just("cat") .delaySubscription(Duration.ofMillis(3)) // We cancel on another scheduler that won't do anything to force it to act like // the event is already in flight .cancelOn(Schedulers.fromExecutor(r -> {})) .timeout(Duration.ofMillis(2)) ) .thenAwait(Duration.ofSeconds(5)) .expectError(TimeoutException.class) .verify(); } }
@Test public void timeoutDropWhenNoCancelWithFallback() { for (int i = 0; i < 50; i++) { StepVerifier.withVirtualTime( () -> Flux.just("cat") .delaySubscription(Duration.ofMillis(3)) // We cancel on another scheduler that won't do anything to force it to act like // the event is already in flight .cancelOn(Schedulers.fromExecutor(r -> {})) .timeout(Duration.ofMillis(2), Flux.just("dog").delayElements(Duration.ofMillis(5))) ) .thenAwait(Duration.ofSeconds(5)) .expectNext("dog") .expectComplete() .verify(); } }
@Test public void prematureCancel2() { StepVerifier.create(Flux.range(1, 10000) .flatMap(Flux::just, 2) .cancelOn(Schedulers.single()), 1) .expectNext(1) .thenRequest(2) .expectNext(2, 3) .thenCancel() .verify(); }
/** * @param scheduler * @return * @see reactor.core.publisher.Flux#cancelOn(reactor.core.scheduler.Scheduler) */ public final Flux<T> cancelOn(Scheduler scheduler) { return boxed.cancelOn(scheduler); } /**