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:org.apache.accumulo.server.security.delegation.ZooAuthenticationKeyWatcherTest.java

License:Apache License

@Test
public void testBaseNodeChildrenChanged() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeChildrenChanged, 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 . j  a va2s.c  o  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()));
}

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

License:Apache License

@Test
public void testBaseNodeDeleted() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeDeleted, null, baseNode);
    AuthenticationKey key1 = new AuthenticationKey(1, 0l, 10000l, keyGen.generateKey()),
            key2 = new AuthenticationKey(2, key1.getExpirationDate(), 20000l, keyGen.generateKey());

    secretManager.addKey(key1);//w ww .  j  a  v  a  2  s.  c o m
    secretManager.addKey(key2);
    assertEquals(2, secretManager.getKeys().size());

    replay(instance, zk);

    keyWatcher.process(event);

    verify(instance, zk);
    assertEquals(0, secretManager.getKeys().size());
    assertFalse(secretManager.isCurrentKeySet());
}

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

License:Apache License

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

    replay(instance, zk);//from  w ww. ja  va 2s .c  om

    keyWatcher.process(event);

    verify(instance, zk);
    assertEquals(0, secretManager.getKeys().size());
    assertFalse(secretManager.isCurrentKeySet());
}

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

License:Apache License

@Test
public void testChildChanged() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeCreated, null, baseNode + "/2");
    AuthenticationKey key1 = new AuthenticationKey(1, 0l, 10000l, keyGen.generateKey()),
            key2 = new AuthenticationKey(2, key1.getExpirationDate(), 20000l, keyGen.generateKey());
    secretManager.addKey(key1);//from   w  w  w .  jav  a  2s. c om
    assertEquals(1, secretManager.getKeys().size());
    byte[] serializedKey2 = serialize(key2);

    expect(zk.getData(event.getPath(), keyWatcher, null)).andReturn(serializedKey2);
    replay(instance, zk);

    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()));
    assertEquals(key2, secretManager.getCurrentKey());
}

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

License:Apache License

@Test
public void testChildDeleted() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeDeleted, null, baseNode + "/1");
    AuthenticationKey key1 = new AuthenticationKey(1, 0l, 10000l, keyGen.generateKey()),
            key2 = new AuthenticationKey(2, key1.getExpirationDate(), 20000l, keyGen.generateKey());
    secretManager.addKey(key1);/*from  ww  w .j av  a2s  .c  om*/
    secretManager.addKey(key2);
    assertEquals(2, secretManager.getKeys().size());

    replay(instance, zk);

    keyWatcher.process(event);

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

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

License:Apache License

@Test
public void testChildChildrenChanged() throws Exception {
    WatchedEvent event = new WatchedEvent(EventType.NodeChildrenChanged, null, baseNode + "/2");
    AuthenticationKey key1 = new AuthenticationKey(1, 0l, 10000l, keyGen.generateKey()),
            key2 = new AuthenticationKey(2, key1.getExpirationDate(), 20000l, keyGen.generateKey());
    secretManager.addKey(key1);/*from  www.j  a v a  2  s  . com*/
    secretManager.addKey(key2);
    assertEquals(2, secretManager.getKeys().size());

    replay(instance, zk);

    // Does nothing
    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()));
    assertEquals(key2, secretManager.getCurrentKey());
}

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

License:Apache License

@Test
public void testDisconnectAndReconnect() throws Exception {
    lostZooKeeperBase(new WatchedEvent(EventType.None, KeeperState.Disconnected, null),
            new WatchedEvent(EventType.None, KeeperState.SyncConnected, null));
}

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

License:Apache License

@Test
public void testExpiredAndReconnect() throws Exception {
    lostZooKeeperBase(new WatchedEvent(EventType.None, KeeperState.Expired, null),
            new WatchedEvent(EventType.None, KeeperState.SyncConnected, null));
}

From source file:org.apache.aries.rsa.discovery.zookeeper.subscribe.InterfaceMonitorTest.java

License:Apache License

public void testInterfaceMonitor() throws KeeperException, InterruptedException {
    IMocksControl c = EasyMock.createControl();

    ZooKeeper zk = c.createMock(ZooKeeper.class);
    expect(zk.getState()).andReturn(ZooKeeper.States.CONNECTED).anyTimes();

    String scope = "(myProp=test)";
    String interf = "es.schaaf.test";
    String node = Utils.getZooKeeperPath(interf);

    EndpointListener endpointListener = c.createMock(EndpointListener.class);
    InterfaceMonitor im = new InterfaceMonitor(zk, interf, endpointListener, scope);
    zk.exists(eq(node), eq(im), eq(im), EasyMock.anyObject());
    EasyMock.expectLastCall().once();/*  ww w  . j a  va  2s .c  o m*/

    expect(zk.exists(eq(node), eq(false))).andReturn(new Stat()).anyTimes();
    expect(zk.getChildren(eq(node), eq(false))).andReturn(Collections.<String>emptyList()).once();
    expect(zk.getChildren(eq(node), eq(im))).andReturn(Collections.<String>emptyList()).once();

    c.replay();
    im.start();
    // simulate a zk callback
    WatchedEvent we = new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected, node);
    im.process(we);
    c.verify();
}

From source file:org.apache.bookkeeper.zookeeper.MockZooKeeperTestCase.java

License:Apache License

protected boolean notifyWatchedEvent(EventType eventType, KeeperState keeperState, String path) {
    Set<Watcher> watcherSet = watchers.remove(path);
    if (null == watcherSet) {
        return false;
    }// ww  w .ja va  2  s  . c o  m
    WatchedEvent event = new WatchedEvent(eventType, keeperState, path);
    for (Watcher watcher : watcherSet) {
        watcher.process(event);
    }
    return true;
}