[logback-dev] svn commit: r1916 - in logback/trunk/logback-core/src/test: java/ch/qos/logback/core/rolling witness/rolling

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Oct 30 15:07:11 CET 2008


Author: ceki
Date: Thu Oct 30 15:07:11 2008
New Revision: 1916

Modified:
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.2

Log:
refactoring TimeBasedRollingTest.java for improved clarity

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java	Thu Oct 30 15:07:11 2008
@@ -10,11 +10,15 @@
 
 package ch.qos.logback.core.rolling;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
+import java.sql.Date;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Calendar;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 import org.junit.After;
@@ -63,13 +67,16 @@
   TimeBasedRollingPolicy tbrp2 = new TimeBasedRollingPolicy();
 
   Calendar cal = Calendar.getInstance();
-  long currentTime;
+  long currentTime; // initialized in setUp()
+  long nextRolloverThreshold; // initialized in setUp()
+  List<String> filenameList = new ArrayList<String>();
 
   @Before
   public void setUp() {
     context.setName("test");
     cal.set(Calendar.MILLISECOND, 0);
     currentTime = cal.getTimeInMillis();
+    recomputeRolloverThreshold(currentTime);
 
     // Delete .log files
     {
@@ -102,18 +109,28 @@
   }
 
   void initTRBP(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy tbrp,
-      String filenamePattern, long currentTime) {
+      String filenamePattern, long givenTime, long lastCheck) {
     tbrp.setContext(context);
     tbrp.setFileNamePattern(filenamePattern);
     tbrp.setParent(rfa);
-    tbrp.setCurrentTime(currentTime);
+    tbrp.setCurrentTime(givenTime);
+    if (lastCheck != 0) {
+      tbrp.setLastCheck(new Date(lastCheck));
+    }
     tbrp.start();
     rfa.setRollingPolicy(tbrp);
     rfa.start();
   }
 
-  String[] computeFilenames(String testStr,
-      boolean compression, String lastFile) {
+  void addFileName(String testId, Date date, boolean compression) {
+    String fn = Constants.OUTPUT_DIR_PREFIX + testId + sdf.format(date);
+    if (compression) {
+      fn += ".gz";
+    }
+    filenameList.add(fn);
+  }
+
+  String[] computeFilenames(String testStr, boolean compression, String lastFile) {
     String[] filenames = new String[3];
     int oneBeforeLast = filenames.length - 1;
     for (int i = 0; i < oneBeforeLast; i++) {
@@ -137,70 +154,71 @@
    * Test rolling without compression, file option left blank, no stop/start
    */
   @Test
-  public void test1() throws Exception {
+  public void noCompression_FileBlank_NoRestart_1() throws Exception {
+    String testId = "test1-";
     initRFA(rfa1, null);
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test1-%d{"
-        + DATE_PATTERN + "}", currentTime);
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "%d{"
+        + DATE_PATTERN + "}", currentTime, 0);
 
-    String[] filenames = computeFilenames("test1-", false, null);
+    // compute the current filename
+    addFileName(testId, getTimeForElapsedPeriod(), false);
 
-    // System.out.println("Waiting until next second and 100 millis.");
-    tbrp1.setCurrentTime(addTime(currentTime, 1100));
-    // System.out.println("Done waiting.");
+    incCurrentTime(1100);
+    tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i < 3; i++) {
+      addFileNameIfNecessary(testId, false);
       rfa1.doAppend("Hello---" + i);
-      tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500));
-      // Thread.sleep(500);
+      incCurrentTime(500);
+      tbrp1.setCurrentTime(currentTime);
     }
 
-    // for (int i = 0; i < 3; i++) {
-    // System.out.println(i + " expected filename [" + filenames[i] + "].");
-    // }
-
-    for (int i = 0; i < 3; i++) {
-      assertTrue(Compare.compare(filenames[i], Constants.TEST_DIR_PREFIX
-          + "witness/rolling/tbr-test1." + i));
+    int i = 0;
+    for (String fn : filenameList) {
+      assertTrue(Compare.compare(fn, Constants.TEST_DIR_PREFIX
+          + "witness/rolling/tbr-test1." + i++));
     }
   }
 
   /**
-   * No compression, with stop/restart, file option left blank
+   * No compression, file option left blank, with stop/restart,
    */
   @Test
-  public void test2() throws Exception {
+  public void noCompression_FileBlank_StopRestart_2() throws Exception {
+    String testId = "test2-";
 
     initRFA(rfa1, null);
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test2-%d{"
-        + DATE_PATTERN + "}", currentTime);
-
-    String[] filenames = computeFilenames("test2-", false, null);
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "%d{"
+        + DATE_PATTERN + "}", currentTime, 0);
 
-    // System.out.println("Waiting until next second and 100 millis.");
-    tbrp1.setCurrentTime(addTime(currentTime, 1100));
-    // System.out.println("Done waiting.");
+    addFileName(testId, getTimeForElapsedPeriod(), false);
+    incCurrentTime(1100);
+    tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i <= 2; i++) {
+      addFileNameIfNecessary(testId, false);
       rfa1.doAppend("Hello---" + i);
-      tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500));
-      // Thread.sleep(500);
+      incCurrentTime(500);
+      tbrp1.setCurrentTime(currentTime);
     }
 
     rfa1.stop();
 
     initRFA(rfa2, null);
     initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + "test2-%d{"
-        + DATE_PATTERN + "}", tbrp1.getCurrentTime());
+        + DATE_PATTERN + "}", tbrp1.getCurrentTime(), 0);
 
     for (int i = 0; i <= 2; i++) {
+      addFileNameIfNecessary(testId, false);
       rfa2.doAppend("Hello---" + i);
-      tbrp2.setCurrentTime(addTime(tbrp2.getCurrentTime(), 100));
-      // Thread.sleep(100);
+      incCurrentTime(100);
+      tbrp2.setCurrentTime(currentTime);
     }
 
-    for (int i = 0; i < 3; i++) {
-      assertTrue(Compare.compare(filenames[i], Constants.TEST_DIR_PREFIX
-          + "witness/rolling/tbr-test2." + i));
+    int i = 0;
+    for (String fn : filenameList) {
+      assertTrue(Compare.compare(fn, Constants.TEST_DIR_PREFIX
+          + "witness/rolling/tbr-test2." + i++));
     }
   }
 
@@ -208,28 +226,35 @@
    * With compression, file option left blank, no stop/restart
    */
   @Test
-  public void test3() throws Exception {
+  public void withCompression_FileBlank_NoRestart_3() throws Exception {
+    String testId = "test3-";
     initRFA(rfa1, null);
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test3-%d{"
-        + DATE_PATTERN + "}.gz", currentTime);
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "%d{"
+        + DATE_PATTERN + "}.gz", currentTime, 0);
 
-    String[] filenames = computeFilenames("test3-", true, null);
+    String[] filenames = computeFilenames(testId, true, null);
 
-    tbrp1.setCurrentTime(addTime(currentTime, 1100));
+    addFileName(testId, getTimeForElapsedPeriod(), true);
+    incCurrentTime(1100);
+    tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i < 3; i++) {
+      addFileNameIfNecessary(testId, true);
       rfa1.doAppend("Hello---" + i);
       tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500));
     }
 
     tbrp1.future.get(2000, TimeUnit.MILLISECONDS);
 
+    System.out.println(Arrays.toString(filenames));
+    System.out.println(filenameList.toString());
+
     for (int i = 0; i < 2; i++) {
-      assertTrue(Compare.gzCompare(filenames[i], Constants.TEST_DIR_PREFIX
-          + "witness/rolling/tbr-test3." + i + ".gz"));
+      assertTrue(Compare.gzCompare(filenameList.get(i),
+          Constants.TEST_DIR_PREFIX + "witness/rolling/tbr-test3." + i + ".gz"));
     }
 
-    assertTrue(Compare.compare(filenames[2], Constants.TEST_DIR_PREFIX
+    assertTrue(Compare.compare(filenameList.get(2), Constants.TEST_DIR_PREFIX
         + "witness/rolling/tbr-test3.2"));
   }
 
@@ -237,16 +262,21 @@
    * Without compression, file option set, with stop/restart
    */
   @Test
-  public void test4() throws Exception {
+  public void noCompression_FileSet_StopRestart_4() throws Exception {
     initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test4.log");
     initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test4-%d{"
-        + DATE_PATTERN + "}", currentTime);
+        + DATE_PATTERN + "}", currentTime, 0);
 
     String[] filenames = computeFilenames("test4-", false, "test4.log");
 
-    // System.out.println("Waiting until next second and 100 millis.");
+    System.out.println("CT=" + sdf.format(new Date(currentTime)));
+    System.out.println("tbrp1 CT="
+        + sdf.format(new Date(tbrp1.getCurrentTime())));
+
     tbrp1.setCurrentTime(addTime(currentTime, 1100));
-    // System.out.println("Done waiting.");
+
+    System.out.println("tbrp1 CT="
+        + sdf.format(new Date(tbrp1.getCurrentTime())));
 
     for (int i = 0; i <= 2; i++) {
       rfa1.doAppend("Hello---" + i);
@@ -257,10 +287,10 @@
 
     initRFA(rfa2, Constants.OUTPUT_DIR_PREFIX + "test4.log");
     initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + "test4-%d{"
-        + DATE_PATTERN + "}", tbrp1.getCurrentTime());
+        + DATE_PATTERN + "}", tbrp1.getCurrentTime(), tbrp1.getCurrentTime());
 
     for (int i = 0; i <= 2; i++) {
-      rfa2.doAppend("Hello---" + i);
+      rfa2.doAppend("World---" + i);
       tbrp2.setCurrentTime(addTime(tbrp2.getCurrentTime(), 100));
     }
 
@@ -270,14 +300,68 @@
     }
   }
 
+  @Test
+  public void noCompression_FileSet_StopRestart_WithLongWait_4B()
+      throws Exception {
+    initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test4B.log");
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test4B-%d{"
+        + DATE_PATTERN + "}", currentTime, 0);
+
+    String[] filenames = computeFilenames("test4B-", false, "test4B.log");
+
+    System.out.println("CT=" + sdf.format(new Date(currentTime)));
+    System.out.println("tbrp1 CT="
+        + sdf.format(new Date(tbrp1.getCurrentTime())));
+
+    tbrp1.setCurrentTime(addTime(currentTime, 1100));
+
+    System.out.println("tbrp1 CT="
+        + sdf.format(new Date(tbrp1.getCurrentTime())));
+
+    for (int i = 0; i <= 2; i++) {
+      rfa1.doAppend("Hello---" + i);
+      tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500));
+    }
+
+    rfa1.stop();
+    System.out.println("post stop tbrp1 CT="
+        + sdf.format(new Date(tbrp1.getCurrentTime())));
+
+    initRFA(rfa2, Constants.OUTPUT_DIR_PREFIX + "test4B.log");
+    initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + "test4B-%d{"
+        + DATE_PATTERN + "}", tbrp1.getCurrentTime() + 3000, tbrp1
+        .getCurrentTime());
+
+    System.out.println("tbrp2 CT="
+        + sdf.format(new Date(tbrp2.getCurrentTime())));
+
+    for (int i = 0; i <= 2; i++) {
+      rfa2.doAppend("World---" + i);
+      System.out.println("in loop tbrp2 CT="
+          + sdf.format(new Date(tbrp2.getCurrentTime())));
+      tbrp2.setCurrentTime(addTime(tbrp2.getCurrentTime(), 500));
+    }
+
+    System.out.println("tbrp2 CT="
+        + sdf.format(new Date(tbrp2.getCurrentTime())));
+
+    if (1 == 1) {
+      return;
+    }
+    for (int i = 0; i < 3; i++) {
+      assertTrue(Compare.compare(filenames[i], Constants.TEST_DIR_PREFIX
+          + "witness/rolling/tbr-test4." + i));
+    }
+  }
+
   /**
    * No compression, file option set, without stop/restart
    */
   @Test
-  public void test5() throws Exception {
+  public void noCompression_FileSet_NoRestart_5() throws Exception {
     initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test5.log");
     initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test5-%d{"
-        + DATE_PATTERN + "}", currentTime);
+        + DATE_PATTERN + "}", currentTime, 0);
 
     String[] filenames = computeFilenames("test5-", false, "test5.log");
 
@@ -298,10 +382,10 @@
    * With compression, file option set, no stop/restart,
    */
   @Test
-  public void test6() throws Exception {
+  public void withCompression_FileSet_NoRestart_6() throws Exception {
     initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test6.log");
     initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test6-%d{"
-        + DATE_PATTERN + "}.gz", currentTime);
+        + DATE_PATTERN + "}.gz", currentTime, 0);
 
     String[] filenames = computeFilenames("test6-", true, "test6.log");
 
@@ -324,8 +408,37 @@
         + "witness/rolling/tbr-test6.2"));
   }
 
+  // =========================================================================
+  // utility methods
+  // =========================================================================
+
+  void addFileNameIfNecessary(String testId, boolean compression) {
+    if (passThresholdTime(nextRolloverThreshold)) {
+      addFileName(testId, getTimeForElapsedPeriod(), compression);
+      recomputeRolloverThreshold(currentTime);
+    }
+  }
+
+  Date getTimeForElapsedPeriod() {
+    long delta = currentTime % 1000;
+    return new Date(currentTime - delta);
+  }
+
   static long addTime(long currentTime, long timeToWait) {
     return currentTime + timeToWait;
   }
 
+  boolean passThresholdTime(long nextRolloverThreshold) {
+    return currentTime >= nextRolloverThreshold;
+  }
+
+  // assuming rollover every second
+  void recomputeRolloverThreshold(long ct) {
+    long delta = ct % 1000;
+    nextRolloverThreshold = (ct - delta) + 1000;
+  }
+
+  void incCurrentTime(long increment) {
+    currentTime += increment;
+  }
 }

Modified: logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.2
==============================================================================
--- logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.2	(original)
+++ logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.2	Thu Oct 30 15:07:11 2008
@@ -1,4 +1,4 @@
 Hello---2
-Hello---0
-Hello---1
-Hello---2
+World---0
+World---1
+World---2


More information about the logback-dev mailing list