|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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.apache.iotdb.confignode.manager.consensus; |
| 21 | + |
| 22 | +import org.apache.iotdb.common.rpc.thrift.TConfigNodeLocation; |
| 23 | +import org.apache.iotdb.common.rpc.thrift.TEndPoint; |
| 24 | +import org.apache.iotdb.confignode.exception.AddPeerException; |
| 25 | +import org.apache.iotdb.confignode.manager.IManager; |
| 26 | +import org.apache.iotdb.consensus.IConsensus; |
| 27 | +import org.apache.iotdb.consensus.common.Peer; |
| 28 | +import org.apache.iotdb.consensus.exception.ConsensusException; |
| 29 | +import org.apache.iotdb.consensus.exception.ConsensusGroupAlreadyExistException; |
| 30 | +import org.apache.iotdb.consensus.exception.PeerAlreadyInConsensusGroupException; |
| 31 | +import org.apache.iotdb.consensus.exception.PeerNotInConsensusGroupException; |
| 32 | + |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.Test; |
| 35 | +import org.mockito.Mockito; |
| 36 | + |
| 37 | +import java.util.Collections; |
| 38 | + |
| 39 | +public class ConsensusManagerTest { |
| 40 | + |
| 41 | + @Test |
| 42 | + public void createPeerForConsensusGroupShouldIgnoreAlreadyCreatedLocalPeer() throws Exception { |
| 43 | + final IConsensus consensus = Mockito.mock(IConsensus.class); |
| 44 | + Mockito.doThrow( |
| 45 | + new ConsensusGroupAlreadyExistException(ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID)) |
| 46 | + .when(consensus) |
| 47 | + .createLocalPeer( |
| 48 | + Mockito.eq(ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID), Mockito.anyList()); |
| 49 | + |
| 50 | + newConsensusManager(consensus) |
| 51 | + .createPeerForConsensusGroup(Collections.singletonList(newConfigNodeLocation(1))); |
| 52 | + |
| 53 | + Mockito.verify(consensus) |
| 54 | + .createLocalPeer( |
| 55 | + Mockito.eq(ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID), Mockito.anyList()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void addConfigNodePeerShouldIgnoreAlreadyAddedPeer() throws Exception { |
| 60 | + final IConsensus consensus = Mockito.mock(IConsensus.class); |
| 61 | + Mockito.doThrow( |
| 62 | + new PeerAlreadyInConsensusGroupException( |
| 63 | + ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID, |
| 64 | + new Peer( |
| 65 | + ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID, |
| 66 | + 1, |
| 67 | + new TEndPoint("127.0.0.1", 10720)))) |
| 68 | + .when(consensus) |
| 69 | + .addRemotePeer( |
| 70 | + Mockito.eq(ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID), Mockito.any(Peer.class)); |
| 71 | + |
| 72 | + newConsensusManager(consensus).addConfigNodePeer(newConfigNodeLocation(1)); |
| 73 | + |
| 74 | + Mockito.verify(consensus) |
| 75 | + .addRemotePeer( |
| 76 | + Mockito.eq(ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID), Mockito.any(Peer.class)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void addConfigNodePeerShouldKeepFailingForOtherConsensusErrors() throws Exception { |
| 81 | + final IConsensus consensus = Mockito.mock(IConsensus.class); |
| 82 | + Mockito.doThrow(new ConsensusException("reconfiguration failed")) |
| 83 | + .when(consensus) |
| 84 | + .addRemotePeer( |
| 85 | + Mockito.eq(ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID), Mockito.any(Peer.class)); |
| 86 | + |
| 87 | + Assert.assertThrows( |
| 88 | + AddPeerException.class, |
| 89 | + () -> newConsensusManager(consensus).addConfigNodePeer(newConfigNodeLocation(1))); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void removeConfigNodePeerShouldIgnoreAlreadyRemovedPeer() throws Exception { |
| 94 | + final IConsensus consensus = Mockito.mock(IConsensus.class); |
| 95 | + Mockito.doThrow( |
| 96 | + new PeerNotInConsensusGroupException( |
| 97 | + ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID, "127.0.0.1:10720")) |
| 98 | + .when(consensus) |
| 99 | + .removeRemotePeer( |
| 100 | + Mockito.eq(ConsensusManager.DEFAULT_CONSENSUS_GROUP_ID), Mockito.any(Peer.class)); |
| 101 | + |
| 102 | + Assert.assertTrue( |
| 103 | + newConsensusManager(consensus).removeConfigNodePeer(newConfigNodeLocation(1))); |
| 104 | + } |
| 105 | + |
| 106 | + private static ConsensusManager newConsensusManager(final IConsensus consensus) throws Exception { |
| 107 | + return new ConsensusManager(Mockito.mock(IManager.class), consensus); |
| 108 | + } |
| 109 | + |
| 110 | + private static TConfigNodeLocation newConfigNodeLocation(final int configNodeId) { |
| 111 | + return new TConfigNodeLocation( |
| 112 | + configNodeId, |
| 113 | + new TEndPoint("127.0.0.1", 10710 + configNodeId), |
| 114 | + new TEndPoint("127.0.0.1", 10720 + configNodeId)); |
| 115 | + } |
| 116 | +} |
0 commit comments