Search in sources :

Example 11 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class OperatorsTest method onNextFailureWithStrategyMatchingDoesntCancel.

@Test
public void onNextFailureWithStrategyMatchingDoesntCancel() {
    Context context = Context.of(OnNextFailureStrategy.KEY_ON_NEXT_ERROR_STRATEGY, new OnNextFailureStrategy() {

        @Override
        public boolean test(Throwable error, @Nullable Object value) {
            return true;
        }

        @Nullable
        @Override
        public Throwable process(Throwable error, @Nullable Object value, Context context) {
            return null;
        }
    });
    Operators.DeferredSubscription s = new Operators.DeferredSubscription();
    Throwable t = Operators.onNextError("foo", new NullPointerException("bar"), context, s);
    assertThat(t).as("exception processed").isNull();
    assertThat(s.isCancelled()).as("subscription cancelled").isFalse();
}
Also used : Context(reactor.util.context.Context) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 12 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class OperatorsTest method onRejectedExecutionLocalTakesPrecedenceOverOnOperatorError.

@Test
public void onRejectedExecutionLocalTakesPrecedenceOverOnOperatorError() {
    BiFunction<Throwable, Object, Throwable> localOperatorErrorHook = (e, v) -> new IllegalStateException("boom_" + v, e);
    BiFunction<Throwable, Object, Throwable> localReeHook = (e, v) -> new IllegalStateException("rejected_" + v, e);
    Context c = Context.of(Hooks.KEY_ON_OPERATOR_ERROR, localOperatorErrorHook, Hooks.KEY_ON_REJECTED_EXECUTION, localReeHook);
    IllegalArgumentException failure = new IllegalArgumentException("foo");
    final Throwable throwable = Operators.onRejectedExecution(failure, null, null, "bar", c);
    assertThat(throwable).isInstanceOf(IllegalStateException.class).hasMessage("rejected_bar").hasNoSuppressedExceptions();
    assertThat(throwable.getCause()).isInstanceOf(RejectedExecutionException.class).hasMessage("Scheduler unavailable").hasCause(failure);
}
Also used : Scannable(reactor.core.Scannable) BiFunction(java.util.function.BiFunction) Context(reactor.util.context.Context) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLongFieldUpdater(java.util.concurrent.atomic.AtomicLongFieldUpdater) Test(org.junit.Test) MultiSubscriptionSubscriber(reactor.core.publisher.Operators.MultiSubscriptionSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CoreSubscriber(reactor.core.CoreSubscriber) Fuseable(reactor.core.Fuseable) CancelledSubscription(reactor.core.publisher.Operators.CancelledSubscription) Subscription(org.reactivestreams.Subscription) ScalarSubscription(reactor.core.publisher.Operators.ScalarSubscription) Exceptions(reactor.core.Exceptions) Nullable(javax.annotation.Nullable) MonoSubscriber(reactor.core.publisher.Operators.MonoSubscriber) DeferredSubscription(reactor.core.publisher.Operators.DeferredSubscription) EmptySubscription(reactor.core.publisher.Operators.EmptySubscription) RaceTestUtils(reactor.test.RaceTestUtils) Context(reactor.util.context.Context) Test(org.junit.Test)

Example 13 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class OperatorsTest method onNextDroppedLocal.

@Test
public void onNextDroppedLocal() {
    AtomicReference<Object> hookState = new AtomicReference<>();
    Consumer<Object> localHook = hookState::set;
    Context c = Context.of(Hooks.KEY_ON_NEXT_DROPPED, localHook);
    Operators.onNextDropped("foo", c);
    assertThat(hookState.get()).isEqualTo("foo");
}
Also used : Context(reactor.util.context.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 14 with Context

use of reactor.util.context.Context in project reactor-core by reactor.

the class FluxContextStart method subscribe.

@Override
public void subscribe(CoreSubscriber<? super T> actual) {
    Context c;
    try {
        c = doOnContext.apply(actual.currentContext());
    } catch (Throwable t) {
        Operators.error(actual, Operators.onOperatorError(t, actual.currentContext()));
        return;
    }
    source.subscribe(new ContextStartSubscriber<>(actual, c));
}
Also used : Context(reactor.util.context.Context)

Example 15 with Context

use of reactor.util.context.Context in project reactor-netty by reactor.

the class HttpServerTests method sendFileSecure.

@Test
public void sendFileSecure() throws CertificateException, SSLException, URISyntaxException {
    Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").toURI());
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    SslContext sslClient = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    NettyContext context = HttpServer.create(opt -> opt.sslContext(sslServer)).newHandler((req, resp) -> resp.sendFile(largeFile)).block();
    HttpClientResponse response = HttpClient.create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).get("/foo").block(Duration.ofSeconds(120));
    context.dispose();
    context.onClose().block();
    String body = response.receive().aggregate().asString(StandardCharsets.UTF_8).block();
    assertThat(body).startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.").contains("1024 mark here -><- 1024 mark here").endsWith("End of File");
}
Also used : Path(java.nio.file.Path) HttpResources(reactor.ipc.netty.http.HttpResources) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) NettyOutbound(reactor.ipc.netty.NettyOutbound) ByteBuffer(java.nio.ByteBuffer) Unpooled(io.netty.buffer.Unpooled) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Path(java.nio.file.Path) Context(reactor.util.context.Context) StandardOpenOption(java.nio.file.StandardOpenOption) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FileSystem(java.nio.file.FileSystem) InetSocketAddress(java.net.InetSocketAddress) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) SSLException(javax.net.ssl.SSLException) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ByteBufFlux(reactor.ipc.netty.ByteBufFlux) HttpVersion(io.netty.handler.codec.http.HttpVersion) FluxSink(reactor.core.publisher.FluxSink) Tuple3(reactor.util.function.Tuple3) Tuple2(reactor.util.function.Tuple2) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) StandardCopyOption(java.nio.file.StandardCopyOption) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ByteBuf(io.netty.buffer.ByteBuf) Assert(org.testng.Assert) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ExecutorService(java.util.concurrent.ExecutorService) TcpClient(reactor.ipc.netty.tcp.TcpClient) SslContext(io.netty.handler.ssl.SslContext) Files(java.nio.file.Files) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) FutureMono(reactor.ipc.netty.FutureMono) CompletionHandler(java.nio.channels.CompletionHandler) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) PoolResources(reactor.ipc.netty.resources.PoolResources) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Paths(java.nio.file.Paths) NettyContext(reactor.ipc.netty.NettyContext) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) FileSystems(java.nio.file.FileSystems) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) SslContext(io.netty.handler.ssl.SslContext) BlockingNettyContext(reactor.ipc.netty.tcp.BlockingNettyContext) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Aggregations

Context (reactor.util.context.Context)35 Test (org.junit.Test)28 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 Consumer (java.util.function.Consumer)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 ArrayList (java.util.ArrayList)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Nullable (javax.annotation.Nullable)8 Scannable (reactor.core.Scannable)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 Function (java.util.function.Function)7 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)7 Flux (reactor.core.publisher.Flux)7 Mono (reactor.core.publisher.Mono)7 StepVerifier (reactor.test.StepVerifier)7 List (java.util.List)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Subscription (org.reactivestreams.Subscription)6 CoreSubscriber (reactor.core.CoreSubscriber)6 Exceptions (reactor.core.Exceptions)6