From ab5043b3c7f04b3ef9744d59e80fcb04b34173d9 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 31 May 2012 21:22:03 -0700 Subject: [PATCH] Fixed LocalAddressTest --- .../netty/channel/local/LocalAddressTest.java | 163 ------------------ .../local/LocalChannelRegistryTest.java | 95 ++++++++++ 2 files changed, 95 insertions(+), 163 deletions(-) delete mode 100644 transport/src/test/java/io/netty/channel/local/LocalAddressTest.java create mode 100644 transport/src/test/java/io/netty/channel/local/LocalChannelRegistryTest.java diff --git a/transport/src/test/java/io/netty/channel/local/LocalAddressTest.java b/transport/src/test/java/io/netty/channel/local/LocalAddressTest.java deleted file mode 100644 index a769f3ea65..0000000000 --- a/transport/src/test/java/io/netty/channel/local/LocalAddressTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright 2011 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.channel.local; - -import io.netty.bootstrap.Bootstrap; -import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.ChannelFuture; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelPipeline; -import io.netty.logging.InternalLogger; -import io.netty.logging.InternalLoggerFactory; - -import java.nio.channels.Channels; - -import org.junit.Assert; -import org.junit.Test; - -public class LocalAddressTest { - - private static final InternalLogger logger = - InternalLoggerFactory.getInstance(LocalAddressTest.class); - - private static String LOCAL_ADDR_ID = "test.id"; - - @Test - public void localConnectOK() - throws Exception { - - Bootstrap cb = new Bootstrap(); - ServerBootstrap sb = new ServerBootstrap(); - - cb.setPipelineFactory(new ChannelPipelineFactory() { - @Override - public ChannelPipeline getPipeline() - throws Exception { - - ChannelPipeline pipeline = Channels.pipeline(); - - pipeline.addLast("test.handler", new TestHandler()); - return pipeline; - } - }); - - sb.setPipelineFactory(new ChannelPipelineFactory() { - @Override - public ChannelPipeline getPipeline() - throws Exception { - - ChannelPipeline pipeline = Channels.pipeline(); - - pipeline.addLast("test.handler", new TestHandler()); - return pipeline; - } - }); - - LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID); - - // Start server - sb.bind(addr); - - // Connect to the server - ChannelFuture connectFuture = cb.connect(addr); - connectFuture.awaitUninterruptibly(); - - // Send a message event up the pipeline. - Channels.fireMessageReceived(connectFuture.channel(), "Hello, World"); - - // Close the channel - connectFuture.channel().close(); - - // Wait until the connection is closed, or the connection attempt fails - connectFuture.channel().getCloseFuture().awaitUninterruptibly(); - - sb.releaseExternalResources(); - cb.releaseExternalResources(); - - Assert.assertTrue(String.format("Expected null, got channel '%s' for local address '%s'", LocalChannelRegistry.getChannel(addr), addr), LocalChannelRegistry.getChannel(addr) == null); - } - - @Test - public void localConnectAgain() - throws Exception { - - ClientBootstrap cb = new ClientBootstrap(new DefaultLocalClientChannelFactory()); - ServerBootstrap sb = new ServerBootstrap(new DefaultLocalServerChannelFactory()); - - cb.setPipelineFactory(new ChannelPipelineFactory() { - @Override - public ChannelPipeline getPipeline() - throws Exception { - - ChannelPipeline pipeline = Channels.pipeline(); - - pipeline.addLast("test.handler", new TestHandler()); - return pipeline; - } - }); - - sb.setPipelineFactory(new ChannelPipelineFactory() { - @Override - public ChannelPipeline getPipeline() - throws Exception { - - ChannelPipeline pipeline = Channels.pipeline(); - - pipeline.addLast("test.handler", new TestHandler()); - return pipeline; - } - }); - - LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID); - - // Start server - sb.bind(addr); - - // Connect to the server - ChannelFuture connectFuture = cb.connect(addr); - connectFuture.awaitUninterruptibly(); - - // Send a message event up the pipeline. - Channels.fireMessageReceived(connectFuture.channel(), "Hello, World"); - - // Close the channel - connectFuture.channel().close(); - - // Wait until the connection is closed, or the connection attempt fails - connectFuture.channel().getCloseFuture().awaitUninterruptibly(); - - - sb.releaseExternalResources(); - cb.releaseExternalResources(); - - Assert.assertTrue(String.format("Expected null, got channel '%s' for local address '%s'", LocalChannelRegistry.getChannel(addr), addr), LocalChannelRegistry.getChannel(addr) == null); - } - - public static class TestHandler - extends SimpleChannelUpstreamHandler { - - public TestHandler() { - } - - @Override - public void handleUpstream(ChannelHandlerContext ctx, - ChannelEvent e) - throws Exception { - - logger.info(String.format("Received upstream event '%s'", e)); - } - } -} \ No newline at end of file diff --git a/transport/src/test/java/io/netty/channel/local/LocalChannelRegistryTest.java b/transport/src/test/java/io/netty/channel/local/LocalChannelRegistryTest.java new file mode 100644 index 0000000000..f5d149b7fc --- /dev/null +++ b/transport/src/test/java/io/netty/channel/local/LocalChannelRegistryTest.java @@ -0,0 +1,95 @@ +/* + * Copyright 2011 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.channel.local; + +import io.netty.bootstrap.Bootstrap; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelInboundHandlerContext; +import io.netty.channel.ChannelInboundMessageHandlerAdapter; +import io.netty.channel.ChannelInitializer; +import io.netty.logging.InternalLogger; +import io.netty.logging.InternalLoggerFactory; + +import org.junit.Assert; +import org.junit.Test; + +public class LocalChannelRegistryTest { + + private static final InternalLogger logger = + InternalLoggerFactory.getInstance(LocalChannelRegistryTest.class); + + private static String LOCAL_ADDR_ID = "test.id"; + + @Test + public void testLocalAddressReuse() throws Exception { + + for (int i = 0; i < 2; i ++) { + LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID); + Bootstrap cb = new Bootstrap(); + ServerBootstrap sb = new ServerBootstrap(); + + cb.eventLoop(new LocalEventLoop()) + .channel(new LocalChannel()) + .remoteAddress(addr) + .initializer(new ChannelInitializer() { + @Override + public void initChannel(LocalChannel ch) throws Exception { + ch.pipeline().addLast(new TestHandler()); + } + }); + + sb.eventLoop(new LocalEventLoop(), new LocalEventLoop()) + .channel(new LocalServerChannel()) + .localAddress(addr) + .childInitializer(new ChannelInitializer() { + @Override + public void initChannel(LocalChannel ch) throws Exception { + ch.pipeline().addLast(new TestHandler()); + } + }); + + + // Start server + sb.bind().sync(); + + // Connect to the server + Channel cc = cb.connect().sync().channel(); + + // Send a message event up the pipeline. + cc.pipeline().inbound().messageBuffer().add("Hello, World"); + cc.pipeline().fireInboundBufferUpdated(); + + // Close the channel + cc.close().sync(); + + sb.shutdown(); + cb.shutdown(); + + Assert.assertTrue(String.format( + "Expected null, got channel '%s' for local address '%s'", + LocalChannelRegistry.get(addr), addr), + LocalChannelRegistry.get(addr) == null); + } + } + + static class TestHandler extends ChannelInboundMessageHandlerAdapter { + @Override + public void messageReceived(ChannelInboundHandlerContext ctx, String msg) throws Exception { + logger.info(String.format("Received mesage: %s", msg)); + } + } +} \ No newline at end of file