use of org.elasticsearch.mocksocket.MockSocket in project elasticsearch by elastic.
the class MockTcpTransport method connectToChannels.
@Override
protected NodeChannels connectToChannels(DiscoveryNode node, ConnectionProfile profile) throws IOException {
final MockChannel[] mockChannels = new MockChannel[1];
// we always use light here
final NodeChannels nodeChannels = new NodeChannels(node, mockChannels, LIGHT_PROFILE);
boolean success = false;
final MockSocket socket = new MockSocket();
try {
Consumer<MockChannel> onClose = (channel) -> {
final NodeChannels connected = connectedNodes.get(node);
if (connected != null && connected.hasChannel(channel)) {
try {
executor.execute(() -> {
disconnectFromNode(node, channel, "channel closed event");
});
} catch (RejectedExecutionException ex) {
logger.debug("failed to run disconnectFromNode - node is shutting down");
}
}
};
final InetSocketAddress address = node.getAddress().address();
// we just use a single connections
configureSocket(socket);
final TimeValue connectTimeout = profile.getConnectTimeout();
try {
socket.connect(address, Math.toIntExact(connectTimeout.millis()));
} catch (SocketTimeoutException ex) {
throw new ConnectTransportException(node, "connect_timeout[" + connectTimeout + "]", ex);
}
MockChannel channel = new MockChannel(socket, address, "none", onClose);
channel.loopRead(executor);
mockChannels[0] = channel;
success = true;
} finally {
if (success == false) {
IOUtils.close(nodeChannels, socket);
}
}
return nodeChannels;
}
use of org.elasticsearch.mocksocket.MockSocket in project elasticsearch by elastic.
the class Netty4SizeHeaderFrameDecoderTests method testThatTextMessageIsReturnedOnHTTPLikeRequest.
public void testThatTextMessageIsReturnedOnHTTPLikeRequest() throws Exception {
String randomMethod = randomFrom("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH");
String data = randomMethod + " / HTTP/1.1";
try (Socket socket = new MockSocket(host, port)) {
socket.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
socket.getOutputStream().flush();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))) {
assertThat(reader.readLine(), is("This is not a HTTP port"));
}
}
}
use of org.elasticsearch.mocksocket.MockSocket in project elasticsearch by elastic.
the class ExampleExternalIT method testExample.
public void testExample() throws Exception {
String stringAddress = Objects.requireNonNull(System.getProperty("external.address"));
URL url = new URL("http://" + stringAddress);
InetAddress address = InetAddress.getByName(url.getHost());
try (Socket socket = new MockSocket(address, url.getPort());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))) {
assertEquals("TEST", reader.readLine());
}
}
use of org.elasticsearch.mocksocket.MockSocket in project elasticsearch by elastic.
the class Netty4SizeHeaderFrameDecoderTests method testThatNothingIsReturnedForOtherInvalidPackets.
public void testThatNothingIsReturnedForOtherInvalidPackets() throws Exception {
try (Socket socket = new MockSocket(host, port)) {
socket.getOutputStream().write("FOOBAR".getBytes(StandardCharsets.UTF_8));
socket.getOutputStream().flush();
// end of stream
assertThat(socket.getInputStream().read(), is(-1));
}
}
Aggregations