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

upthewaterspout 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 cc30e48  GEODE-4872: Fixing logging tests in 
PersistentColocatedPartitionedRegionDUnitTest
cc30e48 is described below

commit cc30e4868f75945061666f633e2953f0a0dea612
Author: Dan Smith <[email protected]>
AuthorDate: Tue Oct 9 17:03:13 2018 -0700

    GEODE-4872: Fixing logging tests in 
PersistentColocatedPartitionedRegionDUnitTest
    
    This test had a number of tests making assertions about log messages.
    Unfortunately, these tests had assertions inside of a try/finally block,
    and the finally had a return. Therefore the assertion failures were
    simply ignored, and the test was just a test that the callables returned
    in 60 seconds.
    
    Fixing this test by removing the bad verify calls and returns in finally
    blocks.
    
    Cleaning up the way the tests hooked into the logging system.
---
 .../internal/cache/partitioned/MockAppender.java   |  61 +++++
 ...sistentColocatedPartitionedRegionDUnitTest.java | 295 +++++----------------
 2 files changed, 122 insertions(+), 234 deletions(-)

diff --git 
a/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/partitioned/MockAppender.java
 
b/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/partitioned/MockAppender.java
new file mode 100644
index 0000000..6d44d6d
--- /dev/null
+++ 
b/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/partitioned/MockAppender.java
@@ -0,0 +1,61 @@
+/*
+ * 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.partitioned;
+
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.mockito.ArgumentCaptor;
+
+public class MockAppender implements AutoCloseable {
+
+  private final Logger logger;
+  private final Appender mockAppender;
+
+  public MockAppender(Class<?> loggerClass) {
+    mockAppender = mock(Appender.class);
+    when(mockAppender.getName()).thenReturn("MockAppender");
+    when(mockAppender.isStarted()).thenReturn(true);
+    when(mockAppender.isStopped()).thenReturn(false);
+    logger = (Logger) LogManager.getLogger(loggerClass);
+    logger.addAppender(mockAppender);
+    logger.setLevel(Level.WARN);
+  }
+
+  public Appender getMock() {
+    return this.mockAppender;
+  }
+
+  public List<LogEvent> getLogs() {
+    ArgumentCaptor<LogEvent> loggingEventCaptor = 
ArgumentCaptor.forClass(LogEvent.class);
+    verify(mockAppender, atLeast(0)).append(loggingEventCaptor.capture());
+    return loggingEventCaptor.getAllValues();
+  }
+
+  public void close() {
+    logger.removeAppender(mockAppender);
+  }
+
+
+}
diff --git 
a/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/partitioned/PersistentColocatedPartitionedRegionDUnitTest.java
 
b/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/partitioned/PersistentColocatedPartitionedRegionDUnitTest.java
index 0895558..9de86a5 100644
--- 
a/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/partitioned/PersistentColocatedPartitionedRegionDUnitTest.java
+++ 
b/geode-core/src/distributedTest/java/org/apache/geode/internal/cache/partitioned/PersistentColocatedPartitionedRegionDUnitTest.java
@@ -33,7 +33,6 @@ import java.util.List;
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.logging.log4j.Level;
@@ -41,7 +40,6 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.core.Appender;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.Logger;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.mockito.ArgumentCaptor;
@@ -62,7 +60,6 @@ import 
org.apache.geode.distributed.internal.DistributionMessage;
 import org.apache.geode.distributed.internal.DistributionMessageObserver;
 import org.apache.geode.distributed.internal.InternalDistributedSystem;
 import org.apache.geode.internal.cache.ColocationLogger;
-import org.apache.geode.internal.cache.DiskRegion;
 import 
org.apache.geode.internal.cache.InitialImageOperation.RequestImageMessage;
 import org.apache.geode.internal.cache.PartitionedRegion;
 import org.apache.geode.internal.cache.PartitionedRegionHelper;
@@ -367,291 +364,126 @@ public class 
PersistentColocatedPartitionedRegionDUnitTest
 
   private SerializableCallable createPRsMissingChildRegionThread =
       new SerializableCallable("createPRsMissingChildRegion") {
-        Appender mockAppender;
-        ArgumentCaptor<LogEvent> loggingEventCaptor;
 
         public Object call() throws Exception {
-          // Setup for capturing logger messages
-          Appender mockAppender = mock(Appender.class);
-          when(mockAppender.getName()).thenReturn("MockAppender");
-          when(mockAppender.isStarted()).thenReturn(true);
-          when(mockAppender.isStopped()).thenReturn(false);
-          Logger logger = (Logger) 
LogManager.getLogger(ColocationLogger.class);
-          logger.addAppender(mockAppender);
-          logger.setLevel(Level.WARN);
-          loggingEventCaptor = ArgumentCaptor.forClass(LogEvent.class);
 
-          // Logger interval may have been hooked by the test, so adjust test 
delays here
-          int logInterval = ColocationLogger.getLogInterval();
-          List<LogEvent> logEvents = Collections.emptyList();
-
-          AtomicBoolean isDone = new AtomicBoolean(false);
-          try {
+          try (MockAppender mockAppender = new 
MockAppender(ColocationLogger.class)) {
             createPR(getPartitionedRegionName(), true);
             // Let this thread continue running long enough for the missing 
region to be logged a
             // couple times.
             // Child regions do not get created by this thread.
-            await().untilAsserted(() -> {
-              verify(mockAppender, times(numExpectedLogMessages))
-                  .append(loggingEventCaptor.capture());
-            });
-            // createPR("region2", PR_REGION_NAME, true); // This child region 
is never created
+            await().untilAsserted(
+                () -> assertEquals(numExpectedLogMessages, 
mockAppender.getLogs().size()));
+
+            return 
mockAppender.getLogs().get(0).getMessage().getFormattedMessage();
           } finally {
-            logEvents = loggingEventCaptor.getAllValues();
-            assertEquals(String.format("Expected %d messages to be logged, got 
%d.",
-                numExpectedLogMessages, logEvents.size()), 
numExpectedLogMessages,
-                logEvents.size());
-            String logMsg = 
logEvents.get(0).getMessage().getFormattedMessage();
-            logger.removeAppender(mockAppender);
             numExpectedLogMessages = 1;
-            return logMsg;
           }
         }
       };
 
   private SerializableCallable createPRsMissingChildRegionDelayedStartThread =
       new SerializableCallable("createPRsMissingChildRegionDelayedStart") {
-        Appender mockAppender;
-        ArgumentCaptor<LogEvent> loggingEventCaptor;
 
         public Object call() throws Exception {
-          // Setup for capturing logger messages
-          mockAppender = mock(Appender.class);
-          when(mockAppender.getName()).thenReturn("MockAppender");
-          when(mockAppender.isStarted()).thenReturn(true);
-          when(mockAppender.isStopped()).thenReturn(false);
-          Logger logger = (Logger) 
LogManager.getLogger(ColocationLogger.class);
-          logger.addAppender(mockAppender);
-          logger.setLevel(Level.WARN);
-          loggingEventCaptor = ArgumentCaptor.forClass(LogEvent.class);
-
-          // Logger interval may have been hooked by the test, so adjust test 
delays here
-          int logInterval = ColocationLogger.getLogInterval();
-          List<LogEvent> logEvents = Collections.emptyList();
 
-          try {
+          try (MockAppender mockAppender = new 
MockAppender(ColocationLogger.class)) {
             createPR(getPartitionedRegionName(), true);
             // Delay creation of second (i.e child) region to see missing 
colocated region log
             // message (logInterval/2 < delay < logInterval)
-            await().untilAsserted(() -> {
-              verify(mockAppender, 
times(1)).append(loggingEventCaptor.capture());
-            });
-            logEvents = loggingEventCaptor.getAllValues();
+            await().untilAsserted(
+                () -> assertEquals(numExpectedLogMessages, 
mockAppender.getLogs().size()));
             createPR("region2", getPartitionedRegionName(), true);
             // Another delay before exiting the thread to make sure that 
missing region logging
             // doesn't continue after missing region is created (delay > 
logInterval)
-            await().untilAsserted(() -> {
-              verifyNoMoreInteractions(mockAppender);
-            });
-            fail("Unexpected missing colocated region log message");
+            Thread.sleep(ColocationLogger.getLogInterval() + 10000);
+            return 
mockAppender.getLogs().get(0).getMessage().getFormattedMessage();
           } finally {
-            assertEquals(String.format("Expected %d messages to be logged, got 
%d.",
-                numExpectedLogMessages, logEvents.size()), 
numExpectedLogMessages,
-                logEvents.size());
-            String logMsg = 
logEvents.get(0).getMessage().getFormattedMessage();
-            logger.removeAppender(mockAppender);
             numExpectedLogMessages = 1;
-            mockAppender = null;
-            return logMsg;
           }
         }
       };
 
   private SerializableCallable createPRsSequencedChildrenCreationThread =
       new SerializableCallable("createPRsSequencedChildrenCreation") {
-        Appender mockAppender;
-        ArgumentCaptor<LogEvent> loggingEventCaptor;
 
         public Object call() throws Exception {
-          // Setup for capturing logger messages
-          mockAppender = mock(Appender.class);
-          when(mockAppender.getName()).thenReturn("MockAppender");
-          when(mockAppender.isStarted()).thenReturn(true);
-          when(mockAppender.isStopped()).thenReturn(false);
-          Logger logger = (Logger) 
LogManager.getLogger(ColocationLogger.class);
-          logger.addAppender(mockAppender);
-          logger.setLevel(Level.WARN);
-          loggingEventCaptor = ArgumentCaptor.forClass(LogEvent.class);
-
-          // Logger interval may have been hooked by the test, so adjust test 
delays here
-          int logInterval = ColocationLogger.getLogInterval();
-          List<LogEvent> logEvents = Collections.emptyList();
-          int numLogEvents = 0;
-
-          createPR(getPartitionedRegionName(), true);
-          // Delay creation of child generation regions to see missing 
colocated region log message
-          // parent region is generation 1, child region is generation 2, 
grandchild is 3, etc.
-          for (int generation = 2; generation < (numChildPRGenerations + 2); 
++generation) {
-            String childPRName = "region" + generation;
-            String colocatedWithRegionName =
-                generation == 2 ? getPartitionedRegionName() : "region" + 
(generation - 1);
-            loggingEventCaptor = ArgumentCaptor.forClass(LogEvent.class);
-
-            // delay between starting generations of child regions until the 
expected missing
-            // colocation messages are logged
-            int n = (generation - 1) * generation / 2;
-            await().untilAsserted(() -> {
-              verify(mockAppender, 
times(n)).append(loggingEventCaptor.capture());
-            });
-            // check for log messages
-            logEvents = loggingEventCaptor.getAllValues();
-            String logMsg = 
loggingEventCaptor.getValue().getMessage().getFormattedMessage();
 
-            // Start the child region
-            createPR(childPRName, colocatedWithRegionName, true);
-          }
-          String logMsg = "";
-          logEvents = loggingEventCaptor.getAllValues();
-          assertEquals(String.format("Expected warning messages to be 
logged."),
-              numExpectedLogMessages, logEvents.size());
-          logMsg = logEvents.get(0).getMessage().getFormattedMessage();
+          try (MockAppender mockAppender = new 
MockAppender(ColocationLogger.class)) {
+            createPR(getPartitionedRegionName(), true);
+            // Delay creation of child generation regions to see missing 
colocated region log
+            // message
+            // parent region is generation 1, child region is generation 2, 
grandchild is 3, etc.
+            for (int generation = 2; generation < (numChildPRGenerations + 2); 
++generation) {
+              String childPRName = "region" + generation;
+              String colocatedWithRegionName =
+                  generation == 2 ? getPartitionedRegionName() : "region" + 
(generation - 1);
+
+              // delay between starting generations of child regions until the 
expected missing
+              // colocation messages are logged
+              int n = (generation - 1) * generation / 2;
+              await().untilAsserted(() -> assertEquals(n, 
mockAppender.getLogs().size()));
+
+              // Start the child region
+              createPR(childPRName, colocatedWithRegionName, true);
+            }
+            assertEquals(numExpectedLogMessages, 
mockAppender.getLogs().size());
 
-          // acknowledge interactions with the mock that have occurred
-          verify(mockAppender, atLeastOnce()).getName();
-          verify(mockAppender, atLeastOnce()).isStarted();
-          try {
             // Another delay before exiting the thread to make sure that 
missing region logging
             // doesn't continue after all regions are created (delay > 
logInterval)
-            Thread.sleep(logInterval * 2);
-            verifyNoMoreInteractions(mockAppender);
+            verify(mockAppender.getMock(), atLeastOnce()).getName();
+            verify(mockAppender.getMock(), atLeastOnce()).isStarted();
+            Thread.sleep(ColocationLogger.getLogInterval() + 10000);
+            verifyNoMoreInteractions(mockAppender.getMock());
+            return 
mockAppender.getLogs().get(0).getMessage().getFormattedMessage();
           } finally {
-            logger.removeAppender(mockAppender);
+            numExpectedLogMessages = 1;
           }
-          numExpectedLogMessages = 1;
-          mockAppender = null;
-          return logMsg;
         }
       };
 
   private SerializableCallable 
createMultipleColocatedChildPRsWithSequencedStart =
       new SerializableCallable("createPRsMultipleSequencedChildrenCreation") {
-        Appender mockAppender;
-        ArgumentCaptor<LogEvent> loggingEventCaptor;
 
         public Object call() throws Exception {
-          // Setup for capturing logger messages
-          mockAppender = mock(Appender.class);
-          when(mockAppender.getName()).thenReturn("MockAppender");
-          when(mockAppender.isStarted()).thenReturn(true);
-          when(mockAppender.isStopped()).thenReturn(false);
-          Logger logger = (Logger) 
LogManager.getLogger(ColocationLogger.class);
-          logger.addAppender(mockAppender);
-          logger.setLevel(Level.WARN);
-          loggingEventCaptor = ArgumentCaptor.forClass(LogEvent.class);
 
-          // Logger interval may have been hooked by the test, so adjust test 
delays here
-          int logInterval = ColocationLogger.getLogInterval();
-          List<LogEvent> logEvents = Collections.emptyList();
-          int numLogEvents = 0;
-
-          createPR(getPartitionedRegionName(), true);
-          // Delay creation of child generation regions to see missing 
colocated region log message
-          for (int regionNum = 2; regionNum < (numChildPRs + 2); ++regionNum) {
-            String childPRName = "region" + regionNum;
-            loggingEventCaptor = ArgumentCaptor.forClass(LogEvent.class);
 
-            // delay between starting generations of child regions until the 
expected missing
-            // colocation messages are logged
-            int n = regionNum - 1;
-            await().untilAsserted(() -> {
-              verify(mockAppender, 
times(n)).append(loggingEventCaptor.capture());
-            });
-            // check for expected number of log messages
-            logEvents = loggingEventCaptor.getAllValues();
-            String logMsg = 
loggingEventCaptor.getValue().getMessage().getFormattedMessage();
-            numLogEvents = logEvents.size();
-            assertEquals("Expected warning messages to be logged.", regionNum 
- 1, numLogEvents);
-
-            // Start the child region
-            createPR(childPRName, getPartitionedRegionName(), true);
-          }
-          String logMsg = "";
-          logEvents = loggingEventCaptor.getAllValues();
-          assertEquals(String.format("Expected warning messages to be 
logged."),
-              numExpectedLogMessages, logEvents.size());
-          logMsg = logEvents.get(0).getMessage().getFormattedMessage();
+          try (MockAppender mockAppender = new 
MockAppender(ColocationLogger.class)) {
+            createPR(getPartitionedRegionName(), true);
+            // Delay creation of child generation regions to see missing 
colocated region log
+            // message
+            for (int regionNum = 2; regionNum < (numChildPRs + 2); 
++regionNum) {
+              String childPRName = "region" + regionNum;
+
+              // delay between starting generations of child regions until the 
expected missing
+              // colocation messages are logged
+              int n = regionNum - 1;
+              await().untilAsserted(() -> assertEquals(n, 
mockAppender.getLogs().size()));
+              int numLogEvents = mockAppender.getLogs().size();
+              assertEquals("Expected warning messages to be logged.", 
regionNum - 1, numLogEvents);
+
+              // Start the child region
+              createPR(childPRName, getPartitionedRegionName(), true);
+            }
+            String logMsg = "";
+            assertEquals(String.format("Expected warning messages to be 
logged."),
+                numExpectedLogMessages, mockAppender.getLogs().size());
+            logMsg = 
mockAppender.getLogs().get(0).getMessage().getFormattedMessage();
 
-          // acknowledge interactions with the mock that have occurred
-          verify(mockAppender, atLeastOnce()).getName();
-          verify(mockAppender, atLeastOnce()).isStarted();
-          try {
             // Another delay before exiting the thread to make sure that 
missing region logging
             // doesn't continue after all regions are created (delay > 
logInterval)
-            Thread.sleep(logInterval * 2);
-            verifyNoMoreInteractions(mockAppender);
+            verify(mockAppender.getMock(), atLeastOnce()).getName();
+            verify(mockAppender.getMock(), atLeastOnce()).isStarted();
+            Thread.sleep(ColocationLogger.getLogInterval() * 2);
+            verifyNoMoreInteractions(mockAppender.getMock());
+            return logMsg;
           } finally {
-            logger.removeAppender(mockAppender);
-          }
-          numExpectedLogMessages = 1;
-          mockAppender = null;
-          return logMsg;
-        }
-      };
-
-  private SerializableCallable createPRsMissingGrandchildRegionThread =
-      new SerializableCallable("createPRsMissingChildRegion") {
-        Appender mockAppender;
-        ArgumentCaptor<LogEvent> loggingEventCaptor;
-
-        public Object call() throws Exception {
-          // Setup for capturing logger messages
-          Appender mockAppender = mock(Appender.class);
-          when(mockAppender.getName()).thenReturn("MockAppender");
-          when(mockAppender.isStarted()).thenReturn(true);
-          when(mockAppender.isStopped()).thenReturn(false);
-          Logger logger = (Logger) 
LogManager.getLogger(ColocationLogger.class);
-          logger.addAppender(mockAppender);
-          logger.setLevel(Level.WARN);
-          loggingEventCaptor = ArgumentCaptor.forClass(LogEvent.class);
-
-          // Logger interval may have been hooked by the test, so adjust test 
delays here
-          int logInterval = ColocationLogger.getLogInterval();
-          List<LogEvent> logEvents = Collections.emptyList();
 
-          try {
-            createPR(getPartitionedRegionName(), true);
-            createPR("region2", getPartitionedRegionName(), true); // This 
child region is never
-                                                                   // created
-            // Let this thread continue running long enough for the missing 
region to be logged a
-            // couple times.
-            // Grandchild region does not get created by this thread. 
(1.5*logInterval < delay <
-            // 2*logInterval)
-            await().untilAsserted(() -> {
-              verify(mockAppender, times(numExpectedLogMessages))
-                  .append(loggingEventCaptor.capture());
-            });
-            // createPR("region3", PR_REGION_NAME, true); // This child region 
is never created
-          } finally {
-            logEvents = loggingEventCaptor.getAllValues();
-            assertEquals(String.format("Expected %d messages to be logged, got 
%d.",
-                numExpectedLogMessages, logEvents.size()), 
numExpectedLogMessages,
-                logEvents.size());
-            String logMsg = 
logEvents.get(0).getMessage().getFormattedMessage();
-            logger.removeAppender(mockAppender);
             numExpectedLogMessages = 1;
-            return logMsg;
           }
         }
       };
 
-  void checkRecoveredFromDisk(VM vm, final int bucketId, final boolean 
recoveredLocally) {
-    vm.invoke(new SerializableRunnable("check recovered from disk") {
-      @Override
-      public void run() {
-        Cache cache = getCache();
-        PartitionedRegion region = (PartitionedRegion) 
cache.getRegion(getPartitionedRegionName());
-        DiskRegion disk = 
region.getRegionAdvisor().getBucket(bucketId).getDiskRegion();
-        if (recoveredLocally) {
-          assertEquals(0, disk.getStats().getRemoteInitializations());
-          assertEquals(1, disk.getStats().getLocalInitializations());
-        } else {
-          assertEquals(1, disk.getStats().getRemoteInitializations());
-          assertEquals(0, disk.getStats().getLocalInitializations());
-        }
-      }
-    });
-  }
-
   private class ColocationLoggerIntervalSetter extends SerializableRunnable {
     private int logInterval;
 
@@ -836,7 +668,6 @@ public class PersistentColocatedPartitionedRegionDUnitTest
    * Testing that missing colocated child persistent PRs are logged as warning
    */
   @Test
-  @Ignore("GEODE-5872: Test is losing assertions")
   public void testMissingColocatedChildPRDueToDelayedStart() throws Throwable {
     int loggerTestInterval = 4000; // millis
     Host host = Host.getHost(0);
@@ -882,7 +713,6 @@ public class PersistentColocatedPartitionedRegionDUnitTest
    * Testing that missing colocated child persistent PRs are logged as warning
    */
   @Test
-  @Ignore("GEODE-5872: Test is losing assertions")
   public void testMissingColocatedChildPR() throws Throwable {
     int loggerTestInterval = 4000; // millis
     Host host = Host.getHost(0);
@@ -984,7 +814,6 @@ public class PersistentColocatedPartitionedRegionDUnitTest
    * appear in the warning.
    */
   @Test
-  @Ignore("GEODE-5872: Test is losing assertions")
   public void testMultipleColocatedChildPRsMissingWithSequencedStart() throws 
Throwable {
     int loggerTestInterval = 4000; // millis
     int numChildPRs = 2;
@@ -1040,7 +869,6 @@ public class PersistentColocatedPartitionedRegionDUnitTest
    * Testing that all missing persistent PRs in a colocation hierarchy are 
logged as warnings
    */
   @Test
-  @Ignore("GEODE-5872: Test is losing assertions")
   public void testHierarchyOfColocatedChildPRsMissing() throws Throwable {
     int loggerTestInterval = 4000; // millis
     int numChildGenerations = 2;
@@ -1096,7 +924,6 @@ public class PersistentColocatedPartitionedRegionDUnitTest
    * Testing that all missing persistent PRs in a colocation hierarchy are 
logged as warnings
    */
   @Test
-  @Ignore("GEODE-5872: Test is losing assertions")
   public void testHierarchyOfColocatedChildPRsMissingGrandchild() throws 
Throwable {
     int loggerTestInterval = 4000; // millis
     int numChildGenerations = 3;

Reply via email to