Search in sources :

Example 11 with MockTransport

use of org.elasticsearch.test.transport.MockTransport in project crate by crate.

the class PreVoteCollectorTests method createObjects.

@Before
public void createObjects() {
    Settings settings = Settings.builder().put(NODE_NAME_SETTING.getKey(), "node").build();
    deterministicTaskQueue = new DeterministicTaskQueue(settings, random());
    final MockTransport mockTransport = new MockTransport() {

        @Override
        protected void onSendRequest(final long requestId, final String action, final TransportRequest request, final DiscoveryNode node) {
            super.onSendRequest(requestId, action, request, node);
            assertThat(action, is(REQUEST_PRE_VOTE_ACTION_NAME));
            assertThat(request, instanceOf(PreVoteRequest.class));
            assertThat(node, not(equalTo(localNode)));
            PreVoteRequest preVoteRequest = (PreVoteRequest) request;
            assertThat(preVoteRequest.getSourceNode(), equalTo(localNode));
            deterministicTaskQueue.scheduleNow(new Runnable() {

                @Override
                public void run() {
                    final PreVoteResponse response = responsesByNode.get(node);
                    if (response == null) {
                        handleRemoteError(requestId, new ConnectTransportException(node, "no response"));
                    } else {
                        handleResponse(requestId, response);
                    }
                }

                @Override
                public String toString() {
                    return "response to " + request + " from " + node;
                }
            });
        }
    };
    lastAcceptedTerm = randomNonNegativeLong();
    currentTerm = randomLongBetween(lastAcceptedTerm, Long.MAX_VALUE);
    lastAcceptedVersion = randomNonNegativeLong();
    localNode = new DiscoveryNode("local-node", buildNewFakeTransportAddress(), Version.CURRENT);
    responsesByNode.put(localNode, new PreVoteResponse(currentTerm, lastAcceptedTerm, lastAcceptedVersion));
    transportService = mockTransport.createTransportService(settings, deterministicTaskQueue.getThreadPool(), boundTransportAddress -> localNode, null);
    transportService.start();
    transportService.acceptIncomingRequests();
    preVoteCollector = new PreVoteCollector(transportService, () -> {
        assert electionOccurred == false;
        electionOccurred = true;
    }, l -> {
    });
    // TODO need tests that check that the max term seen is updated
    preVoteCollector.update(getLocalPreVoteResponse(), null);
}
Also used : Arrays(java.util.Arrays) TransportRequest(org.elasticsearch.transport.TransportRequest) SAME(org.elasticsearch.threadpool.ThreadPool.Names.SAME) Matchers.not(org.hamcrest.Matchers.not) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) VotingConfiguration(org.elasticsearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) Releasable(org.elasticsearch.common.lease.Releasable) Before(org.junit.Before) REQUEST_PRE_VOTE_ACTION_NAME(org.elasticsearch.cluster.coordination.PreVoteCollector.REQUEST_PRE_VOTE_ACTION_NAME) MockTransport(org.elasticsearch.test.transport.MockTransport) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) Version(org.elasticsearch.Version) NODE_NAME_SETTING(org.elasticsearch.node.Node.NODE_NAME_SETTING) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) Matchers.is(org.hamcrest.Matchers.is) TransportException(org.elasticsearch.transport.TransportException) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportRequest(org.elasticsearch.transport.TransportRequest) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) MockTransport(org.elasticsearch.test.transport.MockTransport) Settings(org.elasticsearch.common.settings.Settings) Before(org.junit.Before)

Aggregations

DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)11 Settings (org.elasticsearch.common.settings.Settings)11 MockTransport (org.elasticsearch.test.transport.MockTransport)11 TransportService (org.elasticsearch.transport.TransportService)11 TransportRequest (org.elasticsearch.transport.TransportRequest)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 TransportException (org.elasticsearch.transport.TransportException)6 Version (org.elasticsearch.Version)5 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)5 StreamInput (org.elasticsearch.common.io.stream.StreamInput)5 TransportResponseHandler (org.elasticsearch.transport.TransportResponseHandler)5 Before (org.junit.Before)5 IOException (java.io.IOException)4 Collections.emptyMap (java.util.Collections.emptyMap)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 TimeValue (io.crate.common.unit.TimeValue)3 Collections.emptySet (java.util.Collections.emptySet)3