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

markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit aaf981ff431db8f2c790be1bc0124604c0d23298
Author: Chenjp <ch...@msn.com>
AuthorDate: Wed Sep 11 16:48:43 2024 +0800

    fix bz #69316 with test case.
    
    check whether cached date and current date are in same second.
---
 .../tomcat/util/http/FastHttpDateFormat.java       |  2 +-
 .../tomcat/util/http/TesterFastHttpDateFormat.java | 48 ++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/tomcat/util/http/FastHttpDateFormat.java 
b/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
index cbb7be9151..49830e3dcc 100644
--- a/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
+++ b/java/org/apache/tomcat/util/http/FastHttpDateFormat.java
@@ -105,7 +105,7 @@ public final class FastHttpDateFormat {
     public static String getCurrentDate() {
         long now = System.currentTimeMillis();
         // Handle case where time moves backwards (e.g. system time corrected)
-        if (Math.abs(now - currentDateGenerated) > 1000) {
+        if (now - now%1000L != currentDateGenerated - 
currentDateGenerated%1000L) {
             currentDate = FORMAT_RFC5322.format(new Date(now));
             currentDateGenerated = now;
         }
diff --git a/test/org/apache/tomcat/util/http/TesterFastHttpDateFormat.java 
b/test/org/apache/tomcat/util/http/TesterFastHttpDateFormat.java
new file mode 100644
index 0000000000..f9b17415ae
--- /dev/null
+++ b/test/org/apache/tomcat/util/http/TesterFastHttpDateFormat.java
@@ -0,0 +1,48 @@
+package org.apache.tomcat.util.http;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TesterFastHttpDateFormat {
+    @Test
+    public void testGetCurrentDateInSameSecond() {
+        long now = System.currentTimeMillis();
+        try {
+            Thread.sleep(1000L - now % 1000);
+        } catch (InterruptedException e) {
+        }
+        now = System.currentTimeMillis();
+        String s1 = FastHttpDateFormat.getCurrentDate();
+        System.out.println("1st:" + System.currentTimeMillis() + ", " + s1);
+        long lastMillisInSameSecond = now - now % 1000 + 900L;
+        try {
+            Thread.sleep(lastMillisInSameSecond - now);
+        } catch (InterruptedException e) {
+        }
+        String s2 = FastHttpDateFormat.getCurrentDate();
+        System.out.println("2nd:" + System.currentTimeMillis() + ", " + s2);
+        Assert.assertEquals("Two same RFC5322 format dates are expected.", s1, 
s2);
+    }
+
+    @Test
+    public void testGetCurrentDateNextToAnotherSecond() {
+        long now = System.currentTimeMillis();
+
+        try {
+            Thread.sleep(2000L - now % 1000 + 500L);
+        } catch (InterruptedException e) {
+        }
+        now = System.currentTimeMillis();
+        String s1 = FastHttpDateFormat.getCurrentDate();
+        System.out.println("1st:" + System.currentTimeMillis() + ", " + s1);
+        long firstMillisOfNextSecond = now - now % 1000 + 1100L;
+        try {
+            Thread.sleep(firstMillisOfNextSecond - now);
+        } catch (InterruptedException e) {
+        }
+
+        String s2 = FastHttpDateFormat.getCurrentDate();
+        System.out.println("2nd:" + System.currentTimeMillis() + ", " + s2);
+        Assert.assertFalse("Two different RFC5322 format dates are expected.", 
s1.equals(s2));
+    }
+}


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

Reply via email to