package fault.java.concurrent;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * Created by timbrooks on 12/3/14.
 */
public class ActionThreadPoolTest {

    private ActionThreadPool threadPool;

    @Before
    public void setUp() {
        threadPool = new ActionThreadPool("Test Action", 1);
    }

    @After
    public void tearDown() {
        threadPool.shutdown();
    }

    @Test
    public void testPoolRequiresAtLeastOneThread() {
        try {
            new ActionThreadPool("Test Action", 0);
            fail();
        } catch (IllegalArgumentException e) {
            assertEquals("Cannot have fewer than 1 thread", e.getMessage());
        }
    }

    @Test
    public void testPoolPrioritizesFreeThreadsAndExecutes() {
        threadPool.shutdown();
        threadPool = new ActionThreadPool("Test Action", 2);

        final List<String> resultList = new CopyOnWriteArrayList<>();

        for (int i = 0; i < 20; ++i) {
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    resultList.add(Thread.currentThread().getName());
                }
            });

        }

        while (resultList.size() != 20) {
        }

        int threadZeroCount = 0;
        int threadOneCount = 0;
        for (String threadName : resultList) {
            if ("Test Action-0".equals(threadName)) {
                ++threadZeroCount;
            }
            if ("Test Action-1".equals(threadName)) {
                ++threadOneCount;
            }
        }

        assertEquals(10, threadZeroCount);
        assertEquals(10, threadOneCount);

    }
}

@@ -382,114 +382,8 @@
 l;%0A%0A
-    @Before%0A    public void setUp() %7B%0A        threadPool = new ActionThreadPool(%22Test Action%22, 1);%0A    %7D%0A%0A
     
@@ -389,16 +389,16 @@
  @After%0A
+
     publ
@@ -822,39 +822,8 @@
 ) %7B%0A
-        threadPool.shutdown();%0A
     
@@ -1640,16 +1640,16 @@
 Count);%0A
-
         
@@ -1679,20 +1679,574 @@
 OneCount);%0A%0A
+    %7D%0A%0A    @Test%0A    public void signallyATaskCompleteFreesUpAThread() %7B%0A        threadPool = new ActionThreadPool(%22Test Action%22, 2);%0A%0A        final List%3CString%3E resultList = new CopyOnWriteArrayList%3C%3E();%0A        Runnable action = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                resultList.add(Thread.currentThread().getName());%0A            %7D%0A        %7D;%0A%0A        threadPool.execute(action);%0A        threadPool.execute(action);%0A        threadPool.execute(action);%0A%0A        while(resultList.size() != 3) %7B%0A        %7D%0A%0A
     %7D%0A%7D%0A

implement complete signally tests for threadpool