bg2.addConnector(live2);
TransportConfiguration live3 = generateTC();
bg3.addConnector(live3);
DiscoveryGroup dg = new DiscoveryGroupImpl(RandomUtil.randomString(),
RandomUtil.randomString(),
null,
groupAddress,
groupPort,
timeout);
MyListener listener1 = new MyListener();
dg.registerListener(listener1);
MyListener listener2 = new MyListener();
dg.registerListener(listener2);
dg.start();
bg1.broadcastConnectors();
boolean ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
List<DiscoveryEntry> entries = dg.getDiscoveryEntries();
assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);
Assert.assertTrue(listener1.called);
Assert.assertTrue(listener2.called);
listener1.called = false;
listener2.called = false;
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entries = dg.getDiscoveryEntries();
assertEqualsDiscoveryEntries(Arrays.asList(live1, live2), entries);
Assert.assertTrue(listener1.called);
Assert.assertTrue(listener2.called);
listener1.called = false;
listener2.called = false;
bg3.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entries = dg.getDiscoveryEntries();
assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
Assert.assertTrue(listener1.called);
Assert.assertTrue(listener2.called);
listener1.called = false;
listener2.called = false;
bg1.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entries = dg.getDiscoveryEntries();
assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
Assert.assertFalse(listener1.called);
Assert.assertFalse(listener2.called);
listener1.called = false;
listener2.called = false;
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entries = dg.getDiscoveryEntries();
assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
Assert.assertFalse(listener1.called);
Assert.assertFalse(listener2.called);
listener1.called = false;
listener2.called = false;
bg3.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entries = dg.getDiscoveryEntries();
assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
Assert.assertFalse(listener1.called);
Assert.assertFalse(listener2.called);
listener1.called = false;
listener2.called = false;
bg2.removeConnector(live2);
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
// Connector2 should still be there since not timed out yet
entries = dg.getDiscoveryEntries();
assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
Assert.assertFalse(listener1.called);
Assert.assertFalse(listener2.called);
listener1.called = false;
listener2.called = false;
Thread.sleep(timeout);
bg1.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
bg3.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
entries = dg.getDiscoveryEntries();
assertEqualsDiscoveryEntries(Arrays.asList(live1, live3), entries);
Assert.assertTrue(listener1.called);
Assert.assertTrue(listener2.called);
listener1.called = false;
listener2.called = false;
bg1.removeConnector(live1);
bg3.removeConnector(live3);
Thread.sleep(timeout);
bg1.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
bg3.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
entries = dg.getDiscoveryEntries();
Assert.assertNotNull(entries);
Assert.assertEquals(0, entries.size());
Assert.assertTrue(listener1.called);
Assert.assertTrue(listener2.called);
listener1.called = false;
listener2.called = false;
bg1.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
bg3.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
entries = dg.getDiscoveryEntries();
Assert.assertNotNull(entries);
Assert.assertEquals(0, entries.size());
Assert.assertFalse(listener1.called);
Assert.assertFalse(listener2.called);
bg1.stop();
bg2.stop();
bg3.stop();
dg.stop();
}