use of com.alibaba.nacos.core.cluster.MembersChangeEvent in project nacos by alibaba.
the class ServerMemberManager_ITCase method testMemberHealthCheck.
@Test
public void testMemberHealthCheck() throws Exception {
AtomicReference<Collection<Member>> healthMembers = new AtomicReference<>();
CountDownLatch first = new CountDownLatch(1);
CountDownLatch second = new CountDownLatch(1);
NotifyCenter.registerSubscriber(new Subscriber<MembersChangeEvent>() {
@Override
public void onEvent(MembersChangeEvent event) {
System.out.println(event);
healthMembers.set(MemberUtil.selectTargetMembers(event.getMembers(), member -> !NodeState.DOWN.equals(member.getState())));
if (first.getCount() == 1) {
first.countDown();
return;
}
if (second.getCount() == 1) {
second.countDown();
}
}
@Override
public Class<? extends Event> subscribeType() {
return MembersChangeEvent.class;
}
});
String firstIp = "127.0.0.1:8847";
String secondIp = "127.0.0.1:8848";
String thirdIp = "127.0.0.1:8849";
Map<String, Member> map = new HashMap<>(4);
map.put(firstIp, Member.builder().ip("127.0.0.1").port(8847).state(NodeState.UP).build());
map.put(secondIp, Member.builder().ip("127.0.0.1").port(8848).state(NodeState.UP).build());
map.put(thirdIp, Member.builder().ip("127.0.0.1").port(8849).state(NodeState.UP).build());
Set<Member> firstMemberList = new HashSet<>(map.values());
memberManager.memberJoin(map.values());
first.await();
Set<Member> copy = new HashSet<>(firstMemberList);
copy.removeAll(healthMembers.get());
Assert.assertEquals(2, copy.size());
Member member = map.get(firstIp);
member.setState(NodeState.DOWN);
Assert.assertTrue(memberManager.update(member));
second.await();
copy = new HashSet<>(firstMemberList);
copy.removeAll(healthMembers.get());
Assert.assertEquals(3, copy.size());
Assert.assertTrue(copy.contains(map.get(firstIp)));
}
use of com.alibaba.nacos.core.cluster.MembersChangeEvent 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());
}
Aggregations