Bill commented on a change in pull request #6392:
URL: https://github.com/apache/geode/pull/6392#discussion_r623411740



##########
File path: 
geode-core/src/main/java/org/apache/geode/distributed/internal/ReplyMessage.java
##########
@@ -309,11 +316,17 @@ public void fromData(DataInput in,
     if (testFlag(status, PROCESSOR_ID_FLAG)) {
       this.processorId = in.readInt();
     }
-    if (testFlag(status, EXCEPTION_FLAG)) {
-      this.returnValue = DataSerializer.readObject(in);
-      this.returnValueIsException = true;
-    } else if (testFlag(status, OBJECT_FLAG)) {
-      this.returnValue = DataSerializer.readObject(in);
+    try {
+      if (testFlag(status, EXCEPTION_FLAG)) {
+        this.returnValue = context.getDeserializer().readObject(in);
+        this.returnValueIsException = true;
+      } else if (testFlag(status, OBJECT_FLAG)) {
+        this.returnValue = context.getDeserializer().readObject(in);
+        this.returnValueIsException = (returnValue instanceof ReplyException);
+      }
+    } catch (IOException e) {

Review comment:
       In `toData()` the comment says we will get a `WriteAbortedException`.
   
   Would it be better to catch exactly that? Is there a reason why catching the 
more general class is better? I honestly don't know the answer, but was hoping 
I might learn something by asking.

##########
File path: 
geode-core/src/test/java/org/apache/geode/internal/cache/partitioned/PutPutReplyMessageJUnitTest.java
##########
@@ -48,7 +45,8 @@ protected void toData(OldValueImporter ovi, 
HeapDataOutputStream hdos) throws IO
   @Override
   protected void fromData(OldValueImporter ovi, byte[] bytes)
       throws IOException, ClassNotFoundException {
-    ((PutReplyMessage) ovi).fromData(new DataInputStream(new 
ByteArrayInputStream(bytes)), mock(
-        DeserializationContext.class));
+    final DataInputStream dataInputStream = new DataInputStream(new 
ByteArrayInputStream(bytes));
+    ((PutReplyMessage) ovi).fromData(dataInputStream,
+        InternalDataSerializer.createDeserializationContext(dataInputStream));

Review comment:
       Ah this change is necessitated by the changes in 
`ReplyMessage.fromData()` that causes it to use the `context` (parameter) 
instead of static methods on `DataSerializer`. ok ✓

##########
File path: 
geode-core/src/distributedTest/java/org/apache/geode/distributed/internal/BadCacheLoaderDUnitTest.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.distributed.internal;
+
+import java.io.NotSerializableException;
+import java.io.Serializable;
+import java.util.Properties;
+
+import org.assertj.core.api.Assertions;
+import org.jetbrains.annotations.NotNull;
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.apache.geode.InternalGemFireException;
+import org.apache.geode.cache.Cache;
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.RegionShortcut;
+import org.apache.geode.distributed.ConfigurationProperties;
+import org.apache.geode.test.dunit.SerializableRunnableIF;
+import org.apache.geode.test.dunit.VM;
+import org.apache.geode.test.dunit.rules.CacheRule;
+import org.apache.geode.test.dunit.rules.DistributedRule;
+
+public class BadCacheLoaderDUnitTest implements Serializable {
+  public static final String TEST_REGION = "testRegion";
+  public static final String TEST_KEY = "testKey";
+  public static final String TEST_VALUE = "testValue";
+
+  static class NotSerializableTestException extends RuntimeException {
+    Object unserializableField = new Object();
+  }
+
+  @Rule
+  public DistributedRule distributedRule = new DistributedRule(2);
+
+  CacheRule cacheRule = CacheRule.builder().build();
+
+  /**
+   * Ensure that a cache loader throwing an exception that is not serializable 
is handled
+   * correctly
+   */
+  @Test
+  public void testNonSerializableObjectReturnedByCacheLoader() throws 
Exception {
+    final VM cacheLoaderVM = VM.getVM(0);
+    final VM fetchingVM = VM.getVM(1);
+
+    final Properties properties = new Properties();
+    properties.put(ConfigurationProperties.SERIALIZABLE_OBJECT_FILTER,
+        NotSerializableTestException.class.getName());
+
+    cacheLoaderVM.invoke("create a region with a bad cache loader",
+        createRegionWithBadCacheLoader(properties));
+
+    Assertions.assertThatThrownBy(() -> fetchingVM.invoke("fetch something 
from the cache",
+        fetchValueCausingCacheLoad(properties)))
+        .hasCauseInstanceOf(InternalGemFireException.class)
+        .hasRootCauseInstanceOf(NotSerializableException.class)
+        .hasRootCauseMessage("java.lang.Object");
+  }
+
+  @NotNull
+  private SerializableRunnableIF fetchValueCausingCacheLoad(Properties 
properties) {
+    return () -> {
+      final Cache cache = cacheRule.getOrCreateCache(properties);
+      final Region<String, Object> testRegion =
+          cache.<String, Object>createRegionFactory(RegionShortcut.PARTITION)
+              .setCacheLoader(helper -> new Object())
+              .create(TEST_REGION);
+      testRegion.getAttributesMutator().setCacheLoader(helper -> "should not 
be invoked");
+      testRegion.get(TEST_KEY);
+    };
+  }
+
+  @NotNull
+  private SerializableRunnableIF createRegionWithBadCacheLoader(Properties 
properties) {
+    return () -> {
+      final Cache cache = cacheRule.getOrCreateCache(properties);
+      final Region<String, Object> testRegion =
+          cache.<String, Object>createRegionFactory(RegionShortcut.PARTITION)
+              .setCacheLoader(helper -> {
+                throw new NotSerializableTestException();

Review comment:
       it's so cool that it's possible to inject this lambda here that simply 
throws the exception




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to