From fd001a5c80b9cab9ca8ac75e6490e8a250ef0456 Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Tue, 20 Jun 2017 19:38:06 +0200 Subject: [PATCH 1/2] move disconnects simulation to just close connections --- .../test/transport/MockTransportService.java | 37 ++----------------- 1 file changed, 4 insertions(+), 33 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java b/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java index 467b2c7f3c8fd..3ad8fb9634324 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java +++ b/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java @@ -221,7 +221,9 @@ public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfil @Override protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException { - simulateDisconnect(connection, original, "DISCONNECT: simulated"); + connection.close(); + // send the request, which will blow up + connection.sendRequest(requestId, action, request, options); } }); } @@ -268,7 +270,7 @@ protected void sendRequest(Connection connection, long requestId, String action, TransportRequestOptions options) throws IOException { if (blockedActions.contains(action)) { logger.info("--> preventing {} request", action); - simulateDisconnect(connection, original, "DISCONNECT: prevented " + action + " request"); + connection.close(); } connection.sendRequest(requestId, action, request, options); } @@ -449,37 +451,6 @@ private LookupTestTransport transport() { return (LookupTestTransport) transport; } - /** - * simulates a disconnect by disconnecting from the underlying transport and throwing a - * {@link ConnectTransportException} - */ - private void simulateDisconnect(DiscoveryNode node, Transport transport, String reason) { - simulateDisconnect(node, transport, reason, null); - } - - /** - * simulates a disconnect by disconnecting from the underlying transport and throwing a - * {@link ConnectTransportException}, due to a specific cause exception - */ - private void simulateDisconnect(DiscoveryNode node, Transport transport, String reason, @Nullable Throwable e) { - if (transport.nodeConnected(node)) { - // this a connected node, disconnecting from it will be up the exception - transport.disconnectFromNode(node); - } else { - throw new ConnectTransportException(node, reason, e); - } - } - - /** - * simulates a disconnect by closing the connection and throwing a - * {@link ConnectTransportException} - */ - private void simulateDisconnect(Transport.Connection connection, Transport transport, String reason) throws IOException { - connection.close(); - simulateDisconnect(connection.getNode(), transport, reason); - } - - /** * A lookup transport that has a list of potential Transport implementations to delegate to for node operations, * if none is registered, then the default one is used. From a4dc90b584d93fd96e953fd23b65e44202f8e32f Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Tue, 20 Jun 2017 20:22:12 +0200 Subject: [PATCH 2/2] adapt MockTransportService to the world of connections --- .../test/transport/MockTransportService.java | 39 +++++++++++++++---- .../AbstractSimpleTransportTestCase.java | 16 +------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java b/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java index 3ad8fb9634324..25525de7fbf46 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java +++ b/test/framework/src/main/java/org/elasticsearch/test/transport/MockTransportService.java @@ -218,6 +218,11 @@ public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfil } } + @Override + public Connection openConnection(DiscoveryNode node, ConnectionProfile profile) throws IOException { + throw new ConnectTransportException(node, "DISCONNECT: simulated"); + } + @Override protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException { @@ -258,13 +263,6 @@ public void addFailToSendNoConnectRule(TransportAddress transportAddress, final addDelegate(transportAddress, new DelegateTransport(original) { - @Override - public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfile, - CheckedBiConsumer connectionValidator) - throws ConnectTransportException { - original.connectToNode(node, connectionProfile, connectionValidator); - } - @Override protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException { @@ -304,6 +302,11 @@ public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfil } } + @Override + public Connection openConnection(DiscoveryNode node, ConnectionProfile profile) throws IOException { + throw new ConnectTransportException(node, "UNRESPONSIVE: simulated"); + } + @Override protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException { @@ -370,6 +373,28 @@ public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfil } } + @Override + public Connection openConnection(DiscoveryNode node, ConnectionProfile profile) throws IOException { + TimeValue delay = getDelay(); + if (delay.millis() <= 0) { + return original.openConnection(node, profile); + } + + // TODO: Replace with proper setting + TimeValue connectingTimeout = NetworkService.TcpSettings.TCP_CONNECT_TIMEOUT.getDefault(Settings.EMPTY); + try { + if (delay.millis() < connectingTimeout.millis()) { + Thread.sleep(delay.millis()); + return original.openConnection(node, profile); + } else { + Thread.sleep(connectingTimeout.millis()); + throw new ConnectTransportException(node, "UNRESPONSIVE: simulated"); + } + } catch (InterruptedException e) { + throw new ConnectTransportException(node, "UNRESPONSIVE: simulated"); + } + } + @Override protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException { diff --git a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java index 206cfeeb62ecd..7c0070e0f967f 100644 --- a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java @@ -41,7 +41,6 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; -import org.elasticsearch.common.util.concurrent.CountDown; import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; import org.elasticsearch.mocksocket.MockServerSocket; import org.elasticsearch.node.Node; @@ -61,7 +60,6 @@ import java.net.Socket; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -1463,12 +1461,7 @@ public void handleException(TransportException exp) { // all is well } - try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)) { - serviceB.handshake(connection, 100); - fail("exception should be thrown"); - } catch (IllegalStateException e) { - // all is well - } + expectThrows(ConnectTransportException.class, () -> serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)); } public void testMockUnresponsiveRule() throws IOException { @@ -1519,12 +1512,7 @@ public void handleException(TransportException exp) { // all is well } - try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)) { - serviceB.handshake(connection, 100); - fail("exception should be thrown"); - } catch (IllegalStateException e) { - // all is well - } + expectThrows(ConnectTransportException.class, () -> serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)); }