Search in sources :

Example 36 with Disposable

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

the class FluxReplayTest method cancel.

@Test
public void cancel() {
    ConnectableFlux<Integer> replay = Sinks.many().unicast().<Integer>onBackpressureBuffer().asFlux().replay(2);
    replay.subscribe(v -> {
    }, e -> {
        throw Exceptions.propagate(e);
    });
    Disposable connected = replay.connect();
    // the lambda subscriber itself is cancelled so it will bubble the exception
    // propagated by connect().dispose()
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(connected::dispose).withMessage("Disconnected");
    boolean cancelled = (((FluxReplay.ReplaySubscriber) connected).state & FluxReplay.ReplaySubscriber.DISPOSED_FLAG) == FluxReplay.ReplaySubscriber.DISPOSED_FLAG;
    assertThat(cancelled).isTrue();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(reactor.core.Disposable) FluxOperatorTest(reactor.test.publisher.FluxOperatorTest) Test(org.junit.jupiter.api.Test)

Example 37 with Disposable

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

the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityResetWhenDirectTaskIsExecuted.

@Test
public void estimateRemainingTaskCapacityResetWhenDirectTaskIsExecuted() 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) {
            e.printStackTrace();
        }
    });
    // 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();
    latch.countDown();
    Awaitility.await().untilTrue(taskRan);
    assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity after run").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 38 with Disposable

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

the class BoundedElasticSchedulerTest method taskPutInPendingQueueIsEventuallyExecuted.

@Test
public void taskPutInPendingQueueIsEventuallyExecuted() throws InterruptedException {
    BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(1, 1, Thread::new, 10));
    boundedElasticScheduler.start();
    Worker worker = afterTest.autoDispose(boundedElasticScheduler.createWorker());
    CountDownLatch latch = new CountDownLatch(1);
    // enqueue blocking task on worker
    worker.schedule(() -> {
        try {
            latch.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    // small window to start the first task
    Thread.sleep(10);
    AtomicBoolean ranSecond = new AtomicBoolean();
    Disposable task = worker.schedule(() -> ranSecond.set(true));
    assertThat(ranSecond).as("is pending execution").isFalse();
    assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("queue full").isZero();
    latch.countDown();
    Awaitility.await().atMost(100, TimeUnit.MILLISECONDS).pollInterval(10, TimeUnit.MILLISECONDS).pollDelay(10, TimeUnit.MILLISECONDS).untilAsserted(() -> assertThat(ranSecond).as("did run after task1 done").isTrue());
    assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity restored").isOne();
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Worker(reactor.core.scheduler.Scheduler.Worker) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 39 with Disposable

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

the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityResetWhenWorkerTaskIsExecuted.

@Test
public void estimateRemainingTaskCapacityResetWhenWorkerTaskIsExecuted() throws InterruptedException {
    BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(1, 1, Thread::new, 10));
    boundedElasticScheduler.start();
    Worker worker = afterTest.autoDispose(boundedElasticScheduler.createWorker());
    CountDownLatch latch = new CountDownLatch(1);
    AtomicBoolean taskRan = new AtomicBoolean();
    // occupy the scheduler
    worker.schedule(() -> {
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    // small window to start the first task
    Thread.sleep(10);
    // enqueue task on worker
    Disposable task = worker.schedule(() -> taskRan.set(true));
    assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity when running").isZero();
    latch.countDown();
    Awaitility.await().untilTrue(taskRan);
    assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("capacity after run").isOne();
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Worker(reactor.core.scheduler.Scheduler.Worker) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 40 with Disposable

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

the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityResetWhenWorkerTaskIsDisposed.

@Test
public void estimateRemainingTaskCapacityResetWhenWorkerTaskIsDisposed() throws InterruptedException {
    BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(1, 1, Thread::new, 10));
    boundedElasticScheduler.start();
    Worker worker = afterTest.autoDispose(boundedElasticScheduler.createWorker());
    CountDownLatch latch = new CountDownLatch(1);
    AtomicBoolean taskRan = new AtomicBoolean();
    // occupy the scheduler
    worker.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 = worker.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) Worker(reactor.core.scheduler.Scheduler.Worker) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

Disposable (reactor.core.Disposable)120 Test (org.junit.jupiter.api.Test)101 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