Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.storemate.shared;

This is a container used to allow tests to "warp space/time continuum". That is, during normal operation all it does is to dispatch operations to the usual JDK methods; but when testing it can be diverted to alter how time behaves, from perspective of both server and client side code.

Note on implementation: although it should often be adequate to just use static set up (as most tests are linear), we unfortunately can not rely on this: some tests suites explicitly use concurrent test execution. As a result, we must use separate instances, which get carried throughout the system.

 
 public abstract class TimeMaster
 {
    
Factory method used for creating an instance that does not add any test-time waiting logic.
 
     public static TimeMaster nonTestInstance() {
         return new Std();
     }
     
     /*
     /**********************************************************************
     /* Basic instance API
     /**********************************************************************
      */

    
Method to call instead of java.lang.System.currentTimeMillis() to obtain current timestamp.
 
     public abstract long currentTimeMillis();

    
Method to call instead of java.lang.Thread.sleep(long) for making current thread sleep up to specified amount of time.
 
     public abstract void sleep(long waitTimethrows InterruptedException;

    
Method that by-passes work-arounds and returns actual system time; should be rarely needed, mostly for things that guard against excessive resource usage.
 
     public final long realSystemTimeMillis() {
         return System.currentTimeMillis();
     }

    

Since:
0.9.10
 
     public long nanosForDiagnostics() {
         return System.nanoTime();
     }
     
     /*
     /**********************************************************************
     /* Shared helper methods
     /**********************************************************************
      */
 
     public static String timeDesc(long msecs)
     {
         if (msecs < 0L) {
             msecs = 0L;
         }
         if (msecs < 200L) { // let's switch to "0.x seconds" at 0.2...
             return String.format("%d msecs"msecs);
         }
         double secs = (doublemsecs / 1000.0;
         if (secs < 60.0) {
             return String.format("%.1f secs"secs);
         }
         long minutes = ((longsecs) / 60L;
         if (minutes < 10L) {
             secs -= (60.0 * minutes);
             return String.format("%d mins, %d secs"minutes, (intsecs);
         }
         if (minutes < 60L) {
             return String.format("%d mins"minutes);
         }
         int hours = (int) (minutes / 60);
         if (hours < 48) {
             minutes -= (60 * hours);
             return String.format("%dh, %d mins"hoursminutes);
         }
         int days = hours / 24;
         if (days < 7) {
             hours -= (days * 24);
             return String.format("%dd, %d hours"hoursdays);
         }
         return String.format("%d days"days);
     }
 
     /*
    /**********************************************************************
    /* Standard implementations
    /**********************************************************************
     */

    
Default implementation used when NOT running tests.
    public static class Std extends TimeMaster
    {
        @Override
        public long currentTimeMillis() {
            return System.currentTimeMillis();
        }
        @Override
        public void sleep(long waitTimethrows InterruptedException {
            Thread.sleep(waitTime);
        }
    }
New to GrepCode? Check out our FAQ X