You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/09/06 07:25:05 UTC

[logging-log4j2] branch 2.x updated: Modernize and fix race condition in WatchHttpTest

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

pkarwasz pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 4cdcfda0bc Modernize and fix race condition in WatchHttpTest
4cdcfda0bc is described below

commit 4cdcfda0bc0c382f5815ccbfdc12cb26d543b52a
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Wed Sep 6 09:24:57 2023 +0200

    Modernize and fix race condition in WatchHttpTest
    
    This test removed the HTTP server stub first and only then stopped the
    HTTP client, which caused a race condition and random test failures.
---
 .../logging/log4j/core/util/WatchHttpTest.java     | 69 +++++++++-------------
 .../src/test/resources/logback-test.xml            | 20 +++----
 2 files changed, 37 insertions(+), 52 deletions(-)

diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/WatchHttpTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/WatchHttpTest.java
index dbd0b7df72..624cde5c34 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/WatchHttpTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/WatchHttpTest.java
@@ -26,7 +26,9 @@ import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 
-import com.github.tomakehurst.wiremock.junit.WireMockRule;
+import com.github.tomakehurst.wiremock.client.WireMock;
+import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
+import com.github.tomakehurst.wiremock.junit5.WireMockTest;
 import com.github.tomakehurst.wiremock.stubbing.StubMapping;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationListener;
@@ -35,68 +37,48 @@ import org.apache.logging.log4j.core.config.DefaultConfiguration;
 import org.apache.logging.log4j.core.config.HttpWatcher;
 import org.apache.logging.log4j.core.config.Reconfigurable;
 import org.apache.logging.log4j.core.net.UrlConnectionFactory;
-import org.apache.logging.log4j.core.net.ssl.TestConstants;
 import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
+import org.apache.logging.log4j.test.junit.SetTestProperty;
 import org.apache.logging.log4j.util.PropertiesUtil;
-import org.junit.Assume;
-import org.junit.BeforeClass;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
 import static com.github.tomakehurst.wiremock.client.WireMock.get;
-import static com.github.tomakehurst.wiremock.client.WireMock.removeStub;
-import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
-import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
 
 /**
  * Test the WatchManager
  */
+@SetTestProperty(key = UrlConnectionFactory.ALLOWED_PROTOCOLS, value = "http,https")
+@WireMockTest
 public class WatchHttpTest {
 
     private static final String FORCE_RUN_KEY = WatchHttpTest.class.getSimpleName() + ".forceRun";
     private final String file = "log4j-test1.xml";
-    private static FastDateFormat formatter;
+    private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
+    private static FastDateFormat formatter = FastDateFormat.getInstance("EEE, dd MMM yyyy HH:mm:ss", UTC);
     private static final String XML = "application/xml";
 
     private static final boolean IS_WINDOWS = PropertiesUtil.getProperties().isOsWindows();
 
-    @BeforeClass
-    public static void beforeClass() {
-        System.setProperty(UrlConnectionFactory.ALLOWED_PROTOCOLS, "http,https");
-        try {
-            formatter = FastDateFormat.getInstance("EEE, dd MMM yyyy HH:mm:ss", TimeZone.getTimeZone("UTC"));
-        } catch (Exception ex) {
-            System.err.println("Unable to create date format.");
-            ex.printStackTrace();
-            throw ex;
-        }
-    }
-
-    @Rule
-    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()
-            .dynamicHttpsPort()
-            .keystorePath(TestConstants.KEYSTORE_FILE)
-            .keystorePassword(String.valueOf(TestConstants.KEYSTORE_PWD()))
-            .keyManagerPassword(String.valueOf(TestConstants.KEYSTORE_PWD()))
-            .keystoreType(TestConstants.KEYSTORE_TYPE));
-
     @Test
-    public void testWatchManager() throws Exception {
+    public void testWatchManager(final WireMockRuntimeInfo info) throws Exception {
+        assumeTrue(!IS_WINDOWS || Boolean.getBoolean(FORCE_RUN_KEY));
+        final WireMock wireMock = info.getWireMock();
+
         final BlockingQueue<String> queue = new LinkedBlockingQueue<>();
         final List<ConfigurationListener> listeners = new ArrayList<>();
         listeners.add(new TestConfigurationListener(queue, "log4j-test1.xml"));
-        final TimeZone timeZone = TimeZone.getTimeZone("UTC");
-        final Calendar now = Calendar.getInstance(timeZone);
+        final Calendar now = Calendar.getInstance(UTC);
         final Calendar previous = now;
         previous.add(Calendar.MINUTE, -5);
         final Configuration configuration = new DefaultConfiguration();
-        Assume.assumeTrue(!IS_WINDOWS || Boolean.getBoolean(FORCE_RUN_KEY));
-        final URL url = new URL("http://localhost:" + wireMockRule.port() + "/log4j-test1.xml");
-        final StubMapping stubMapping = stubFor(get(urlPathEqualTo("/log4j-test1.xml"))
+        final URL url = new URL(info.getHttpBaseUrl() + "/log4j-test1.xml");
+        final StubMapping stubMapping = wireMock
+                .register(get(urlPathEqualTo("/log4j-test1.xml"))
                 .willReturn(aResponse()
                         .withBodyFile(file)
                         .withStatus(200)
@@ -114,14 +96,17 @@ public class WatchHttpTest {
             final String str = queue.poll(2, TimeUnit.SECONDS);
             assertNotNull("File change not detected", str);
         } finally {
-            removeStub(stubMapping);
             watchManager.stop();
             scheduler.stop();
+            wireMock.removeStubMapping(stubMapping);
         }
     }
 
     @Test
-    public void testNotModified() throws Exception {
+    public void testNotModified(final WireMockRuntimeInfo info) throws Exception {
+        assumeTrue(!IS_WINDOWS || Boolean.getBoolean(FORCE_RUN_KEY));
+        final WireMock wireMock = info.getWireMock();
+
         final BlockingQueue<String> queue = new LinkedBlockingQueue<>();
         final List<ConfigurationListener> listeners = new ArrayList<>();
         listeners.add(new TestConfigurationListener(queue, "log4j-test2.xml"));
@@ -130,9 +115,9 @@ public class WatchHttpTest {
         final Calendar previous = now;
         previous.add(Calendar.MINUTE, -5);
         final Configuration configuration = new DefaultConfiguration();
-        Assume.assumeTrue(!IS_WINDOWS || Boolean.getBoolean(FORCE_RUN_KEY));
-        final URL url = new URL("http://localhost:" + wireMockRule.port() + "/log4j-test2.xml");
-        final StubMapping stubMapping = stubFor(get(urlPathEqualTo("/log4j-test2.xml"))
+        final URL url = new URL(info.getHttpBaseUrl() + "/log4j-test2.xml");
+        final StubMapping stubMapping = wireMock
+                .register(get(urlPathEqualTo("/log4j-test2.xml"))
                 .willReturn(aResponse()
                         .withBodyFile(file)
                         .withStatus(304)
@@ -150,9 +135,9 @@ public class WatchHttpTest {
             final String str = queue.poll(2, TimeUnit.SECONDS);
             assertNull("File changed.", str);
         } finally {
-            removeStub(stubMapping);
             watchManager.stop();
             scheduler.stop();
+            wireMock.removeStubMapping(stubMapping);
         }
     }
 
diff --git a/log4j-core-test/src/test/resources/logback-test.xml b/log4j-core-test/src/test/resources/logback-test.xml
index a3db96d8a4..3040c29941 100644
--- a/log4j-core-test/src/test/resources/logback-test.xml
+++ b/log4j-core-test/src/test/resources/logback-test.xml
@@ -16,16 +16,16 @@
   ~ limitations under the License.
   -->
 <configuration>
- <appender name="TestLogfile" class="ch.qos.logback.core.FileAppender">
-   <file>target/testlogback.log</file>
+  <appender name="TestLogfile" class="ch.qos.logback.core.FileAppender">
+    <file>target/testlogback.log</file>
     <append>false</append>
-   <encoder>
-     <immediateFlush>false</immediateFlush>
-     <Pattern>%d %5p [%t] %c{0} %X{transactionId} - %m%n</Pattern>
-   </encoder>
- </appender>
+    <immediateFlush>false</immediateFlush>
+    <encoder>
+      <pattern>%d %5p [%t] %c{0} %X{transactionId} - %m%n</pattern>
+    </encoder>
+  </appender>
 
- <root level="debug">
-   <appender-ref ref="TestLogfile" />
- </root>
+  <root level="debug">
+    <appender-ref ref="TestLogfile" />
+  </root>
 </configuration>