Skip to content

Commit 342c1e9

Browse files
bizybotkcm
authored andcommitted
[TESTS] Set SO_LINGER and SO_REUSEADDR on the mock socket (#34211)
In SessionFactoryLoadBalancingTests#testRoundRobinWithFailures() we kill ldap servers randomly and immediately bind to that port connecting to mock server socket. This is done to avoid someone else listening to this port. As the creation of mock socket and binding to the port is immediate, sometimes the earlier socket would be in TIME_WAIT state thereby having problems with either bind or connect. This commit sets the SO_REUSEADDR explicitly to true and also sets the linger on time to 0(as we are not writing any data) so as to allow re-use of the port and close immediately. Note: I could not find other places where this might be problematic but looking at test runs and netstat output I do see lot of sockets in TIME_WAIT. If we find that this needs to be addressed we can wrap ServerSocketFactory to set these options and use that with in memory ldap server configuration during tests. Closes #32190
1 parent 18b2267 commit 342c1e9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactoryLoadBalancingTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
99
import com.unboundid.ldap.sdk.LDAPConnection;
1010
import org.elasticsearch.action.ActionListener;
11+
import org.elasticsearch.common.SuppressForbidden;
1112
import org.elasticsearch.common.settings.SecureString;
1213
import org.elasticsearch.common.settings.Settings;
1314
import org.elasticsearch.common.util.concurrent.ThreadContext;
@@ -17,6 +18,7 @@
1718
import org.elasticsearch.test.junit.annotations.TestLogging;
1819
import org.elasticsearch.threadpool.TestThreadPool;
1920
import org.elasticsearch.threadpool.ThreadPool;
21+
import org.elasticsearch.xpack.core.common.socket.SocketAccess;
2022
import org.elasticsearch.xpack.core.security.authc.RealmConfig;
2123
import org.elasticsearch.xpack.core.security.authc.ldap.support.LdapSearchScope;
2224
import org.elasticsearch.xpack.core.ssl.SSLService;
@@ -25,6 +27,7 @@
2527

2628
import java.io.IOException;
2729
import java.net.InetAddress;
30+
import java.net.InetSocketAddress;
2831
import java.net.Socket;
2932
import java.util.ArrayList;
3033
import java.util.Arrays;
@@ -112,7 +115,7 @@ public void testRoundRobinWithFailures() throws Exception {
112115
// of the ldap server and the opening of the socket
113116
logger.debug("opening mock server socket listening on [{}]", port);
114117
Runnable runnable = () -> {
115-
try (Socket socket = new MockSocket(InetAddress.getByName("localhost"), mockServerSocket.getLocalPort(), local, port)) {
118+
try (Socket socket = openMockSocket(local, mockServerSocket.getLocalPort(), local, port)) {
116119
logger.debug("opened socket [{}]", socket);
117120
latch.countDown();
118121
closeLatch.await();
@@ -149,6 +152,17 @@ public void testRoundRobinWithFailures() throws Exception {
149152
}
150153
}
151154

155+
@SuppressForbidden(reason = "Allow opening socket for test")
156+
private MockSocket openMockSocket(InetAddress remoteAddress, int remotePort, InetAddress localAddress, int localPort)
157+
throws IOException {
158+
final MockSocket socket = new MockSocket();
159+
socket.setReuseAddress(true); // allow binding even if the previous socket is in timed wait state.
160+
socket.setSoLinger(true, 0); // close immediately as we are not writing anything here.
161+
socket.bind(new InetSocketAddress(localAddress, localPort));
162+
SocketAccess.doPrivileged(() -> socket.connect(new InetSocketAddress(localAddress, remotePort)));
163+
return socket;
164+
}
165+
152166
public void testFailover() throws Exception {
153167
assumeTrue("at least one ldap server should be present for this test", ldapServers.length > 1);
154168
logger.debug("using [{}] ldap servers, urls {}", ldapServers.length, ldapUrls());

0 commit comments

Comments
 (0)