Search in sources :

Example 6 with TestLogger

use of reactor.test.util.TestLogger in project reactor-core by reactor.

the class SinkOneMulticastTest method doubleError.

@Test
void doubleError() {
    TestLogger testLogger = new TestLogger();
    LoggerUtils.enableCaptureWith(testLogger);
    try {
        SinkOneMulticast<String> sink = new SinkOneMulticast<>();
        sink.emitError(new Exception("test"), Sinks.EmitFailureHandler.FAIL_FAST);
        sink.emitError(new Exception("test2"), Sinks.EmitFailureHandler.FAIL_FAST);
        Assertions.assertThat(testLogger.getErrContent()).contains("Operator called default onErrorDropped").contains("test2");
    } finally {
        LoggerUtils.disableCapture();
    }
}
Also used : TestLogger(reactor.test.util.TestLogger) Test(org.junit.jupiter.api.Test)

Example 7 with TestLogger

use of reactor.test.util.TestLogger 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 8 with TestLogger

use of reactor.test.util.TestLogger 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 9 with TestLogger

use of reactor.test.util.TestLogger 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 10 with TestLogger

use of reactor.test.util.TestLogger in project reactor-core by reactor.

the class SignalLoggerTests method safeLogsWhenPassingQueueSubscription.

@Test
public void safeLogsWhenPassingQueueSubscription() {
    TestLogger logger = new TestLogger();
    SignalLogger signalLogger = new SignalLogger<>(Flux.empty(), null, Level.INFO, false, it -> logger);
    signalLogger.safeLog(SignalType.ON_NEXT, new FluxPeekFuseableTest.AssertQueueSubscription<>());
    assertThat(logger.getOutContent()).contains("A Fuseable Subscription has been passed to the logging framework, this is generally a sign of a misplaced log(), " + "eg. 'window(2).log()' instead of 'window(2).flatMap(w -> w.log())'").contains("onNext(reactor.core.publisher.FluxPeekFuseableTest$AssertQueueSubscription");
}
Also used : TestLogger(reactor.test.util.TestLogger) Test(org.junit.jupiter.api.Test)

Aggregations

TestLogger (reactor.test.util.TestLogger)39 Test (org.junit.jupiter.api.Test)38 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Subscription (org.reactivestreams.Subscription)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 StepVerifier (reactor.test.StepVerifier)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 CoreSubscriber (reactor.core.CoreSubscriber)9 Fuseable (reactor.core.Fuseable)9 Duration (java.time.Duration)8 TimeUnit (java.util.concurrent.TimeUnit)8 Function (java.util.function.Function)8 Awaitility (org.awaitility.Awaitility)8 Publisher (org.reactivestreams.Publisher)8 TestPublisher (reactor.test.publisher.TestPublisher)8 Loggers (reactor.util.Loggers)8 Context (reactor.util.context.Context)8 LongAdder (java.util.concurrent.atomic.LongAdder)7 FluxOperatorTest (reactor.test.publisher.FluxOperatorTest)7