Search in sources :

Example 6 with MockTransportClient

use of org.elasticsearch.transport.MockTransportClient in project elasticsearch by elastic.

the class SearchRequestBuilderTests method initClient.

@BeforeClass
public static void initClient() {
    //this client will not be hit by any request, but it needs to be a non null proper client
    //that is why we create it but we don't add any transport address to it
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    client = new MockTransportClient(settings);
}
Also used : MockTransportClient(org.elasticsearch.transport.MockTransportClient) Settings(org.elasticsearch.common.settings.Settings) BeforeClass(org.junit.BeforeClass)

Example 7 with MockTransportClient

use of org.elasticsearch.transport.MockTransportClient in project elasticsearch by elastic.

the class TransportClientTests method testThatUsingAClosedClientThrowsAnException.

public void testThatUsingAClosedClientThrowsAnException() throws ExecutionException, InterruptedException {
    final TransportClient client = new MockTransportClient(Settings.EMPTY);
    client.close();
    final IllegalStateException e = expectThrows(IllegalStateException.class, () -> client.admin().cluster().health(new ClusterHealthRequest()).get());
    assertThat(e, hasToString(containsString("transport client is closed")));
}
Also used : MockTransportClient(org.elasticsearch.transport.MockTransportClient) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) MockTransportClient(org.elasticsearch.transport.MockTransportClient)

Example 8 with MockTransportClient

use of org.elasticsearch.transport.MockTransportClient in project elasticsearch by elastic.

the class NodeDisconnectIT method testNotifyOnDisconnectInSniffer.

public void testNotifyOnDisconnectInSniffer() throws IOException {
    internalCluster().ensureAtLeastNumDataNodes(2);
    final Set<DiscoveryNode> disconnectedNodes = Collections.synchronizedSet(new HashSet<>());
    try (TransportClient client = new MockTransportClient(Settings.builder().put("cluster.name", internalCluster().getClusterName()).build(), Collections.emptySet(), (n, e) -> disconnectedNodes.add(n))) {
        int numNodes = 0;
        for (TransportService service : internalCluster().getInstances(TransportService.class)) {
            numNodes++;
            client.addTransportAddress(service.boundAddress().publishAddress());
        }
        Set<TransportAddress> discoveryNodes = client.connectedNodes().stream().map(n -> n.getAddress()).collect(Collectors.toSet());
        assertEquals(numNodes, discoveryNodes.size());
        assertEquals(0, disconnectedNodes.size());
        internalCluster().stopRandomDataNode();
        client.getNodesService().doSample();
        assertEquals(1, disconnectedNodes.size());
        assertTrue(discoveryNodes.contains(disconnectedNodes.stream().findAny().get().getAddress()));
    }
    assertEquals(1, disconnectedNodes.size());
}
Also used : HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportClient(org.elasticsearch.transport.MockTransportClient) Settings(org.elasticsearch.common.settings.Settings) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ESIntegTestCase(org.elasticsearch.test.ESIntegTestCase) CLIENT_TRANSPORT_NODES_SAMPLER_INTERVAL(org.elasticsearch.client.transport.TransportClient.CLIENT_TRANSPORT_NODES_SAMPLER_INTERVAL) Set(java.util.Set) IOException(java.io.IOException) TransportService(org.elasticsearch.transport.TransportService) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportClient(org.elasticsearch.transport.MockTransportClient) TransportService(org.elasticsearch.transport.TransportService) TransportAddress(org.elasticsearch.common.transport.TransportAddress) MockTransportClient(org.elasticsearch.transport.MockTransportClient)

Example 9 with MockTransportClient

use of org.elasticsearch.transport.MockTransportClient in project elasticsearch by elastic.

the class NodeDisconnectIT method testNotifyOnDisconnect.

public void testNotifyOnDisconnect() throws IOException {
    internalCluster().ensureAtLeastNumDataNodes(2);
    final Set<DiscoveryNode> disconnectedNodes = Collections.synchronizedSet(new HashSet<>());
    try (TransportClient client = new MockTransportClient(Settings.builder().put("cluster.name", internalCluster().getClusterName()).put(CLIENT_TRANSPORT_NODES_SAMPLER_INTERVAL.getKey(), // disable sniffing for better control
    "1h").build(), Collections.emptySet(), (n, e) -> disconnectedNodes.add(n))) {
        for (TransportService service : internalCluster().getInstances(TransportService.class)) {
            client.addTransportAddress(service.boundAddress().publishAddress());
        }
        internalCluster().stopRandomDataNode();
        for (int i = 0; i < 20; i++) {
            // fire up requests such that we hit the node and pass it to the listener
            client.admin().cluster().prepareState().get();
        }
        assertEquals(1, disconnectedNodes.size());
    }
    assertEquals(1, disconnectedNodes.size());
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportClient(org.elasticsearch.transport.MockTransportClient) TransportService(org.elasticsearch.transport.TransportService) MockTransportClient(org.elasticsearch.transport.MockTransportClient)

Example 10 with MockTransportClient

use of org.elasticsearch.transport.MockTransportClient in project elasticsearch by elastic.

the class TransportClientHeadersTests method buildClient.

@Override
protected Client buildClient(Settings headersSettings, GenericAction[] testedActions) {
    transportService = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null);
    transportService.start();
    transportService.acceptIncomingRequests();
    TransportClient client = new MockTransportClient(Settings.builder().put("client.transport.sniff", false).put("cluster.name", "cluster1").put("node.name", "transport_client_" + this.getTestName()).put(headersSettings).build(), InternalTransportServiceInterceptor.TestPlugin.class);
    InternalTransportServiceInterceptor.TestPlugin plugin = client.injector.getInstance(PluginsService.class).filterPlugins(InternalTransportServiceInterceptor.TestPlugin.class).stream().findFirst().get();
    plugin.instance.threadPool = client.threadPool();
    plugin.instance.address = transportService.boundAddress().publishAddress();
    client.addTransportAddress(transportService.boundAddress().publishAddress());
    return client;
}
Also used : MockTransportClient(org.elasticsearch.transport.MockTransportClient) MockTransportClient(org.elasticsearch.transport.MockTransportClient)

Aggregations

MockTransportClient (org.elasticsearch.transport.MockTransportClient)10 Settings (org.elasticsearch.common.settings.Settings)6 TransportAddress (org.elasticsearch.common.transport.TransportAddress)3 TransportService (org.elasticsearch.transport.TransportService)3 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Collectors (java.util.stream.Collectors)1 ClusterHealthRequest (org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)1 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)1 ClusterStateRequest (org.elasticsearch.action.admin.cluster.state.ClusterStateRequest)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 PlainListenableActionFuture (org.elasticsearch.action.support.PlainListenableActionFuture)1 Client (org.elasticsearch.client.Client)1 TransportClient (org.elasticsearch.client.transport.TransportClient)1 CLIENT_TRANSPORT_NODES_SAMPLER_INTERVAL (org.elasticsearch.client.transport.TransportClient.CLIENT_TRANSPORT_NODES_SAMPLER_INTERVAL)1 ClusterState (org.elasticsearch.cluster.ClusterState)1