Search in sources :

Example 46 with Disposable

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

the class AbstractSchedulerTest method workerScheduleAndDispose.

@Test
@Timeout(10)
public final void workerScheduleAndDispose() throws Exception {
    Scheduler s = schedulerNotCached();
    try {
        Scheduler.Worker w = s.createWorker();
        assertThat(w.isDisposed()).isFalse();
        CountDownLatch latch = new CountDownLatch(1);
        CountDownLatch latch2 = shouldCheckDisposeTask() ? new CountDownLatch(1) : null;
        try {
            Disposable d = w.schedule(() -> {
                try {
                    latch.countDown();
                    if (latch2 != null && !latch2.await(10, TimeUnit.SECONDS) && shouldCheckInterrupted()) {
                        fail("Should have interrupted");
                    }
                } catch (InterruptedException e) {
                }
            });
            latch.await();
            if (shouldCheckDisposeTask()) {
                assertThat(d.isDisposed()).isFalse();
            }
            d.dispose();
            // noop
            d.dispose();
        } catch (Throwable schedulingError) {
            fail("unexpected scheduling error", schedulingError);
        }
        Thread.yield();
        if (latch2 != null) {
            latch2.countDown();
        }
        Disposable[] massCancel;
        boolean hasErrors = false;
        if (shouldCheckMassWorkerDispose()) {
            int n = 10;
            massCancel = new Disposable[n];
            Throwable[] errors = new Throwable[n];
            Thread current = Thread.currentThread();
            for (int i = 0; i < n; i++) {
                try {
                    massCancel[i] = w.schedule(() -> {
                        if (current == Thread.currentThread()) {
                            return;
                        }
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException ie) {
                        }
                    });
                } catch (RejectedExecutionException ree) {
                    errors[i] = ree;
                    hasErrors = true;
                }
            }
        } else {
            massCancel = null;
        }
        w.dispose();
        // noop
        w.dispose();
        assertThat(w.isDisposed()).isTrue();
        if (massCancel != null) {
            assertThat(hasErrors).as("mass cancellation errors").isFalse();
            for (Disposable _d : massCancel) {
                assertThat(_d.isDisposed()).isTrue();
            }
        }
        assertThatExceptionOfType(RejectedExecutionException.class).isThrownBy(() -> w.schedule(() -> {
        })).isSameAs(Exceptions.failWithRejected());
    } finally {
        s.dispose();
        // noop
        s.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) CountDownLatch(java.util.concurrent.CountDownLatch) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 47 with Disposable

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

the class ElasticSchedulerTest method doesntRecycleWhileRunningAfterDisposed.

@Test
public void doesntRecycleWhileRunningAfterDisposed() throws Exception {
    Scheduler s = Schedulers.newElastic("test-recycle");
    ((ElasticScheduler) s).evictor.shutdownNow();
    try {
        AtomicBoolean stop = new AtomicBoolean(false);
        CountDownLatch started = new CountDownLatch(1);
        Disposable d = s.schedule(() -> {
            started.countDown();
            // simulate uninterruptible computation
            for (; ; ) {
                if (stop.get()) {
                    break;
                }
            }
        });
        assertThat(started.await(10, TimeUnit.SECONDS)).as("latch timeout").isTrue();
        d.dispose();
        Thread.sleep(100);
        assertThat(((ElasticScheduler) s).cache).isEmpty();
        stop.set(true);
        Thread.sleep(100);
        assertThat(((ElasticScheduler) s).cache.size()).isEqualTo(1);
    } finally {
        s.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 48 with Disposable

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

the class ElasticSchedulerTest method eviction.

@Test
@Timeout(10)
public void eviction() throws Exception {
    Scheduler s = Schedulers.newElastic("test-recycle", 2);
    ((ElasticScheduler) s).evictor.shutdownNow();
    try {
        for (int i = 0; i < 100; i++) {
            Disposable d = s.schedule(() -> {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
            d.dispose();
        }
        while (((ElasticScheduler) s).cache.peek() != null) {
            ((ElasticScheduler) s).eviction();
            Thread.sleep(100);
        }
        assertThat(((ElasticScheduler) s).all).isEmpty();
    } finally {
        s.dispose();
        // noop
        s.dispose();
    }
    assertThat(((ElasticScheduler) s).cache).isEmpty();
    assertThat(s.isDisposed()).isTrue();
}
Also used : Disposable(reactor.core.Disposable) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 49 with Disposable

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

the class GuideTests method errorHandlingUsing.

@Test
public void errorHandlingUsing() {
    AtomicBoolean isDisposed = new AtomicBoolean();
    Disposable disposableInstance = new Disposable() {

        @Override
        public void dispose() {
            // <4>
            isDisposed.set(true);
        }

        @Override
        public String toString() {
            return "DISPOSABLE";
        }
    };
    Flux<String> flux = Flux.using(// <1>
    () -> disposableInstance, // <2>
    disposable -> Flux.just(disposable.toString()), // <3>
    Disposable::dispose);
    StepVerifier.create(flux).expectNext("DISPOSABLE").verifyComplete();
    assertThat(isDisposed.get()).isTrue();
}
Also used : Disposable(reactor.core.Disposable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 50 with Disposable

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

the class EmitterProcessorTest method state.

@Test
public void state() {
    EmitterProcessor<Integer> tp = EmitterProcessor.create();
    assertThat(tp.getPending()).isEqualTo(0);
    assertThat(tp.getBufferSize()).isEqualTo(Queues.SMALL_BUFFER_SIZE);
    assertThat(tp.isCancelled()).isFalse();
    assertThat(tp.inners()).isEmpty();
    assertThat(tp.currentSubscriberCount()).as("has subscriber").isZero();
    Disposable d1 = tp.subscribe();
    assertThat(tp.inners()).hasSize(1);
    assertThat(tp.currentSubscriberCount()).isPositive();
    FluxSink<Integer> s = tp.sink();
    s.next(2);
    s.next(3);
    s.next(4);
    assertThat(tp.getPending()).isEqualTo(0);
    AtomicReference<Subscription> d2 = new AtomicReference<>();
    tp.subscribe(new CoreSubscriber<Integer>() {

        @Override
        public void onSubscribe(Subscription s) {
            d2.set(s);
        }

        @Override
        public void onNext(Integer integer) {
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onComplete() {
        }
    });
    s.next(5);
    s.next(6);
    s.next(7);
    assertThat(tp.scan(BUFFERED)).isEqualTo(3);
    assertThat(tp.isTerminated()).isFalse();
    s.complete();
    assertThat(tp.isTerminated()).isFalse();
    d1.dispose();
    d2.get().cancel();
    assertThat(tp.isTerminated()).isTrue();
    StepVerifier.create(tp).verifyComplete();
    // noop
    tp.onNext(8);
    EmitterProcessor<Void> empty = EmitterProcessor.create();
    empty.onComplete();
    assertThat(empty.isTerminated()).isTrue();
}
Also used : Disposable(reactor.core.Disposable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) 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