use of com.alibaba.nacos.core.cluster.Member in project nacos by alibaba.
the class ServerMemberManager_ITCase method testMemberChange.
@Test
public void testMemberChange() throws Exception {
AtomicInteger integer = new AtomicInteger(0);
CountDownLatch latch = new CountDownLatch(1);
NotifyCenter.registerSubscriber(new Subscriber<MembersChangeEvent>() {
@Override
public void onEvent(MembersChangeEvent event) {
integer.incrementAndGet();
latch.countDown();
}
@Override
public Class<? extends Event> subscribeType() {
return MembersChangeEvent.class;
}
});
Collection<Member> members = memberManager.allMembers();
System.out.println(members);
memberManager.memberJoin(members);
members.add(Member.builder().ip("115.159.3.213").port(8848).build());
boolean changed = memberManager.memberJoin(members);
Assert.assertTrue(changed);
latch.await(10_000L, TimeUnit.MILLISECONDS);
Assert.assertEquals(1, integer.get());
}
use of com.alibaba.nacos.core.cluster.Member in project nacos by alibaba.
the class NacosClusterV2ControllerTest method testSelf.
@Test
public void testSelf() {
Member self = new Member();
Mockito.when(serverMemberManager.getSelf()).thenReturn(self);
RestResult<Member> result = nacosClusterV2Controller.self();
Assert.assertTrue(result.ok());
Assert.assertEquals(self, result.getData());
}
use of com.alibaba.nacos.core.cluster.Member in project nacos by alibaba.
the class DistroLoadDataTaskTest method setUp.
@Before
public void setUp() throws Exception {
List<Member> memberList = new LinkedList<>();
memberList.add(Member.builder().ip("2.2.2.2").port(8848).build());
memberList.add(Member.builder().ip("1.1.1.1").port(8848).build());
when(memberManager.allMembersWithoutSelf()).thenReturn(memberList);
componentHolder = new DistroComponentHolder();
componentHolder.registerDataStorage(type, distroDataStorage);
componentHolder.registerTransportAgent(type, distroTransportAgent);
componentHolder.registerFailedTaskHandler(type, distroFailedTaskHandler);
when(distroDataProcessor.processType()).thenReturn(type);
componentHolder.registerDataProcessor(distroDataProcessor);
when(distroTransportAgent.getDatumSnapshot(any(String.class))).thenReturn(distroData);
when(distroDataProcessor.processSnapshot(distroData)).thenReturn(true);
distroLoadDataTask = new DistroLoadDataTask(memberManager, componentHolder, distroConfig, loadCallback);
}
use of com.alibaba.nacos.core.cluster.Member in project nacos by alibaba.
the class JRaftServerTest method testPeerChange.
@Test
public void testPeerChange() {
AtomicBoolean changed = new AtomicBoolean(false);
JRaftMaintainService service = new JRaftMaintainService(server) {
@Override
public RestResult<String> execute(Map<String, String> args) {
changed.set(true);
return RestResultUtils.success();
}
};
Collection<Member> firstEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.1").port(80).build(), Member.builder().ip("127.0.0.2").port(81).build(), Member.builder().ip("127.0.0.3").port(82).build());
server.peerChange(service, ProtocolManager.toCPMembersInfo(firstEvent));
Assert.assertFalse(changed.get());
changed.set(false);
Collection<Member> secondEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.1").port(80).build(), Member.builder().ip("127.0.0.2").port(81).build(), Member.builder().ip("127.0.0.4").port(83).build());
server.peerChange(service, ProtocolManager.toCPMembersInfo(secondEvent));
Assert.assertTrue(changed.get());
changed.set(false);
Collection<Member> thirdEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.2").port(81).build(), Member.builder().ip("127.0.0.5").port(82).build());
server.peerChange(service, ProtocolManager.toCPMembersInfo(thirdEvent));
Assert.assertTrue(changed.get());
changed.set(false);
Collection<Member> fourEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.1").port(80).build());
server.peerChange(service, ProtocolManager.toCPMembersInfo(fourEvent));
Assert.assertTrue(changed.get());
changed.set(false);
Collection<Member> fiveEvent = Arrays.asList(Member.builder().ip("1.1.1.1").port(7848).build(), Member.builder().ip("127.0.0.1").port(80).build(), Member.builder().ip("127.0.0.3").port(81).build());
server.peerChange(service, ProtocolManager.toCPMembersInfo(fiveEvent));
Assert.assertFalse(changed.get());
changed.set(false);
}
use of com.alibaba.nacos.core.cluster.Member in project nacos by alibaba.
the class JRaftServerTest method before.
@Before
public void before() throws NoSuchFieldException, IllegalAccessException {
initPeersAndConfiguration();
RaftConfig config = new RaftConfig();
Collection<Member> initEvent = Collections.singletonList(Member.builder().ip("1.1.1.1").port(7848).build());
config.setMembers("1.1.1.1:7848", ProtocolManager.toCPMembersInfo(initEvent));
server = new JRaftServer() {
@Override
boolean peerChange(JRaftMaintainService maintainService, Set<String> newPeers) {
return super.peerChange(maintainService, newPeers);
}
};
server.init(config);
Map<String, JRaftServer.RaftGroupTuple> map = new HashMap<>();
map.put("test_nacos", new JRaftServer.RaftGroupTuple(node, requestProcessor, raftGroupService, nacosStateMachine));
server.mockMultiRaftGroup(map);
mockcliClientService();
mockcliService();
setLeaderAs(peerId1);
// Inject the mocked cliClientServiceMock into server.
Field cliClientServiceField = JRaftServer.class.getDeclaredField("cliClientService");
cliClientServiceField.setAccessible(true);
cliClientServiceField.set(server, cliClientServiceMock);
// Inject the mocked cliServiceMock into server.
Field cliServiceField = JRaftServer.class.getDeclaredField("cliService");
cliServiceField.setAccessible(true);
cliServiceField.set(server, cliServiceMock);
// currently useless
ReadRequest.Builder readRequestBuilder = ReadRequest.newBuilder();
readRequest = readRequestBuilder.build();
when(mockProcessor4CP.loadSnapshotOperate()).thenReturn(Collections.emptyList());
when(mockProcessor4CP.group()).thenReturn(groupId);
when(future.completeExceptionally(any(IllegalArgumentException.class))).thenReturn(true);
Field isStartedField = JRaftServer.class.getDeclaredField("isStarted");
isStartedField.setAccessible(true);
isStartedField.set(server, true);
}
Aggregations