Search in sources :

Example 1 with MockSocket

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;
}
Also used : CancellableThreads(org.elasticsearch.common.util.CancellableThreads) Socket(java.net.Socket) BufferedInputStream(java.io.BufferedInputStream) BigArrays(org.elasticsearch.common.util.BigArrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) BufferedOutputStream(java.io.BufferedOutputStream) ServerSocket(java.net.ServerSocket) HashSet(java.util.HashSet) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NetworkService(org.elasticsearch.common.network.NetworkService) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Settings(org.elasticsearch.common.settings.Settings) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) TimeValue(org.elasticsearch.common.unit.TimeValue) SocketTimeoutException(java.net.SocketTimeoutException) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ExecutorService(java.util.concurrent.ExecutorService) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) OutputStream(java.io.OutputStream) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) MockSocket(org.elasticsearch.mocksocket.MockSocket) Executor(java.util.concurrent.Executor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOUtils(org.apache.lucene.util.IOUtils) Set(java.util.Set) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) InetSocketAddress(java.net.InetSocketAddress) Executors(java.util.concurrent.Executors) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) List(java.util.List) Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Closeable(java.io.Closeable) Collections(java.util.Collections) MockSocket(org.elasticsearch.mocksocket.MockSocket) SocketTimeoutException(java.net.SocketTimeoutException) InetSocketAddress(java.net.InetSocketAddress) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 2 with MockSocket

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"));
        }
    }
}
Also used : MockSocket(org.elasticsearch.mocksocket.MockSocket) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) MockSocket(org.elasticsearch.mocksocket.MockSocket)

Example 3 with MockSocket

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());
    }
}
Also used : MockSocket(org.elasticsearch.mocksocket.MockSocket) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) InetAddress(java.net.InetAddress) URL(java.net.URL) Socket(java.net.Socket) MockSocket(org.elasticsearch.mocksocket.MockSocket)

Example 4 with MockSocket

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));
    }
}
Also used : MockSocket(org.elasticsearch.mocksocket.MockSocket) Socket(java.net.Socket) MockSocket(org.elasticsearch.mocksocket.MockSocket)

Aggregations

Socket (java.net.Socket)4 MockSocket (org.elasticsearch.mocksocket.MockSocket)4 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URL (java.net.URL)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1