package com.wizzardo.tools.misc.pool;

import com.wizzardo.tools.misc.Supplier;
import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by wizzardo on 10/07/16.
 */
public class PoolBuilderTest {

    @Test
    public void test_initSize() {
        final AtomicInteger counter = new AtomicInteger();
        Pool<Integer> pool = new PoolBuilder<Integer>()
                .supplier(new Supplier<Integer>() {
                    @Override
                    public Integer supply() {
                        return counter.incrementAndGet();
                    }
                })
                .queue(PoolBuilder.<Integer>createSharedQueueSupplier())
                .initialSize(1)
                .build();

        Assert.assertEquals(1, pool.size());
        Assert.assertEquals(1, counter.get());

    }
}

@@ -859,16 +859,2770 @@
 get());%0A
+    %7D%0A%0A    @Test%0A    public void test_limitSize() %7B%0A        final AtomicInteger counter = new AtomicInteger();%0A        final Pool%3CInteger%3E pool = new PoolBuilder%3CInteger%3E()%0A                .supplier(new Supplier%3CInteger%3E() %7B%0A                    @Override%0A                    public Integer supply() %7B%0A                        return counter.incrementAndGet();%0A                    %7D%0A                %7D)%0A                .queue(PoolBuilder.%3CInteger%3EcreateSharedQueueSupplier())%0A                .limitSize(1)%0A                .build();%0A%0A        new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                Holder%3CInteger%3E holder = pool.holder();%0A                Integer value = 0;%0A                try %7B%0A                    value = holder.get();%0A                    Thread.sleep(100);%0A                %7D catch (Exception ignored) %7B%0A                %7D finally %7B%0A                    holder.close();%0A                %7D%0A                Assert.assertEquals(1, value.intValue());%0A            %7D%0A        %7D).start();%0A%0A        long time = System.currentTimeMillis();%0A        try %7B%0A            Thread.sleep(10);%0A        %7D catch (InterruptedException ignore) %7B%0A        %7D%0A%0A        Assert.assertEquals(1, pool.get().intValue());%0A        time = System.currentTimeMillis() - time;%0A        Assert.assertTrue(time %3E= 100);%0A        Assert.assertEquals(1, counter.get());%0A    %7D%0A%0A    @Test%0A    public void test_limitSize_2() %7B%0A        final AtomicInteger counter = new AtomicInteger();%0A        final Pool%3CInteger%3E pool = new PoolBuilder%3CInteger%3E()%0A                .supplier(new Supplier%3CInteger%3E() %7B%0A                    @Override%0A                    public Integer supply() %7B%0A                        return counter.incrementAndGet();%0A                    %7D%0A                %7D)%0A                .queue(PoolBuilder.%3CInteger%3EcreateSharedQueueSupplier())%0A                .limitSize(1)%0A                .build();%0A%0A        new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                Integer value = pool.provide(new Pool.Consumer%3CInteger, Integer%3E() %7B%0A                    @Override%0A                    public Integer consume(Integer integer) throws Exception %7B%0A                        Thread.sleep(100);%0A                        return integer;%0A                    %7D%0A                %7D);%0A                Assert.assertEquals(1, value.intValue());%0A            %7D%0A        %7D).start();%0A%0A        long time = System.currentTimeMillis();%0A        try %7B%0A            Thread.sleep(10);%0A        %7D catch (InterruptedException ignore) %7B%0A        %7D%0A%0A        Assert.assertEquals(1, pool.get().intValue());%0A        time = System.currentTimeMillis() - time;%0A        Assert.assertTrue(time %3E= 100);%0A        Assert.assertEquals(1, counter.get());
 %0A    %7D%0A%7D

add tests for limit