Example usage for org.apache.zookeeper WatchedEvent WatchedEvent

List of usage examples for org.apache.zookeeper WatchedEvent WatchedEvent

Introduction

In this page you can find the example usage for org.apache.zookeeper WatchedEvent WatchedEvent.

Prototype

public WatchedEvent(EventType eventType, KeeperState keeperState, String path) 

Source Link

Document

Create a WatchedEvent with specified type, state and path

Usage

From source file:net.sf.katta.zk.ZKClientTest.java

License:Apache License

public void testWatchProcessThreadSafeness() throws Exception {
    final ZKClient zkClient = new ZKClient(_conf);
    zkClient.start(5000);//from w w  w. ja va 2 s.  co  m
    final String path = "/";
    final int listenerCount = 20;
    final int threadCount = 10;
    final int watchEventsPerThread = 100;
    final Mockery mockery = new Mockery();
    final List<IZkChildListener> childListeners = new ArrayList<IZkChildListener>();
    for (int i = 0; i < listenerCount; i++) {
        childListeners.add(mockery.mock(IZkChildListener.class, "cl" + i));
    }
    mockery.checking(new Expectations() {
        {
            for (final IZkChildListener childListener : childListeners) {
                atLeast(1).of(childListener).handleChildChange(with(equal(path)), with(aNonNull(List.class)));
            }
        }
    });

    final AtomicInteger concurrentModificationExceptions = new AtomicInteger();
    final AtomicInteger unknownExceptions = new AtomicInteger();
    final Runnable fireWatchEventsRunnable = new Runnable() {
        public void run() {
            try {
                for (int i = 0; i < watchEventsPerThread; i++) {
                    zkClient.process(
                            new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected, path));
                    for (final IZkChildListener childListener : childListeners) {
                        zkClient.unsubscribeChildChanges(path, childListener);
                        zkClient.subscribeChildChanges(path, childListener);
                    }
                }
            } catch (final ConcurrentModificationException e) {
                concurrentModificationExceptions.incrementAndGet();
                e.printStackTrace();
            } catch (final Exception e) {
                unknownExceptions.incrementAndGet();
                e.printStackTrace();
            }
        }
    };
    final Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread(fireWatchEventsRunnable);
        threads[i].start();
    }
    for (final Thread thread : threads) {
        thread.join();
    }
    if (concurrentModificationExceptions.get() > 0) {
        fail(concurrentModificationExceptions.get() + " ConcurrentModificationException exceptions was thrown");
    }
    if (unknownExceptions.get() > 0) {
        fail(unknownExceptions.get() + " unknown exceptions was thrown");
    }
    zkClient.close();
    mockery.assertIsSatisfied();
}

From source file:net.spy.memcached.CacheMonitorTest.java

License:Apache License

public void testProcess_syncConnected() throws Exception {
    // when//from  w ww .  j  a v  a2s .  c  o  m
    WatchedEvent event = new WatchedEvent(EventType.None, KeeperState.SyncConnected,
            ARCUS_BASE_CACHE_LIST_ZPATH + "/dev");

    // test
    cacheMonitor.process(event);

    // then
    // do nothing
}

From source file:net.spy.memcached.CacheMonitorTest.java

License:Apache License

public void testProcess_disconnected() throws Exception {
    // when//from www.j av  a2  s. c om
    WatchedEvent event = new WatchedEvent(EventType.None, KeeperState.Disconnected,
            ARCUS_BASE_CACHE_LIST_ZPATH + "/dev");

    // test
    cacheMonitor.process(event);

    // then
    // do nothing
}

From source file:net.spy.memcached.CacheMonitorTest.java

License:Apache License

public void testProcess_expired() throws Exception {
    // when//from   ww  w .  j  av  a 2  s  .c  om
    WatchedEvent event = new WatchedEvent(EventType.None, KeeperState.Expired,
            ARCUS_BASE_CACHE_LIST_ZPATH + "/dev");
    listener.expects(once()).method("closing");

    // test
    cacheMonitor.process(event);

    // then
    assertTrue(cacheMonitor.dead);
}

From source file:net.spy.memcached.CacheMonitorTest.java

License:Apache License

public void testProcess_nodeChildrenChanged() throws Exception {
    // when//from   w  w w.  ja  va  2s. c o  m
    WatchedEvent event = new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected,
            ARCUS_BASE_CACHE_LIST_ZPATH + "/dev");

    // test
    cacheMonitor.process(event);

    // then
    // do nothing      
}

From source file:org.apache.accumulo.fate.zookeeper.ZooCacheTest.java

License:Apache License

private void testWatchDataNode(byte[] initialData, Watcher.Event.EventType eventType, boolean stillCached)
        throws Exception {
    WatchedEvent event = new WatchedEvent(eventType, Watcher.Event.KeeperState.SyncConnected, ZPATH);
    TestWatcher exw = new TestWatcher(event);
    zc = new ZooCache(zr, exw);

    Watcher w = watchData(initialData);//www.j  a  v a  2 s.  c  o  m
    w.process(event);
    assertTrue(exw.wasCalled());
    assertEquals(stillCached, zc.dataCached(ZPATH));
}

From source file:org.apache.accumulo.fate.zookeeper.ZooCacheTest.java

License:Apache License

private void testWatchDataNode_Clear(Watcher.Event.KeeperState state) throws Exception {
    WatchedEvent event = new WatchedEvent(Watcher.Event.EventType.None, state, null);
    TestWatcher exw = new TestWatcher(event);
    zc = new ZooCache(zr, exw);

    Watcher w = watchData(DATA);/*w  ww . j  a  v a  2 s . co  m*/
    assertTrue(zc.dataCached(ZPATH));
    w.process(event);
    assertTrue(exw.wasCalled());
    assertFalse(zc.dataCached(ZPATH));
}

From source file:org.apache.accumulo.fate.zookeeper.ZooCacheTest.java

License:Apache License

private void testWatchChildrenNode(List<String> initialChildren, Watcher.Event.EventType eventType,
        boolean stillCached) throws Exception {
    WatchedEvent event = new WatchedEvent(eventType, Watcher.Event.KeeperState.SyncConnected, ZPATH);
    TestWatcher exw = new TestWatcher(event);
    zc = new ZooCache(zr, exw);

    Watcher w = watchChildren(initialChildren);
    w.process(event);//from ww  w  .  j  a v  a  2  s . co  m
    assertTrue(exw.wasCalled());
    assertEquals(stillCached, zc.childrenCached(ZPATH));
}

From source file:org.apache.accumulo.server.security.delegation.ZooAuthenticationKeyWatcherTest.java

License:Apache License

@Test
public void testBaseNodeCreated() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeCreated, null, baseNode);

    expect(zk.getChildren(baseNode, keyWatcher)).andReturn(Collections.<String>emptyList());
    replay(instance, zk);/*from  w ww  .j  a v a2 s.  c  o m*/

    keyWatcher.process(event);

    verify(instance, zk);
    assertTrue(secretManager.getKeys().isEmpty());
}

From source file:org.apache.accumulo.server.security.delegation.ZooAuthenticationKeyWatcherTest.java

License:Apache License

@Test
public void testBaseNodeCreatedWithChildren() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeCreated, null, baseNode);
    AuthenticationKey key1 = new AuthenticationKey(1, 0l, 10000l, keyGen.generateKey()),
            key2 = new AuthenticationKey(2, key1.getExpirationDate(), 20000l, keyGen.generateKey());
    byte[] serializedKey1 = serialize(key1), serializedKey2 = serialize(key2);
    List<String> children = Arrays.asList("1", "2");

    expect(zk.getChildren(baseNode, keyWatcher)).andReturn(children);
    expect(zk.getData(baseNode + "/1", keyWatcher, null)).andReturn(serializedKey1);
    expect(zk.getData(baseNode + "/2", keyWatcher, null)).andReturn(serializedKey2);
    replay(instance, zk);/*  w  w  w.  jav a  2 s  . co m*/

    keyWatcher.process(event);

    verify(instance, zk);
    assertEquals(2, secretManager.getKeys().size());
    assertEquals(key1, secretManager.getKeys().get(key1.getKeyId()));
    assertEquals(key2, secretManager.getKeys().get(key2.getKeyId()));
}