This is an automated email from the ASF dual-hosted git repository. zhouxj pushed a commit to branch feature/GEODE-6143-7 in repository https://gitbox.apache.org/repos/asf/geode.git
commit 57cc068af2a4433494b819e1eab9c0848b84fa70 Author: zhouxh <[email protected]> AuthorDate: Wed Dec 12 23:21:32 2018 -0800 GEODE-6143: remove PowerMock from HeapMemoryMonitorTest --- ...MemoryMonitorDisableLowMemoryExceptionTest.java | 52 +++++++++ ...MemoryMonitorEnableLowMemoryExceptionTest.java} | 126 +------------------- .../cache/control/HeapMemoryMonitorTestBase.java | 128 +++++++++++++++++++++ 3 files changed, 182 insertions(+), 124 deletions(-) diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorDisableLowMemoryExceptionTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorDisableLowMemoryExceptionTest.java new file mode 100644 index 0000000..63edd96 --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorDisableLowMemoryExceptionTest.java @@ -0,0 +1,52 @@ +/* + * 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.cache.control; + + +import org.junit.BeforeClass; +import org.junit.Test; + +import org.apache.geode.distributed.internal.DistributionConfig; + +public class HeapMemoryMonitorDisableLowMemoryExceptionTest extends HeapMemoryMonitorTestBase { + + private static String savedParam = "false"; + + @BeforeClass + public static void setupSystemProperties() { + savedParam = + System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "disableLowMemoryException", "true"); + } + + @BeforeClass + public static void resetSystemProperties() { + System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "disableLowMemoryException", savedParam); + } + + // ========== tests for createLowMemoryIfNeeded (with Set argument) ========== + @Test + public void createLowMemoryIfNeededWithSetArg_ReturnsNullWhenLowMemoryExceptionDisabled() + throws Exception { + createLowMemoryIfNeededWithSetArg_returnsNull(true, true, memberSet); + } + + // ========== tests for checkForLowMemory (with Set argument) ========== + @Test + public void checkForLowMemoryWithSetArg_DoesNotThrowWhenLowMemoryExceptionDisabled() + throws Exception { + checkForLowMemoryWithSetArg_doesNotThrow(true, true, memberSet); + } + +} diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorEnableLowMemoryExceptionTest.java similarity index 58% rename from geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorTest.java rename to geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorEnableLowMemoryExceptionTest.java index f5ddc52..7c34c80 100644 --- a/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorTest.java +++ b/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorEnableLowMemoryExceptionTest.java @@ -14,62 +14,20 @@ */ package org.apache.geode.internal.cache.control; -import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import java.util.Collections; import java.util.HashSet; import java.util.Set; -import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import org.apache.geode.cache.LowMemoryException; -import org.apache.geode.cache.execute.Function; import org.apache.geode.distributed.DistributedMember; -import org.apache.geode.distributed.DistributedSystem; import org.apache.geode.distributed.internal.membership.InternalDistributedMember; -import org.apache.geode.internal.cache.InternalCache; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({MemoryThresholds.class}) -public class HeapMemoryMonitorTest { - - private HeapMemoryMonitor heapMonitor; - private Function function; - private Set memberSet; - private DistributedMember member; - private InternalDistributedMember myself; - private ResourceAdvisor resourceAdvisor; - private static final String LOW_MEMORY_REGEX = - "Function: null cannot be executed because the members.*are running low on memory"; - - @Before - public void setup() { - InternalCache internalCache = mock(InternalCache.class); - DistributedSystem distributedSystem = mock(DistributedSystem.class); - function = mock(Function.class); - member = mock(InternalDistributedMember.class); - myself = mock(InternalDistributedMember.class); - resourceAdvisor = mock(ResourceAdvisor.class); - - when(internalCache.getDistributedSystem()).thenReturn(distributedSystem); - when(internalCache.getDistributionAdvisor()).thenReturn(resourceAdvisor); - when(internalCache.getMyId()).thenReturn(myself); - - heapMonitor = new HeapMemoryMonitor(null, internalCache, null); - memberSet = new HashSet<>(); - memberSet.add(member); - heapMonitor.setMostRecentEvent(new MemoryEvent(InternalResourceManager.ResourceType.HEAP_MEMORY, - MemoryThresholds.MemoryState.DISABLED, MemoryThresholds.MemoryState.DISABLED, null, 0L, - true, null)); // myself is not critical - } + +public class HeapMemoryMonitorEnableLowMemoryExceptionTest extends HeapMemoryMonitorTestBase { // ========== tests for getHeapCriticalMembersFrom ========== @Test @@ -154,12 +112,6 @@ public class HeapMemoryMonitorTest { } @Test - public void createLowMemoryIfNeededWithSetArg_ReturnsNullWhenLowMemoryExceptionDisabled() - throws Exception { - createLowMemoryIfNeededWithSetArg_returnsNull(true, true, memberSet); - } - - @Test public void createLowMemoryIfNeededWithSetArg_ReturnsNullWhenNoCriticalMembers() throws Exception { createLowMemoryIfNeededWithSetArg_returnsNull(true, false, Collections.emptySet()); @@ -209,12 +161,6 @@ public class HeapMemoryMonitorTest { } @Test - public void checkForLowMemoryWithSetArg_DoesNotThrowWhenLowMemoryExceptionDisabled() - throws Exception { - checkForLowMemoryWithSetArg_doesNotThrow(true, true, memberSet); - } - - @Test public void checkForLowMemoryWithSetArg_DoesNotThrowWhenNoCriticalMembers() throws Exception { checkForLowMemoryWithSetArg_doesNotThrow(true, false, Collections.emptySet()); } @@ -254,72 +200,4 @@ public class HeapMemoryMonitorTest { .isExactlyInstanceOf(LowMemoryException.class).hasMessageMatching(LOW_MEMORY_REGEX); } - // ========== private methods ========== - private void getHeapCriticalMembersFrom_returnsEmptySet(Set adviseCriticalMembers, Set argSet) { - when(resourceAdvisor.adviseCriticalMembers()).thenReturn(adviseCriticalMembers); - - Set<DistributedMember> criticalMembers = heapMonitor.getHeapCriticalMembersFrom(argSet); - - assertThat(criticalMembers).isEmpty(); - } - - private void getHeapCriticalMembersFrom_returnsNonEmptySet(Set adviseCriticalMembers, Set argSet, - Set expectedResult) { - when(resourceAdvisor.adviseCriticalMembers()).thenReturn(adviseCriticalMembers); - - Set<DistributedMember> criticalMembers = heapMonitor.getHeapCriticalMembersFrom(argSet); - - assertThat(criticalMembers).containsAll(expectedResult); - } - - private void createLowMemoryIfNeededWithSetArg_returnsNull(boolean optimizeForWrite, - boolean isLowMemoryExceptionDisabled, Set memberSetArg) throws Exception { - setMocking(optimizeForWrite, isLowMemoryExceptionDisabled, memberSetArg); - - LowMemoryException exception = heapMonitor.createLowMemoryIfNeeded(function, memberSetArg); - - assertThat(exception).isNull(); - } - - private void createLowMemoryIfNeededWithMemberArg_returnsNull(boolean optimizeForWrite, - boolean isLowMemoryExceptionDisabled, DistributedMember memberArg) throws Exception { - setMocking(optimizeForWrite, isLowMemoryExceptionDisabled, Collections.emptySet()); - - LowMemoryException exception = heapMonitor.createLowMemoryIfNeeded(function, memberArg); - - assertThat(exception).isNull(); - } - - private void checkForLowMemoryWithSetArg_doesNotThrow(boolean optimizeForWrite, - boolean isLowMemoryExceptionDisabled, Set memberSetArg) throws Exception { - setMocking(optimizeForWrite, isLowMemoryExceptionDisabled, memberSetArg); - - heapMonitor.checkForLowMemory(function, memberSetArg); - } - - private void checkForLowMemoryWithMemberArg_doesNotThrow(boolean optimizeForWrite, - boolean isLowMemoryExceptionDisabled, DistributedMember memberArg) throws Exception { - setMocking(optimizeForWrite, isLowMemoryExceptionDisabled, Collections.emptySet()); - - heapMonitor.checkForLowMemory(function, memberArg); - } - - private void setMocking(boolean optimizeForWrite, boolean isLowMemoryExceptionDisabled, - Set argSet) throws Exception { - when(function.optimizeForWrite()).thenReturn(optimizeForWrite); - setIsLowMemoryExceptionDisabled(isLowMemoryExceptionDisabled); - when(resourceAdvisor.adviseCriticalMembers()).thenReturn(argSet); - } - - private void assertLowMemoryException(LowMemoryException exception) { - assertThat(exception).isExactlyInstanceOf(LowMemoryException.class); - assertThat(exception.getMessage()).containsPattern(LOW_MEMORY_REGEX); - } - - private void setIsLowMemoryExceptionDisabled(boolean isLowMemoryExceptionDisabled) - throws Exception { - PowerMockito.mockStatic(MemoryThresholds.class); - PowerMockito.when(MemoryThresholds.class, MemoryThresholds.isLowMemoryExceptionDisabled()) - .thenReturn(isLowMemoryExceptionDisabled); - } } diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorTestBase.java b/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorTestBase.java new file mode 100644 index 0000000..1b9b62f --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/internal/cache/control/HeapMemoryMonitorTestBase.java @@ -0,0 +1,128 @@ +/* + * 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.cache.control; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import org.junit.Before; + +import org.apache.geode.cache.LowMemoryException; +import org.apache.geode.cache.execute.Function; +import org.apache.geode.distributed.DistributedMember; +import org.apache.geode.distributed.DistributedSystem; +import org.apache.geode.distributed.internal.membership.InternalDistributedMember; +import org.apache.geode.internal.cache.InternalCache; + +public class HeapMemoryMonitorTestBase { + + protected HeapMemoryMonitor heapMonitor; + protected Function function; + protected Set memberSet; + protected DistributedMember member; + protected InternalDistributedMember myself; + protected ResourceAdvisor resourceAdvisor; + protected static final String LOW_MEMORY_REGEX = + "Function: null cannot be executed because the members.*are running low on memory"; + + @Before + public void setup() { + InternalCache internalCache = mock(InternalCache.class); + DistributedSystem distributedSystem = mock(DistributedSystem.class); + function = mock(Function.class); + member = mock(InternalDistributedMember.class); + myself = mock(InternalDistributedMember.class); + resourceAdvisor = mock(ResourceAdvisor.class); + + when(internalCache.getDistributedSystem()).thenReturn(distributedSystem); + when(internalCache.getDistributionAdvisor()).thenReturn(resourceAdvisor); + when(internalCache.getMyId()).thenReturn(myself); + + heapMonitor = new HeapMemoryMonitor(null, internalCache, null); + memberSet = new HashSet<>(); + memberSet.add(member); + heapMonitor.setMostRecentEvent(new MemoryEvent(InternalResourceManager.ResourceType.HEAP_MEMORY, + MemoryThresholds.MemoryState.DISABLED, MemoryThresholds.MemoryState.DISABLED, null, 0L, + true, null)); // myself is not critical + } + + // ========== protected methods ========== + protected void getHeapCriticalMembersFrom_returnsEmptySet(Set adviseCriticalMembers, Set argSet) { + when(resourceAdvisor.adviseCriticalMembers()).thenReturn(adviseCriticalMembers); + + Set<DistributedMember> criticalMembers = heapMonitor.getHeapCriticalMembersFrom(argSet); + + assertThat(criticalMembers).isEmpty(); + } + + protected void getHeapCriticalMembersFrom_returnsNonEmptySet(Set adviseCriticalMembers, + Set argSet, + Set expectedResult) { + when(resourceAdvisor.adviseCriticalMembers()).thenReturn(adviseCriticalMembers); + + Set<DistributedMember> criticalMembers = heapMonitor.getHeapCriticalMembersFrom(argSet); + + assertThat(criticalMembers).containsAll(expectedResult); + } + + protected void createLowMemoryIfNeededWithSetArg_returnsNull(boolean optimizeForWrite, + boolean isLowMemoryExceptionDisabled, Set memberSetArg) throws Exception { + setMocking(optimizeForWrite, isLowMemoryExceptionDisabled, memberSetArg); + + LowMemoryException exception = heapMonitor.createLowMemoryIfNeeded(function, memberSetArg); + + assertThat(exception).isNull(); + } + + protected void createLowMemoryIfNeededWithMemberArg_returnsNull(boolean optimizeForWrite, + boolean isLowMemoryExceptionDisabled, DistributedMember memberArg) throws Exception { + setMocking(optimizeForWrite, isLowMemoryExceptionDisabled, Collections.emptySet()); + + LowMemoryException exception = heapMonitor.createLowMemoryIfNeeded(function, memberArg); + + assertThat(exception).isNull(); + } + + protected void checkForLowMemoryWithSetArg_doesNotThrow(boolean optimizeForWrite, + boolean isLowMemoryExceptionDisabled, Set memberSetArg) throws Exception { + setMocking(optimizeForWrite, isLowMemoryExceptionDisabled, memberSetArg); + + heapMonitor.checkForLowMemory(function, memberSetArg); + } + + protected void checkForLowMemoryWithMemberArg_doesNotThrow(boolean optimizeForWrite, + boolean isLowMemoryExceptionDisabled, DistributedMember memberArg) throws Exception { + setMocking(optimizeForWrite, isLowMemoryExceptionDisabled, Collections.emptySet()); + + heapMonitor.checkForLowMemory(function, memberArg); + } + + protected void setMocking(boolean optimizeForWrite, boolean isLowMemoryExceptionDisabled, + Set argSet) throws Exception { + when(function.optimizeForWrite()).thenReturn(optimizeForWrite); + when(resourceAdvisor.adviseCriticalMembers()).thenReturn(argSet); + } + + protected void assertLowMemoryException(LowMemoryException exception) { + assertThat(exception).isExactlyInstanceOf(LowMemoryException.class); + assertThat(exception.getMessage()).containsPattern(LOW_MEMORY_REGEX); + } + +}
