Search in sources :

Example 6 with ParameterizedTestWithName

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

the class FluxUsingWhenTest method apiCancelFailure.

@ParameterizedTestWithName
@MethodSource("sourcesFullTransaction")
public void apiCancelFailure(Flux<String> transaction) {
    TestLogger testLogger = new TestLogger();
    Loggers.useCustomLoggers(s -> testLogger);
    try {
        final AtomicReference<TestResource> ref = new AtomicReference<>();
        Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> {
            ref.set(d);
            return transaction;
        }, TestResource::commit, TestResource::rollback, TestResource::cancelError);
        StepVerifier.create(flux.take(1), 1).expectNext("Transaction started").verifyComplete();
        assertThat(ref.get()).isNotNull().matches(tr -> !tr.commitProbe.wasSubscribed(), "no commit").matches(tr -> !tr.rollbackProbe.wasSubscribed(), "no rollback").matches(tr -> tr.cancelProbe.wasSubscribed(), "cancel method used");
        // since the CancelInner is subscribed in a fire-and-forget fashion, the log comes later
        // the test must be done before the finally, lest the error message be printed too late for TestLogger to catch it
        Awaitility.await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> assertThat(testLogger.getErrContent()).startsWith("[ WARN]").contains("Async resource cleanup failed after cancel - java.lang.ArithmeticException: / by zero"));
    } finally {
        Loggers.resetLoggerFactory();
    }
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) TestPublisher(reactor.test.publisher.TestPublisher) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UsingWhenSubscriber(reactor.core.publisher.FluxUsingWhen.UsingWhenSubscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Nullable(reactor.util.annotation.Nullable) Level(java.util.logging.Level) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) Attr(reactor.core.Scannable.Attr) ResourceSubscriber(reactor.core.publisher.FluxUsingWhen.ResourceSubscriber) Duration(java.time.Duration) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) Arguments(org.junit.jupiter.params.provider.Arguments) PublisherProbe(reactor.test.publisher.PublisherProbe) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) Fuseable(reactor.core.Fuseable) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) Named.named(org.junit.jupiter.api.Named.named) Awaitility(org.awaitility.Awaitility) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestLogger(reactor.test.util.TestLogger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Example 7 with ParameterizedTestWithName

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

the class FluxUsingWhenTest method apiCancelGeneratingNullLogs.

@ParameterizedTestWithName
@MethodSource("sourcesFullTransaction")
public void apiCancelGeneratingNullLogs(Flux<String> transactionToCancel) {
    TestLogger testLogger = new TestLogger();
    Loggers.useCustomLoggers(s -> testLogger);
    try {
        final AtomicReference<TestResource> ref = new AtomicReference<>();
        Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> {
            ref.set(d);
            return transactionToCancel;
        }, TestResource::commit, TestResource::rollback, TestResource::cancelNull);
        StepVerifier.create(flux.take(1), 1).expectNext("Transaction started").verifyComplete();
        assertThat(ref.get()).isNotNull().matches(tr -> !tr.commitProbe.wasSubscribed(), "no commit").matches(tr -> !tr.rollbackProbe.wasSubscribed(), "no rollback").matches(tr -> !tr.cancelProbe.wasSubscribed(), "cancel method short-circuited");
    } finally {
        Loggers.resetLoggerFactory();
    }
    assertThat(testLogger.getErrContent()).contains("[ WARN] (" + Thread.currentThread().getName() + ") " + "Error generating async resource cleanup during onCancel - java.lang.NullPointerException");
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) TestPublisher(reactor.test.publisher.TestPublisher) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UsingWhenSubscriber(reactor.core.publisher.FluxUsingWhen.UsingWhenSubscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Nullable(reactor.util.annotation.Nullable) Level(java.util.logging.Level) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) Attr(reactor.core.Scannable.Attr) ResourceSubscriber(reactor.core.publisher.FluxUsingWhen.ResourceSubscriber) Duration(java.time.Duration) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) Arguments(org.junit.jupiter.params.provider.Arguments) PublisherProbe(reactor.test.publisher.PublisherProbe) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) Fuseable(reactor.core.Fuseable) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) Named.named(org.junit.jupiter.api.Named.named) Awaitility(org.awaitility.Awaitility) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestLogger(reactor.test.util.TestLogger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Example 8 with ParameterizedTestWithName

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

the class FluxUsingWhenTest method cancelWithHandlerFailure.

@ParameterizedTestWithName
@MethodSource("sources01")
public void cancelWithHandlerFailure(Flux<String> source) {
    TestResource testResource = new TestResource();
    final TestLogger tl = new TestLogger();
    Loggers.useCustomLoggers(name -> tl);
    try {
        Flux<String> test = Flux.usingWhen(Mono.just(testResource), tr -> source, TestResource::commit, TestResource::rollback, r -> r.cancel().concatWith(Mono.error(new IllegalStateException("cancel error")))).take(2);
        StepVerifier.create(test).expectNext("0", "1").verifyComplete();
        testResource.commitProbe.assertWasNotSubscribed();
        testResource.rollbackProbe.assertWasNotSubscribed();
        testResource.cancelProbe.assertWasSubscribed();
    } finally {
        Loggers.resetLoggerFactory();
    }
    assertThat(tl.getErrContent()).contains("Async resource cleanup failed after cancel").contains("java.lang.IllegalStateException: cancel error");
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) TestPublisher(reactor.test.publisher.TestPublisher) Disposable(reactor.core.Disposable) StepVerifier(reactor.test.StepVerifier) TestLogger(reactor.test.util.TestLogger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UsingWhenSubscriber(reactor.core.publisher.FluxUsingWhen.UsingWhenSubscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Nullable(reactor.util.annotation.Nullable) Level(java.util.logging.Level) CoreSubscriber(reactor.core.CoreSubscriber) Loggers(reactor.util.Loggers) Attr(reactor.core.Scannable.Attr) ResourceSubscriber(reactor.core.publisher.FluxUsingWhen.ResourceSubscriber) Duration(java.time.Duration) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Assertions.assertThatNullPointerException(org.assertj.core.api.Assertions.assertThatNullPointerException) Publisher(org.reactivestreams.Publisher) Context(reactor.util.context.Context) Arguments(org.junit.jupiter.params.provider.Arguments) PublisherProbe(reactor.test.publisher.PublisherProbe) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) Fuseable(reactor.core.Fuseable) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) Named.named(org.junit.jupiter.api.Named.named) Awaitility(org.awaitility.Awaitility) TestLogger(reactor.test.util.TestLogger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Example 9 with ParameterizedTestWithName

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

the class FluxStreamTest method fromStreamWithFailingIteratorNextInFusion.

// see https://github.com/reactor/reactor-core/issues/2761
@ParameterizedTestWithName
@ValueSource(booleans = { false, true })
void fromStreamWithFailingIteratorNextInFusion(boolean conditionalSubscriber) throws InterruptedException {
    CountDownLatch thrown = new CountDownLatch(1);
    Iterator<Integer> throwingIterator = new Iterator<Integer>() {

        int count = 0;

        @Override
        public boolean hasNext() {
            return count < 3;
        }

        @Override
        public Integer next() {
            if (++count > 2) {
                thrown.countDown();
                throw new RuntimeException("boom");
            } else {
                return count;
            }
        }
    };
    CompletableFuture<Throwable> error = new CompletableFuture<>();
    CountDownLatch terminated = new CountDownLatch(1);
    Subscriber<Integer> simpleAsyncSubscriber = new BaseSubscriber<Integer>() {

        @Override
        protected void hookOnSubscribe(Subscription subscription) {
            request(1);
        }

        @Override
        protected void hookOnNext(Integer value) {
            // proceed on a different thread
            CompletableFuture.runAsync(() -> request(1));
        }

        @Override
        protected void hookOnError(Throwable throwable) {
            // expected to be called, but isn't
            error.complete(throwable);
        }

        @Override
        protected void hookOnComplete() {
            // not expected to happen
            error.complete(null);
        }
    };
    Flux<Integer> flux = Flux.fromStream(StreamSupport.stream(Spliterators.spliteratorUnknownSize(throwingIterator, 0), false));
    if (conditionalSubscriber) {
        flux = flux.filter(v -> true);
    }
    flux.publishOn(Schedulers.boundedElastic()).doOnTerminate(terminated::countDown).subscribe(simpleAsyncSubscriber);
    assertThat(thrown.await(3, TimeUnit.SECONDS)).isTrue();
    assertThat(terminated.await(2, TimeUnit.SECONDS)).withFailMessage("Pipeline should terminate").isTrue();
    assertThat(error).succeedsWithin(Duration.ofSeconds(2), InstanceOfAssertFactories.THROWABLE).hasMessage("boom");
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Spliterators(java.util.Spliterators) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) StreamSupport(java.util.stream.StreamSupport) Subscriber(org.reactivestreams.Subscriber) ValueSource(org.junit.jupiter.params.provider.ValueSource) Iterator(java.util.Iterator) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) Iterator(java.util.Iterator) Subscription(org.reactivestreams.Subscription) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Example 10 with ParameterizedTestWithName

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

the class FluxDoOnEachTest method normal.

@ParameterizedTestWithName
@MethodSource("sources12Complete")
void normal(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(2);
    assertThat(onError).hasValue(null);
    assertThat(onComplete).isTrue();
    assertThat(state.intValue()).isEqualTo(2);
}
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)

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