Home | History | Annotate | Download | only in mockitoutil
      1 /*
      2  * Copyright (c) 2017 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockitoutil;
      6 
      7 import org.mockito.exceptions.base.MockitoAssertionError;
      8 
      9 import java.util.concurrent.TimeUnit;
     10 
     11 import static java.lang.String.format;
     12 import static java.lang.System.nanoTime;
     13 import static java.util.concurrent.TimeUnit.NANOSECONDS;
     14 
     15 /**
     16  * This class can be uses as stopwatch to assert that a given time is elapsed or not.
     17  */
     18 public class Stopwatch {
     19 
     20     /**
     21      * The start time in nano seconds or <code>null</code> if this stop watch was not started yet
     22      */
     23     private Long startNanos = null;
     24 
     25     /**
     26      * To create an instance use {@link #createNotStarted()}
     27      */
     28     private Stopwatch() {
     29     }
     30 
     31     /**
     32      * Return a new and not started {@link Stopwatch}.
     33      */
     34     public static Stopwatch createNotStarted() {
     35         return new Stopwatch();
     36     }
     37 
     38     public void start() {
     39         if (startNanos != null)
     40             throw new IllegalStateException("This stop watch is already started!");
     41 
     42         startNanos = nanoTime();
     43     }
     44 
     45     public void assertElapsedTimeIsMoreThan(long expected, TimeUnit unit) {
     46         long elapsedNanos = elapsedNanos();
     47         long expectedNanos = unit.toNanos(expected);
     48 
     49         if (elapsedNanos <= expectedNanos)
     50             fail("Expected that more than %dms elapsed! But was: %dms", expectedNanos, elapsedNanos);
     51     }
     52 
     53     public void assertElapsedTimeIsLessThan(long expected, TimeUnit unit) {
     54         long elapsedNanos = elapsedNanos();
     55         long expectedNanos = unit.toNanos(expected);
     56 
     57         if (elapsedNanos >= expectedNanos)
     58             fail("Expected that less than %dms elapsed! But was: %dms", expectedNanos, elapsedNanos);
     59     }
     60 
     61     private long elapsedNanos() {
     62         if (startNanos == null)
     63             throw new IllegalStateException("This stop watch is not started!");
     64         return nanoTime() - startNanos;
     65     }
     66 
     67     private static void fail(String message, long expectedNanos, long elapsedNanos) {
     68         throw new MockitoAssertionError(format(message, NANOSECONDS.toMillis(expectedNanos), NANOSECONDS.toMillis(elapsedNanos)));
     69     }
     70 
     71     /**
     72      * Waits for specific amount of millis using 'Thread.sleep()'.
     73      * Rethrows InterruptedException.
     74      */
     75     public void waitFor(int millis) {
     76         try {
     77             Thread.sleep(millis);
     78         } catch (InterruptedException e) {
     79             throw new RuntimeException(e);
     80         }
     81     }
     82 }
     83