Search in sources :

Example 51 with LocalAddress

use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.

the class WriteBufferingAndExceptionHandlerTest method writesBuffered.

@Test
public void writesBuffered() throws Exception {
    final AtomicBoolean handlerAdded = new AtomicBoolean();
    final AtomicBoolean flush = new AtomicBoolean();
    final AtomicReference<Object> write = new AtomicReference<>();
    final WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler(new ChannelOutboundHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            assertFalse(handlerAdded.getAndSet(true));
            super.handlerAdded(ctx);
        }

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            assertFalse(flush.getAndSet(true));
            super.flush(ctx);
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            assertNull(write.getAndSet(msg));
            promise.setSuccess();
        }
    });
    LocalAddress addr = new LocalAddress("local");
    ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register();
    chan = cf.channel();
    cf.sync();
    ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class).childHandler(new ChannelHandlerAdapter() {
    }).group(group).bind(addr);
    server = sf.channel();
    sf.sync();
    assertTrue(handlerAdded.get());
    chan.write(new Object());
    chan.connect(addr).sync();
    assertNull(write.get());
    chan.flush();
    assertNull(write.get());
    assertFalse(flush.get());
    assertThat(chan.pipeline().context(handler)).isNotNull();
    chan.eventLoop().submit(new Runnable() {

        @Override
        public void run() {
            handler.writeBufferedAndRemove(chan.pipeline().context(handler));
        }
    }).sync();
    assertThat(chan.pipeline().context(handler)).isNull();
    assertThat(write.get().getClass()).isSameInstanceAs(Object.class);
    assertTrue(flush.get());
    assertThat(chan.pipeline().toMap().values()).doesNotContain(handler);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ConnectException(java.net.ConnectException) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Example 52 with LocalAddress

use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.

the class WriteBufferingAndExceptionHandlerTest method uncaughtReadFails.

@Test
public void uncaughtReadFails() throws Exception {
    WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler(new ChannelHandlerAdapter() {
    });
    LocalAddress addr = new LocalAddress("local");
    ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register();
    chan = cf.channel();
    cf.sync();
    ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class).childHandler(new ChannelHandlerAdapter() {
    }).group(group).bind(addr);
    server = sf.channel();
    sf.sync();
    ChannelFuture wf = chan.writeAndFlush(new Object());
    chan.connect(addr);
    chan.pipeline().fireChannelRead(Unpooled.copiedBuffer(new byte[] { 'a' }));
    try {
        wf.sync();
        fail();
    } catch (Exception e) {
        Status status = Status.fromThrowable(e);
        assertThat(status.getCode()).isEqualTo(Code.INTERNAL);
        assertThat(status.getDescription()).contains("channelRead() missed");
    }
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ChannelFuture(io.netty.channel.ChannelFuture) Status(io.grpc.Status) LocalAddress(io.netty.channel.local.LocalAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 53 with LocalAddress

use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.

the class WriteBufferingAndExceptionHandlerTest method handlerRemovedFailuresPropagated.

@Test
public void handlerRemovedFailuresPropagated() throws Exception {
    WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler(new ChannelHandlerAdapter() {

        @Override
        public void handlerRemoved(ChannelHandlerContext ctx) {
            ctx.pipeline().remove(ctx.pipeline().context(WriteBufferingAndExceptionHandler.class).name());
        }
    });
    LocalAddress addr = new LocalAddress("local");
    ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register();
    chan = cf.channel();
    cf.sync();
    ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class).childHandler(new ChannelHandlerAdapter() {
    }).group(group).bind(addr);
    server = sf.channel();
    sf.sync();
    chan.connect(addr);
    ChannelFuture wf = chan.writeAndFlush(new Object());
    chan.pipeline().removeFirst();
    try {
        wf.sync();
        fail();
    } catch (Exception e) {
        Status status = Status.fromThrowable(e);
        assertThat(status.getCode()).isEqualTo(Code.INTERNAL);
        assertThat(status.getDescription()).contains("Buffer removed");
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Status(io.grpc.Status) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Example 54 with LocalAddress

use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.

the class TransportBenchmark method setUp.

@Setup
public void setUp() throws Exception {
    ServerCredentials serverCreds = InsecureServerCredentials.create();
    ServerBuilder<?> serverBuilder;
    ManagedChannelBuilder<?> channelBuilder;
    switch(transport) {
        case INPROCESS:
            {
                String name = "bench" + Math.random();
                serverBuilder = InProcessServerBuilder.forName(name);
                channelBuilder = InProcessChannelBuilder.forName(name);
                break;
            }
        case NETTY:
            {
                InetSocketAddress address = new InetSocketAddress("localhost", pickUnusedPort());
                serverBuilder = NettyServerBuilder.forAddress(address, serverCreds);
                channelBuilder = NettyChannelBuilder.forAddress(address).negotiationType(NegotiationType.PLAINTEXT);
                break;
            }
        case NETTY_LOCAL:
            {
                String name = "bench" + Math.random();
                LocalAddress address = new LocalAddress(name);
                EventLoopGroup group = new DefaultEventLoopGroup();
                serverBuilder = NettyServerBuilder.forAddress(address, serverCreds).bossEventLoopGroup(group).workerEventLoopGroup(group).channelType(LocalServerChannel.class);
                channelBuilder = NettyChannelBuilder.forAddress(address).eventLoopGroup(group).channelType(LocalChannel.class).negotiationType(NegotiationType.PLAINTEXT);
                groupToShutdown = group;
                break;
            }
        case NETTY_EPOLL:
            {
                InetSocketAddress address = new InetSocketAddress("localhost", pickUnusedPort());
                // Reflection used since they are only available on linux.
                Class<?> groupClass = Class.forName("io.netty.channel.epoll.EpollEventLoopGroup");
                EventLoopGroup group = (EventLoopGroup) groupClass.getConstructor().newInstance();
                Class<? extends ServerChannel> serverChannelClass = Class.forName("io.netty.channel.epoll.EpollServerSocketChannel").asSubclass(ServerChannel.class);
                serverBuilder = NettyServerBuilder.forAddress(address, serverCreds).bossEventLoopGroup(group).workerEventLoopGroup(group).channelType(serverChannelClass);
                Class<? extends Channel> channelClass = Class.forName("io.netty.channel.epoll.EpollSocketChannel").asSubclass(Channel.class);
                channelBuilder = NettyChannelBuilder.forAddress(address).eventLoopGroup(group).channelType(channelClass).negotiationType(NegotiationType.PLAINTEXT);
                groupToShutdown = group;
                break;
            }
        case OKHTTP:
            {
                int port = pickUnusedPort();
                InetSocketAddress address = new InetSocketAddress("localhost", port);
                serverBuilder = NettyServerBuilder.forAddress(address, serverCreds);
                channelBuilder = OkHttpChannelBuilder.forAddress("localhost", port, InsecureChannelCredentials.create());
                break;
            }
        default:
            throw new Exception("Unknown transport: " + transport);
    }
    if (direct) {
        serverBuilder.directExecutor();
        // Because blocking stubs avoid the executor, this doesn't do much.
        channelBuilder.directExecutor();
    }
    server = serverBuilder.addService(new AsyncServer.BenchmarkServiceImpl()).build();
    server.start();
    channel = channelBuilder.build();
    stub = BenchmarkServiceGrpc.newBlockingStub(channel);
    asyncStub = BenchmarkServiceGrpc.newStub(channel);
    // Wait for channel to start
    stub.unaryCall(SimpleRequest.getDefaultInstance());
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) ServerCredentials(io.grpc.ServerCredentials) InsecureServerCredentials(io.grpc.InsecureServerCredentials) InetSocketAddress(java.net.InetSocketAddress) LocalChannel(io.netty.channel.local.LocalChannel) ManagedChannel(io.grpc.ManagedChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) AsyncServer(io.grpc.benchmarks.qps.AsyncServer) ByteString(com.google.protobuf.ByteString) LocalServerChannel(io.netty.channel.local.LocalServerChannel) ServerChannel(io.netty.channel.ServerChannel) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) StatusRuntimeException(io.grpc.StatusRuntimeException) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Setup(org.openjdk.jmh.annotations.Setup)

Example 55 with LocalAddress

use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.

the class Http2NettyLocalChannelTest method getServerBuilder.

@Override
protected ServerBuilder<?> getServerBuilder() {
    NettyServerBuilder builder = NettyServerBuilder.forAddress(new LocalAddress("in-process-1")).flowControlWindow(AbstractInteropTest.TEST_FLOW_CONTROL_WINDOW).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).channelType(LocalServerChannel.class).workerEventLoopGroup(eventLoopGroup).bossEventLoopGroup(eventLoopGroup);
    // Disable the default census stats tracer, use testing tracer instead.
    InternalNettyServerBuilder.setStatsEnabled(builder, false);
    return builder.addStreamTracerFactory(createCustomCensusTracerFactory());
}
Also used : NettyServerBuilder(io.grpc.netty.NettyServerBuilder) InternalNettyServerBuilder(io.grpc.netty.InternalNettyServerBuilder) LocalAddress(io.netty.channel.local.LocalAddress)

Aggregations

LocalAddress (io.netty.channel.local.LocalAddress)116 Bootstrap (io.netty.bootstrap.Bootstrap)66 LocalChannel (io.netty.channel.local.LocalChannel)66 LocalServerChannel (io.netty.channel.local.LocalServerChannel)66 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)63 Channel (io.netty.channel.Channel)61 Test (org.junit.Test)47 Test (org.junit.jupiter.api.Test)39 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)31 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)28 ConnectException (java.net.ConnectException)27 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)27 Subscription (rx.Subscription)27 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)26 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)26 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)25 ChannelFuture (io.netty.channel.ChannelFuture)24 SSLException (javax.net.ssl.SSLException)22