/*
 * 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.internal.tcp;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionShortcut;
import org.apache.geode.distributed.internal.ClusterDistributionManager;
import org.apache.geode.distributed.internal.DistributionImpl;
import org.apache.geode.test.dunit.VM;
import org.apache.geode.test.dunit.cache.CacheTestCase;

public class CloseConnectionTest extends CacheTestCase {

  @Test(timeout = 60_000)
  public void sharedSenderShouldRecoverFromClosedSocket() {
    VM vm0 = VM.getVM(0);
    VM vm1 = VM.getVM(1);

    // Create a region in each member. VM0 has a proxy region, so state must be in VM1
    vm0.invoke(() -> {
      getCache().createRegionFactory(RegionShortcut.REPLICATE_PROXY).create("region");
    });
    vm1.invoke(() -> {
      getCache().createRegionFactory(RegionShortcut.REPLICATE).create("region");
    });


    // Force VM1 to close it's connections.
    vm1.invoke(() -> {
      ConnectionTable conTable = getConnectionTable();
      assertThat(conTable.getNumberOfReceivers()).isEqualTo(2);
      conTable.closeReceivers(false);
      assertThat(conTable.getNumberOfReceivers()).isEqualTo(0);
    });

    // See if VM0 noticed the closed connections. Try to do a couple of region
    // operations
    vm0.invoke(() -> {
      Region<Object, Object> region = getCache().getRegion("region");
      region.put("1", "1");

      assertThat(region.get("1")).isEqualTo("1");
    });

    // Make sure connections were reestablished
    vm1.invoke(() -> {
      ConnectionTable conTable = getConnectionTable();
      assertThat(conTable.getNumberOfReceivers()).isEqualTo(2);
    });
  }

  private ConnectionTable getConnectionTable() {
    ClusterDistributionManager cdm =
        (ClusterDistributionManager) getSystem().getDistributionManager();
    DistributionImpl distribution = (DistributionImpl) cdm.getDistribution();
    return distribution.getDirectChannel().getConduit().getConTable();
  }


}

@@ -846,471 +846,1406 @@
 rg.a
-ssertj.core.api.Assertions.assertThat;%0A%0Aimport org.junit.Test;%0A%0Aimport org.apache.geode.cache.Region;%0Aimport org.apache.geode.cache.RegionShortcut;%0Aimport org.apache.geode.distributed.internal.ClusterDistributionManager;%0Aimport org.apache.geode.distributed.internal.DistributionImpl;%0Aimport org.apache.geode.test.dunit.VM;%0Aimport org.apache.geode.test.dunit.cache.CacheTestCase;%0A%0Apublic class CloseConnectionTest extends CacheTestCase %7B%0A%0A  @Test(timeout = 60_000)
+pache.geode.test.awaitility.GeodeAwaitility.await;%0Aimport static org.apache.geode.test.dunit.Disconnect.disconnectAllFromDS;%0Aimport static org.apache.geode.test.dunit.VM.getController;%0Aimport static org.apache.geode.test.dunit.VM.getVM;%0Aimport static org.apache.geode.test.dunit.VM.toArray;%0Aimport static org.assertj.core.api.Assertions.assertThat;%0A%0Aimport java.io.Serializable;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Rule;%0Aimport org.junit.Test;%0A%0Aimport org.apache.geode.cache.Region;%0Aimport org.apache.geode.cache.RegionShortcut;%0Aimport org.apache.geode.distributed.internal.ClusterDistributionManager;%0Aimport org.apache.geode.distributed.internal.DistributionImpl;%0Aimport org.apache.geode.internal.cache.InternalCache;%0Aimport org.apache.geode.test.dunit.VM;%0Aimport org.apache.geode.test.dunit.rules.CacheRule;%0Aimport org.apache.geode.test.dunit.rules.DistributedRule;%0A%0Apublic class CloseConnectionTest implements Serializable %7B%0A  private VM vm0;%0A  private VM vm1;%0A%0A  @Rule%0A  public DistributedRule distributedRule = new DistributedRule();%0A%0A  @Rule%0A  public CacheRule cacheRule = new CacheRule();%0A%0A  @Before%0A  public void setUp() %7B%0A    vm0 = getVM(0);%0A    vm1 = getVM(1);%0A  %7D%0A%0A  @After%0A  public void tearDown() %7B%0A    for (VM vm : toArray(getController(), vm0, vm1)) %7B%0A      vm.invoke(() -%3E cacheRule.closeAndNullCache());%0A    %7D%0A%0A    disconnectAllFromDS();%0A  %7D%0A%0A  @Test
 %0A  p
@@ -2305,61 +2305,8 @@
 ) %7B%0A
-    VM vm0 = VM.getVM(0);%0A    VM vm1 = VM.getVM(1);%0A%0A
     
@@ -2841,32 +2841,60 @@
 rs(false);%0A     
+ await().untilAsserted(() -%3E
  assertThat(conT
@@ -2934,16 +2934,17 @@
 ualTo(0)
+)
 ;%0A    %7D)
@@ -3347,32 +3347,60 @@
 onTable();%0A     
+ await().untilAsserted(() -%3E
  assertThat(conT
@@ -3436,24 +3436,25 @@
 isEqualTo(2)
+)
 ;%0A    %7D);%0A  
@@ -3586,14 +3586,13 @@
  get
-System
+Cache
 ().g
@@ -3770,11 +3770,92 @@
 );%0A  %7D%0A%0A
+  private InternalCache getCache() %7B%0A    return cacheRule.getOrCreateCache();%0A  %7D
 %0A%7D%0A

Fix CloseConnectionTest