ClusterDiskSpaceManagerHealthCheck extends HealthCheck { @Override protected Result check() throws Exception { final Result result; StringBuilder message = new StringBuilder(); boolean healthy = true; for (String server : manager.getServers()) { if (message.length() > 0) { message.append("; "); } if (manager.getDiskSpace().get(server) == null) { message.append(server).append(" : no disk space recorded"); healthy = false; } else { DiskSpace disk = manager.getDiskSpace().get(server); Date checkDate = new Date(System.currentTimeMillis() - (EXPIRY_TIME_MINS * 60 * 1000)); long updateInterval = (System.currentTimeMillis() - disk.getCreationDate().getTime()) / 1000; if (disk.getCreationDate().before(checkDate)) { healthy = false; } message.append(server).append(" : last updated ").append(updateInterval).append(" seconds ago"); } } message.append("."); if (healthy) { result = Result.healthy(message.toString()); } else { result = Result.unhealthy(message.toString()); } return result; } ClusterDiskSpaceManagerHealthCheck(ClusterDiskSpaceManager manager);   }
@Test public void returnsUnhealthyWhenDiskStatsMissing() throws Exception { diskSpaceMap.put(serverNames[0], new DiskSpace(123L, 1024L)); HealthCheck.Result result = healthCheck.check(); assertThat(result.isHealthy()).isFalse(); }
@Test public void returnsUnhealthyWhenDiskStatsOutOfDate() throws Exception { diskSpaceMap.put(serverNames[0], new DiskSpace(123L, 1024L)); DiskSpace disk2 = mock(DiskSpace.class); when(disk2.getCreationDate()).thenReturn(new Date( System.currentTimeMillis() - ((ClusterDiskSpaceManagerHealthCheck.EXPIRY_TIME_MINS + 1) * 60 * 1000))); diskSpaceMap.put(serverNames[1], disk2); HealthCheck.Result result = healthCheck.check(); assertThat(result.isHealthy()).isFalse(); }
@Test public void returnsHealthyWhenDiskStatsAvailable() throws Exception { diskSpaceMap.put(serverNames[0], new DiskSpace(123L, 1024L)); diskSpaceMap.put(serverNames[1], new DiskSpace(456L, 4560L)); HealthCheck.Result result = healthCheck.check(); assertThat(result.isHealthy()).isTrue(); }