DonalEvans commented on a change in pull request #6176: URL: https://github.com/apache/geode/pull/6176#discussion_r604375409
########## File path: geode-core/src/test/java/org/apache/geode/internal/cache/control/TenuredHeapConsumptionMonitorTest.java ########## @@ -0,0 +1,93 @@ +/* + * 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 java.lang.management.MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED; +import static java.lang.management.MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.management.MemoryNotificationInfo; +import java.lang.management.MemoryUsage; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import javax.management.Notification; +import javax.management.openmbean.CompositeData; + +import org.junit.Before; +import org.junit.Test; + +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.cache.client.ClientCacheFactory; +import org.apache.geode.cache.client.Pool; +import org.apache.geode.internal.cache.GemFireCacheImpl; + +public class TenuredHeapConsumptionMonitorTest { + + private BiConsumer<String, Throwable> infoLogger; + private Function<CompositeData, MemoryNotificationInfo> memoryNotificationInfoFactory; + private Notification notification; + + + @Before + public void setUp() { + infoLogger = mock(BiConsumer.class); + memoryNotificationInfoFactory = mock(Function.class); + + + } + + private void notificationType(String type) { + notification = new Notification(type, this, 1); + ClientCache clientCache; + clientCache = new ClientCacheFactory().create(); + GemFireCacheImpl gfc = (GemFireCacheImpl) clientCache; + Pool defPool = gfc.getDefaultPool(); + MemoryUsage usage = new MemoryUsage(50, 50, 50, 100); + MemoryNotificationInfo memoryNotificationInfo = + new MemoryNotificationInfo(defPool.getName(), usage, 1); + when(memoryNotificationInfoFactory.apply(any())).thenReturn(memoryNotificationInfo); + TenuredHeapConsumptionMonitor monitor = + new TenuredHeapConsumptionMonitor(infoLogger, memoryNotificationInfoFactory); + + monitor.checkTenuredHeapConsumption(notification); + + } + + @Test + public void assertIfTenuredGCLogMessageIsPrintedAfterGCAndWhenMemoryThresholdExceeds() { + notificationType(MEMORY_THRESHOLD_EXCEEDED); Review comment: With the changes to the `setUp()` method, this would become: ```suggestion when(notification.getType()).thenReturn(MEMORY_THRESHOLD_EXCEEDED); monitor.checkTenuredHeapConsumption(notification); ``` ########## File path: geode-core/src/test/java/org/apache/geode/internal/cache/control/TenuredHeapConsumptionMonitorTest.java ########## @@ -0,0 +1,93 @@ +/* + * 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 java.lang.management.MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED; +import static java.lang.management.MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.management.MemoryNotificationInfo; +import java.lang.management.MemoryUsage; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import javax.management.Notification; +import javax.management.openmbean.CompositeData; + +import org.junit.Before; +import org.junit.Test; + +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.cache.client.ClientCacheFactory; +import org.apache.geode.cache.client.Pool; +import org.apache.geode.internal.cache.GemFireCacheImpl; + +public class TenuredHeapConsumptionMonitorTest { + + private BiConsumer<String, Throwable> infoLogger; + private Function<CompositeData, MemoryNotificationInfo> memoryNotificationInfoFactory; + private Notification notification; + + + @Before + public void setUp() { + infoLogger = mock(BiConsumer.class); + memoryNotificationInfoFactory = mock(Function.class); + + + } + + private void notificationType(String type) { + notification = new Notification(type, this, 1); + ClientCache clientCache; + clientCache = new ClientCacheFactory().create(); + GemFireCacheImpl gfc = (GemFireCacheImpl) clientCache; + Pool defPool = gfc.getDefaultPool(); + MemoryUsage usage = new MemoryUsage(50, 50, 50, 100); + MemoryNotificationInfo memoryNotificationInfo = + new MemoryNotificationInfo(defPool.getName(), usage, 1); + when(memoryNotificationInfoFactory.apply(any())).thenReturn(memoryNotificationInfo); + TenuredHeapConsumptionMonitor monitor = + new TenuredHeapConsumptionMonitor(infoLogger, memoryNotificationInfoFactory); + + monitor.checkTenuredHeapConsumption(notification); + + } + + @Test + public void assertIfTenuredGCLogMessageIsPrintedAfterGCAndWhenMemoryThresholdExceeds() { + notificationType(MEMORY_THRESHOLD_EXCEEDED); + verify(infoLogger).accept( + eq("A tenured heap garbage collection has occurred. New tenured heap consumption: 50"), + isNull()); Review comment: If the `used` field is introduced, this verification doesn't need to use a hard-coded value but can instead be: ```suggestion verify(infoLogger).accept( eq("A tenured heap garbage collection has occurred. New tenured heap consumption: " + used), isNull()); ``` ########## File path: geode-core/src/test/java/org/apache/geode/internal/cache/control/TenuredHeapConsumptionMonitorTest.java ########## @@ -0,0 +1,93 @@ +/* + * 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 java.lang.management.MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED; +import static java.lang.management.MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.management.MemoryNotificationInfo; +import java.lang.management.MemoryUsage; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import javax.management.Notification; +import javax.management.openmbean.CompositeData; + +import org.junit.Before; +import org.junit.Test; + +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.cache.client.ClientCacheFactory; +import org.apache.geode.cache.client.Pool; +import org.apache.geode.internal.cache.GemFireCacheImpl; + +public class TenuredHeapConsumptionMonitorTest { + + private BiConsumer<String, Throwable> infoLogger; + private Function<CompositeData, MemoryNotificationInfo> memoryNotificationInfoFactory; + private Notification notification; + + + @Before + public void setUp() { + infoLogger = mock(BiConsumer.class); + memoryNotificationInfoFactory = mock(Function.class); + + + } Review comment: By replacing the object creation in the `notificationType()` method with mocking and extracting it to the `setUp()` method, and adding `private final long used = 50;` and `private TenuredHeapConsumptionMonitor monitor;` fields, this class can be made a little more flexible and some new test cases added: ```suggestion @Before public void setUp() { notification = mock(Notification.class); MemoryUsage usage = mock(MemoryUsage.class); MemoryNotificationInfo memoryNotificationInfo = mock(MemoryNotificationInfo.class); when(memoryNotificationInfo.getUsage()).thenReturn(usage); when(usage.getUsed()).thenReturn(used); memoryNotificationInfoFactory = mock(Function.class); when(memoryNotificationInfoFactory.apply(any())).thenReturn(memoryNotificationInfo); infoLogger = mock(BiConsumer.class); monitor = new TenuredHeapConsumptionMonitor(infoLogger, memoryNotificationInfoFactory); } ``` ########## File path: geode-core/src/test/java/org/apache/geode/internal/cache/control/TenuredHeapConsumptionMonitorTest.java ########## @@ -0,0 +1,93 @@ +/* + * 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 java.lang.management.MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED; +import static java.lang.management.MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.management.MemoryNotificationInfo; +import java.lang.management.MemoryUsage; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import javax.management.Notification; +import javax.management.openmbean.CompositeData; + +import org.junit.Before; +import org.junit.Test; + +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.cache.client.ClientCacheFactory; +import org.apache.geode.cache.client.Pool; +import org.apache.geode.internal.cache.GemFireCacheImpl; + +public class TenuredHeapConsumptionMonitorTest { + + private BiConsumer<String, Throwable> infoLogger; + private Function<CompositeData, MemoryNotificationInfo> memoryNotificationInfoFactory; + private Notification notification; + + + @Before + public void setUp() { + infoLogger = mock(BiConsumer.class); + memoryNotificationInfoFactory = mock(Function.class); + + + } + + private void notificationType(String type) { + notification = new Notification(type, this, 1); + ClientCache clientCache; + clientCache = new ClientCacheFactory().create(); + GemFireCacheImpl gfc = (GemFireCacheImpl) clientCache; + Pool defPool = gfc.getDefaultPool(); + MemoryUsage usage = new MemoryUsage(50, 50, 50, 100); + MemoryNotificationInfo memoryNotificationInfo = + new MemoryNotificationInfo(defPool.getName(), usage, 1); + when(memoryNotificationInfoFactory.apply(any())).thenReturn(memoryNotificationInfo); + TenuredHeapConsumptionMonitor monitor = + new TenuredHeapConsumptionMonitor(infoLogger, memoryNotificationInfoFactory); + + monitor.checkTenuredHeapConsumption(notification); + + } + + @Test + public void assertIfTenuredGCLogMessageIsPrintedAfterGCAndWhenMemoryThresholdExceeds() { + notificationType(MEMORY_THRESHOLD_EXCEEDED); + verify(infoLogger).accept( + eq("A tenured heap garbage collection has occurred. New tenured heap consumption: 50"), + isNull()); + + + } + + @Test + public void assertIfTenuredGCLogMessageIsPrintedAfterGCAndWhenMemoryCollectionThresholdExceeds() { + notificationType(MEMORY_COLLECTION_THRESHOLD_EXCEEDED); + verify(infoLogger).accept( + eq("A tenured heap garbage collection has occurred. New tenured heap consumption: 50"), + isNull()); + + Review comment: With the changes to `setUp()` and the inclusion of the `used` field, this would become: ```suggestion when(notification.getType()).thenReturn(MEMORY_COLLECTION_THRESHOLD_EXCEEDED); monitor.checkTenuredHeapConsumption(notification); verify(infoLogger).accept( eq("A tenured heap garbage collection has occurred. New tenured heap consumption: " + used), isNull()); ``` ########## File path: geode-core/src/test/java/org/apache/geode/internal/cache/control/TenuredHeapConsumptionMonitorTest.java ########## @@ -0,0 +1,93 @@ +/* + * 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 java.lang.management.MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED; +import static java.lang.management.MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.management.MemoryNotificationInfo; +import java.lang.management.MemoryUsage; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import javax.management.Notification; +import javax.management.openmbean.CompositeData; + +import org.junit.Before; +import org.junit.Test; + +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.cache.client.ClientCacheFactory; +import org.apache.geode.cache.client.Pool; +import org.apache.geode.internal.cache.GemFireCacheImpl; + +public class TenuredHeapConsumptionMonitorTest { + + private BiConsumer<String, Throwable> infoLogger; + private Function<CompositeData, MemoryNotificationInfo> memoryNotificationInfoFactory; + private Notification notification; + + + @Before + public void setUp() { + infoLogger = mock(BiConsumer.class); + memoryNotificationInfoFactory = mock(Function.class); + + + } + + private void notificationType(String type) { Review comment: This method can be removed if the suggested changed to the `setUp()` method are made. -- 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]
