On Thu, Mar 13, 2025 at 11:58 AM <ma...@apache.org> wrote:
>
> This is an automated email from the ASF dual-hosted git repository.
>
> markt pushed a commit to branch main
> in repository https://gitbox.apache.org/repos/asf/tomcat.git
>
>
> The following commit(s) were added to refs/heads/main by this push:
>      new 05490b5be8 Fix test failures seen on Gump. Use approach from 
> TestAccessLogValve.
> 05490b5be8 is described below
>
> commit 05490b5be83db66de209240d63ffdd3aebdff701
> Author: Mark Thomas <ma...@apache.org>
> AuthorDate: Thu Mar 13 10:57:56 2025 +0000
>
>     Fix test failures seen on Gump. Use approach from TestAccessLogValve.

Thanks. Indeed it was still not enough. Too bad.

Rémy

> ---
>  .../valves/TestExtendedAccessLogValve.java         | 54 
> +++++++++++++++++-----
>  1 file changed, 43 insertions(+), 11 deletions(-)
>
> diff --git a/test/org/apache/catalina/valves/TestExtendedAccessLogValve.java 
> b/test/org/apache/catalina/valves/TestExtendedAccessLogValve.java
> index 4cf73fd901..fefc342963 100644
> --- a/test/org/apache/catalina/valves/TestExtendedAccessLogValve.java
> +++ b/test/org/apache/catalina/valves/TestExtendedAccessLogValve.java
> @@ -16,10 +16,9 @@
>   */
>  package org.apache.catalina.valves;
>
> +import java.io.CharArrayWriter;
>  import java.io.File;
>  import java.io.IOException;
> -import java.nio.charset.StandardCharsets;
> -import java.nio.file.Files;
>  import java.util.ArrayList;
>  import java.util.Collection;
>  import java.util.HashMap;
> @@ -46,6 +45,10 @@ import org.apache.tomcat.util.buf.ByteChunk;
>  @RunWith(Parameterized.class)
>  public class TestExtendedAccessLogValve extends TomcatBaseTest {
>
> +    // Requests can return in the client before log() has been called
> +    private static final long SLEEP = 2;
> +    private static final long SLEEP_MAX = 1000;
> +
>      @Parameterized.Parameters(name = "{index}: pattern=[{0}]")
>      public static Collection<Object[]> data() {
>          List<Object[]> patterns = new ArrayList<>();
> @@ -68,6 +71,29 @@ public class TestExtendedAccessLogValve extends 
> TomcatBaseTest {
>      @Parameter(1)
>      public String logPattern;
>
> +
> +    /**
> +     * Extend AbstractAccessLogValve to retrieve log output.
> +     */
> +    public final class TesterExtendedAccessLogValve extends 
> ExtendedAccessLogValve {
> +
> +        private CharArrayWriter writer;
> +
> +        public TesterExtendedAccessLogValve(CharArrayWriter writer) {
> +            this.writer = writer;
> +        }
> +
> +        @Override
> +        public void log(CharArrayWriter message) {
> +            try {
> +                message.writeTo(writer);
> +            } catch (IOException ex) {
> +                log.error("Could not write to writer", ex);
> +            }
> +        }
> +    }
> +
> +
>      @Test
>      public void testLogFormat() throws Exception {
>
> @@ -77,7 +103,8 @@ public class TestExtendedAccessLogValve extends 
> TomcatBaseTest {
>          // Create temporary directory for logs
>          File logDir = getTemporaryDirectory();
>
> -        ExtendedAccessLogValve valve = new ExtendedAccessLogValve();
> +        CharArrayWriter writer = new CharArrayWriter();
> +        TesterExtendedAccessLogValve valve = new 
> TesterExtendedAccessLogValve(writer);
>          valve.setBuffered(false);
>          valve.setPattern(logPattern);
>          valve.setDirectory(logDir.getAbsolutePath());
> @@ -121,18 +148,21 @@ public class TestExtendedAccessLogValve extends 
> TomcatBaseTest {
>
>          tomcat.stop();
>
> -        // Read log file
> -        File[] logFiles = logDir.listFiles((dir, fname) -> 
> fname.startsWith("access_log_" + name));
> -        Assert.assertNotNull(logFiles);
> -        Assert.assertTrue(logFiles.length > 0);
> -
> -        File logFile = logFiles[0];
> -        String content = new String(Files.readAllBytes(logFile.toPath()), 
> StandardCharsets.UTF_8);
> +        long startWait = System.currentTimeMillis();
> +        String content = writer.toString();
> +        while ("".equals(content) && System.currentTimeMillis() - startWait 
> < SLEEP_MAX) {
> +            try {
> +                Thread.sleep(SLEEP);
> +            } catch (InterruptedException ex) {
> +                log.error("Exception during sleep", ex);
> +            }
> +            content = writer.toString();
> +        }
>
>          processLogContent(content);
> -
>      }
>
> +
>      private void processLogContent(String content) {
>          String[] lines = content.split("\\r?\\n");
>
> @@ -163,6 +193,7 @@ public class TestExtendedAccessLogValve extends 
> TomcatBaseTest {
>          }
>      }
>
> +
>      private void checkField(String fieldId, String value) {
>          if ("time".equals(fieldId)) {
>              Assert.assertTrue("Invalid time format", isTimeFormat(value));
> @@ -186,6 +217,7 @@ public class TestExtendedAccessLogValve extends 
> TomcatBaseTest {
>          }
>      }
>
> +
>      private boolean isTimeFormat(String s) {
>          return Pattern.matches("^\\d{2}:\\d{2}(:\\d{2}(\\.\\d+)?)?$", s);
>      }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: dev-h...@tomcat.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to