/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gemstone.gemfire.internal.cache;

import static org.junit.Assert.*;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.gemstone.gemfire.test.fake.Fakes;
import com.gemstone.gemfire.test.junit.categories.UnitTest;
import com.jayway.awaitility.Awaitility;

@Category(UnitTest.class)
public class GemFireCacheImplTest {

  @Test
  public void checkThatAsyncEventListenersUseAllThreadsInPool() {
    
    GemFireCacheImpl gfc = GemFireCacheImpl.createWithAsyncEventListeners(Fakes.distributedSystem(), new CacheConfig());
    ThreadPoolExecutor executor = (ThreadPoolExecutor) gfc.getEventThreadPool();
    final long initialCount = executor.getCompletedTaskCount();
    try {
      int MAX_THREADS = GemFireCacheImpl.EVENT_THREAD_LIMIT;
      final CountDownLatch cdl = new CountDownLatch(MAX_THREADS);
      for (int i = 1; i <= MAX_THREADS; i++) {
        Runnable r = new Runnable() {
          @Override
          public void run() {
            cdl.countDown();
            try {
              cdl.await();
            } catch (InterruptedException e) {
            }
          }
        };
        executor.execute(r);
      }
      Awaitility.await().pollInterval(10, TimeUnit.MILLISECONDS).pollDelay(10, TimeUnit.MILLISECONDS).timeout(15, TimeUnit.SECONDS)
      .until(() -> {
        return executor.getCompletedTaskCount() == MAX_THREADS+initialCount;
      });
    } finally {
      executor.shutdown();
    }
  }

}

@@ -1008,16 +1008,65 @@
 eUnit;%0A%0A
+import org.junit.After;%0Aimport org.junit.Before;%0A
 import o
@@ -1076,24 +1076,24 @@
 junit.Test;%0A
-
 import org.j
@@ -1542,16 +1542,26 @@
 fig());%0A
+    try %7B%0A
     Thre
@@ -1697,26 +1697,16 @@
 ount();%0A
-    try %7B%0A
       in
@@ -2420,25 +2420,253 @@
     
-executor.shutdown
+// Note: if close is called it tries to dispatch and event which does not work%0A      // because the async event pool has been shutdown.%0A      // Once GEODE-1428 is fixed this test can call close instead of emergencyClose%0A      gfc.emergencyClose
 ();%0A
@@ -2675,11 +2675,10 @@
   %7D%0A
+
   %7D%0A
-%0A
 %7D%0A

change test to close the cache