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);
}
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");
}
}
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");
}
}
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());
}
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());
}
Aggregations