Ensure EpollSocketChannel.localAddress() returns correct address after connect(...) call.
Motivation: We missed to correctly retrieve the localAddress() after we called Socket.connect(..) and so the user would always see an incorrect address when calling EpollSocketChannel.localAddress(). Modifications: - Ensure we always retrieve the localAddress() after we called Socket.connect(...) as only after this we will be able to receive the correct address. - Add unit test Result: Correct and consistent behaviour across different transports (NIO/OIO/EPOLL).
This commit is contained in:
parent
87b8bdbbdd
commit
8617df7f2d
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016 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.testsuite.transport.socket;
|
||||||
|
|
||||||
|
import io.netty.bootstrap.Bootstrap;
|
||||||
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.channel.Channel;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
import io.netty.util.concurrent.ImmediateEventExecutor;
|
||||||
|
import io.netty.util.concurrent.Promise;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class SocketConnectTest extends AbstractSocketTest {
|
||||||
|
|
||||||
|
@Test(timeout = 30000)
|
||||||
|
public void testLocalAddressAfterConnect() throws Throwable {
|
||||||
|
run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testLocalAddressAfterConnect(ServerBootstrap sb, Bootstrap cb) throws Throwable {
|
||||||
|
Channel serverChannel = null;
|
||||||
|
Channel clientChannel = null;
|
||||||
|
try {
|
||||||
|
final Promise<InetSocketAddress> localAddressPromise = ImmediateEventExecutor.INSTANCE.newPromise();
|
||||||
|
serverChannel = sb.childHandler(new ChannelInboundHandlerAdapter() {
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
localAddressPromise.setSuccess((InetSocketAddress) ctx.channel().localAddress());
|
||||||
|
}
|
||||||
|
}).bind().syncUninterruptibly().channel();
|
||||||
|
|
||||||
|
clientChannel = cb.handler(new ChannelInboundHandlerAdapter()).register().syncUninterruptibly().channel();
|
||||||
|
|
||||||
|
assertNull(clientChannel.localAddress());
|
||||||
|
assertNull(clientChannel.remoteAddress());
|
||||||
|
|
||||||
|
clientChannel.connect(serverChannel.localAddress()).syncUninterruptibly().channel();
|
||||||
|
assertLocalAddress((InetSocketAddress) clientChannel.localAddress());
|
||||||
|
assertNotNull(clientChannel.remoteAddress());
|
||||||
|
|
||||||
|
assertLocalAddress(localAddressPromise.get());
|
||||||
|
} finally {
|
||||||
|
if (clientChannel != null) {
|
||||||
|
clientChannel.close().syncUninterruptibly();
|
||||||
|
}
|
||||||
|
if (serverChannel != null) {
|
||||||
|
serverChannel.close().syncUninterruptibly();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertLocalAddress(InetSocketAddress address) {
|
||||||
|
assertTrue(address.getPort() > 0);
|
||||||
|
assertFalse(address.getAddress().isAnyLocalAddress());
|
||||||
|
}
|
||||||
|
}
|
@ -179,21 +179,21 @@ public final class EpollSocketChannel extends AbstractEpollStreamChannel impleme
|
|||||||
if (localAddress != null) {
|
if (localAddress != null) {
|
||||||
checkResolvable((InetSocketAddress) localAddress);
|
checkResolvable((InetSocketAddress) localAddress);
|
||||||
}
|
}
|
||||||
// We always need to set the localAddress even if not connected yet
|
InetSocketAddress remoteAddr = (InetSocketAddress) remoteAddress;
|
||||||
|
checkResolvable(remoteAddr);
|
||||||
|
|
||||||
|
boolean connected = super.doConnect(remoteAddress, localAddress);
|
||||||
|
if (connected) {
|
||||||
|
remote = computeRemoteAddr(remoteAddr, fd().remoteAddress());
|
||||||
|
} else {
|
||||||
|
// Store for later usage in doFinishConnect()
|
||||||
|
requestedRemote = remoteAddr;
|
||||||
|
}
|
||||||
|
// We always need to set the localAddress even if not connected yet as the bind already took place.
|
||||||
//
|
//
|
||||||
// See https://github.com/netty/netty/issues/3463
|
// See https://github.com/netty/netty/issues/3463
|
||||||
local = fd().localAddress();
|
local = fd().localAddress();
|
||||||
|
return connected;
|
||||||
InetSocketAddress remoteAddr = (InetSocketAddress) remoteAddress;
|
|
||||||
checkResolvable(remoteAddr);
|
|
||||||
if (super.doConnect(remoteAddress, localAddress)) {
|
|
||||||
remote = computeRemoteAddr(remoteAddr, fd().remoteAddress());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store for later usage in doFinishConnect()
|
|
||||||
requestedRemote = remoteAddr;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class EpollSocketChannelUnsafe extends EpollStreamUnsafe {
|
private final class EpollSocketChannelUnsafe extends EpollStreamUnsafe {
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016 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.epoll;
|
||||||
|
|
||||||
|
import io.netty.bootstrap.Bootstrap;
|
||||||
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.testsuite.transport.TestsuitePermutation;
|
||||||
|
import io.netty.testsuite.transport.socket.SocketConnectTest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EpollSocketConnectTest extends SocketConnectTest {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> newFactories() {
|
||||||
|
return EpollSocketTestPermutation.INSTANCE.socket();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user