/*
 * 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 org.apache.geode.test.dunit.rules.tests;

import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
import static org.apache.geode.test.awaitility.GeodeAwaitility.getTimeout;
import static org.apache.geode.test.dunit.VM.getController;
import static org.apache.geode.test.dunit.VM.getVM;
import static org.apache.geode.test.dunit.rules.DistributedExecutorServiceRule.builder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import org.apache.geode.test.dunit.rules.DistributedExecutorServiceRule;

@SuppressWarnings("serial")
public class DistributedExecutorServiceRuleLimitedThreadCountTest implements Serializable {

  private static final int PARALLEL_TASK_COUNT = 2;
  private static final int PARTY_COUNT = 3;
  private static final long TIMEOUT = getTimeout().toMinutes();
  private static final TimeUnit UNIT = TimeUnit.MINUTES;
  private static final AtomicBoolean COMPLETED = spy(new AtomicBoolean());
  private static final AtomicReference<CyclicBarrier> BARRIER = new AtomicReference<>();

  private static final AtomicReference<Collection<Future<Void>>> TASKS =
      new AtomicReference<>(new ArrayList<>());

  @Rule
  public DistributedExecutorServiceRule executorServiceRule = builder()
      .threadCount(PARALLEL_TASK_COUNT).vmCount(1).build();

  @Before
  public void setUp() {
    Stream.of(getController(), getVM(0)).forEach(vm -> vm.invoke(() -> {
      COMPLETED.set(false);
      reset(COMPLETED);
      BARRIER.set(new CyclicBarrier(PARTY_COUNT, () -> COMPLETED.set(true)));
      TASKS.get().clear();
    }));
  }

  @Test
  public void limitsThreadCount() {
    Stream.of(getController(), getVM(0)).forEach(vm -> vm.invoke(() -> {
      for (int i = 1; i <= PARALLEL_TASK_COUNT; i++) {
        TASKS.get().add(executorServiceRule.submit(() -> {
          BARRIER.get().await(TIMEOUT, UNIT);
        }));
      }

      await().untilAsserted(
          () -> assertThat(BARRIER.get().getNumberWaiting()).isEqualTo(PARALLEL_TASK_COUNT));

      for (Future<Void> task : TASKS.get()) {
        assertThat(task).isNotDone();
      }

      verify(COMPLETED, never()).get();

      for (Future<Void> task : TASKS.get()) {
        task.cancel(true);
      }

      await().untilAsserted(() -> {
        for (Future<Void> task : TASKS.get()) {
          assertThat(task).isCancelled();
          assertThat(task).isDone();
        }
      });
    }));
  }
}

@@ -1239,171 +1239,8 @@
 hat;
-%0Aimport static org.mockito.Mockito.never;%0Aimport static org.mockito.Mockito.reset;%0Aimport static org.mockito.Mockito.spy;%0Aimport static org.mockito.Mockito.verify;
 %0A%0Aim
@@ -1323,16 +1323,68 @@
 ection;%0A
+import java.util.concurrent.BrokenBarrierException;%0A
 import j
@@ -2111,16 +2111,85 @@
 INUTES;%0A
+  private static final AtomicBoolean CANCELED = new AtomicBoolean();%0A
   privat
@@ -2233,12 +2233,8 @@
 D = 
-spy(
 new 
@@ -2248,17 +2248,16 @@
 oolean()
-)
 ;%0A  priv
@@ -2706,24 +2706,51 @@
 oke(() -%3E %7B%0A
+      CANCELED.set(false);%0A
       COMPLE
@@ -2769,32 +2769,8 @@
 e);%0A
-      reset(COMPLETED);%0A
     
@@ -3107,32 +3107,50 @@
 .submit(() -%3E %7B%0A
+          try %7B%0A  
           BARRIE
@@ -3179,16 +3179,147 @@
  UNIT);%0A
+          %7D catch (BrokenBarrierException e) %7B%0A            if (!CANCELED.get()) %7B%0A              throw e;%0A            %7D%0A          %7D%0A
         
@@ -3559,38 +3559,37 @@
     
-verify(COMPLETED, never()).get
+assertThat(COMPLETED).isFalse
 ();%0A
@@ -3627,32 +3627,60 @@
  TASKS.get()) %7B%0A
+        CANCELED.set(true);%0A
         task.can

Handle BrokenBarrierException when canceled.