ActiveStandbyElector implements StatCallback, StringCallback { public synchronized byte[] getActiveData() throws ActiveNotFoundException, KeeperException, InterruptedException, IOException { try { if (zkClient == null) { createConnection(); } Stat stat = new Stat(); return getDataWithRetries(zkLockFilePath, false, stat); } catch(KeeperException e) { Code code = e.code(); if (isNodeDoesNotExist(code)) { throw new ActiveNotFoundException(); } else { throw e; } } } ActiveStandbyElector(String zookeeperHostPorts,
      int zookeeperSessionTimeout, String parentZnodeName, List<ACL> acl,
      List<ZKAuthInfo> authInfo,
      ActiveStandbyElectorCallback app, int maxRetryNum); synchronized void joinElection(byte[] data); synchronized boolean parentZNodeExists(); synchronized void ensureParentZNode(); synchronized void clearParentZNode(); synchronized void quitElection(boolean needFence); synchronized byte[] getActiveData(); @Override synchronized void processResult(int rc, String path, Object ctx,
      String name); @Override synchronized void processResult(int rc, String path, Object ctx,
      Stat stat); @InterfaceAudience.Private synchronized void terminateConnection(); @Override String toString(); String getHAZookeeperConnectionState(); static final Log LOG; }
@Test public void testGetActiveData() throws ActiveNotFoundException, KeeperException, InterruptedException, IOException { byte[] data = new byte[8]; Mockito.when( mockZK.getData(Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false), Mockito.<Stat> anyObject())).thenReturn(data); Assert.assertEquals(data, elector.getActiveData()); Mockito.verify(mockZK, Mockito.times(1)).getData( Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false), Mockito.<Stat> anyObject()); Mockito.when( mockZK.getData(Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false), Mockito.<Stat> anyObject())).thenThrow( new KeeperException.NoNodeException()); try { elector.getActiveData(); Assert.fail("ActiveNotFoundException expected"); } catch(ActiveNotFoundException e) { Mockito.verify(mockZK, Mockito.times(2)).getData( Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false), Mockito.<Stat> anyObject()); } try { Mockito.when( mockZK.getData(Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false), Mockito.<Stat> anyObject())).thenThrow( new KeeperException.AuthFailedException()); elector.getActiveData(); Assert.fail("KeeperException.AuthFailedException expected"); } catch(KeeperException.AuthFailedException ke) { Mockito.verify(mockZK, Mockito.times(3)).getData( Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false), Mockito.<Stat> anyObject()); } }