/**
 * 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.assertEquals;
import static org.neo4j.test.Mute.muteAll;

import java.util.concurrent.CountDownLatch;

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 numCores = 1;
        int configuredMaxThreads = 12; // 12 is the new min max Threads value, for one core
        server.setMaxThreads( numCores );
        server.setPort( 7480 );
        try
        {
            server.start();
            QueuedThreadPool threadPool = (QueuedThreadPool) server.getJetty().getThreadPool();
            threadPool.start();
            CountDownLatch cb = loadThreadPool( threadPool, configuredMaxThreads + 1 );
            Thread.sleep( 10 ); // Wait for threadPool to create threads
            int threads = threadPool.getThreads();
            assertEquals( "Wrong number of threads in pool", configuredMaxThreads, threads );
            cb.countDown();
        }
        finally
        {
            server.stop();
        }
    }

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

@@ -825,97 +825,8 @@
 b;%0A%0A
-import static org.junit.Assert.assertEquals;%0Aimport static org.neo4j.test.Mute.muteAll;%0A%0A
 impo
@@ -967,16 +967,17 @@
 t.Test;%0A
+%0A
 import o
@@ -1052,16 +1052,106 @@
 .Mute;%0A%0A
+import static org.junit.Assert.assertEquals;%0A%0Aimport static org.neo4j.test.Mute.muteAll;%0A%0A
 public c
@@ -1522,16 +1522,309 @@
 ne core%0A
+        int acceptorThreads = 1; // In this configuration, 1 thread will become an acceptor...%0A        int selectorThreads = 1; // ... and 1 thread will become a selector...%0A        int jobThreads = configuredMaxThreads - acceptorThreads - selectorThreads; // ... and the rest are job threads%0A
         
@@ -2094,18 +2094,98 @@
 wnLatch 
-cb
+startLatch = new CountDownLatch( jobThreads );%0A            CountDownLatch endLatch
  = loadT
@@ -2231,16 +2231,28 @@
 eads + 1
+, startLatch
  );%0A    
@@ -2263,25 +2263,25 @@
     
-Thread.sleep( 10 
+startLatch.await(
 ); /
@@ -2477,18 +2477,24 @@
         
-cb
+endLatch
 .countDo
@@ -2621,16 +2621,28 @@
 eadPool(
+%0A           
  QueuedT
@@ -2662,16 +2662,28 @@
 eadPool,
+%0A           
  int tas
@@ -2692,16 +2692,61 @@
 ToSubmit
+,%0A            final CountDownLatch startLatch
  )%0A    %7B
@@ -2775,18 +2775,24 @@
 wnLatch 
-cb
+endLatch
  = new C
@@ -3003,32 +3003,76 @@
               %7B%0A
+                    startLatch.countDown();%0A
                 
@@ -3125,18 +3125,24 @@
         
-cb
+endLatch
 .await()
@@ -3371,10 +3371,16 @@
 urn 
-cb
+endLatch
 ;%0A  

Fix the flakiness of JettyThreadLimitTest