Search in sources :

Example 1 with DistributedInstanceEvent

use of org.bboxdb.distribution.membership.DistributedInstanceEvent in project bboxdb by jnidzwetzki.

the class TestDistributedInstanceManager method testChangedEventOnChange.

/**
 * Test changed event generation
 * @throws InterruptedException
 * @throws ZookeeperException
 */
@Test(timeout = 30000)
public void testChangedEventOnChange() throws InterruptedException, ZookeeperException {
    final BBoxDBInstanceManager distributedInstanceManager = BBoxDBInstanceManager.getInstance();
    final BBoxDBInstance instance1 = new BBoxDBInstance("node6:5050");
    final BBoxDBInstance instance2 = new BBoxDBInstance("node7:5050");
    final ZookeeperClient zookeeperClient1 = getNewZookeeperClient(instance1);
    final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter1 = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient1);
    BBoxDBInstanceManager.getInstance().startMembershipObserver(zookeeperClient1);
    final ZookeeperClient zookeeperClient2 = getNewZookeeperClient(instance2);
    final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter2 = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient2);
    zookeeperBBoxDBInstanceAdapter1.updateStateData(instance1, BBoxDBInstanceState.READY);
    zookeeperBBoxDBInstanceAdapter2.updateStateData(instance2, BBoxDBInstanceState.READY);
    Thread.sleep(5000);
    final CountDownLatch changedLatch = new CountDownLatch(1);
    distributedInstanceManager.registerListener(new BiConsumer<DistributedInstanceEvent, BBoxDBInstance>() {

        @Override
        public void accept(final DistributedInstanceEvent event, final BBoxDBInstance instance) {
            if (event == DistributedInstanceEvent.CHANGED) {
                if (instance.socketAddressEquals(instance2)) {
                    Assert.assertTrue(instance.getState() == BBoxDBInstanceState.OUTDATED);
                    changedLatch.countDown();
                }
            } else {
                // Unexpected event
                Assert.assertTrue(false);
            }
        }
    });
    zookeeperBBoxDBInstanceAdapter2.updateStateData(instance2, BBoxDBInstanceState.OUTDATED);
    changedLatch.await();
    distributedInstanceManager.removeAllListener();
    zookeeperClient1.shutdown();
    zookeeperClient2.shutdown();
}
Also used : ZookeeperClient(org.bboxdb.distribution.zookeeper.ZookeeperClient) BBoxDBInstanceManager(org.bboxdb.distribution.membership.BBoxDBInstanceManager) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) ZookeeperBBoxDBInstanceAdapter(org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter) DistributedInstanceEvent(org.bboxdb.distribution.membership.DistributedInstanceEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with DistributedInstanceEvent

use of org.bboxdb.distribution.membership.DistributedInstanceEvent in project bboxdb by jnidzwetzki.

the class TestDistributedInstanceManager method testAddEvent.

/**
 * Test add event generation
 * @throws InterruptedException
 * @throws ZookeeperException
 */
@Test(timeout = 30000)
public void testAddEvent() throws InterruptedException {
    final BBoxDBInstanceManager distributedInstanceManager = BBoxDBInstanceManager.getInstance();
    final BBoxDBInstance instance1 = new BBoxDBInstance("node4:5050");
    final BBoxDBInstance instance2 = new BBoxDBInstance("node5:5050");
    final ZookeeperClient zookeeperClient1 = getNewZookeeperClient(instance1);
    BBoxDBInstanceManager.getInstance().startMembershipObserver(zookeeperClient1);
    Thread.sleep(5000);
    final CountDownLatch changedLatch = new CountDownLatch(1);
    distributedInstanceManager.registerListener(new BiConsumer<DistributedInstanceEvent, BBoxDBInstance>() {

        @Override
        public void accept(final DistributedInstanceEvent event, final BBoxDBInstance instance) {
            if (event == DistributedInstanceEvent.ADD) {
                if (instance.socketAddressEquals(instance2)) {
                    changedLatch.countDown();
                }
            } else {
                // Unexpected event
                System.out.println("Got unexpeceted event: " + event);
                Assert.assertTrue(false);
            }
        }
    });
    final ZookeeperClient zookeeperClient2 = getNewZookeeperClient(instance2);
    changedLatch.await();
    distributedInstanceManager.removeAllListener();
    zookeeperClient1.shutdown();
    zookeeperClient2.shutdown();
}
Also used : ZookeeperClient(org.bboxdb.distribution.zookeeper.ZookeeperClient) BBoxDBInstanceManager(org.bboxdb.distribution.membership.BBoxDBInstanceManager) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) DistributedInstanceEvent(org.bboxdb.distribution.membership.DistributedInstanceEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with DistributedInstanceEvent

use of org.bboxdb.distribution.membership.DistributedInstanceEvent in project bboxdb by jnidzwetzki.

the class TestDistributedInstanceManager method testChangedEventOnDeletion.

/**
 * Test delete event generation
 * @throws InterruptedException
 * @throws ZookeeperException
 */
@Test(timeout = 30000)
public void testChangedEventOnDeletion() throws InterruptedException, ZookeeperException {
    final BBoxDBInstanceManager distributedInstanceManager = BBoxDBInstanceManager.getInstance();
    final BBoxDBInstance instance1 = new BBoxDBInstance("node6:5050");
    final BBoxDBInstance instance2 = new BBoxDBInstance("node7:5050");
    final ZookeeperClient zookeeperClient1 = getNewZookeeperClient(instance1);
    final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter1 = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient1);
    BBoxDBInstanceManager.getInstance().startMembershipObserver(zookeeperClient1);
    final ZookeeperClient zookeeperClient2 = getNewZookeeperClient(instance2);
    final ZookeeperBBoxDBInstanceAdapter zookeeperBBoxDBInstanceAdapter2 = new ZookeeperBBoxDBInstanceAdapter(zookeeperClient2);
    zookeeperBBoxDBInstanceAdapter1.updateStateData(instance1, BBoxDBInstanceState.READY);
    zookeeperBBoxDBInstanceAdapter2.updateStateData(instance2, BBoxDBInstanceState.READY);
    Thread.sleep(5000);
    final CountDownLatch changedLatch = new CountDownLatch(1);
    distributedInstanceManager.registerListener(new BiConsumer<DistributedInstanceEvent, BBoxDBInstance>() {

        @Override
        public void accept(final DistributedInstanceEvent event, final BBoxDBInstance instance) {
            if (event == DistributedInstanceEvent.CHANGED) {
                if (instance.socketAddressEquals(instance2)) {
                    changedLatch.countDown();
                }
            } else {
                System.out.println("Got unexpeced event: " + event);
                // Unexpected event
                Assert.assertTrue(false);
            }
        }
    });
    zookeeperClient2.shutdown();
    changedLatch.await();
    distributedInstanceManager.removeAllListener();
    zookeeperClient1.shutdown();
}
Also used : ZookeeperClient(org.bboxdb.distribution.zookeeper.ZookeeperClient) BBoxDBInstanceManager(org.bboxdb.distribution.membership.BBoxDBInstanceManager) BBoxDBInstance(org.bboxdb.distribution.membership.BBoxDBInstance) ZookeeperBBoxDBInstanceAdapter(org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter) DistributedInstanceEvent(org.bboxdb.distribution.membership.DistributedInstanceEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)3 BBoxDBInstance (org.bboxdb.distribution.membership.BBoxDBInstance)3 BBoxDBInstanceManager (org.bboxdb.distribution.membership.BBoxDBInstanceManager)3 DistributedInstanceEvent (org.bboxdb.distribution.membership.DistributedInstanceEvent)3 ZookeeperClient (org.bboxdb.distribution.zookeeper.ZookeeperClient)3 Test (org.junit.Test)3 ZookeeperBBoxDBInstanceAdapter (org.bboxdb.distribution.membership.ZookeeperBBoxDBInstanceAdapter)2