|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.discovery; |
| 21 | + |
| 22 | +import org.elasticsearch.Version; |
| 23 | +import org.elasticsearch.cluster.coordination.PeersResponse; |
| 24 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 25 | +import org.elasticsearch.test.ESTestCase; |
| 26 | +import org.elasticsearch.test.EqualsHashCodeTestUtils; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Optional; |
| 33 | +import java.util.stream.Collectors; |
| 34 | + |
| 35 | +import static java.util.Collections.emptyList; |
| 36 | +import static java.util.Collections.singletonList; |
| 37 | + |
| 38 | +public class PeerFinderMessagesTests extends ESTestCase { |
| 39 | + private DiscoveryNode createNode(String id) { |
| 40 | + return new DiscoveryNode(id, buildNewFakeTransportAddress(), Version.CURRENT); |
| 41 | + } |
| 42 | + |
| 43 | + public void testPeersRequestEqualsHashCodeSerialization() { |
| 44 | + final PeersRequest initialPeersRequest = new PeersRequest(createNode(randomAlphaOfLength(10)), |
| 45 | + Arrays.stream(generateRandomStringArray(10, 10, false)).map(this::createNode).collect(Collectors.toList())); |
| 46 | + |
| 47 | + EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialPeersRequest, |
| 48 | + publishRequest -> copyWriteable(publishRequest, writableRegistry(), PeersRequest::new), |
| 49 | + in -> { |
| 50 | + final List<DiscoveryNode> discoveryNodes = new ArrayList<>(in.getKnownPeers()); |
| 51 | + if (randomBoolean()) { |
| 52 | + return new PeersRequest(createNode(randomAlphaOfLength(10)), discoveryNodes); |
| 53 | + } else { |
| 54 | + return new PeersRequest(in.getSourceNode(), modifyDiscoveryNodesList(in.getKnownPeers(), true)); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + public void testPeersResponseEqualsHashCodeSerialization() { |
| 60 | + final long initialTerm = randomNonNegativeLong(); |
| 61 | + final PeersResponse initialPeersResponse; |
| 62 | + |
| 63 | + if (randomBoolean()) { |
| 64 | + initialPeersResponse = new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), emptyList(), initialTerm); |
| 65 | + } else { |
| 66 | + initialPeersResponse = new PeersResponse(Optional.empty(), |
| 67 | + Arrays.stream(generateRandomStringArray(10, 10, false, false)).map(this::createNode).collect(Collectors.toList()), |
| 68 | + initialTerm); |
| 69 | + } |
| 70 | + |
| 71 | + EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialPeersResponse, |
| 72 | + publishResponse -> copyWriteable(publishResponse, writableRegistry(), PeersResponse::new), |
| 73 | + in -> { |
| 74 | + final long term = in.getTerm(); |
| 75 | + if (randomBoolean()) { |
| 76 | + return new PeersResponse(in.getMasterNode(), in.getKnownPeers(), |
| 77 | + randomValueOtherThan(term, ESTestCase::randomNonNegativeLong)); |
| 78 | + } else { |
| 79 | + if (in.getMasterNode().isPresent()) { |
| 80 | + if (randomBoolean()) { |
| 81 | + return new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), in.getKnownPeers(), term); |
| 82 | + } else { |
| 83 | + return new PeersResponse(Optional.empty(), singletonList(createNode(randomAlphaOfLength(10))), term); |
| 84 | + } |
| 85 | + } else { |
| 86 | + if (randomBoolean()) { |
| 87 | + return new PeersResponse(Optional.of(createNode(randomAlphaOfLength(10))), emptyList(), term); |
| 88 | + } else { |
| 89 | + return new PeersResponse(in.getMasterNode(), modifyDiscoveryNodesList(in.getKnownPeers(), false), term); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + private List<DiscoveryNode> modifyDiscoveryNodesList(Collection<DiscoveryNode> originalNodes, boolean allowEmpty) { |
| 98 | + final List<DiscoveryNode> discoveryNodes = new ArrayList<>(originalNodes); |
| 99 | + if (discoveryNodes.isEmpty() == false && randomBoolean() && (allowEmpty || discoveryNodes.size() > 1)) { |
| 100 | + discoveryNodes.remove(randomIntBetween(0, discoveryNodes.size() - 1)); |
| 101 | + } else if (discoveryNodes.isEmpty() == false && randomBoolean()) { |
| 102 | + discoveryNodes.set(randomIntBetween(0, discoveryNodes.size() - 1), createNode(randomAlphaOfLength(10))); |
| 103 | + } else { |
| 104 | + discoveryNodes.add(createNode(randomAlphaOfLength(10))); |
| 105 | + } |
| 106 | + return discoveryNodes; |
| 107 | + } |
| 108 | +} |
0 commit comments