kirklund commented on a change in pull request #6426: URL: https://github.com/apache/geode/pull/6426#discussion_r658190685
########## 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(); Review comment: Need to annotate this with `@Rule` or it won't be torn down between tests. ########## 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) { Review comment: Not a request for change, just a note... I recommend not importing and using `SerializableRunnableIF`. Best way to use VM.invoke with methods is to use void as the return type: ``` private void createRegionWithBadCacheLoader(Properties properties) ``` And then have the caller invoke it with a lambda: ``` cacheLoaderVM.invoke("create a region with a bad cache loader", () -> createRegionWithBadCacheLoader(properties)); ``` -- 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]
