MemoryBasedConnectionLimitStrategy implements ConnectionLimitStrategy { @Override public int getRecommendedLimit() { final long recommendedLimit = (maxMemory - memoryRequiredToStart) / memoryRequiredPerConnection; if (recommendedLimit <= 0) { LOG.warn("Not enough memory. It is recommended to provide more than {} MB (currently {}).", (memoryRequiredToStart + memoryRequiredPerConnection) / 1_000_000, getResourcesDescription()); return 0; } else if (recommendedLimit > Integer.MAX_VALUE) { return Integer.MAX_VALUE; } else { return (int) recommendedLimit; } } MemoryBasedConnectionLimitStrategy(final long memoryRequiredToStart, final long memoryRequiredPerConnection);   MemoryBasedConnectionLimitStrategy(final long memoryRequiredToStart, final long memoryRequiredPerConnection,
            final long maxMemory); @Override int getRecommendedLimit(); @Override String getResourcesDescription();  }
@Test public void testGetRecommendedConnectionLimitLowerLimit() { assertEquals(0, new MemoryBasedConnectionLimitStrategy(MINIMAL_MEMORY, MEMORY_PER_CONNECTION, MINIMAL_MEMORY * -2).getRecommendedLimit()); }
@Test public void testGetRecommendedConnectionLimit() { final int requiredFor1Connection = MINIMAL_MEMORY + MEMORY_PER_CONNECTION; assertEquals(0, new MemoryBasedConnectionLimitStrategy(MINIMAL_MEMORY, MEMORY_PER_CONNECTION, requiredFor1Connection - 1).getRecommendedLimit()); assertEquals(1, new MemoryBasedConnectionLimitStrategy(MINIMAL_MEMORY, MEMORY_PER_CONNECTION, requiredFor1Connection).getRecommendedLimit()); final int requiredFor2Connections = requiredFor1Connection + MEMORY_PER_CONNECTION; assertEquals(1, new MemoryBasedConnectionLimitStrategy(MINIMAL_MEMORY, MEMORY_PER_CONNECTION, requiredFor2Connections - 1).getRecommendedLimit()); assertEquals(2, new MemoryBasedConnectionLimitStrategy(MINIMAL_MEMORY, MEMORY_PER_CONNECTION, requiredFor2Connections).getRecommendedLimit()); }
@Test public void testGetRecommendedConnectionLimitUpperLimit() { final long biggerThanMaxInt = Long.MAX_VALUE; assertEquals(Integer.MAX_VALUE, new MemoryBasedConnectionLimitStrategy(MINIMAL_MEMORY, MEMORY_PER_CONNECTION, biggerThanMaxInt).getRecommendedLimit()); }