Exemplo n.º 1
0
  public void testDiscoveryListenersCalled() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    String nodeID = RandomUtil.randomString();

    bg = newBroadcast(nodeID, RandomUtil.randomString(), null, -1, groupAddress, groupPort);

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    MyListener listener1 = new MyListener();
    MyListener listener2 = new MyListener();
    MyListener listener3 = new MyListener();

    dg.registerListener(listener1);
    dg.registerListener(listener2);
    dg.registerListener(listener3);

    dg.start();

    verifyBroadcast(bg, dg);

    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    Assert.assertTrue(listener3.called);

    listener1.called = false;
    listener2.called = false;
    listener3.called = false;

    verifyBroadcast(bg, dg);

    // Won't be called since connectors haven't changed
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    Assert.assertFalse(listener3.called);
  }
Exemplo n.º 2
0
  public void testConnectorsUpdatedMultipleBroadcasters() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    String node1 = RandomUtil.randomString();
    String node2 = RandomUtil.randomString();
    String node3 = RandomUtil.randomString();

    bg1 = newBroadcast(node1, RandomUtil.randomString(), null, -1, groupAddress, groupPort);
    bg1.start();

    bg2 = newBroadcast(node2, RandomUtil.randomString(), null, -1, groupAddress, groupPort);
    bg2.start();

    bg3 = newBroadcast(node3, RandomUtil.randomString(), null, -1, groupAddress, groupPort);
    bg3.start();

    TransportConfiguration live1 = generateTC();
    bg1.addConnector(live1);

    TransportConfiguration live2 = generateTC();
    bg2.addConnector(live2);

    TransportConfiguration live3 = generateTC();
    bg3.addConnector(live3);

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    MyListener listener1 = new MyListener();
    dg.registerListener(listener1);
    MyListener listener2 = new MyListener();
    dg.registerListener(listener2);

    dg.start();

    verifyBroadcast(bg1, dg);
    List<DiscoveryEntry> entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg2, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg3, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg1, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg2, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg3, dg);
    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);
    verifyBroadcast(bg2, dg);

    // 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 * 2);

    bg1.broadcastConnectors();
    boolean 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 * 2);

    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);
  }