Search in sources :

Example 86 with Disposable

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

the class SchedulerTask method call.

@Override
@Nullable
public Void call() {
    thread = Thread.currentThread();
    Disposable d = null;
    try {
        for (; ; ) {
            d = parent;
            if (d == TAKEN || d == null) {
                break;
            }
            if (PARENT.compareAndSet(this, d, TAKEN)) {
                break;
            }
        }
        try {
            task.run();
        } catch (Throwable ex) {
            Schedulers.handleError(ex);
        }
    } finally {
        thread = null;
        Future f;
        for (; ; ) {
            f = future;
            if (f == CANCELLED || FUTURE.compareAndSet(this, f, FINISHED)) {
                break;
            }
        }
        if (d != null) {
            d.dispose();
        }
    }
    return null;
}
Also used : Disposable(reactor.core.Disposable) Future(java.util.concurrent.Future) Nullable(reactor.util.annotation.Nullable)

Example 87 with Disposable

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

the class MonoCacheTimeTest method coordinatorReachableThroughCacheInnerSubscriptionsOnly.

@Test
public void coordinatorReachableThroughCacheInnerSubscriptionsOnly() throws InterruptedException {
    TestPublisher<Integer> source = TestPublisher.create();
    MonoCacheTime<Integer> cached = new MonoCacheTime<>(source.mono(), // short cache TTL should trigger state change if source is not never
    Duration.ofMillis(100), Schedulers.parallel());
    Disposable d1 = cached.subscribe();
    cached.subscribe();
    WeakReference<Signal<Integer>> refCoordinator = new WeakReference<>(cached.state);
    assertThat(refCoordinator.get()).isInstanceOf(MonoCacheTime.CoordinatorSubscriber.class);
    Thread.sleep(150);
    source = null;
    cached = null;
    System.gc();
    assertThat(refCoordinator.get()).isInstanceOf(MonoCacheTime.CoordinatorSubscriber.class);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(reactor.core.Disposable) WeakReference(java.lang.ref.WeakReference) MonoOperatorTest(reactor.test.publisher.MonoOperatorTest) Test(org.junit.jupiter.api.Test)

Example 88 with Disposable

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

the class SchedulersTest method testWorkerScheduleSupportZeroPeriod.

@Test
public void testWorkerScheduleSupportZeroPeriod() throws InterruptedException {
    try (TaskCheckingScheduledExecutor executorService = new TaskCheckingScheduledExecutor()) {
        CountDownLatch latch = new CountDownLatch(2);
        Disposable.Composite tasks = Disposables.composite();
        Disposable disposable = Schedulers.workerSchedulePeriodically(executorService, tasks, latch::countDown, 0, 0, TimeUnit.MILLISECONDS);
        latch.await();
        disposable.dispose();
        Thread.sleep(100);
        int tasksBefore = executorService.tasks.size();
        Thread.sleep(100);
        int tasksAfter = executorService.tasks.size();
        assertThat(tasksAfter).isEqualTo(tasksBefore);
        assertThat(tasks.size()).isEqualTo(0);
    }
}
Also used : Disposable(reactor.core.Disposable) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 89 with Disposable

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

the class SchedulersTest method schedulerDecoratorDisposedWhenRemoved.

@Test
public void schedulerDecoratorDisposedWhenRemoved() {
    AtomicBoolean disposeTracker = new AtomicBoolean();
    class DisposableDecorator implements BiFunction<Scheduler, ScheduledExecutorService, ScheduledExecutorService>, Disposable {

        @Override
        public ScheduledExecutorService apply(Scheduler scheduler, ScheduledExecutorService service) {
            return service;
        }

        @Override
        public void dispose() {
            disposeTracker.set(true);
        }
    }
    DisposableDecorator decorator = new DisposableDecorator();
    Schedulers.addExecutorServiceDecorator("k1", decorator);
    assertThat(Schedulers.removeExecutorServiceDecorator("k1")).as("decorator removed").isSameAs(decorator);
    assertThat(disposeTracker).as("decorator disposed").isTrue();
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BiFunction(java.util.function.BiFunction) Test(org.junit.jupiter.api.Test)

Example 90 with Disposable

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

the class SchedulersTest method testDirectScheduleZeroPeriodicallyCancelsSchedulerTask.

@Test
public void testDirectScheduleZeroPeriodicallyCancelsSchedulerTask() throws Exception {
    try (TaskCheckingScheduledExecutor executorService = new TaskCheckingScheduledExecutor()) {
        CountDownLatch latch = new CountDownLatch(2);
        Disposable disposable = Schedulers.directSchedulePeriodically(executorService, latch::countDown, 0, 0, TimeUnit.MILLISECONDS);
        latch.await();
        disposable.dispose();
        // avoid race of checking the status of futures vs cancelling said futures
        await().atMost(500, TimeUnit.MILLISECONDS).pollDelay(10, TimeUnit.MILLISECONDS).pollInterval(50, TimeUnit.MILLISECONDS).until(executorService::isAllTasksCancelledOrDone);
    }
}
Also used : Disposable(reactor.core.Disposable) 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