use of io.netty.channel.local.LocalAddress in project netty by netty.
the class RenegotiateTest method testRenegotiateServer.
@Test
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
public void testRenegotiateServer() throws Throwable {
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
final CountDownLatch latch = new CountDownLatch(2);
SelfSignedCertificate cert = new SelfSignedCertificate();
EventLoopGroup group = new LocalEventLoopGroup();
try {
final SslContext context = SslContextBuilder.forServer(cert.key(), cert.cert()).sslProvider(serverSslProvider()).protocols(SslProtocols.TLS_v1_2).build();
ServerBootstrap sb = new ServerBootstrap();
sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
SslHandler handler = context.newHandler(ch.alloc());
handler.setHandshakeTimeoutMillis(0);
ch.pipeline().addLast(handler);
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
private boolean renegotiate;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ReferenceCountUtil.release(msg);
}
@Override
public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
if (!renegotiate && evt instanceof SslHandshakeCompletionEvent) {
SslHandshakeCompletionEvent event = (SslHandshakeCompletionEvent) evt;
if (event.isSuccess()) {
final SslHandler handler = ctx.pipeline().get(SslHandler.class);
renegotiate = true;
handler.renegotiate().addListener(new FutureListener<Channel>() {
@Override
public void operationComplete(Future<Channel> future) throws Exception {
if (!future.isSuccess()) {
error.compareAndSet(null, future.cause());
ctx.close();
}
latch.countDown();
}
});
} else {
error.compareAndSet(null, event.cause());
latch.countDown();
ctx.close();
}
}
}
});
}
});
Channel channel = sb.bind(new LocalAddress("test")).syncUninterruptibly().channel();
final SslContext clientContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).sslProvider(SslProvider.JDK).protocols(SslProtocols.TLS_v1_2).build();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(LocalChannel.class).handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
SslHandler handler = clientContext.newHandler(ch.alloc());
handler.setHandshakeTimeoutMillis(0);
ch.pipeline().addLast(handler);
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof SslHandshakeCompletionEvent) {
SslHandshakeCompletionEvent event = (SslHandshakeCompletionEvent) evt;
if (!event.isSuccess()) {
error.compareAndSet(null, event.cause());
ctx.close();
}
latch.countDown();
}
}
});
}
});
Channel clientChannel = bootstrap.connect(channel.localAddress()).syncUninterruptibly().channel();
latch.await();
clientChannel.close().syncUninterruptibly();
channel.close().syncUninterruptibly();
verifyResult(error);
} finally {
group.shutdownGracefully();
}
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class ReentrantChannelTest method testFlushInWritabilityChanged.
/**
* Similar to {@link #testWritabilityChanged()} with slight variation.
*/
@Test
public void testFlushInWritabilityChanged() throws Exception {
LocalAddress addr = new LocalAddress("testFlushInWritabilityChanged");
ServerBootstrap sb = getLocalServerBootstrap();
sb.bind(addr).sync().channel();
Bootstrap cb = getLocalClientBootstrap();
setInterest(Event.WRITE, Event.FLUSH, Event.WRITABILITY);
Channel clientChannel = cb.connect(addr).sync().channel();
clientChannel.config().setWriteBufferLowWaterMark(512);
clientChannel.config().setWriteBufferHighWaterMark(1024);
clientChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
if (!ctx.channel().isWritable()) {
ctx.channel().flush();
}
ctx.fireChannelWritabilityChanged();
}
});
assertTrue(clientChannel.isWritable());
clientChannel.write(createTestBuf(2000)).sync();
clientChannel.close().sync();
assertLog(// Case 1:
"WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITE\n" + "WRITABILITY: writable=false\n" + "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITABILITY: writable=true\n", // Case 2:
"WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITE\n" + "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITABILITY: writable=true\n" + "WRITABILITY: writable=true\n");
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class ReentrantChannelTest method testFlushFailure.
@Test
public void testFlushFailure() throws Exception {
LocalAddress addr = new LocalAddress("testFlushFailure");
ServerBootstrap sb = getLocalServerBootstrap();
sb.bind(addr).sync().channel();
Bootstrap cb = getLocalClientBootstrap();
setInterest(Event.WRITE, Event.FLUSH, Event.CLOSE, Event.EXCEPTION);
Channel clientChannel = cb.connect(addr).sync().channel();
clientChannel.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
throw new Exception("intentional failure");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
});
try {
clientChannel.writeAndFlush(createTestBuf(2000)).sync();
fail();
} catch (Throwable cce) {
// FIXME: shouldn't this contain the "intentional failure" exception?
assertThat(cce, Matchers.instanceOf(ClosedChannelException.class));
}
clientChannel.closeFuture().sync();
assertLog("WRITE\nCLOSE\n");
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class ReentrantChannelTest method testWriteFlushPingPong.
@Test
public void testWriteFlushPingPong() throws Exception {
LocalAddress addr = new LocalAddress("testWriteFlushPingPong");
ServerBootstrap sb = getLocalServerBootstrap();
sb.bind(addr).sync().channel();
Bootstrap cb = getLocalClientBootstrap();
setInterest(Event.WRITE, Event.FLUSH, Event.CLOSE, Event.EXCEPTION);
Channel clientChannel = cb.connect(addr).sync().channel();
clientChannel.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
int writeCount;
int flushCount;
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (writeCount < 5) {
writeCount++;
ctx.channel().flush();
}
super.write(ctx, msg, promise);
}
@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
if (flushCount < 5) {
flushCount++;
ctx.channel().write(createTestBuf(2000));
}
super.flush(ctx);
}
});
clientChannel.writeAndFlush(createTestBuf(2000));
clientChannel.close().sync();
assertLog("WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "CLOSE\n");
}
use of io.netty.channel.local.LocalAddress in project netty by netty.
the class FixedChannelPoolMapDeadlockTest method testDeadlockOnRemove.
@Test
public void testDeadlockOnRemove() throws Exception {
final EventLoop thread1 = new DefaultEventLoop();
final Bootstrap bootstrap1 = new Bootstrap().channel(LocalChannel.class).group(thread1).localAddress(new LocalAddress("#1"));
final EventLoop thread2 = new DefaultEventLoop();
final Bootstrap bootstrap2 = new Bootstrap().channel(LocalChannel.class).group(thread2).localAddress(new LocalAddress("#2"));
// pool1 runs on thread2, pool2 runs on thread1
final FixedChannelPool pool1 = new FixedChannelPool(bootstrap2, NOOP_HANDLER, 1);
final FixedChannelPool pool2 = new FixedChannelPool(bootstrap1, NOOP_HANDLER, 1);
final AbstractChannelPoolMap<String, FixedChannelPool> channelPoolMap = new AbstractChannelPoolMap<String, FixedChannelPool>() {
@Override
protected FixedChannelPool newPool(String key) {
if ("#1".equals(key)) {
return pool1;
} else if ("#2".equals(key)) {
return pool2;
} else {
throw new AssertionError("Unexpected key=" + key);
}
}
};
assertSame(pool1, channelPoolMap.get("#1"));
assertSame(pool2, channelPoolMap.get("#2"));
// thread1 tries to remove pool1 which is running on thread2
// thread2 tries to remove pool2 which is running on thread1
final CyclicBarrier barrier = new CyclicBarrier(2);
Future<?> future1 = thread1.submit(new Runnable() {
@Override
public void run() {
await(barrier);
channelPoolMap.remove("#1");
}
});
Future<?> future2 = thread2.submit(new Runnable() {
@Override
public void run() {
await(barrier);
channelPoolMap.remove("#2");
}
});
// A blocking close on remove will cause a deadlock here and the test will time out
try {
future1.get(1, TimeUnit.SECONDS);
future2.get(1, TimeUnit.SECONDS);
} catch (TimeoutException e) {
// Fail the test on timeout to distinguish from other errors
throw new AssertionError(e);
} finally {
pool1.close();
pool2.close();
channelPoolMap.close();
shutdown(thread1, thread2);
}
}
Aggregations