TransportConfiguration backup3 = generateTC();
Pair<TransportConfiguration, TransportConfiguration> connectorPair3 = new Pair<TransportConfiguration, TransportConfiguration>(live3,
backup3);
bg3.addConnectorPair(connectorPair3);
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);
Map<String, DiscoveryEntry> entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(1, entryMap.size());
DiscoveryEntry entry = entryMap.get(node1);
Assert.assertNotNull(entry);
Assert.assertEquals(connectorPair1, entry.getConnectorPair());
Assert.assertTrue(listener1.called);
Assert.assertTrue(listener2.called);
listener1.called = false;
listener2.called = false;
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(2, entryMap.size());
DiscoveryEntry entry1 = entryMap.get(node1);
Assert.assertNotNull(entry1);
Assert.assertEquals(connectorPair1, entry1.getConnectorPair());
DiscoveryEntry entry2 = entryMap.get(node2);
Assert.assertNotNull(entry2);
Assert.assertEquals(connectorPair2, entry2.getConnectorPair());
Assert.assertTrue(listener1.called);
Assert.assertTrue(listener2.called);
listener1.called = false;
listener2.called = false;
bg3.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(3, entryMap.size());
entry1 = entryMap.get(node1);
Assert.assertNotNull(entry1);
Assert.assertEquals(connectorPair1, entry1.getConnectorPair());
entry2 = entryMap.get(node2);
Assert.assertNotNull(entry2);
Assert.assertEquals(connectorPair2, entry2.getConnectorPair());
DiscoveryEntry entry3 = entryMap.get(node3);
Assert.assertNotNull(entry3);
Assert.assertEquals(connectorPair3, entry3.getConnectorPair());
Assert.assertTrue(listener1.called);
Assert.assertTrue(listener2.called);
listener1.called = false;
listener2.called = false;
bg1.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(3, entryMap.size());
entry1 = entryMap.get(node1);
Assert.assertNotNull(entry1);
Assert.assertEquals(connectorPair1, entry1.getConnectorPair());
entry2 = entryMap.get(node2);
Assert.assertNotNull(entry2);
Assert.assertEquals(connectorPair2, entry2.getConnectorPair());
entry3 = entryMap.get(node3);
Assert.assertNotNull(entry3);
Assert.assertEquals(connectorPair3, entry3.getConnectorPair());
Assert.assertFalse(listener1.called);
Assert.assertFalse(listener2.called);
listener1.called = false;
listener2.called = false;
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(3, entryMap.size());
entry1 = entryMap.get(node1);
Assert.assertNotNull(entry1);
Assert.assertEquals(connectorPair1, entry1.getConnectorPair());
entry2 = entryMap.get(node2);
Assert.assertNotNull(entry2);
Assert.assertEquals(connectorPair2, entry2.getConnectorPair());
entry3 = entryMap.get(node3);
Assert.assertNotNull(entry3);
Assert.assertEquals(connectorPair3, entry3.getConnectorPair());
Assert.assertFalse(listener1.called);
Assert.assertFalse(listener2.called);
listener1.called = false;
listener2.called = false;
bg3.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(3, entryMap.size());
entry1 = entryMap.get(node1);
Assert.assertNotNull(entry1);
Assert.assertEquals(connectorPair1, entry1.getConnectorPair());
entry2 = entryMap.get(node2);
Assert.assertNotNull(entry2);
Assert.assertEquals(connectorPair2, entry2.getConnectorPair());
entry3 = entryMap.get(node3);
Assert.assertNotNull(entry3);
Assert.assertEquals(connectorPair3, entry3.getConnectorPair());
Assert.assertFalse(listener1.called);
Assert.assertFalse(listener2.called);
listener1.called = false;
listener2.called = false;
bg2.removeConnectorPair(connectorPair2);
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
Assert.assertTrue(ok);
// Connector2 should still be there since not timed out yet
entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(3, entryMap.size());
entry1 = entryMap.get(node1);
Assert.assertNotNull(entry1);
Assert.assertEquals(connectorPair1, entry1.getConnectorPair());
entry2 = entryMap.get(node2);
Assert.assertNotNull(entry2);
Assert.assertEquals(connectorPair2, entry2.getConnectorPair());
entry3 = entryMap.get(node3);
Assert.assertNotNull(entry3);
Assert.assertEquals(connectorPair3, entry3.getConnectorPair());
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);
entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(2, entryMap.size());
entry1 = entryMap.get(node1);
Assert.assertNotNull(entry1);
Assert.assertEquals(connectorPair1, entry1.getConnectorPair());
entry3 = entryMap.get(node3);
Assert.assertNotNull(entry3);
Assert.assertEquals(connectorPair3, entry3.getConnectorPair());
Assert.assertTrue(listener1.called);
Assert.assertTrue(listener2.called);
listener1.called = false;
listener2.called = false;
bg1.removeConnectorPair(connectorPair1);
bg3.removeConnectorPair(connectorPair3);
Thread.sleep(timeout);
bg1.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
bg2.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
bg3.broadcastConnectors();
ok = dg.waitForBroadcast(1000);
entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(0, entryMap.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);
entryMap = dg.getDiscoveryEntryMap();
Assert.assertNotNull(entryMap);
Assert.assertEquals(0, entryMap.size());
Assert.assertFalse(listener1.called);
Assert.assertFalse(listener2.called);
bg1.stop();
bg2.stop();
bg3.stop();
dg.stop();
}