/**
 * Copyright (c) 2002-2014 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.server.web;

import static org.junit.Assert.assertTrue;
import static org.neo4j.test.Mute.muteAll;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.kernel.logging.DevNullLoggingService;
import org.neo4j.test.Mute;

public class JettyThreadLimitTest
{
    @Rule
    public Mute mute = muteAll();

    @Test
    public void shouldHaveConfigurableJettyThreadPoolSize() throws Exception
    {
    	Jetty9WebServer server = new Jetty9WebServer( DevNullLoggingService.DEV_NULL );
        int maxThreads = 13; // 12 is the new min maxThreads value
        server.setMaxThreads( maxThreads );
        server.setPort( 7480 );
        try {
	        server.start();
	        QueuedThreadPool threadPool = (QueuedThreadPool) server.getJetty().getThreadPool();
	        threadPool.start();
            int configuredMaxThreads = maxThreads * 4;
            loadThreadPool( threadPool, configuredMaxThreads + 1);
	        int threads = threadPool.getThreads();
	        assertTrue( threads <= maxThreads );
        }
        finally
        {
        	server.stop();
        }
    }

    private void loadThreadPool(QueuedThreadPool threadPool, int tasksToSubmit)
    {
        final CyclicBarrier cb = new CyclicBarrier(tasksToSubmit);
        for ( int i = 0; i < tasksToSubmit; i++ )
        {
            threadPool.dispatch( new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        cb.await();
                    }
                    catch ( InterruptedException e )
                    {
                        e.printStackTrace();
                    }
                    catch ( BrokenBarrierException e )
                    {
                        e.printStackTrace();
                    }
                }
            } );
        }
    }
}

@@ -821,16 +821,61 @@
 r.web;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import s
@@ -1005,16 +1005,60 @@
 eption;%0A
+import java.util.concurrent.CountDownLatch;%0A
 import j
@@ -1456,17 +1456,20 @@
   %7B%0A    
-%09
+    
 Jetty9We
@@ -1552,17 +1552,53 @@
     int 
-m
+numCores = 1;%0A        int configuredM
 axThread
@@ -1602,17 +1602,17 @@
 eads = 1
-3
+2
 ; // 12 
@@ -1625,24 +1625,25 @@
  new min max
+ 
 Threads valu
@@ -1643,16 +1643,30 @@
 ds value
+, for one core
 %0A       
@@ -1688,25 +1688,23 @@
 hreads( 
-maxThread
+numCore
 s );%0A   
@@ -1747,12 +1747,23 @@
  try
- %7B%0A%09
+%0A        %7B%0A    
     
@@ -1778,25 +1778,28 @@
 er.start();%0A
-%09
+    
         Queu
@@ -1874,25 +1874,28 @@
 readPool();%0A
-%09
+    
         thre
@@ -1914,35 +1914,79 @@
 ();%0A            
-int
+CountDownLatch cb = loadThreadPool( threadPool,
  configuredMaxTh
@@ -1995,24 +1995,13 @@
 ads 
-= maxThreads * 4
++ 1 )
 ;%0A  
@@ -2014,64 +2014,74 @@
     
-load
 Thread
-Pool( threadPool, configuredMaxThreads + 1);%0A%09
+.sleep( 10 ); // Wait for threadPool to create threads %0A    
     
@@ -2127,17 +2127,19 @@
 ();%0A
-%09
+   
         
 asse
@@ -2138,42 +2138,116 @@
     
+ 
 assert
-True( threads %3C= maxThreads 
+Equals( %22Wrong number of threads in pool%22, configuredMaxThreads, threads );%0A            cb.countDown(
 );%0A 
@@ -2293,9 +2293,12 @@
     
-%09
+    
 serv
@@ -2337,20 +2337,30 @@
 private 
-void
+CountDownLatch
  loadThr
@@ -2367,16 +2367,17 @@
 eadPool(
+ 
 QueuedTh
@@ -2414,16 +2414,17 @@
 ToSubmit
+ 
 )%0A    %7B%0A
@@ -2442,57 +2442,49 @@
 al C
-yclicBarrier cb = new CyclicBarrier(tasksToSubmit
+ountDownLatch cb = new CountDownLatch( 1 
 );%0A 
@@ -2932,152 +2932,8 @@
   %7D%0A
-                    catch ( BrokenBarrierException e )%0A                    %7B%0A                        e.printStackTrace();%0A                    %7D%0A
     
@@ -2969,20 +2969,39 @@
 ;%0A        %7D%0A
+        return cb;%0A
     %7D%0A%7D%0A

Improve JettyThreadLimitTest It should now fail less and catch more errors