diff --git a/src/io/aleph/dirigiste/Pool.java b/src/io/aleph/dirigiste/Pool.java index de92fee..365d8b2 100644 --- a/src/io/aleph/dirigiste/Pool.java +++ b/src/io/aleph/dirigiste/Pool.java @@ -346,7 +346,7 @@ private void adjust() { q.drop(); } q.cleanup(); - } else if (n > 1) { + } else if (n > 0) { for (int i = 0; i < n; i++) { upward.add(entry.getKey()); } diff --git a/test/java/io/aleph/dirigiste/PoolTest.java b/test/java/io/aleph/dirigiste/PoolTest.java index 1a0f454..8c4c162 100644 --- a/test/java/io/aleph/dirigiste/PoolTest.java +++ b/test/java/io/aleph/dirigiste/PoolTest.java @@ -219,6 +219,15 @@ public void testPoolWithSimpleUtilizationExecutor() throws InterruptedException assertNull(pool._queues.get(KEY)); } + @Test + public void testPoolIncByOne() throws InterruptedException { + Pool pool = newPool(incByOneController(), generator(), 1 ,10, 100); + pool.acquire(KEY); + // Let's wait a bit it has been adjusted + Thread.sleep(150); + assertEquals(pool._queues.get(KEY).objects.get(), 2); + } + @Test public void testPoolOnAHighlyConcurrentEnvironment() throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(30); @@ -329,6 +338,10 @@ private Pool newPool(Controller controller, Generator(generator, controller, maxQueueSize, 10, 1000, TimeUnit.MICROSECONDS); } + private Pool newPool(Controller controller, Generator generator, int maxQueueSize, int samplePeriod, int controlPeriod) { + return new Pool<>(generator, controller, maxQueueSize, samplePeriod, controlPeriod, TimeUnit.MILLISECONDS); + } + private double getUtilization(Pool pool) { return pool.getUtilization(pool.queue(KEY).availableObjectsCount(), pool.queue(KEY).getQueueLength(), pool.queue(KEY).objects.get()); } @@ -376,6 +389,20 @@ public void destroy(Key key, Value val) { } private Controller noopController() { + return new Controller() { + @Override + public boolean shouldIncrement(Key key, int objectsForKey, int totalObjects) { + return true; + } + + @Override + public Map adjustment(Map stats) { + return stats.entrySet().stream().collect(toMap(Map.Entry::getKey, __ -> 0)); + } + }; + } + + private Controller incByOneController() { return new Controller() { @Override public boolean shouldIncrement(Key key, int objectsForKey, int totalObjects) {