commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joe...@apache.org
Subject svn commit: r442360 - /jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java
Date Mon, 11 Sep 2006 21:43:28 GMT
Author: joehni
Date: Mon Sep 11 14:43:28 2006
New Revision: 442360

URL: http://svn.apache.org/viewvc?view=rev&rev=442360
Log:
Detect future time shifts and fail immediately.

Modified:
    jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java

Modified: jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java?view=diff&rev=442360&r1=442359&r2=442360
==============================================================================
--- jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java
(original)
+++ jakarta/commons/sandbox/id/trunk/src/test/org/apache/commons/id/serial/TimeBasedAlphanumericIdentifierGeneratorTest.java
Mon Sep 11 14:43:28 2006
@@ -98,9 +98,8 @@
 
     /**
      * Test that the generator can be tweaked to start with '0'.
-     * @throws InterruptedException unexpected
      */
-    public void testMayStartWithIdentifierOfZeros() throws InterruptedException {
+    public void testMayStartWithIdentifierOfZeros() {
         final int maxSize = Long.toString(Long.MAX_VALUE, 36).length();
         final char[] zeros = new char[maxSize];
         Arrays.fill(zeros, '0');
@@ -170,9 +169,8 @@
     /**
      * Test ensures, that generator can recalculate the time from the id if internally no
overflow
      * had happened.
-     * @throws InterruptedException unexpected
      */
-    public void testCanRetrieveTimeFromIdWithoutInternalOverflow() throws InterruptedException
{
+    public void testCanRetrieveTimeFromIdWithoutInternalOverflow() {
         final TimeSliceSynchronizer synchronizer = new TimeSliceSynchronizer() {
             TimeBasedAlphanumericIdentifierGenerator idGenerator;
             void initialize() {
@@ -193,9 +191,8 @@
     /**
      * Test ensures, that generator can recalculate the time from the id even if internally
an
      * overflow had happened.
-     * @throws InterruptedException unexpected
      */
-    public void testCanRetrieveTimeFromIdWithInternalOverflow() throws InterruptedException
{
+    public void testCanRetrieveTimeFromIdWithInternalOverflow() {
         final TimeSliceSynchronizer synchronizer = new TimeSliceSynchronizer() {
             TimeBasedAlphanumericIdentifierGenerator idGenerator;
             void initialize() {
@@ -262,39 +259,40 @@
     }
  
     /**
-     * A guardian class that tries hard to synchronize the test to run in the same time slice
of the CPU even on time
-     * shifting machines.
+     * A guardian class that tries hard to synchronize the test to run in the same time slice
of
+     * the CPU even on time shifting machines.
      */
     abstract static class TimeSliceSynchronizer {
 
         long waitForNextPeriod;
         long next;
 
-        void runSynced() throws InterruptedException {
-            // idea here is to generate an id in the same time slice of the CPU as the last
System.currentTimeMillis() call
-            int tries = 0;
-            for (;;) {
+        void runSynced() {
+            // idea here is to generate an id in the same time slice of the CPU as the last
+            // System.currentTimeMillis() call
+            int fails = 0;
+            for (int tries = 10; tries-- > 0;) {
                 waitForNextPeriod = System.currentTimeMillis();
                 initialize();
 
                 next = waitForNextPeriod;
-                while (next == waitForNextPeriod) {
+                // allow time shifts back into time, the generator can handle such shifts
+                while (next <= waitForNextPeriod) {
                     next = System.currentTimeMillis();
                 }
 
-                // sanity check, neither condition should normally occur, but on time shifting
machines we start over
-                if (next < waitForNextPeriod || next - waitForNextPeriod > 100) {
-                    // we already tried 10 times to find a situation without time shifting
-                    if (++tries > 10) {
-                        fail("Cannot perform this test on a machine, that is steadily time
shifting!");
-                    }
-                    Thread.sleep(1);
-                    continue;
+                // time shift into future, no problem for the generator, but breaks tests
that
+                // assume to run in the same time slice
+                if (next > System.currentTimeMillis()) {
+                    ++fails;
                 }
-
-                runTest();
-                break;
             }
+            // we tried 10 times to be sure not running on a time shifting machine
+            if (fails > 0) {
+                fail("Cannot perform this test on a machine, that is steadily time shifting
into future!");
+            }
+
+            runTest();
         }
 
         void initialize() {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message