Search in sources :

Example 96 with Disposable

use of reactor.core.Disposable in project reactor-core by reactor.

the class FluxTests method cancelOn.

@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();
    assertThat(countDownLatch.await(3, TimeUnit.SECONDS)).isTrue();
    assertThat(thread.get()).isNotSameAs(Thread.currentThread());
}
Also used : Disposable(reactor.core.Disposable) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 97 with Disposable

use of reactor.core.Disposable in project reactor-core by reactor.

the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityResetWhenDirectTaskIsDisposed.

@Test
public void estimateRemainingTaskCapacityResetWhenDirectTaskIsDisposed() throws InterruptedException {
    BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(1, 1, Thread::new, 10));
    boundedElasticScheduler.start();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicBoolean taskRan = new AtomicBoolean();
    // occupy the scheduler
    boundedElasticScheduler.schedule(() -> {
        try {
            latch.await();
        } catch (InterruptedException e) {
        // expected to be interrupted
        }
    });
    // small window to start the first task
    Thread.sleep(10);
    // enqueue task on worker
    Disposable task = boundedElasticScheduler.schedule(() -> taskRan.set(true));
    assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity when running").isZero();
    task.dispose();
    Awaitility.with().pollDelay(50, TimeUnit.MILLISECONDS).await().atMost(100, TimeUnit.MILLISECONDS).untilAsserted(() -> assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity after dispose").isOne());
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 98 with Disposable

use of reactor.core.Disposable in project reactor-core by reactor.

the class FluxTests method endLessTimer.

/**
 * This test case demonstrates a silent failure of {@link Flux#interval(Duration)}
 * when a resolution is specified that
 * is less than the backing {@link Timer} class.
 *
 * @throws InterruptedException - on failure.
 * @throws TimeoutException     - on failure. <p> by @masterav10 : https://github.com/reactor/reactor/issues/469
 */
@Test
@Disabled
public void endLessTimer() throws InterruptedException, TimeoutException {
    int tasks = 50;
    // XXX: Fails when less than 100
    long delayMS = 50;
    Phaser barrier = new Phaser(tasks + 1);
    List<Long> times = new ArrayList<>();
    // long localTime = System.currentTimeMillis(); for java 7
    long localTime = Instant.now().toEpochMilli();
    long elapsed = System.nanoTime();
    Disposable ctrl = Flux.interval(Duration.ofMillis(delayMS)).log("test").map((signal) -> {
        return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - elapsed);
    }).doOnNext((elapsedMillis) -> {
        times.add(localTime + elapsedMillis);
        barrier.arrive();
    }).subscribe();
    barrier.awaitAdvanceInterruptibly(barrier.arrive(), tasks * delayMS + 1000, TimeUnit.MILLISECONDS);
    ctrl.dispose();
    assertThat(times.size()).isEqualTo(tasks);
    for (int i = 1; i < times.size(); i++) {
        Long prev = times.get(i - 1);
        Long time = times.get(i);
        assertThat(prev).isGreaterThan(0L);
        assertThat(time).isGreaterThan(0L);
        assertThat(time - prev).isLessThanOrEqualTo((long) (delayMS * 1.2));
    }
}
Also used : Disposable(reactor.core.Disposable) Sinks(reactor.core.publisher.Sinks) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Hooks(reactor.core.publisher.Hooks) Timer(java.util.Timer) Disabled(org.junit.jupiter.api.Disabled) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Logger(reactor.util.Logger) Tag(org.junit.jupiter.api.Tag) AutoDisposingExtension(reactor.test.AutoDisposingExtension) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Fuseable(reactor.core.Fuseable) Assertions.fail(org.assertj.core.api.Assertions.fail) LoggerUtils(reactor.test.util.LoggerUtils) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) Scannable(reactor.core.Scannable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) Callable(java.util.concurrent.Callable) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Queues(reactor.util.concurrent.Queues) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Operators(reactor.core.publisher.Operators) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Phaser(java.util.concurrent.Phaser) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) ArrayList(java.util.ArrayList) Phaser(java.util.concurrent.Phaser) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 99 with Disposable

use of reactor.core.Disposable in project reactor-core by reactor.

the class FluxTests method testBeyondLongMaxMicroBatching.

@Test
public void testBeyondLongMaxMicroBatching() throws InterruptedException {
    List<Integer> tasks = IntStream.range(0, 1500).boxed().collect(Collectors.toList());
    CountDownLatch countDownLatch = new CountDownLatch(tasks.size());
    Flux<Integer> worker = Flux.fromIterable(tasks).log("before", Level.FINE).publishOn(asyncGroup);
    /*Disposable tail = */
    worker.log("after", Level.FINE).parallel(2).groups().subscribe(s -> s.log("w" + s.key(), Level.FINE).publishOn(asyncGroup).map(v -> v).subscribe(v -> countDownLatch.countDown(), Throwable::printStackTrace));
    countDownLatch.await(5, TimeUnit.SECONDS);
    assertThat(countDownLatch.getCount()).as("Count max: %d", tasks.size()).isEqualTo(0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Sinks(reactor.core.publisher.Sinks) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) TimeoutException(java.util.concurrent.TimeoutException) Hooks(reactor.core.publisher.Hooks) Timer(java.util.Timer) Disabled(org.junit.jupiter.api.Disabled) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) Logger(reactor.util.Logger) Tag(org.junit.jupiter.api.Tag) AutoDisposingExtension(reactor.test.AutoDisposingExtension) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Signal(reactor.core.publisher.Signal) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) LockSupport(java.util.concurrent.locks.LockSupport) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Fuseable(reactor.core.Fuseable) Assertions.fail(org.assertj.core.api.Assertions.fail) LoggerUtils(reactor.test.util.LoggerUtils) Exceptions(reactor.core.Exceptions) IntStream(java.util.stream.IntStream) Disposable(reactor.core.Disposable) Scannable(reactor.core.Scannable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) Callable(java.util.concurrent.Callable) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Queues(reactor.util.concurrent.Queues) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) Operators(reactor.core.publisher.Operators) Semaphore(java.util.concurrent.Semaphore) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) FAIL_FAST(reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST) Phaser(java.util.concurrent.Phaser) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Comparator(java.util.Comparator) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 100 with Disposable

use of reactor.core.Disposable in project reactor-core by reactor.

the class PopularTagTests method sampleTest.

@Test
public void sampleTest() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    Disposable top10every1second = Flux.fromIterable(PULP_SAMPLE).publishOn(asyncGroup).flatMap(samuelJackson -> Flux.fromArray(samuelJackson.split(" ")).publishOn(asyncGroup).filter(w -> !w.trim().isEmpty()).doOnNext(i -> simulateLatency())).window(Duration.ofSeconds(2)).flatMap(s -> s.groupBy(w -> w).flatMap(w -> w.count().map(c -> Tuples.of(w.key(), c))).collectSortedList((a, b) -> -a.getT2().compareTo(b.getT2())).flatMapMany(Flux::fromIterable).take(10).doAfterTerminate(() -> LOG.info("------------------------ window terminated" + "----------------------"))).subscribe(entry -> LOG.info(entry.getT1() + ": " + entry.getT2()), error -> LOG.error("", error), latch::countDown);
    awaitLatch(top10every1second, latch);
}
Also used : Disposable(reactor.core.Disposable) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) Arrays(java.util.Arrays) List(java.util.List) Loggers(reactor.util.Loggers) Disposable(reactor.core.Disposable) Duration(java.time.Duration) Logger(reactor.util.Logger) Tuples(reactor.util.function.Tuples) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Flux(reactor.core.publisher.Flux) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

Disposable (reactor.core.Disposable)118 Test (org.junit.jupiter.api.Test)99 CountDownLatch (java.util.concurrent.CountDownLatch)33 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 StepVerifier (reactor.test.StepVerifier)29 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 Duration (java.time.Duration)15 List (java.util.List)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)15 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 Subscription (org.reactivestreams.Subscription)15 TimeUnit (java.util.concurrent.TimeUnit)14 Timeout (org.junit.jupiter.api.Timeout)13 CoreSubscriber (reactor.core.CoreSubscriber)12 ArrayList (java.util.ArrayList)11 Arrays (java.util.Arrays)11 Disabled (org.junit.jupiter.api.Disabled)11 Scannable (reactor.core.Scannable)11 Fuseable (reactor.core.Fuseable)10