djencks 2003/12/13 00:27:53
Modified: modules/common/src/test/org/apache/geronimo/common
StopWatchTest.java
Log:
Let's hope sleep() isn't more than 10% off
Revision Changes Path
1.4 +23 -23
incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/StopWatchTest.java
Index: StopWatchTest.java
===================================================================
RCS file:
/home/cvs/incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/StopWatchTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- StopWatchTest.java 11 Dec 2003 22:11:47 -0000 1.3
+++ StopWatchTest.java 13 Dec 2003 08:27:53 -0000 1.4
@@ -63,6 +63,8 @@
* @version $Revision$ $Date$
*/
public class StopWatchTest extends TestCase {
+ private static long TIME = 100L;
+ private static long EPSILON = TIME /10;
public void testConstructor() {
StopWatch sw = new StopWatch();
@@ -83,15 +85,15 @@
sw.start();
assertTrue(sw.isRunning());
try {
- Thread.sleep(1000);
+ Thread.sleep(TIME);
} catch (Exception ex) {
}
sw.stop();
assertFalse(sw.isRunning());
assertEquals(1, sw.getLapCount());
- assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= 1000L);
- assertTrue(sw.getTime() >= 1000L);
+ assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= TIME - EPSILON);
+ assertTrue(sw.getTime() >= TIME - EPSILON);
assertEquals(sw.getLapTime(), sw.getAverageLapTime());
}
@@ -103,26 +105,25 @@
sw.start();
assertTrue(sw.isRunning());
try {
- Thread.sleep(1000);
+ Thread.sleep(TIME);
} catch (Exception ex) {
}
sw.stop();
assertFalse(sw.isRunning());
assertEquals(1, sw.getLapCount());
- assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= 1000L);
- assertTrue(sw.getTime() >= 1000L);
+ assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= TIME - EPSILON);
+ assertTrue(sw.getTime() >= TIME - EPSILON);
assertEquals(sw.getLapTime(), sw.getAverageLapTime());
}
- /* BAD TEST: sleep is not an accurate measure of time
public void testTwoStops() {
StopWatch sw = new StopWatch();
sw.start();
assertTrue(sw.isRunning());
try {
- Thread.sleep(1000);
+ Thread.sleep(TIME);
} catch (Exception ex) {
}
sw.stop();
@@ -131,37 +132,36 @@
assertFalse(sw.isRunning());
assertEquals(1, sw.getLapCount());
- assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= 1000L);
- assertTrue(sw.getTime() >= 1000L);
+ assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= TIME - EPSILON);
+ assertTrue(sw.getTime() >= TIME - EPSILON);
assertEquals(sw.getLapTime(), sw.getAverageLapTime());
}
- */
public void testTwoLaps() {
StopWatch sw = new StopWatch();
sw.start();
try {
- Thread.sleep(1000);
+ Thread.sleep(TIME);
} catch (Exception ex) {
}
sw.stop();
assertEquals(1, sw.getLapCount());
- assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= 1000L);
- assertTrue(sw.getTime() >= 1000L);
+ assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= TIME - EPSILON);
+ assertTrue(sw.getTime() >= TIME - EPSILON);
assertEquals(sw.getLapTime(), sw.getAverageLapTime());
sw.start();
try {
- Thread.sleep(1000);
+ Thread.sleep(TIME);
} catch (Exception ex) {
}
sw.stop();
assertEquals(2, sw.getLapCount());
- assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= 1000L);
- assertTrue("Expected more than two seconds, not " + sw.getTime(),
sw.getTime() >= 2000L);
+ assertTrue("Expected more than one second, not " + sw.getLapTime(),
sw.getLapTime() >= TIME - EPSILON);
+ assertTrue("Expected more than two seconds, not " + sw.getTime(),
sw.getTime() >= 2 * (TIME - EPSILON));
assertEquals(sw.getTime() / 2, sw.getAverageLapTime());
}
@@ -170,14 +170,14 @@
sw.start();
try {
- Thread.sleep(1000);
+ Thread.sleep(TIME);
} catch (Exception ex) {
}
sw.stop();
assertEquals(1, sw.getLapCount());
- assertTrue(sw.getLapTime() >= 1000L);
- assertTrue(sw.getTime() >= 1000L);
+ assertTrue(sw.getLapTime() >= TIME - EPSILON);
+ assertTrue(sw.getTime() >= TIME - EPSILON);
assertEquals(sw.getLapTime(), sw.getAverageLapTime());
sw.reset();
@@ -192,12 +192,12 @@
sw.start();
try {
- Thread.sleep(1000);
+ Thread.sleep(TIME);
} catch (Exception ex) {
}
sw.stop();
Duration d = sw.toDuration();
- assertTrue(d.compareTo(1000) >= 0);
+ assertTrue(d.compareTo(1000) >= 0 - EPSILON);
}
}