NvaLeaderSelectorListenerAdapter extends LeaderSelectorListenerAdapter implements AutoCloseable { @Override public void takeLeadership(CuratorFramework curatorFramework) throws Exception { log.info("Leadership acquired"); try { log.info("Starting nvaMonitor"); Future<Void> task = nvaMonitor.start(); task.get(); log.debug("task.get() returned"); } catch (InterruptedException e) { log.info("takeLeadership interrupted"); Thread.currentThread().interrupt(); } catch (NvaMonitorException e){ log.error("Error creating NvaMonitor", e); throw e; } catch (ExecutionException e) { log.error("Error executing NvaMonitor", e); throw e; } finally { this.nvaMonitor.stop(); } } NvaLeaderSelectorListenerAdapter(NvaDaemonConfiguration configuration,
                                            CuratorFramework client); synchronized void start(); synchronized void close(); @Override void takeLeadership(CuratorFramework curatorFramework);  }
@Test void testTakeLeadership() throws Exception { ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("connection-string", "/leader-selector-path", null, null); List<MonitorConfiguration> monitors = new ArrayList<>(); Map<String, Object> settings = new HashMap<>(); settings.put("probeReturnValue", "false"); settings.put("awaitTime", "2000"); monitors.add( new MonitorConfiguration( "com.microsoft.azure.practices.nvadaemon.NvaMonitorTest$ScheduledMonitorValidConstructor", settings)); DaemonConfiguration daemonConfiguration = new DaemonConfiguration(monitors, null); NvaDaemonConfiguration nvaDaemonConfiguration = new NvaDaemonConfiguration( zookeeperConfiguration, daemonConfiguration); CuratorFramework curatorFramework = mock(CuratorFramework.class); NvaLeaderSelectorListenerAdapter nvaLeaderSelectorListenerAdapter = new NvaLeaderSelectorListenerAdapter(nvaDaemonConfiguration, curatorFramework); ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<Void> task = executorService.submit( () -> { nvaLeaderSelectorListenerAdapter.takeLeadership(curatorFramework); return null; }); Thread.sleep(3000); Assertions.assertThrows(IllegalStateException.class, () -> nvaLeaderSelectorListenerAdapter.close()); task.get(); }
@Test void testTakeLeadershipInterrupted() throws Exception { ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("connection-string", "/leader-selector-path", null, null); List<MonitorConfiguration> monitors = new ArrayList<>(); Map<String, Object> settings = new HashMap<>(); settings.put("constructorShouldThrowException", "false"); settings.put("shouldCauseInterrupt", "false"); monitors.add( new MonitorConfiguration( "com.microsoft.azure.practices.nvadaemon.NvaMonitorTest$MonitorValidConstructor", settings)); DaemonConfiguration daemonConfiguration = new DaemonConfiguration(monitors, null); NvaDaemonConfiguration nvaDaemonConfiguration = new NvaDaemonConfiguration( zookeeperConfiguration, daemonConfiguration); CuratorFramework curatorFramework = mock(CuratorFramework.class); NvaLeaderSelectorListenerAdapter nvaLeaderSelectorListenerAdapter = new NvaLeaderSelectorListenerAdapter(nvaDaemonConfiguration, curatorFramework); AtomicReference<Thread> thread = new AtomicReference<>(); ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<Void> task = executorService.submit( () -> { thread.set(Thread.currentThread()); nvaLeaderSelectorListenerAdapter.takeLeadership(curatorFramework); return null; }); Thread.sleep(3000); thread.get().interrupt(); task.get(); }
@Test void testTakeLeadershipExecutionException() throws Exception { ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("connection-string", "/leader-selector-path", null, null); List<MonitorConfiguration> monitors = new ArrayList<>(); Map<String, Object> settings = new HashMap<>(); settings.put(NvaMonitorTest.MonitorValidConstructor.INIT_SHOULD_THROW_EXCEPTION, "true"); monitors.add( new MonitorConfiguration( "com.microsoft.azure.practices.nvadaemon.NvaMonitorTest$MonitorValidConstructor", settings)); DaemonConfiguration daemonConfiguration = new DaemonConfiguration(monitors, null); NvaDaemonConfiguration nvaDaemonConfiguration = new NvaDaemonConfiguration( zookeeperConfiguration, daemonConfiguration); CuratorFramework curatorFramework = mock(CuratorFramework.class); NvaLeaderSelectorListenerAdapter nvaLeaderSelectorListenerAdapter = new NvaLeaderSelectorListenerAdapter(nvaDaemonConfiguration, curatorFramework); ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<Void> task = executorService.submit( () -> { nvaLeaderSelectorListenerAdapter.takeLeadership(curatorFramework); return null; }); Assertions.assertThrows(ExecutionException.class, () -> task.get()); }
@Test void testTakeLeadershipNvaMonitorException() throws Exception { ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("connection-string", "/leader-selector-path", null, null); List<MonitorConfiguration> monitors = new ArrayList<>(); monitors.add( new MonitorConfiguration( "com.microsoft.azure.practices.nvadaemon.NvaMonitorTest$InvalidMonitor", null)); DaemonConfiguration daemonConfiguration = new DaemonConfiguration(monitors, null); NvaDaemonConfiguration nvaDaemonConfiguration = new NvaDaemonConfiguration( zookeeperConfiguration, daemonConfiguration); CuratorFramework curatorFramework = mock(CuratorFramework.class); NvaLeaderSelectorListenerAdapter nvaLeaderSelectorListenerAdapter = new NvaLeaderSelectorListenerAdapter(nvaDaemonConfiguration, curatorFramework); Assertions.assertThrows(NvaMonitor.NvaMonitorException.class, () -> nvaLeaderSelectorListenerAdapter.takeLeadership(curatorFramework)); }