Search in sources :

Example 21 with ParameterizedTestWithName

use of reactor.test.ParameterizedTestWithName in project reactor-core by reactor.

the class BoundedElasticSchedulerTest method whenCapReachedPicksLeastBusyExecutorWithContention.

@ParameterizedTestWithName
@CsvSource(value = { "4, 1", "4, 100", "4, 1000", "100, 1", "100, 100", "100, 1000", "1000, 1", "1000, 100", "1000, 1000", "10000, 1", "10000, 100", "10000, 1000" })
void whenCapReachedPicksLeastBusyExecutorWithContention(int maxThreads, int contention) throws InterruptedException {
    BoundedElasticScheduler s = scheduler(maxThreads);
    HashSet<BoundedElasticScheduler.BoundedState> boundedStates = new HashSet<>();
    // reach the cap of workers and keep track of boundedStates
    for (int i = 0; i < maxThreads; i++) {
        boundedStates.add(afterTest.autoDispose(s.boundedServices.pick()));
    }
    assertThat(boundedStates).as("all distinct").hasSize(maxThreads);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger countDown = new AtomicInteger(contention);
    AtomicInteger errors = new AtomicInteger();
    ExecutorService executorService = Executors.newFixedThreadPool(contention);
    AtomicIntegerArray busyPattern = new AtomicIntegerArray(Math.max(5, contention));
    for (int i = 0; i < contention; i++) {
        executorService.submit(() -> {
            if (countDown.get() <= 0) {
                return;
            }
            try {
                BoundedElasticScheduler.BoundedState bs = s.boundedServices.pick();
                assertThat(boundedStates).as("picked a busy one").contains(bs);
                assertThat(bs.markPicked()).isTrue();
                int business = bs.markCount;
                busyPattern.incrementAndGet(business);
            } catch (Throwable t) {
                errors.incrementAndGet();
                t.printStackTrace();
                countDown.set(0);
                latch.countDown();
            } finally {
                if (countDown.decrementAndGet() <= 0) {
                    latch.countDown();
                }
            }
        });
    }
    assertThat(latch.await(10, TimeUnit.SECONDS)).as("latch").isTrue();
    executorService.shutdownNow();
    assertThat(errors).as("errors").hasValue(0);
    int maxPicks = 0;
    for (int businessFactor = 0; businessFactor < busyPattern.length(); businessFactor++) {
        int pickCount = busyPattern.get(businessFactor);
        if (pickCount == 0)
            continue;
        if (pickCount > maxPicks)
            maxPicks = pickCount;
        LOGGER.trace("Picked {} times at business level {}", pickCount, businessFactor);
    }
    final int expectedMaxPick = Math.min(contention, maxThreads);
    Offset<Integer> offset;
    if (expectedMaxPick < 10) {
        offset = Offset.offset(1);
    } else if (expectedMaxPick < 1000) {
        offset = Offset.offset(10);
    } else {
        offset = Offset.offset(50);
    }
    assertThat(maxPicks).as("maxPicks").isCloseTo(expectedMaxPick, offset);
    LOGGER.info("Max picks {} ", maxPicks);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) BoundedScheduledExecutorService(reactor.core.scheduler.BoundedElasticScheduler.BoundedScheduledExecutorService) HashSet(java.util.HashSet) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Example 22 with ParameterizedTestWithName

use of reactor.test.ParameterizedTestWithName in project reactor-core by reactor.

the class FluxDoOnEachTest method errorCallbackError.

@ParameterizedTestWithName
@MethodSource("sourcesError")
void errorCallbackError(Flux<Integer> source) {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    LongAdder state = new LongAdder();
    IllegalStateException err = new IllegalStateException("test");
    source.doOnEach(s -> {
        if (s.isOnError()) {
            state.increment();
            throw Exceptions.propagate(err);
        }
    }).filter(t -> true).subscribe(ts);
    ts.assertNoValues();
    ts.assertError(IllegalStateException.class);
    ts.assertErrorWith(e -> e.getSuppressed()[0].getMessage().equals(sourceErrorMessage));
    ts.assertErrorWith(e -> e.getMessage().equals("test"));
    assertThat(state.intValue()).isEqualTo(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdder(java.util.concurrent.atomic.LongAdder) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AssertQueueSubscription(reactor.core.publisher.FluxPeekFuseableTest.AssertQueueSubscription) ConditionalSubscriber(reactor.core.Fuseable.ConditionalSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DoOnEachFuseableConditionalSubscriber(reactor.core.publisher.FluxDoOnEach.DoOnEachFuseableConditionalSubscriber) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(reactor.core.scheduler.Schedulers) Assertions(org.assertj.core.api.Assertions) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) StepVerifierOptions(reactor.test.StepVerifierOptions) MethodSource(org.junit.jupiter.params.provider.MethodSource) DoOnEachConditionalSubscriber(reactor.core.publisher.FluxDoOnEach.DoOnEachConditionalSubscriber) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Context(reactor.util.context.Context) Arguments(org.junit.jupiter.params.provider.Arguments) Test(org.junit.jupiter.api.Test) List(java.util.List) Fuseable(reactor.core.Fuseable) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Named.named(org.junit.jupiter.api.Named.named) Exceptions(reactor.core.Exceptions) LongAdder(java.util.concurrent.atomic.LongAdder) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Example 23 with ParameterizedTestWithName

use of reactor.test.ParameterizedTestWithName in project reactor-core by reactor.

the class FluxCreateTest method secondOnDisposeHandlerIsDisposedImmediately.

@ParameterizedTestWithName
@EnumSource(OverflowStrategy.class)
void secondOnDisposeHandlerIsDisposedImmediately(OverflowStrategy overflowStrategy) {
    AtomicInteger firstDisposed = new AtomicInteger();
    AtomicInteger secondDisposed = new AtomicInteger();
    Flux.create(sink -> sink.onDispose(firstDisposed::incrementAndGet).onDispose(secondDisposed::incrementAndGet), overflowStrategy).subscribe();
    assertThat(firstDisposed).as("first handler for %s", overflowStrategy).hasValue(0);
    assertThat(secondDisposed).as("second handler for %s", overflowStrategy).hasValue(1);
}
Also used : IntStream(java.util.stream.IntStream) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SerializedFluxSink(reactor.core.publisher.FluxCreate.SerializedFluxSink) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OverflowStrategy(reactor.core.publisher.FluxSink.OverflowStrategy) EnumSource(org.junit.jupiter.params.provider.EnumSource) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) BufferAsyncSink(reactor.core.publisher.FluxCreate.BufferAsyncSink) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LatestAsyncSink(reactor.core.publisher.FluxCreate.LatestAsyncSink) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Schedulers(reactor.core.scheduler.Schedulers) RaceTestUtils(reactor.test.util.RaceTestUtils) Subscriber(org.reactivestreams.Subscriber) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Semaphore(java.util.concurrent.Semaphore) Step(reactor.test.StepVerifier.Step) Context(reactor.util.context.Context) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Assertions.fail(org.assertj.core.api.Assertions.fail) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Exceptions(reactor.core.Exceptions) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Example 24 with ParameterizedTestWithName

use of reactor.test.ParameterizedTestWithName in project reactor-core by reactor.

the class FluxDoOnEachTest method error.

@ParameterizedTestWithName
@MethodSource("sourcesError")
void error(Flux<Integer> source) {
    AssertSubscriber<Integer> ts = AssertSubscriber.create();
    AtomicReference<Integer> onNext = new AtomicReference<>();
    AtomicReference<Throwable> onError = new AtomicReference<>();
    AtomicBoolean onComplete = new AtomicBoolean();
    LongAdder state = new LongAdder();
    source.doOnEach(s -> {
        if (s.isOnNext()) {
            onNext.set(s.get());
            state.increment();
        } else if (s.isOnError()) {
            onError.set(s.getThrowable());
        } else if (s.isOnComplete()) {
            onComplete.set(true);
        }
    }).filter(t -> true).subscribe(ts);
    assertThat(onNext).hasValue(null);
    assertThat(onError.get()).isInstanceOf(IllegalStateException.class).hasMessage(sourceErrorMessage);
    assertThat(onComplete).isFalse();
    assertThat(state.intValue()).isZero();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongAdder(java.util.concurrent.atomic.LongAdder) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Scannable(reactor.core.Scannable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AssertQueueSubscription(reactor.core.publisher.FluxPeekFuseableTest.AssertQueueSubscription) ConditionalSubscriber(reactor.core.Fuseable.ConditionalSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DoOnEachFuseableConditionalSubscriber(reactor.core.publisher.FluxDoOnEach.DoOnEachFuseableConditionalSubscriber) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(reactor.core.scheduler.Schedulers) Assertions(org.assertj.core.api.Assertions) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) StepVerifierOptions(reactor.test.StepVerifierOptions) MethodSource(org.junit.jupiter.params.provider.MethodSource) DoOnEachConditionalSubscriber(reactor.core.publisher.FluxDoOnEach.DoOnEachConditionalSubscriber) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Context(reactor.util.context.Context) Arguments(org.junit.jupiter.params.provider.Arguments) Test(org.junit.jupiter.api.Test) List(java.util.List) Fuseable(reactor.core.Fuseable) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Named.named(org.junit.jupiter.api.Named.named) Exceptions(reactor.core.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) AtomicReference(java.util.concurrent.atomic.AtomicReference) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Example 25 with ParameterizedTestWithName

use of reactor.test.ParameterizedTestWithName in project reactor-core by reactor.

the class FluxOnAssemblyTest method checkpointDescriptionAndForceStackForParallel.

@ParameterizedTestWithName
@ValueSource(booleans = { false, true })
void checkpointDescriptionAndForceStackForParallel(boolean debugModeOn) {
    if (debugModeOn) {
        Hooks.onOperatorDebug();
    }
    StringWriter sw = new StringWriter();
    Flux<Integer> tested = Flux.range(1, 10).parallel(2).transformGroups(g -> g.map(i -> (Integer) null)).checkpoint("heavy", true).sequential().doOnError(t -> t.printStackTrace(new PrintWriter(sw)));
    StepVerifier.create(tested).verifyError();
    String debugStack = sw.toString();
    if (debugModeOn) {
        // the traceback "error has been observed" contains both individual ops and checkpoint with description,
        // assembly points to map, with no description
        assertThat(debugStack).contains("Assembly trace from producer [reactor.core.publisher.FluxMap] :").contains("*__ParallelFlux.transformGroups ⇢ at reactor.core.publisher.FluxOnAssemblyTest.checkpointDescriptionAndForceStackForParallel(").contains("checkpoint(heavy) ⇢ at reactor.core.publisher.FluxOnAssemblyTest.checkpointDescriptionAndForceStackForParallel(");
    } else {
        // the traceback "error has been observed" only contains the checkpoint, with callsite and description,
        // assembly points to parallelSource and reflects description
        assertThat(debugStack).contains("Assembly trace from producer [reactor.core.publisher.ParallelSource], described as [heavy] :").contains("checkpoint(heavy) ⇢ at reactor.core.publisher.FluxOnAssemblyTest.checkpointDescriptionAndForceStackForParallel(").doesNotContain("ParallelFlux.transformGroups ⇢ at");
    }
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Retry(reactor.util.retry.Retry) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AssemblySnapshot(reactor.core.publisher.FluxOnAssembly.AssemblySnapshot) CoreSubscriber(reactor.core.CoreSubscriber) Duration(java.time.Duration) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) ValueSource(org.junit.jupiter.params.provider.ValueSource) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Iterator(java.util.Iterator) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) StringWriter(java.io.StringWriter) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) Exceptions(reactor.core.Exceptions) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Aggregations

ParameterizedTestWithName (reactor.test.ParameterizedTestWithName)29 Test (org.junit.jupiter.api.Test)28 Subscription (org.reactivestreams.Subscription)25 StepVerifier (reactor.test.StepVerifier)25 Stream (java.util.stream.Stream)24 CoreSubscriber (reactor.core.CoreSubscriber)24 Exceptions (reactor.core.Exceptions)21 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)20 Scannable (reactor.core.Scannable)20 Arrays (java.util.Arrays)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 Context (reactor.util.context.Context)17 Duration (java.time.Duration)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Function (java.util.function.Function)14 Arguments (org.junit.jupiter.params.provider.Arguments)14 Arguments.arguments (org.junit.jupiter.params.provider.Arguments.arguments)14 MethodSource (org.junit.jupiter.params.provider.MethodSource)14 Publisher (org.reactivestreams.Publisher)14