ElasticsearchMemoryHealthy { public boolean isMemoryHealthy(long limitBytes) { final long memoryUsage = this.memoryUsage.usage(); log.debug("Memory usage:{}, limit:{}", memoryUsage, limitBytes); if (memoryUsage < limitBytes) { return true; } else { log.warn("Memory usage:{} exceed limit:{}", memoryUsage, limitBytes); if (randomFail.shouldFail()) { log.warn("Fast failure the current request"); throw new MemoryUsageExceedFastFailureException(); } else { throw new MemoryUsageExceedException(); } } } ElasticsearchMemoryHealthy(); @VisibleForTesting  ElasticsearchMemoryHealthy(
      RandomFail randomFail,
      MemoryUsage memoryUsage); boolean isMemoryHealthy(long limitBytes);  }
@Test void isMemoryHealthy() { when(memoryUsage.usage()).thenReturn(10L); assertTrue(monitor.isMemoryHealthy(11L)); }
@Test void memoryUsageExceedLimitFastFailure() { when(memoryUsage.usage()).thenReturn(10L); when(randomFail.shouldFail()).thenReturn(true); assertThrows(ElasticsearchMemoryHealthy.MemoryUsageExceedFastFailureException.class, () -> monitor.isMemoryHealthy(9L)); }
@Test void memoryUsageExceedLimitWithoutFastFailure() { when(memoryUsage.usage()).thenReturn(10L); when(randomFail.shouldFail()).thenReturn(false); assertThrows(ElasticsearchMemoryHealthy.MemoryUsageExceedException.class, () -> monitor.isMemoryHealthy(9L)); }