Search in sources :

Example 1 with CopyFunction

use of org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction in project OpenSearch by opensearch-project.

the class CoordinationMetadataTests method testVotingTombstoneSerializationEqualsHashCode.

public void testVotingTombstoneSerializationEqualsHashCode() {
    VotingConfigExclusion tombstone = new VotingConfigExclusion(randomAlphaOfLength(10), randomAlphaOfLength(10));
    // Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
    EqualsHashCodeTestUtils.checkEqualsAndHashCode(tombstone, (CopyFunction<VotingConfigExclusion>) orig -> OpenSearchTestCase.copyWriteable(orig, new NamedWriteableRegistry(Collections.emptyList()), VotingConfigExclusion::new), orig -> randomlyChangeVotingTombstone(orig));
}
Also used : VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) VotingConfiguration(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) BytesReference(org.opensearch.common.bytes.BytesReference) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) IOException(java.io.IOException) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) HashSet(java.util.HashSet) Sets(org.opensearch.common.util.set.Sets) Matchers.equalTo(org.hamcrest.Matchers.equalTo) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) EqualsHashCodeTestUtils(org.opensearch.test.EqualsHashCodeTestUtils) CopyFunction(org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction) Collections(java.util.Collections) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry)

Example 2 with CopyFunction

use of org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction in project OpenSearch by opensearch-project.

the class CoordinationMetadataTests method testCoordinationMetadataSerializationEqualsHashCode.

public void testCoordinationMetadataSerializationEqualsHashCode() {
    CoordinationMetadata initialMetadata = new CoordinationMetadata(randomNonNegativeLong(), randomVotingConfig(), randomVotingConfig(), randomVotingTombstones());
    // Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
    EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialMetadata, (CopyFunction<CoordinationMetadata>) orig -> OpenSearchTestCase.copyWriteable(orig, new NamedWriteableRegistry(Collections.emptyList()), CoordinationMetadata::new), meta -> {
        CoordinationMetadata.Builder builder = CoordinationMetadata.builder(meta);
        switch(randomInt(3)) {
            case 0:
                builder.term(randomValueOtherThan(meta.term(), OpenSearchTestCase::randomNonNegativeLong));
                break;
            case 1:
                builder.lastCommittedConfiguration(randomlyChangeVotingConfiguration(meta.getLastCommittedConfiguration()));
                break;
            case 2:
                builder.lastAcceptedConfiguration(randomlyChangeVotingConfiguration(meta.getLastAcceptedConfiguration()));
                break;
            case 3:
                if (meta.getVotingConfigExclusions().isEmpty() == false && randomBoolean()) {
                    builder.clearVotingConfigExclusions();
                } else {
                    randomVotingTombstones().forEach(dn -> builder.addVotingConfigExclusion(dn));
                }
                break;
        }
        return builder.build();
    });
}
Also used : VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) VotingConfiguration(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) BytesReference(org.opensearch.common.bytes.BytesReference) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) IOException(java.io.IOException) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) HashSet(java.util.HashSet) Sets(org.opensearch.common.util.set.Sets) Matchers.equalTo(org.hamcrest.Matchers.equalTo) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) EqualsHashCodeTestUtils(org.opensearch.test.EqualsHashCodeTestUtils) CopyFunction(org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction) Collections(java.util.Collections) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry)

Example 3 with CopyFunction

use of org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction in project OpenSearch by opensearch-project.

the class PeerFinderMessagesTests method testPeersResponseEqualsHashCodeSerialization.

public void testPeersResponseEqualsHashCodeSerialization() {
    final long initialTerm = randomNonNegativeLong();
    final PeersResponse initialPeersResponse;
    if (randomBoolean()) {
        initialPeersResponse = new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), emptyList(), initialTerm);
    } else {
        initialPeersResponse = new PeersResponse(Optional.empty(), Arrays.stream(generateRandomStringArray(10, 10, false, false)).map(this::createNode).collect(Collectors.toList()), initialTerm);
    }
    // Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
    EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialPeersResponse, (CopyFunction<PeersResponse>) publishResponse -> copyWriteable(publishResponse, writableRegistry(), PeersResponse::new), in -> {
        final long term = in.getTerm();
        if (randomBoolean()) {
            return new PeersResponse(in.getMasterNode(), in.getKnownPeers(), randomValueOtherThan(term, OpenSearchTestCase::randomNonNegativeLong));
        } else {
            if (in.getMasterNode().isPresent()) {
                if (randomBoolean()) {
                    return new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), in.getKnownPeers(), term);
                } else {
                    return new PeersResponse(Optional.empty(), singletonList(createNode(randomAlphaOfLength(10))), term);
                }
            } else {
                if (randomBoolean()) {
                    return new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), emptyList(), term);
                } else {
                    return new PeersResponse(in.getMasterNode(), modifyDiscoveryNodesList(in.getKnownPeers(), false), term);
                }
            }
        }
    });
}
Also used : Arrays(java.util.Arrays) Collections.emptyList(java.util.Collections.emptyList) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) Version(org.opensearch.Version) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) List(java.util.List) Optional(java.util.Optional) PeersResponse(org.opensearch.cluster.coordination.PeersResponse) EqualsHashCodeTestUtils(org.opensearch.test.EqualsHashCodeTestUtils) CopyFunction(org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction) PeersResponse(org.opensearch.cluster.coordination.PeersResponse)

Example 4 with CopyFunction

use of org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction in project OpenSearch by opensearch-project.

the class LeaderCheckerTests method testLeaderCheckRequestEqualsHashcodeSerialization.

public void testLeaderCheckRequestEqualsHashcodeSerialization() {
    LeaderCheckRequest request = new LeaderCheckRequest(new DiscoveryNode(randomAlphaOfLength(10), buildNewFakeTransportAddress(), Version.CURRENT));
    // noinspection RedundantCast since it is needed for some IDEs (specifically Eclipse 4.8.0) to infer the right type
    EqualsHashCodeTestUtils.checkEqualsAndHashCode(request, (CopyFunction<LeaderCheckRequest>) rq -> copyWriteable(rq, writableRegistry(), LeaderCheckRequest::new), rq -> new LeaderCheckRequest(new DiscoveryNode(randomAlphaOfLength(10), buildNewFakeTransportAddress(), Version.CURRENT)));
}
Also used : LeaderCheckRequest(org.opensearch.cluster.coordination.LeaderChecker.LeaderCheckRequest) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) LEADER_CHECK_ACTION_NAME(org.opensearch.cluster.coordination.LeaderChecker.LEADER_CHECK_ACTION_NAME) HANDSHAKE_ACTION_NAME(org.opensearch.transport.TransportService.HANDSHAKE_ACTION_NAME) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) OpenSearchException(org.opensearch.OpenSearchException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.matchesRegex(org.hamcrest.Matchers.matchesRegex) LEADER_CHECK_TIMEOUT_SETTING(org.opensearch.cluster.coordination.LeaderChecker.LEADER_CHECK_TIMEOUT_SETTING) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransport(org.opensearch.test.transport.MockTransport) Matchers.nullValue(org.hamcrest.Matchers.nullValue) LEADER_CHECK_RETRY_COUNT_SETTING(org.opensearch.cluster.coordination.LeaderChecker.LEADER_CHECK_RETRY_COUNT_SETTING) LEADER_CHECK_INTERVAL_SETTING(org.opensearch.cluster.coordination.LeaderChecker.LEADER_CHECK_INTERVAL_SETTING) EqualsHashCodeTestUtils(org.opensearch.test.EqualsHashCodeTestUtils) UNHEALTHY(org.opensearch.monitor.StatusInfo.Status.UNHEALTHY) StreamInput(org.opensearch.common.io.stream.StreamInput) Empty(org.opensearch.transport.TransportResponse.Empty) TransportRequest(org.opensearch.transport.TransportRequest) Collections.emptySet(java.util.Collections.emptySet) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) HEALTHY(org.opensearch.monitor.StatusInfo.Status.HEALTHY) Settings(org.opensearch.common.settings.Settings) TransportResponse(org.opensearch.transport.TransportResponse) TransportService(org.opensearch.transport.TransportService) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) AtomicLong(java.util.concurrent.atomic.AtomicLong) NODE_NAME_SETTING(org.opensearch.node.Node.NODE_NAME_SETTING) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ClusterName(org.opensearch.cluster.ClusterName) StatusInfo(org.opensearch.monitor.StatusInfo) ConnectTransportException(org.opensearch.transport.ConnectTransportException) CopyFunction(org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction) Matchers.anyOf(org.hamcrest.Matchers.anyOf) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Matchers.endsWith(org.hamcrest.Matchers.endsWith) TransportException(org.opensearch.transport.TransportException) Names(org.opensearch.threadpool.ThreadPool.Names) NOOP_TRANSPORT_INTERCEPTOR(org.opensearch.transport.TransportService.NOOP_TRANSPORT_INTERCEPTOR) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LeaderCheckRequest(org.opensearch.cluster.coordination.LeaderChecker.LeaderCheckRequest)

Example 5 with CopyFunction

use of org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction in project OpenSearch by opensearch-project.

the class CoordinationMetadataTests method testVotingConfigurationSerializationEqualsHashCode.

public void testVotingConfigurationSerializationEqualsHashCode() {
    VotingConfiguration initialConfig = randomVotingConfig();
    // Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
    EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialConfig, (CopyFunction<VotingConfiguration>) orig -> OpenSearchTestCase.copyWriteable(orig, new NamedWriteableRegistry(Collections.emptyList()), VotingConfiguration::new), cfg -> randomlyChangeVotingConfiguration(cfg));
}
Also used : VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) VotingConfiguration(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) BytesReference(org.opensearch.common.bytes.BytesReference) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) IOException(java.io.IOException) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) HashSet(java.util.HashSet) Sets(org.opensearch.common.util.set.Sets) Matchers.equalTo(org.hamcrest.Matchers.equalTo) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) EqualsHashCodeTestUtils(org.opensearch.test.EqualsHashCodeTestUtils) CopyFunction(org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction) Collections(java.util.Collections) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) VotingConfiguration(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)

Aggregations

EqualsHashCodeTestUtils (org.opensearch.test.EqualsHashCodeTestUtils)5 CopyFunction (org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction)5 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)5 Matchers.equalTo (org.hamcrest.Matchers.equalTo)4 IOException (java.io.IOException)3 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 VotingConfigExclusion (org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion)3 VotingConfiguration (org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration)3 BytesReference (org.opensearch.common.bytes.BytesReference)3 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)3 Sets (org.opensearch.common.util.set.Sets)3 ToXContent (org.opensearch.common.xcontent.ToXContent)3 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 JsonXContent (org.opensearch.common.xcontent.json.JsonXContent)3 Version (org.opensearch.Version)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2 ArrayList (java.util.ArrayList)1