This is an automated email from the ASF dual-hosted git repository.

jasonhuynh pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new c16f280  GEODE-4770: Prevent NPE when destroying unstarted 
GatewayReceiver (#1556)
c16f280 is described below

commit c16f28040523e72efdada6da0398657723e6c262
Author: Jason Huynh <huyn...@gmail.com>
AuthorDate: Tue Mar 6 10:50:12 2018 -0800

    GEODE-4770: Prevent NPE when destroying unstarted GatewayReceiver (#1556)
---
 .../internal/cache/wan/GatewayReceiverImpl.java    | 15 ++++---
 .../cache/wan/serial/GatewayReceiverDUnitTest.java | 46 ++++++++++++++++++++++
 2 files changed, 56 insertions(+), 5 deletions(-)

diff --git 
a/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverImpl.java
 
b/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverImpl.java
index 3e3c725..0f0fc63 100644
--- 
a/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverImpl.java
+++ 
b/geode-wan/src/main/java/org/apache/geode/internal/cache/wan/GatewayReceiverImpl.java
@@ -217,12 +217,17 @@ public class GatewayReceiverImpl implements 
GatewayReceiver {
 
   public void destroy() {
     logger.info("Destroying Gateway Receiver: " + this);
-    if (receiver.isRunning()) {
-      throw new GatewayReceiverException(
-          "Gateway Receiver is running and needs to be stopped first");
+    if (receiver == null) {
+      // receiver was not started
+      this.cache.removeGatewayReceiver(this);
+    } else {
+      if (receiver.isRunning()) {
+        throw new GatewayReceiverException(
+            "Gateway Receiver is running and needs to be stopped first");
+      }
+      this.cache.removeGatewayReceiver(this);
+      this.cache.removeCacheServer(receiver);
     }
-    this.cache.removeGatewayReceiver(this);
-    this.cache.removeCacheServer(receiver);
     InternalDistributedSystem system = 
this.cache.getInternalDistributedSystem();
     system.handleResourceEvent(ResourceEvent.GATEWAYRECEIVER_DESTROY, this);
   }
diff --git 
a/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/serial/GatewayReceiverDUnitTest.java
 
b/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/serial/GatewayReceiverDUnitTest.java
index db97754..21cbaeb 100644
--- 
a/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/serial/GatewayReceiverDUnitTest.java
+++ 
b/geode-wan/src/test/java/org/apache/geode/internal/cache/wan/serial/GatewayReceiverDUnitTest.java
@@ -78,6 +78,18 @@ public class GatewayReceiverDUnitTest extends WANTestBase {
             .getDistributionAdvisor());
   }
 
+  @Test
+  public void canDestroyUnstartedGatewayReceiverFromReplicated() throws 
Exception {
+    testCanDestroyUnstartedGatewayReceiver(
+        () -> WANTestBase.createReplicatedRegion(getTestMethodName(), null, 
isOffHeap()));
+  }
+
+  @Test
+  public void canDestroyUnstartedReceiverFromPartitionedRegion() throws 
Exception {
+    testCanDestroyUnstartedGatewayReceiver(
+        () -> WANTestBase.createPartitionedRegion(getTestMethodName(), null, 
1, 10, isOffHeap()));
+  }
+
   public <T> void testRemoveGatewayReceiver(SerializableRunnableIF 
createRegionLambda,
       SerializableCallableIF<DistributionAdvisor> extractAdvisorLambda) throws 
Exception {
     InternalDistributedMember[] memberIds = new InternalDistributedMember[8];
@@ -184,6 +196,30 @@ public class GatewayReceiverDUnitTest extends WANTestBase {
 
   }
 
+  public <T> void 
testCanDestroyUnstartedGatewayReceiver(SerializableRunnableIF 
createRegionLambda)
+      throws Exception {
+    InternalDistributedMember[] memberIds = new InternalDistributedMember[8];
+
+    Integer lnPort = (Integer) vm0.invoke(() -> 
WANTestBase.createFirstLocatorWithDSId(1));
+    Integer nyPort = (Integer) vm1.invoke(() -> 
WANTestBase.createFirstRemoteLocator(2, lnPort));
+
+    vm2.invoke(() -> WANTestBase.createCache(nyPort));
+
+    memberIds[2] = (InternalDistributedMember) vm2
+        .invoke(() -> 
WANTestBase.cache.getDistributedSystem().getDistributedMember());
+    vm2.invoke(createRegionLambda);
+
+    vm2.invoke(() -> {
+      GatewayReceiverDUnitTest.receiver =
+          GatewayReceiverDUnitTest.createAndReturnUnstartedReceiver();
+      return;
+    });
+
+    vm2.invoke(() -> {
+      GatewayReceiverDUnitTest.receiver.destroy();
+    });
+  }
+
 
 
   private void assertProfileCacheServerFlagEquals(InternalDistributedMember 
member,
@@ -212,4 +248,14 @@ public class GatewayReceiverDUnitTest extends WANTestBase {
     return receiver;
   }
 
+  public static GatewayReceiver createAndReturnUnstartedReceiver() {
+    GatewayReceiverFactory fact = cache.createGatewayReceiverFactory();
+    int port = AvailablePortHelper.getRandomAvailablePortForDUnitSite();
+    fact.setStartPort(port);
+    fact.setEndPort(port);
+    fact.setManualStart(true);
+    GatewayReceiver receiver = fact.create();
+    return receiver;
+  }
+
 }

-- 
To stop receiving notification emails like this one, please contact
jasonhu...@apache.org.

Reply via email to