public void resize(int size) {
        int limit = size == TransferConnectionLimiter.AUTO ?
                PreferencesFactory.get().getInteger("queue.connections.limit.default") : size;
        if(log.isDebugEnabled()) {
            log.debug(String.format("Resize queue to %d", limit));
        }
        if(limit < permits) {
            // Reduce number of permits
            semaphore.reducePermits(permits - limit);
        }
        else {
            // Increase number of permits
            semaphore.release(limit - permits);
        }
        this.permits = limit;
    }
@Test
    public void testResize() throws Exception {
        final TransferQueue queue = new TransferQueue(1);
        final DownloadTransfer d1 = new DownloadTransfer(new Host(new TestProtocol()), new Path("/t1", EnumSet.of(Path.Type.directory)), null);
        final DownloadTransfer d2 = new DownloadTransfer(new Host(new TestProtocol()), new Path("/t2", EnumSet.of(Path.Type.directory)), null);
        queue.add(d1, new DisabledProgressListener());
        final CountDownLatch c = new CountDownLatch(1);
        final AtomicBoolean set = new AtomicBoolean();
        new Thread(new Runnable() {
            @Override
            public void run() {
                queue.add(d2, new DisabledProgressListener() {
                    @Override
                    public void message(final String message) {
                        assertEquals("Maximum allowed connections exceeded. Waiting", message);
                        set.set(true);
                    }
                });
                c.countDown();
            }
        }).start();
        assertEquals(1, c.getCount());
        assertFalse(c.await(10, TimeUnit.SECONDS));
        queue.resize(2);
        assertTrue(c.await(10, TimeUnit.SECONDS));
        assertTrue(set.get());
    }