ClusterDiskSpaceChecker implements DiskSpaceChecker { @Override public boolean isSpaceAvailable() throws DiskSpaceCheckerException { Map<String, DiskSpace> clusterSpace = clusterManager.getDiskSpace(); boolean withinThreshold = true; for (String server : clusterManager.getServers()) { DiskSpace ds = clusterSpace.get(server); if (ds == null) { LOGGER.warn("No DiskSpace details for {} - returning false", server); withinThreshold = false; break; } else if (!threshold.withinThreshold(ds)) { LOGGER.debug("Server {} does not have enough space", server); withinThreshold = false; } } LOGGER.debug("Cluster threshold check returning {}", withinThreshold); return withinThreshold; } ClusterDiskSpaceChecker(ClusterDiskSpaceManager clusterManager); @Override boolean isSpaceAvailable(); @Override void configure(Map<String, Object> configuration); @Override boolean requiresHttpClient(); @Override void setHttpClient(Client httpClient); @Override void setThreshold(DiskSpaceThreshold threshold); static final String CLUSTER_SERVERS_CONFIG_OPTION; }
@Test public void returnsFalseWhenNoDataForServer() throws Exception { final DiskSpace server0Space = new DiskSpace(128L, 1024L); spaceMap.put(servers[0], server0Space); when(threshold.withinThreshold(server0Space)).thenReturn(true); boolean isSpace = checker.isSpaceAvailable(); assertThat(isSpace).isFalse(); verify(threshold).withinThreshold(server0Space); verify(clusterManager).getServers(); verify(clusterManager).getDiskSpace(); }
@Test public void returnsFalseWhenOneServerFails() throws Exception { final DiskSpace server0Space = new DiskSpace(128L, 1024L); spaceMap.put(servers[0], server0Space); final DiskSpace server1Space = new DiskSpace(128L, 512L); spaceMap.put(servers[1], server1Space); when(threshold.withinThreshold(server0Space)).thenReturn(true); when(threshold.withinThreshold(server1Space)).thenReturn(false); boolean isSpace = checker.isSpaceAvailable(); assertThat(isSpace).isFalse(); verify(threshold).withinThreshold(server0Space); verify(threshold).withinThreshold(server1Space); verify(clusterManager).getServers(); verify(clusterManager).getDiskSpace(); }
@Test public void returnsTrueWhenServersPass() throws Exception { final DiskSpace server0Space = new DiskSpace(128L, 1024L); spaceMap.put(servers[0], server0Space); final DiskSpace server1Space = new DiskSpace(128L, 512L); spaceMap.put(servers[1], server1Space); when(threshold.withinThreshold(server0Space)).thenReturn(true); when(threshold.withinThreshold(server1Space)).thenReturn(true); boolean isSpace = checker.isSpaceAvailable(); assertThat(isSpace).isTrue(); verify(threshold).withinThreshold(server0Space); verify(threshold).withinThreshold(server1Space); verify(clusterManager).getServers(); verify(clusterManager).getDiskSpace(); }