use of io.atomix.cluster.messaging.impl.TestUnicastServiceFactory in project atomix by atomix.
the class DefaultClusterMembershipServiceTest method testClusterService.
@Test
public void testClusterService() throws Exception {
TestMessagingServiceFactory messagingServiceFactory = new TestMessagingServiceFactory();
TestUnicastServiceFactory unicastServiceFactory = new TestUnicastServiceFactory();
TestBroadcastServiceFactory broadcastServiceFactory = new TestBroadcastServiceFactory();
Collection<Node> bootstrapLocations = buildBootstrapNodes(3);
Member localMember1 = buildMember(1);
BootstrapService bootstrapService1 = new TestBootstrapService(messagingServiceFactory.newMessagingService(localMember1.address()).start().join(), unicastServiceFactory.newUnicastService(localMember1.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
ManagedClusterMembershipService clusterService1 = new DefaultClusterMembershipService(localMember1, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService1, localMember1, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService1, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
Member localMember2 = buildMember(2);
BootstrapService bootstrapService2 = new TestBootstrapService(messagingServiceFactory.newMessagingService(localMember2.address()).start().join(), unicastServiceFactory.newUnicastService(localMember2.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
ManagedClusterMembershipService clusterService2 = new DefaultClusterMembershipService(localMember2, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService2, localMember2, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService2, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
Member localMember3 = buildMember(3);
BootstrapService bootstrapService3 = new TestBootstrapService(messagingServiceFactory.newMessagingService(localMember3.address()).start().join(), unicastServiceFactory.newUnicastService(localMember3.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
ManagedClusterMembershipService clusterService3 = new DefaultClusterMembershipService(localMember3, Version.from("1.0.1"), new DefaultNodeDiscoveryService(bootstrapService3, localMember3, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService3, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
assertNull(clusterService1.getMember(MemberId.from("1")));
assertNull(clusterService1.getMember(MemberId.from("2")));
assertNull(clusterService1.getMember(MemberId.from("3")));
CompletableFuture.allOf(new CompletableFuture[] { clusterService1.start(), clusterService2.start(), clusterService3.start() }).join();
Thread.sleep(5000);
assertEquals(3, clusterService1.getMembers().size());
assertEquals(3, clusterService2.getMembers().size());
assertEquals(3, clusterService3.getMembers().size());
assertTrue(clusterService1.getLocalMember().isActive());
assertTrue(clusterService1.getMember(MemberId.from("1")).isActive());
assertTrue(clusterService1.getMember(MemberId.from("2")).isActive());
assertTrue(clusterService1.getMember(MemberId.from("3")).isActive());
assertEquals("1.0.0", clusterService1.getMember("1").version().toString());
assertEquals("1.0.0", clusterService1.getMember("2").version().toString());
assertEquals("1.0.1", clusterService1.getMember("3").version().toString());
Member anonymousMember = buildMember(4);
BootstrapService ephemeralBootstrapService = new TestBootstrapService(messagingServiceFactory.newMessagingService(anonymousMember.address()).start().join(), unicastServiceFactory.newUnicastService(anonymousMember.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
ManagedClusterMembershipService ephemeralClusterService = new DefaultClusterMembershipService(anonymousMember, Version.from("1.1.0"), new DefaultNodeDiscoveryService(ephemeralBootstrapService, anonymousMember, new BootstrapDiscoveryProvider(bootstrapLocations)), ephemeralBootstrapService, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
assertFalse(ephemeralClusterService.getLocalMember().isActive());
assertNull(ephemeralClusterService.getMember(MemberId.from("1")));
assertNull(ephemeralClusterService.getMember(MemberId.from("2")));
assertNull(ephemeralClusterService.getMember(MemberId.from("3")));
assertNull(ephemeralClusterService.getMember(MemberId.from("4")));
assertNull(ephemeralClusterService.getMember(MemberId.from("5")));
ephemeralClusterService.start().join();
Thread.sleep(1000);
assertEquals(4, clusterService1.getMembers().size());
assertEquals(4, clusterService2.getMembers().size());
assertEquals(4, clusterService3.getMembers().size());
assertEquals(4, ephemeralClusterService.getMembers().size());
assertEquals("1.0.0", clusterService1.getMember("1").version().toString());
assertEquals("1.0.0", clusterService1.getMember("2").version().toString());
assertEquals("1.0.1", clusterService1.getMember("3").version().toString());
assertEquals("1.1.0", clusterService1.getMember("4").version().toString());
clusterService1.stop().join();
Thread.sleep(5000);
assertEquals(3, clusterService2.getMembers().size());
assertNull(clusterService2.getMember(MemberId.from("1")));
assertTrue(clusterService2.getMember(MemberId.from("2")).isActive());
assertTrue(clusterService2.getMember(MemberId.from("3")).isActive());
assertTrue(clusterService2.getMember(MemberId.from("4")).isActive());
ephemeralClusterService.stop().join();
Thread.sleep(5000);
assertEquals(2, clusterService2.getMembers().size());
assertNull(clusterService2.getMember(MemberId.from("1")));
assertTrue(clusterService2.getMember(MemberId.from("2")).isActive());
assertTrue(clusterService2.getMember(MemberId.from("3")).isActive());
assertNull(clusterService2.getMember(MemberId.from("4")));
Thread.sleep(2500);
assertEquals(2, clusterService2.getMembers().size());
assertNull(clusterService2.getMember(MemberId.from("1")));
assertTrue(clusterService2.getMember(MemberId.from("2")).isActive());
assertTrue(clusterService2.getMember(MemberId.from("3")).isActive());
assertNull(clusterService2.getMember(MemberId.from("4")));
TestClusterMembershipEventListener eventListener = new TestClusterMembershipEventListener();
clusterService2.addListener(eventListener);
ClusterMembershipEvent event;
clusterService3.getLocalMember().properties().put("foo", "bar");
event = eventListener.nextEvent();
assertEquals(ClusterMembershipEvent.Type.METADATA_CHANGED, event.type());
assertEquals("bar", event.subject().properties().get("foo"));
clusterService3.getLocalMember().properties().put("foo", "baz");
event = eventListener.nextEvent();
assertEquals(ClusterMembershipEvent.Type.METADATA_CHANGED, event.type());
assertEquals("baz", event.subject().properties().get("foo"));
CompletableFuture.allOf(new CompletableFuture[] { clusterService1.stop(), clusterService2.stop(), clusterService3.stop() }).join();
}
use of io.atomix.cluster.messaging.impl.TestUnicastServiceFactory in project atomix by atomix.
the class SwimProtocolTest method reset.
@Before
@SuppressWarnings("unchecked")
public void reset() {
messagingServiceFactory = new TestMessagingServiceFactory();
unicastServiceFactory = new TestUnicastServiceFactory();
broadcastServiceFactory = new TestBroadcastServiceFactory();
member1 = member("1", "localhost", 5001, version1);
member2 = member("2", "localhost", 5002, version1);
member3 = member("3", "localhost", 5003, version1);
members = Arrays.asList(member1, member2, member3);
nodes = (Collection) members;
listeners = Maps.newConcurrentMap();
}
Aggregations